2016-08-05 18:34:17 +00:00
|
|
|
package expr
|
|
|
|
|
2016-08-09 01:43:05 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"llvm.org/llvm/bindings/go/llvm"
|
|
|
|
)
|
2016-08-05 18:34:17 +00:00
|
|
|
|
2016-08-06 18:20:53 +00:00
|
|
|
type BuildCtx struct {
|
2016-08-05 18:34:17 +00:00
|
|
|
B llvm.Builder
|
|
|
|
M llvm.Module
|
|
|
|
}
|
|
|
|
|
2016-08-09 01:43:05 +00:00
|
|
|
func (bctx BuildCtx) Build(ctx Ctx, stmts ...Statement) llvm.Value {
|
2016-08-06 18:20:53 +00:00
|
|
|
var lastVal llvm.Value
|
2016-08-05 18:34:17 +00:00
|
|
|
for _, stmt := range stmts {
|
2016-08-09 01:43:05 +00:00
|
|
|
fmt.Println(stmt)
|
|
|
|
if e := bctx.BuildStmt(ctx, stmt); e != nil {
|
|
|
|
if lv, ok := e.(llvmVal); ok {
|
|
|
|
lastVal = llvm.Value(lv)
|
|
|
|
}
|
2016-08-06 18:20:53 +00:00
|
|
|
}
|
2016-08-05 18:34:17 +00:00
|
|
|
}
|
2016-08-06 18:20:53 +00:00
|
|
|
if (lastVal == llvm.Value{}) {
|
|
|
|
lastVal = bctx.B.CreateRetVoid()
|
|
|
|
}
|
|
|
|
return lastVal
|
2016-08-05 18:34:17 +00:00
|
|
|
}
|
|
|
|
|
2016-08-09 01:43:05 +00:00
|
|
|
func (bctx BuildCtx) BuildStmt(ctx Ctx, s Statement) Expr {
|
2016-08-06 18:20:53 +00:00
|
|
|
m := s.Op.(Macro)
|
2016-08-09 01:43:05 +00:00
|
|
|
return ctx.Macro(m)(bctx, ctx, s.Arg)
|
2016-08-06 18:20:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// may return nil if e is a Statement which has no return
|
2016-08-09 01:43:05 +00:00
|
|
|
func (bctx BuildCtx) buildExpr(ctx Ctx, e Expr) Expr {
|
|
|
|
return bctx.buildExprTill(ctx, e, func(Expr) bool { return false })
|
2016-08-07 00:10:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// like buildExpr, but will stop short and stop recursing when the function
|
|
|
|
// returns true
|
2016-08-09 01:43:05 +00:00
|
|
|
func (bctx BuildCtx) buildExprTill(ctx Ctx, e Expr, fn func(e Expr) bool) Expr {
|
2016-08-07 00:10:31 +00:00
|
|
|
if fn(e) {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2016-08-06 18:20:53 +00:00
|
|
|
switch ea := e.(type) {
|
|
|
|
case llvmVal:
|
|
|
|
return e
|
|
|
|
case Int:
|
|
|
|
return llvmVal(llvm.ConstInt(llvm.Int64Type(), uint64(ea), false))
|
|
|
|
case Identifier:
|
2016-08-09 01:43:05 +00:00
|
|
|
return ctx.Identifier(ea)
|
2016-08-06 18:20:53 +00:00
|
|
|
case Statement:
|
2016-08-09 01:43:05 +00:00
|
|
|
return bctx.BuildStmt(ctx, ea)
|
2016-08-06 18:20:53 +00:00
|
|
|
case Tuple:
|
|
|
|
for i := range ea {
|
2016-08-09 01:43:05 +00:00
|
|
|
ea[i] = bctx.buildExprTill(ctx, ea[i], fn)
|
2016-08-06 18:20:53 +00:00
|
|
|
}
|
|
|
|
return ea
|
2016-08-07 15:25:03 +00:00
|
|
|
case List:
|
|
|
|
for i := range ea {
|
2016-08-09 01:43:05 +00:00
|
|
|
ea[i] = bctx.buildExprTill(ctx, ea[i], fn)
|
2016-08-07 15:25:03 +00:00
|
|
|
}
|
|
|
|
return ea
|
2016-08-09 01:43:05 +00:00
|
|
|
case Ctx:
|
|
|
|
return ea
|
2016-08-06 18:20:53 +00:00
|
|
|
default:
|
2016-08-09 01:43:05 +00:00
|
|
|
panicf("%v (type %T) can't express a value", ea, ea)
|
2016-08-05 18:34:17 +00:00
|
|
|
}
|
2016-08-06 18:20:53 +00:00
|
|
|
panic("go is dumb")
|
|
|
|
}
|
|
|
|
|
2016-08-09 01:43:05 +00:00
|
|
|
func (bctx BuildCtx) buildVal(ctx Ctx, e Expr) llvm.Value {
|
|
|
|
return llvm.Value(bctx.buildExpr(ctx, e).(llvmVal))
|
2016-08-06 18:20:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// globalCtx describes what's available to *all* contexts, and is what all
|
2016-08-09 01:43:05 +00:00
|
|
|
// contexts should have as the root parent in the tree.
|
|
|
|
//
|
|
|
|
// We define in this weird way cause NewCtx actually references globalCtx
|
|
|
|
var globalCtx *Ctx
|
|
|
|
var _ = func() bool {
|
|
|
|
globalCtx = &Ctx{
|
|
|
|
macros: map[Macro]MacroFn{
|
|
|
|
"add": func(bctx BuildCtx, ctx Ctx, e Expr) Expr {
|
|
|
|
tup := bctx.buildExpr(ctx, e).(Tuple)
|
|
|
|
a := bctx.buildVal(ctx, tup[0])
|
|
|
|
b := bctx.buildVal(ctx, tup[1])
|
|
|
|
return llvmVal(bctx.B.CreateAdd(a, b, ""))
|
|
|
|
},
|
|
|
|
|
|
|
|
// TODO this chould be a user macro!!!! WUT this language is baller
|
|
|
|
"bind": func(bctx BuildCtx, ctx Ctx, e Expr) Expr {
|
|
|
|
tup := bctx.buildExprTill(ctx, e, isIdentifier).(Tuple)
|
|
|
|
id := bctx.buildExprTill(ctx, tup[0], isIdentifier).(Identifier)
|
|
|
|
ctx.Bind(id, bctx.buildExpr(ctx, tup[1]))
|
|
|
|
return NewTuple()
|
|
|
|
},
|
|
|
|
|
|
|
|
"ctxnew": func(bctx BuildCtx, ctx Ctx, e Expr) Expr {
|
|
|
|
return NewCtx()
|
|
|
|
},
|
2016-08-06 18:20:53 +00:00
|
|
|
|
2016-08-09 01:43:05 +00:00
|
|
|
"ctxthis": func(bctx BuildCtx, ctx Ctx, e Expr) Expr {
|
|
|
|
return ctx
|
|
|
|
},
|
|
|
|
|
|
|
|
"ctxbind": func(bctx BuildCtx, ctx Ctx, e Expr) Expr {
|
|
|
|
tup := bctx.buildExprTill(ctx, e, isIdentifier).(Tuple)
|
|
|
|
thisCtx := bctx.buildExpr(ctx, tup[0]).(Ctx)
|
|
|
|
id := bctx.buildExprTill(ctx, tup[1], isIdentifier).(Identifier)
|
|
|
|
thisCtx.Bind(id, bctx.buildExpr(ctx, tup[2]))
|
|
|
|
return NewTuple()
|
|
|
|
},
|
|
|
|
|
|
|
|
"ctxget": func(bctx BuildCtx, ctx Ctx, e Expr) Expr {
|
|
|
|
tup := bctx.buildExprTill(ctx, e, isIdentifier).(Tuple)
|
|
|
|
thisCtx := bctx.buildExpr(ctx, tup[0]).(Ctx)
|
|
|
|
id := bctx.buildExprTill(ctx, tup[1], isIdentifier).(Identifier)
|
|
|
|
return thisCtx.Identifier(id)
|
|
|
|
},
|
|
|
|
|
|
|
|
"do": func(bctx BuildCtx, ctx Ctx, e Expr) Expr {
|
|
|
|
tup := bctx.buildExprTill(ctx, e, isStmt).(Tuple)
|
|
|
|
thisCtx := tup[0].(Ctx)
|
|
|
|
for _, stmtE := range tup[1].(List) {
|
|
|
|
bctx.BuildStmt(thisCtx, stmtE.(Statement))
|
|
|
|
}
|
|
|
|
return NewTuple()
|
|
|
|
},
|
2016-08-06 18:20:53 +00:00
|
|
|
},
|
2016-08-09 01:43:05 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}()
|