2016-07-29 00:39:18 +00:00
|
|
|
package expr
|
|
|
|
|
2016-08-05 18:34:17 +00:00
|
|
|
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
|
2016-08-05 18:34:17 +00:00
|
|
|
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.
|
2016-08-05 18:34:17 +00:00
|
|
|
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
|
|
|
|
}
|