ginger/expr/ctx.go

31 lines
801 B
Go
Raw Normal View History

2016-07-29 00:39:18 +00:00
package expr
import "llvm.org/llvm/bindings/go/llvm"
type MacroFn func(*Ctx, LLVMCtx, Expr) (llvm.Value, bool)
2016-07-29 00:39:18 +00:00
// Ctx contains all the Macros and Identifiers available. A Ctx is based on the
// parent it was created from. If the current Ctx doesn't have a particular key
// being looked up, the parent is called instead, and so on. A consequence of
// this is that keys in the children take precedence over the parent's
type Ctx struct {
Parent *Ctx
Macros map[Macro]MacroFn
LastVal llvm.Value
2016-07-29 00:39:18 +00:00
}
// GetMacro returns the first instance of the given of the given Macro found. If
// not found nil is returned.
func (c *Ctx) GetMacro(m Macro) MacroFn {
2016-07-29 00:39:18 +00:00
if c.Macros != nil {
if fn, ok := c.Macros[m]; ok {
return fn
}
}
if c.Parent != nil {
return c.Parent.GetMacro(m)
}
return nil
}