ginger/expr/ctx.go

60 lines
1.5 KiB
Go
Raw Normal View History

2016-07-29 00:39:18 +00:00
package expr
// MacroFn is a compiler function which takes in an existing Expr and returns
// the llvm Value for it
type MacroFn func(BuildCtx, Expr) Expr
// Ctx contains all the Macros and Identifiers available. A Ctx also keeps a
// reference to the global context, which has a number of macros available for
// all contexts to use.
2016-07-29 00:39:18 +00:00
type Ctx struct {
global *Ctx
macros map[Macro]MacroFn
idents map[Identifier]Expr
}
// NewCtx returns a blank context instance
func NewCtx() *Ctx {
return &Ctx{
global: globalCtx,
macros: map[Macro]MacroFn{},
idents: map[Identifier]Expr{},
}
2016-07-29 00:39:18 +00:00
}
// GetMacro returns the MacroFn associated with the given identifier, or panics
// if the macro isn't found
func (c *Ctx) GetMacro(m Macro) MacroFn {
if fn := c.macros[m]; fn != nil {
return fn
2016-07-29 00:39:18 +00:00
}
if fn := c.global.macros[m]; fn != nil {
return fn
2016-07-29 00:39:18 +00:00
}
panicf("macro %q not found in context", m)
2016-07-29 00:39:18 +00:00
return nil
}
// GetIdentifier returns the llvm.Value for the Identifier, or panics
func (c *Ctx) GetIdentifier(i Identifier) Expr {
// The global context doesn't have any identifiers, so don't bother checking
return c.idents[i]
}
// NewWith returns a new Ctx instance which imports the given macros from the
// parent
//func (c *Ctx) NewWith(mm ...Macro) *Ctx {
// nc := &Ctx{
// global: c.global,
// macros: map[Macro]MacroFn{},
// }
// for _, m := range mm {
// fn := c.macros[m]
// if fn == nil {
// panicf("no macro %q found in context", m)
// }
// nc.macros[m] = fn
// }
// return nc
//}