2009-07-15 12:59:13 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package eval
|
|
|
|
|
|
|
|
import (
|
|
|
|
"eval";
|
2009-07-27 14:01:23 -06:00
|
|
|
"fmt";
|
2009-07-29 12:57:46 -06:00
|
|
|
"log";
|
2009-07-15 12:59:13 -06:00
|
|
|
)
|
|
|
|
|
2009-07-29 12:57:46 -06:00
|
|
|
func (b *block) enterChild() *block {
|
|
|
|
if b.inner != nil {
|
|
|
|
log.Crash("Failed to exit child block before entering another child");
|
|
|
|
}
|
|
|
|
sub := &block{
|
|
|
|
outer: b,
|
|
|
|
scope: b.scope,
|
2009-07-27 14:01:23 -06:00
|
|
|
defs: make(map[string] Def),
|
2009-07-29 12:57:46 -06:00
|
|
|
offset: b.offset+b.numVars,
|
2009-07-27 14:01:23 -06:00
|
|
|
};
|
2009-07-29 12:57:46 -06:00
|
|
|
b.inner = sub;
|
|
|
|
return sub;
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *block) exit() {
|
|
|
|
if b.outer == nil {
|
|
|
|
log.Crash("Cannot exit top-level block");
|
|
|
|
}
|
|
|
|
if b.outer.inner != b {
|
|
|
|
log.Crash("Already exited block");
|
|
|
|
}
|
|
|
|
if b.inner != nil {
|
|
|
|
log.Crash("Exit of parent block without exit of child block");
|
|
|
|
}
|
|
|
|
b.outer.inner = nil;
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
|
|
|
|
2009-07-29 12:57:46 -06:00
|
|
|
func (b *block) ChildScope() *Scope {
|
|
|
|
if b.inner != nil {
|
|
|
|
log.Crash("Failed to exit child block before entering a child scope");
|
|
|
|
}
|
|
|
|
sub := b.enterChild();
|
|
|
|
sub.offset = 0;
|
|
|
|
sub.scope = &Scope{sub, 0};
|
|
|
|
return sub.scope;
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *block) DefineVar(name string, t Type) *Variable {
|
|
|
|
if _, ok := b.defs[name]; ok {
|
2009-07-15 12:59:13 -06:00
|
|
|
return nil;
|
|
|
|
}
|
2009-07-29 12:57:46 -06:00
|
|
|
v := b.DefineTemp(t);
|
|
|
|
if v != nil {
|
|
|
|
b.defs[name] = v;
|
|
|
|
}
|
2009-07-27 14:01:23 -06:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2009-07-29 12:57:46 -06:00
|
|
|
func (b *block) DefineTemp(t Type) *Variable {
|
|
|
|
if b.inner != nil {
|
|
|
|
log.Crash("Failed to exit child block before defining variable");
|
|
|
|
}
|
|
|
|
index := b.offset+b.numVars;
|
|
|
|
v := &Variable{index, t};
|
|
|
|
b.numVars++;
|
|
|
|
if index+1 > b.scope.maxVars {
|
|
|
|
b.scope.maxVars = index+1;
|
|
|
|
}
|
2009-07-15 12:59:13 -06:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2009-07-29 12:57:46 -06:00
|
|
|
func (b *block) DefineConst(name string, t Type, v Value) *Constant {
|
|
|
|
if _, ok := b.defs[name]; ok {
|
2009-07-15 12:59:13 -06:00
|
|
|
return nil;
|
|
|
|
}
|
2009-07-21 14:31:23 -06:00
|
|
|
c := &Constant{t, v};
|
2009-07-29 12:57:46 -06:00
|
|
|
b.defs[name] = c;
|
2009-07-15 12:59:13 -06:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2009-07-29 12:57:46 -06:00
|
|
|
func (b *block) DefineType(name string, t Type) Type {
|
|
|
|
if _, ok := b.defs[name]; ok {
|
2009-07-27 14:01:23 -06:00
|
|
|
return nil;
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
2009-07-27 14:01:23 -06:00
|
|
|
// We take the representative type of t because multiple
|
|
|
|
// levels of naming are useless.
|
2009-07-29 12:57:46 -06:00
|
|
|
nt := &NamedType{name, t.rep()};
|
|
|
|
b.defs[name] = nt;
|
2009-07-27 14:01:23 -06:00
|
|
|
return nt;
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
|
|
|
|
2009-07-29 12:57:46 -06:00
|
|
|
func (b *block) Lookup(name string) (level int, def Def) {
|
|
|
|
for b != nil {
|
|
|
|
if d, ok := b.defs[name]; ok {
|
|
|
|
return level, d;
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
2009-07-29 12:57:46 -06:00
|
|
|
if b.outer != nil && b.scope != b.outer.scope {
|
|
|
|
level++;
|
|
|
|
}
|
|
|
|
b = b.outer;
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
2009-07-29 12:57:46 -06:00
|
|
|
return 0, nil;
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
2009-07-15 18:56:17 -06:00
|
|
|
|
2009-07-21 14:31:23 -06:00
|
|
|
func (s *Scope) NewFrame(outer *Frame) *Frame {
|
2009-07-29 12:57:46 -06:00
|
|
|
return outer.child(s.maxVars);
|
2009-07-21 14:31:23 -06:00
|
|
|
}
|
|
|
|
|
2009-07-29 12:57:46 -06:00
|
|
|
func (f *Frame) Get(level int, index int) Value {
|
|
|
|
for ; level > 0; level-- {
|
2009-07-15 18:56:17 -06:00
|
|
|
f = f.Outer;
|
|
|
|
}
|
|
|
|
return f.Vars[index];
|
|
|
|
}
|
2009-07-27 14:01:23 -06:00
|
|
|
|
2009-07-29 12:57:46 -06:00
|
|
|
func (f *Frame) child(numVars int) *Frame {
|
|
|
|
// TODO(austin) This is probably rather expensive. All values
|
|
|
|
// require heap allocation and zeroing them when we execute a
|
|
|
|
// definition typically requires some computation.
|
|
|
|
return &Frame{f, make([]Value, numVars)};
|
2009-07-27 14:01:23 -06:00
|
|
|
}
|