tiancaiamao / shen-go

A Go implementation of the Shen programming language
Other
55 stars 4 forks source link

kl: support try-catch exception in stub.go #6

Closed tiancaiamao closed 5 years ago

tiancaiamao commented 5 years ago
// (trap-error (+ 2 (simple-error "xxx")) (lambda X (error-to-string X)))

res := Try(MakeNative(func(ctx *Trampoline, args ...Obj) {
  regXX := MakeString("xxx")
  regYY := PrimSimpleError(regXX)
  res := PrimNumberAdd(MakeNumber(2), regYY)
  ctx.Return(res)
  return
}, 1)).Catch(MakeNative(func(ctx *Trampoline, args ...Obj) {
  err := args[0]
  res := PrimErrorToString(err)
  ctx.Return(res)
  return
}, 1))

Use defer and panic to implement try-catch. trap-error may need this feature when translating the Shen code to Go.