diff --git a/compiler/ast/builtin.go b/compiler/ast/builtin.go index 38284f5d..4ac1a557 100644 --- a/compiler/ast/builtin.go +++ b/compiler/ast/builtin.go @@ -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 } @@ -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, }, diff --git a/compiler/ast/eval.go b/compiler/ast/eval.go index 26faf55a..82291492 100644 --- a/compiler/ast/eval.go +++ b/compiler/ast/eval.go @@ -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()) } diff --git a/compiler/ast/ssagen.go b/compiler/ast/ssagen.go index 998ca917..b3eeecf1 100644 --- a/compiler/ast/ssagen.go +++ b/compiler/ast/ssagen.go @@ -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 {