Skip to content

Commit

Permalink
add Time builtin
Browse files Browse the repository at this point in the history
Signed-off-by: Flipez <code@brauser.io>
  • Loading branch information
Flipez committed Sep 12, 2022
1 parent 604787f commit 25f9d60
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
33 changes: 33 additions & 0 deletions docs/docs/builtins/Time.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Time




## Module Function

### sleep(INTEGER)
> Returns `NIL`
Stops the RocketLang routine for at least the stated duration in seconds


```js
🚀 > Time.sleep(2)
```


### unix()
> Returns `INTEGER`
Returns the current time as unix timestamp


```js
🚀 > Time.Unix()
```



## Properties
| Name | Value |
| ---- | ----- |
1 change: 1 addition & 0 deletions evaluator/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ func TestBuiltinFunctions(t *testing.T) {
{`IO.open("fixtures/module.rl", 1, "0644")`, "wrong argument type on position 2: got=INTEGER, want=STRING"},
{`IO.open("fixtures/module.rl", "r", 1)`, "wrong argument type on position 3: got=INTEGER, want=STRING"},
{`IO.open("fixtures/module.rl", "nope", "0644").read(1)`, "undefined method `.read()` for ERROR"},
{"a = Time.unix(); Time.sleep(2); b = Time.unix(); b - a", 2},
}

for _, tt := range tests {
Expand Down
1 change: 1 addition & 0 deletions stdlib/std.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func init() {
RegisterModule("JSON", "", jsonFunctions, jsonProperties)
RegisterModule("IO", "", ioFunctions, ioProperties)
RegisterModule("OS", "", osFunctions, osProperties)
RegisterModule("Time", "", timeFunctions, timeProperties)
}

func RegisterFunction(name string, layout object.MethodLayout, function func(object.Environment, ...object.Object) object.Object) {
Expand Down
41 changes: 41 additions & 0 deletions stdlib/time.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package stdlib

import (
"time"

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

var timeFunctions = map[string]*object.BuiltinFunction{}
var timeProperties = map[string]*object.BuiltinProperty{}

func init() {
timeFunctions["sleep"] = object.NewBuiltinFunction(
"sleep",
object.MethodLayout{
Description: "Stops the RocketLang routine for at least the stated duration in seconds",
ArgPattern: object.Args(
object.Arg(object.INTEGER_OBJ),
),
ReturnPattern: object.Args(
object.Arg(object.NIL_OBJ),
),
Example: `🚀 > Time.sleep(2)`,
},
func(_ object.Environment, args ...object.Object) object.Object {
time.Sleep(time.Duration(args[0].(*object.Integer).Value) * time.Second)
return object.NIL
})
timeFunctions["unix"] = object.NewBuiltinFunction(
"unix",
object.MethodLayout{
Description: "Returns the current time as unix timestamp",
ReturnPattern: object.Args(
object.Arg(object.INTEGER_OBJ),
),
Example: `🚀 > Time.Unix()`,
},
func(_ object.Environment, args ...object.Object) object.Object {
return object.NewInteger(time.Now().Unix())
})
}

0 comments on commit 25f9d60

Please sign in to comment.