Skip to content

Commit

Permalink
Builtin functions as a map.
Browse files Browse the repository at this point in the history
  • Loading branch information
markkurossi committed Sep 17, 2023
1 parent 435fefe commit b985a27
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 26 deletions.
24 changes: 9 additions & 15 deletions compiler/ast/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ import (
"github.com/markkurossi/mpc/types"
)

// Builtin implements MPCL builtin elements.
// Builtin implements MPCL builtin functions.
type Builtin struct {
Name string
SSA SSA
Eval Eval
}
Expand All @@ -33,27 +32,22 @@ type Eval func(args []AST, env *Env, ctx *Codegen, gen *ssa.Generator,
loc utils.Point) (ssa.Value, bool, error)

// Predeclared identifiers.
var builtins = []Builtin{
{
Name: "copy",
SSA: copySSA,
var builtins = map[string]Builtin{
"copy": {
SSA: copySSA,
},
{
Name: "floorPow2",
"floorPow2": {
SSA: floorPow2SSA,
Eval: floorPow2Eval,
},
{
Name: "len",
"len": {
SSA: lenSSA,
Eval: lenEval,
},
{
Name: "native",
SSA: nativeSSA,
"native": {
SSA: nativeSSA,
},
{
Name: "size",
"size": {
SSA: sizeSSA,
Eval: sizeEval,
},
Expand Down
9 changes: 2 additions & 7 deletions compiler/ast/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,8 @@ func (ast *Call) Eval(env *Env, ctx *Codegen, gen *ssa.Generator) (
return ssa.Undefined, false, nil
}
// Check builtin functions.
for _, bi := range builtins {
if bi.Name != ast.Ref.Name.Name {
continue
}
if bi.Eval == nil {
return ssa.Undefined, false, nil
}
bi, ok := builtins[ast.Ref.Name.Name]
if ok && bi.Eval != nil {
return bi.Eval(ast.Exprs, env, ctx, gen, ast.Location())
}

Expand Down
6 changes: 2 additions & 4 deletions compiler/ast/ssagen.go
Original file line number Diff line number Diff line change
Expand Up @@ -636,10 +636,8 @@ func (ast *Call) SSA(block *ssa.Block, ctx *Codegen, gen *ssa.Generator) (
}
if called == nil {
// Check builtin functions.
for _, bi := range builtins {
if bi.Name != ast.Ref.Name.Name {
continue
}
bi, ok := builtins[ast.Ref.Name.Name]
if ok {
// Flatten arguments.
var args []ssa.Value
for _, arg := range callValues {
Expand Down

0 comments on commit b985a27

Please sign in to comment.