-
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.
Signed-off-by: Flipez <code@brauser.io>
- Loading branch information
Showing
9 changed files
with
151 additions
and
99 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -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 } |
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,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 | ||
} |
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,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()) | ||
} | ||
} |
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,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) | ||
} |
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,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 | ||
} |
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,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 | ||
} |
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,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...)} | ||
} |