From e868e7662705967938446ac37cff3fefd1c132cb Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Sat, 6 Aug 2016 18:40:55 -0600 Subject: [PATCH] fix Identifiers so that Tuples can be assigned to them --- expr/build.go | 9 ++++++--- expr/ctx.go | 13 ++++--------- main.go | 4 ++-- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/expr/build.go b/expr/build.go index 84ad460..a599861 100644 --- a/expr/build.go +++ b/expr/build.go @@ -50,8 +50,8 @@ func (bctx BuildCtx) buildExprTill(e Expr, fn func(e Expr) bool) Expr { case Int: return llvmVal(llvm.ConstInt(llvm.Int64Type(), uint64(ea), false)) case Identifier: - if v, ok := bctx.C.GetIdentifier(ea); ok { - return llvmVal(v) + if ev := bctx.C.GetIdentifier(ea); ev != nil { + return ev } panicf("identifier %q not found", ea) case Statement: @@ -85,7 +85,10 @@ var globalCtx = &Ctx{ "bind": func(bctx BuildCtx, e Expr) Expr { tup := bctx.buildExprTill(e, isIdentifier).(Tuple) 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 }, }, diff --git a/expr/ctx.go b/expr/ctx.go index da3b979..b54f7d7 100644 --- a/expr/ctx.go +++ b/expr/ctx.go @@ -1,7 +1,5 @@ package expr -import "llvm.org/llvm/bindings/go/llvm" - // MacroFn is a compiler function which takes in an existing Expr and returns // the llvm Value for it type MacroFn func(BuildCtx, Expr) Expr @@ -12,7 +10,7 @@ type MacroFn func(BuildCtx, Expr) Expr type Ctx struct { global *Ctx macros map[Macro]MacroFn - idents map[Identifier]llvm.Value + idents map[Identifier]Expr } // NewCtx returns a blank context instance @@ -20,7 +18,7 @@ func NewCtx() *Ctx { return &Ctx{ global: globalCtx, 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 -func (c *Ctx) GetIdentifier(i Identifier) (llvm.Value, bool) { - if v, ok := c.idents[i]; ok { - return v, true - } +func (c *Ctx) GetIdentifier(i Identifier) Expr { // 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 diff --git a/main.go b/main.go index e2052d8..13e358b 100644 --- a/main.go +++ b/main.go @@ -37,8 +37,8 @@ func main() { stmts := []expr.Statement{ expr.NewStatement(bind, idA, expr.NewStatement(add, expr.Int(1), expr.Int(2))), expr.NewStatement(bind, idB, expr.Int(3)), - expr.NewStatement(bind, idC, expr.NewStatement(add, idA, idB)), - expr.NewStatement(add, idC, idC), + expr.NewStatement(bind, idC, expr.NewTuple(idA, idB)), + expr.NewStatement(add, expr.NewStatement(add, idC), expr.NewStatement(add, idC)), } //block := expr.Block([]expr.Expr{stmt})