-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #113 from Flipez/add-sleep
add Time builtin
- Loading branch information
Showing
4 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | | ||
| ---- | ----- | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
}) | ||
} |
b4d08e5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
rocket-lang – ./docs
rocket-lang-git-main-flipez.vercel.app
rocket-lang.vercel.app
www.rocket-lang.org
rocket-lang-flipez.vercel.app
rocket-lang.org
b4d08e5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
rocket-lang-play – ./wasm
play.rocket-lang.org
rocket-lang-play-flipez.vercel.app
rocket-lang-play.vercel.app
rocket-lang-play-git-main-flipez.vercel.app