Skip to content

Commit

Permalink
Merge pull request #111 from Flipez/add-os-module
Browse files Browse the repository at this point in the history
move exit and raise to os module
  • Loading branch information
Flipez authored Sep 12, 2022
2 parents d5fdffc + af51321 commit 604787f
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 54 deletions.
36 changes: 36 additions & 0 deletions docs/docs/builtins/OS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# OS




## Module Function

### exit(INTEGER)
> Returns ``
Terminates the program with the given exit code.


```js
🚀 > OS.exit(1)
exit status 1
```


### raise(INTEGER, STRING)
> Returns ``
Terminates the program with the given exit code and prints the error message.


```js
🚀 > OS.raise(1, "broken")
🔥 RocketLang raised an error: "broken"
exit status 1
```



## Properties
| Name | Value |
| ---- | ----- |
19 changes: 0 additions & 19 deletions docs/docs/specification/builtins.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,6 @@ menu:
toc: true
---
# Builtin Functions
## exit(INTEGER)

Terminates the program with the given exit code.

```js
🚀 > exit(1)
exit status 1
```

## raise(INTEGER, STRING)

Terminates the program with the given exit code and prints the error message.

```js
🚀 > raise(1, "broken")
🔥 RocketLang raised an error: "broken"
exit status 1
```

## puts(STRING|ARRAY|HASH|BOOLEAN|INTEGER|NIL|FILE)

Prints the string representation of the given object to STDOUT.
Expand Down
10 changes: 5 additions & 5 deletions evaluator/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,11 +371,11 @@ func TestBuiltinFunctions(t *testing.T) {
expected interface{}
}{
{`puts("test")`, nil},
{`raise("Error")`, "to few arguments: got=1, want=2"},
{`raise("Error", 1)`, "wrong argument type on position 1: got=STRING, want=INTEGER"},
{`raise(1, 1)`, "wrong argument type on position 2: got=INTEGER, want=STRING"},
{`exit()`, "to few arguments: got=0, want=1"},
{`exit("Error")`, "wrong argument type on position 1: got=STRING, want=INTEGER"},
{`OS.raise("Error")`, "to few arguments: got=1, want=2"},
{`OS.raise("Error", 1)`, "wrong argument type on position 1: got=STRING, want=INTEGER"},
{`OS.raise(1, 1)`, "wrong argument type on position 2: got=INTEGER, want=STRING"},
{`OS.exit()`, "to few arguments: got=0, want=1"},
{`OS.exit("Error")`, "wrong argument type on position 1: got=STRING, want=INTEGER"},
{`IO.open()`, "to few arguments: got=0, want=1"},
{`IO.open(1, "r", "0644")`, "wrong argument type on position 1: got=INTEGER, want=STRING"},
{`IO.open("fixtures/module.rl", 1, "0644")`, "wrong argument type on position 2: got=INTEGER, want=STRING"},
Expand Down
13 changes: 0 additions & 13 deletions stdlib/exit.go

This file was deleted.

48 changes: 48 additions & 0 deletions stdlib/os.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package stdlib

import (
"fmt"
"os"

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

var osFunctions = map[string]*object.BuiltinFunction{}
var osProperties = map[string]*object.BuiltinProperty{}

func init() {
osFunctions["exit"] = object.NewBuiltinFunction(
"exit",
object.MethodLayout{
Description: "Terminates the program with the given exit code.",
ArgPattern: object.Args(
object.Arg(object.INTEGER_OBJ),
),
Example: `🚀 > OS.exit(1)
exit status 1`,
},
func(_ object.Environment, args ...object.Object) object.Object {
os.Exit(int(args[0].(*object.Integer).Value))

return nil
})

osFunctions["raise"] = object.NewBuiltinFunction(
"raise",
object.MethodLayout{
Description: "Terminates the program with the given exit code and prints the error message.",
ArgPattern: object.Args(
object.Arg(object.INTEGER_OBJ),
object.Arg(object.STRING_OBJ),
),
Example: `🚀 > OS.raise(1, "broken")
🔥 RocketLang raised an error: "broken"
exit status 1`,
},
func(_ object.Environment, args ...object.Object) object.Object {
fmt.Printf("🔥 RocketLang raised an error: %s\n", args[1].Inspect())
os.Exit(int(args[0].(*object.Integer).Value))

return nil
})
}
15 changes: 0 additions & 15 deletions stdlib/raise.go

This file was deleted.

3 changes: 1 addition & 2 deletions stdlib/std.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ var Modules = map[string]*object.BuiltinModule{}

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

RegisterModule("Math", "", mathFunctions, mathProperties)
RegisterModule("HTTP", "", httpFunctions, httpProperties)
RegisterModule("JSON", "", jsonFunctions, jsonProperties)
RegisterModule("IO", "", ioFunctions, ioProperties)
RegisterModule("OS", "", osFunctions, osProperties)
}

func RegisterFunction(name string, layout object.MethodLayout, function func(object.Environment, ...object.Object) object.Object) {
Expand Down

2 comments on commit 604787f

@vercel
Copy link

@vercel vercel bot commented on 604787f Sep 12, 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 604787f Sep 12, 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.