fix Identifiers so that Tuples can be assigned to them

This commit is contained in:
Brian Picciano 2016-08-06 18:40:55 -06:00
parent 4fc4d48412
commit e868e76627
3 changed files with 12 additions and 14 deletions

View File

@ -50,8 +50,8 @@ func (bctx BuildCtx) buildExprTill(e Expr, fn func(e Expr) bool) Expr {
case Int: case Int:
return llvmVal(llvm.ConstInt(llvm.Int64Type(), uint64(ea), false)) return llvmVal(llvm.ConstInt(llvm.Int64Type(), uint64(ea), false))
case Identifier: case Identifier:
if v, ok := bctx.C.GetIdentifier(ea); ok { if ev := bctx.C.GetIdentifier(ea); ev != nil {
return llvmVal(v) return ev
} }
panicf("identifier %q not found", ea) panicf("identifier %q not found", ea)
case Statement: case Statement:
@ -85,7 +85,10 @@ var globalCtx = &Ctx{
"bind": func(bctx BuildCtx, e Expr) Expr { "bind": func(bctx BuildCtx, e Expr) Expr {
tup := bctx.buildExprTill(e, isIdentifier).(Tuple) tup := bctx.buildExprTill(e, isIdentifier).(Tuple)
id := bctx.buildExprTill(tup[0], isIdentifier).(Identifier) id := bctx.buildExprTill(tup[0], isIdentifier).(Identifier)
bctx.C.idents[id] = bctx.buildVal(tup[1]) if bctx.C.idents[id] != nil {
panicf("identifier %q is already bound", id)
}
bctx.C.idents[id] = bctx.buildExpr(tup[1])
return nil return nil
}, },
}, },

View File

@ -1,7 +1,5 @@
package expr package expr
import "llvm.org/llvm/bindings/go/llvm"
// MacroFn is a compiler function which takes in an existing Expr and returns // MacroFn is a compiler function which takes in an existing Expr and returns
// the llvm Value for it // the llvm Value for it
type MacroFn func(BuildCtx, Expr) Expr type MacroFn func(BuildCtx, Expr) Expr
@ -12,7 +10,7 @@ type MacroFn func(BuildCtx, Expr) Expr
type Ctx struct { type Ctx struct {
global *Ctx global *Ctx
macros map[Macro]MacroFn macros map[Macro]MacroFn
idents map[Identifier]llvm.Value idents map[Identifier]Expr
} }
// NewCtx returns a blank context instance // NewCtx returns a blank context instance
@ -20,7 +18,7 @@ func NewCtx() *Ctx {
return &Ctx{ return &Ctx{
global: globalCtx, global: globalCtx,
macros: map[Macro]MacroFn{}, macros: map[Macro]MacroFn{},
idents: map[Identifier]llvm.Value{}, idents: map[Identifier]Expr{},
} }
} }
@ -38,12 +36,9 @@ func (c *Ctx) GetMacro(m Macro) MacroFn {
} }
// GetIdentifier returns the llvm.Value for the Identifier, or panics // GetIdentifier returns the llvm.Value for the Identifier, or panics
func (c *Ctx) GetIdentifier(i Identifier) (llvm.Value, bool) { func (c *Ctx) GetIdentifier(i Identifier) Expr {
if v, ok := c.idents[i]; ok {
return v, true
}
// The global context doesn't have any identifiers, so don't bother checking // The global context doesn't have any identifiers, so don't bother checking
return llvm.Value{}, false return c.idents[i]
} }
// NewWith returns a new Ctx instance which imports the given macros from the // NewWith returns a new Ctx instance which imports the given macros from the

View File

@ -37,8 +37,8 @@ func main() {
stmts := []expr.Statement{ stmts := []expr.Statement{
expr.NewStatement(bind, idA, expr.NewStatement(add, expr.Int(1), expr.Int(2))), expr.NewStatement(bind, idA, expr.NewStatement(add, expr.Int(1), expr.Int(2))),
expr.NewStatement(bind, idB, expr.Int(3)), expr.NewStatement(bind, idB, expr.Int(3)),
expr.NewStatement(bind, idC, expr.NewStatement(add, idA, idB)), expr.NewStatement(bind, idC, expr.NewTuple(idA, idB)),
expr.NewStatement(add, idC, idC), expr.NewStatement(add, expr.NewStatement(add, idC), expr.NewStatement(add, idC)),
} }
//block := expr.Block([]expr.Expr{stmt}) //block := expr.Block([]expr.Expr{stmt})