umx_compiler

UMX virtual machine "Monkey" interpreter / bytecode compiler
git clone git://bsandro.tech/umx_compiler
Log | Files | Refs | README | LICENSE

context.go (555B)


      1 package object
      2 
      3 type Context struct {
      4 	store  map[string]Object
      5 	parent *Context
      6 }
      7 
      8 func NewContext() *Context {
      9 	s := make(map[string]Object)
     10 	return &Context{store: s, parent: nil}
     11 }
     12 
     13 func (ctx *Context) Get(name string) (Object, bool) {
     14 	obj, ok := ctx.store[name]
     15 	if !ok && ctx.parent != nil {
     16 		obj, ok = ctx.parent.Get(name)
     17 	}
     18 	return obj, ok
     19 }
     20 
     21 func (ctx *Context) Set(name string, obj Object) Object {
     22 	ctx.store[name] = obj
     23 	return obj
     24 }
     25 
     26 func NewChildContext(parent *Context) *Context {
     27 	ctx := NewContext()
     28 	ctx.parent = parent
     29 	return ctx
     30 }