Skip to content

Commit

Permalink
move builtins to stdlib layout
Browse files Browse the repository at this point in the history
Signed-off-by: Flipez <code@brauser.io>
  • Loading branch information
Flipez committed Dec 28, 2021
1 parent 7574adc commit a51553e
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 99 deletions.
97 changes: 0 additions & 97 deletions evaluator/builtins.go

This file was deleted.

3 changes: 2 additions & 1 deletion evaluator/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package evaluator
import (
"github.com/flipez/rocket-lang/ast"
"github.com/flipez/rocket-lang/object"
"github.com/flipez/rocket-lang/stdlib"

"fmt"
)
Expand Down Expand Up @@ -246,7 +247,7 @@ func evalIdentifier(node *ast.Identifier, env *object.Environment) object.Object
return val
}

if builtin, ok := builtins[node.Value]; ok {
if builtin, ok := stdlib.Builtins[node.Value]; ok {
return builtin
}

Expand Down
5 changes: 4 additions & 1 deletion object/builtin.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package object

type Builtin struct {
Fn func(args ...Object) Object
Name string
Fn BuiltinFunction
}

type BuiltinFunction func(args ...Object) Object

func (b *Builtin) Type() ObjectType { return BUILTIN_OBJ }
func (b *Builtin) Inspect() string { return "builtin function" }
func (b *Builtin) InvokeMethod(method string, env Environment, args ...Object) Object { return nil }
20 changes: 20 additions & 0 deletions stdlib/exit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package stdlib

import (
"os"

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

func exitFunction(args ...object.Object) object.Object {
if len(args) != 1 {
return newError("wrong number of arguments. got=%d, want=1", len(args))
}
if args[0].Type() != object.INTEGER_OBJ {
return newError("argument to `exit` must be INTEGER, got=%s", args[0].Type())
}

os.Exit(int(args[0].(*object.Integer).Value))

return nil
}
20 changes: 20 additions & 0 deletions stdlib/len.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package stdlib

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

func lenFunction(args ...object.Object) object.Object {
if len(args) != 1 {
return newError("wrong number of arguments. got=%d, want=1", len(args))
}

switch arg := args[0].(type) {
case *object.Array:
return &object.Integer{Value: int64(len(arg.Elements))}
case *object.String:
return &object.Integer{Value: int64(len(arg.Value))}
default:
return newError("argument to `len` not supported, got %s", args[0].Type())
}
}
34 changes: 34 additions & 0 deletions stdlib/open.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package stdlib

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

func openFunction(args ...object.Object) object.Object {
path := ""
mode := "r"

if len(args) < 1 {
return newError("wrong number of arguments. got=%d, want=1", len(args))
}

switch args[0].(type) {
case *object.String:
path = args[0].(*object.String).Value
default:
return newError("argument to `file` not supported, got=%s", args[0].Type())
}

if len(args) > 1 {
switch args[1].(type) {
case *object.String:
mode = args[1].(*object.String).Value
default:
return newError("argument mode to `file` not supported, got=%s", args[1].Type())
}
}

file := &object.File{Filename: path}
file.Open(mode)
return (file)
}
15 changes: 15 additions & 0 deletions stdlib/puts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package stdlib

import (
"fmt"

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

func putsFunction(args ...object.Object) object.Object {
for _, arg := range args {
fmt.Println(arg.Inspect())
}

return nil
}
25 changes: 25 additions & 0 deletions stdlib/raise.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package stdlib

import (
"fmt"
"os"

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

func raiseFunction(args ...object.Object) object.Object {
if len(args) != 2 {
return newError("wrong number of arguments. got=%d, want=2", len(args))
}
if args[0].Type() != object.INTEGER_OBJ {
return newError("first argument to `raise` must be INTEGER, got=%s", args[0].Type())
}
if args[1].Type() != object.STRING_OBJ {
return newError("second argument to `raise` must be STRING, got=%s", args[1].Type())
}

fmt.Printf("🔥 RocketLang raised an error: %s\n", args[1].Inspect())
os.Exit(int(args[0].(*object.Integer).Value))

return nil
}
31 changes: 31 additions & 0 deletions stdlib/std.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package stdlib

import (
"fmt"

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

var Builtins = map[string]*object.Builtin{}

var (
TRUE = &object.Boolean{Value: true}
FALSE = &object.Boolean{Value: false}
NULL = &object.Null{}
)

func init() {
RegisterFunction("len", lenFunction)
RegisterFunction("puts", putsFunction)
RegisterFunction("exit", exitFunction)
RegisterFunction("raise", raiseFunction)
RegisterFunction("open", openFunction)
}

func RegisterFunction(name string, function object.BuiltinFunction) {
Builtins[name] = &object.Builtin{Fn: function}
}

func newError(format string, a ...interface{}) *object.Error {
return &object.Error{Message: fmt.Sprintf(format, a...)}
}

0 comments on commit a51553e

Please sign in to comment.