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:
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
},
},

View File

@ -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

View File

@ -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})