Skip to content

Commit

Permalink
added sleep function, closes #157
Browse files Browse the repository at this point in the history
  • Loading branch information
odino committed Feb 2, 2019
1 parent d16a8d7 commit 94a4d0c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
8 changes: 8 additions & 0 deletions docs/types/builtin-function.md
Expand Up @@ -166,6 +166,14 @@ Type 'quit' when you're done, 'help' if you get lost!
STRING
```
### sleep(ms)
Halts the process for as many `ms` you specified:
``` bash
sleep(1000) # sleeps for 1 second
```
## Next
That's about it for this section!
Expand Down
1 change: 1 addition & 0 deletions evaluator/evaluator_test.go
Expand Up @@ -776,6 +776,7 @@ c")`, []string{"a", "b", "c"}},
{`"A great movie".upper()`, "A GREAT MOVIE"},
{`" A great movie ".trim()`, "A great movie"},
{`" A great movie ".trim_by(" A")`, "great movie"},
{`sleep(1000)`, nil},
}
for _, tt := range tests {
evaluated := testEval(tt.input)
Expand Down
18 changes: 18 additions & 0 deletions evaluator/functions.go
Expand Up @@ -9,6 +9,7 @@ import (
"sort"
"strconv"
"strings"
"time"

"github.com/abs-lang/abs/ast"
"github.com/abs-lang/abs/lexer"
Expand Down Expand Up @@ -271,6 +272,11 @@ func getFns() map[string]*object.Builtin {
Types: []string{object.ARRAY_OBJ},
Fn: joinFn,
},
// sleep(3000)
"sleep": &object.Builtin{
Types: []string{object.NUMBER_OBJ},
Fn: sleepFn,
},
}
}

Expand Down Expand Up @@ -1269,3 +1275,15 @@ func joinFn(args ...object.Object) object.Object {

return &object.String{Token: tok, Value: strings.Join(newElements, args[1].(*object.String).Value)}
}

func sleepFn(args ...object.Object) object.Object {
err := validateArgs("sleep", args, 1, [][]string{{object.NUMBER_OBJ}})
if err != nil {
return err
}

ms := args[0].(*object.Number)
time.Sleep(time.Duration(ms.Value) * time.Millisecond)

return NULL
}

0 comments on commit 94a4d0c

Please sign in to comment.