Skip to content

Commit

Permalink
exit() should support a dying message, closes #261
Browse files Browse the repository at this point in the history
```
exit(99)
exit(99, "I'm gone...")
```
  • Loading branch information
odino committed Aug 16, 2019
1 parent 117bb1f commit e740416
Showing 1 changed file with 16 additions and 2 deletions.
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) == 1 {
err = validateArgs(tok, "exit", args, 1, [][]string{{object.NUMBER_OBJ}})
} else {
err = validateArgs(tok, "exit", args, 2, [][]string{{object.NUMBER_OBJ}, {object.STRING_OBJ}})
message = args[1].(*object.String).Value
}

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

0 comments on commit e740416

Please sign in to comment.