Skip to content

Commit

Permalink
Merge pull request #145 from Flipez/add-raise
Browse files Browse the repository at this point in the history
[object/error] Add `raise()` builtin
  • Loading branch information
Flipez authored Nov 1, 2022
2 parents 2bfbdc0 + de6c1e3 commit 6210594
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 1 deletion.
4 changes: 3 additions & 1 deletion docs/docs/literals/error.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The documentation of those functions does indicate ERROR as a potential return v

A program can rescue from errors within a block or alter it's behavior within other blocks like 'if' or 'def'.

It is possible for the user to create errors using 'raise(STRING)' which will return an ERROR object with STRING as the message.



```js
Expand Down Expand Up @@ -50,7 +52,7 @@ Please note that performing `.msg()` on a ERROR object does result in a STRING o


```js
» def ()
» def test()
puts(nope)
rescue e
puts((rescued error: + e.msg()))
Expand Down
2 changes: 2 additions & 0 deletions docs/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ An error does often replace the original return value of a function or identifie
The documentation of those functions does indicate ERROR as a potential return value.
A program can rescue from errors within a block or alter it's behavior within other blocks like 'if' or 'def'.
It is possible for the user to create errors using 'raise(STRING)' which will return an ERROR object with STRING as the message.
`,
Example: `def test()
puts(nope)
Expand Down
1 change: 1 addition & 0 deletions evaluator/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ func TestErrorHandling(t *testing.T) {
{"begin puts(nope) end", "identifier not found: nope"},
{"begin puts(nope) rescue e e.nope() end", "undefined method `.nope()` for ERROR"},
{"a = begin puts(nope) rescue e e.msg() end; a.nope()", "undefined method `.nope()` for STRING"},
{`raise("custom error")`, "custom error"},
}

for _, tt := range tests {
Expand Down
9 changes: 9 additions & 0 deletions stdlib/raise.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package stdlib

import (
"github.com/flipez/rocket-lang/object"
)

func raiseFunction(_ object.Environment, args ...object.Object) object.Object {
return object.NewError(args[0].(*object.String).Value)
}
8 changes: 8 additions & 0 deletions stdlib/std.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ var Modules = map[string]*object.BuiltinModule{}

func init() {
RegisterFunction("puts", object.MethodLayout{ArgPattern: object.Args(object.Arg(object.ANY_OBJ...))}, putsFunction)
RegisterFunction(
"raise",
object.MethodLayout{
ArgPattern: object.Args(object.Arg(object.STRING_OBJ)),
ReturnPattern: object.Args(object.Arg(object.ERROR_OBJ)),
},
raiseFunction,
)

RegisterModule("Math", "", mathFunctions, mathProperties)
RegisterModule("HTTP", "", httpFunctions, httpProperties)
Expand Down

2 comments on commit 6210594

@vercel
Copy link

@vercel vercel bot commented on 6210594 Nov 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 6210594 Nov 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.