pocketlang icon indicating copy to clipboard operation
pocketlang copied to clipboard

[Enhancement] add experimental error handling: Fiber.try() and built-in raise()

Open khchen opened this issue 2 years ago • 0 comments

Basic error handling via fiber. Example:

fb = Fiber fn
  [][1]
end

fb.try()
if fb.error
  print(fb.error)
else
  print("OK")
end

Output:

List index out of bound.

Also fix following bug:

1.times fn (i)
  1.times fn (i)
    [][1]
  end
end
assert(false) # should be unreachable

Output before this patch:

Error: List index out of bound.
  @func() [/workspace/pocketlang/build/test.pk:3]
Error: Assertion failed.
  @main() [/workspace/pocketlang/build/test.pk:6]

After this patch:

Error: List index out of bound.
  @func() [/workspace/pocketlang/build/test.pk:3]
  @func() [/workspace/pocketlang/build/test.pk:4]
  @main() [/workspace/pocketlang/build/test.pk:5]

Traditional try, except, finally can be emulated. Example: tests/lang/try.pk.

khchen avatar Jul 04 '22 14:07 khchen