ginger/expr/macros.go

35 lines
687 B
Go
Raw Normal View History

2016-07-28 19:57:17 +00:00
package expr
import (
"errors"
"llvm.org/llvm/bindings/go/llvm"
)
type addActual []Expr
func (aa addActual) Equal(Actual) bool { return false }
func (aa addActual) LLVMVal(builder llvm.Builder) llvm.Value {
a := builder.CreateLoad(aa[0].LLVMVal(builder), "")
for i := range aa[1:] {
b := builder.CreateLoad(aa[i+1].LLVMVal(builder), "")
a = builder.CreateAdd(a, b, "")
}
return a
}
var macros = map[Macro]func(Expr) (Expr, error){
"add": func(e Expr) (Expr, error) {
tup, ok := e.Actual.(Tuple)
if !ok {
// TODO proper error
return Expr{}, errors.New("add only accepts a tuple")
}
return Expr{
Actual: addActual(tup),
Token: e.Token,
}, nil
},
}