-
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.
core/builtin: type built-in for accessing type information
- Loading branch information
Showing
2 changed files
with
35 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
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 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 | ||
} | ||
} |