Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exit() should support a dying message, closes #261 #266

Merged
merged 1 commit into from
Aug 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions docs/types/builtin-function.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,22 @@ for input in stdin {
...
```

### exit(code)
### exit(code [, message])

Exists the script with status `code`:
Exits the script with status `code`:

``` bash
exit(99)
```

You can specify a message that's going to be outputted right
before exiting:

``` bash
⧐ exit(99, "Got problems...")
Got problems...%
```

### rand(max)

Returns a random integer number between 0 and `max`:
Expand Down
18 changes: 16 additions & 2 deletions evaluator/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,12 +396,26 @@ func randFn(tok token.Token, args ...object.Object) object.Object {
}

// exit(code:0)
// exit(code:0, message:"Adios!")
func exitFn(tok token.Token, args ...object.Object) object.Object {
err := validateArgs(tok, "exit", args, 1, [][]string{{object.NUMBER_OBJ}})
var err object.Object
var message string

if len(args) == 2 {
err = validateArgs(tok, "exit", args, 2, [][]string{{object.NUMBER_OBJ}, {object.STRING_OBJ}})
message = args[1].(*object.String).Value
} else {
err = validateArgs(tok, "exit", args, 1, [][]string{{object.NUMBER_OBJ}})
}

if err != nil {
return err
}

if message != "" {
fmt.Fprintf(globalEnv.Writer, message)
}

arg := args[0].(*object.Number)
os.Exit(int(arg.Value))
return arg
Expand Down Expand Up @@ -715,7 +729,7 @@ func typeFn(tok token.Token, args ...object.Object) object.Object {
return &object.String{Token: tok, Value: string(args[0].Type())}
}

// split(string:"hello")
// split(string:"hello world!", sep:" ")
func splitFn(tok token.Token, args ...object.Object) object.Object {
err := validateArgs(tok, "split", args, 2, [][]string{{object.STRING_OBJ}, {object.STRING_OBJ}})
if err != nil {
Expand Down