Skip to content

Commit

Permalink
core/builtin: type built-in for accessing type information
Browse files Browse the repository at this point in the history
  • Loading branch information
xNaCly committed Jan 2, 2024
1 parent 3340cf7 commit ede8e96
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func init() {
builtins := map[string]types.KnownFunctionInterface{
"len": builtinLen,
"map": builtinMap,
"type": builtinType,
"println": builtinPrintln,
"filter": builtinFilter,
"assert": builtinAssert,
Expand Down
34 changes: 34 additions & 0 deletions core/builtin/type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package builtin

import (
"sophia/core/serror"
"sophia/core/token"
"sophia/core/types"
)

func builtinType(tok *token.Token, args ...types.Node) any {
if len(args) < 1 {
serror.Add(tok, "Argument error", "Expected 1 argument for assert builtin")
serror.Panic()
}
if len(args) > 1 {
serror.Add(args[1].GetToken(), "Argument error", "Too many arguments, expected 1 argument for assert builtin")
serror.Panic()
}

// TODO: add all missing types
switch args[0].Eval().(type) {
case []any:
return "array"
case map[string]any:
return "object"
case float64:
return "float"
case string:
return "string"
default:
serror.Add(args[0].GetToken(), "Not implemented", "type built-in Not implemented for %T", args[0])
serror.Panic()
return nil
}
}

0 comments on commit ede8e96

Please sign in to comment.