ThakeeNathees / pocketlang

A lightweight, fast embeddable scripting language.
https://thakeenathees.github.io/pocketlang/
MIT License
1.52k stars 80 forks source link

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

Open khchen opened 2 years ago

khchen commented 2 years ago

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.