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 (
|
2009-07-31 16:51:27 -06:00
|
|
|
"go/token";
|
2009-07-29 12:57:46 -06:00
|
|
|
"log";
|
2009-07-15 12:59:13 -06:00
|
|
|
)
|
|
|
|
|
2009-08-28 19:25:56 -06:00
|
|
|
/*
|
|
|
|
* Blocks and scopes
|
|
|
|
*/
|
|
|
|
|
|
|
|
// A definition can be a *Variable, *Constant, or Type.
|
|
|
|
type Def interface {
|
|
|
|
Pos() token.Position;
|
|
|
|
}
|
|
|
|
|
|
|
|
type Variable struct {
|
|
|
|
token.Position;
|
|
|
|
// Index of this variable in the Frame structure
|
|
|
|
Index int;
|
|
|
|
// Static type of this variable
|
|
|
|
Type Type;
|
|
|
|
// Value of this variable. This is only used by Scope.NewFrame;
|
|
|
|
// therefore, it is useful for global scopes but cannot be used
|
|
|
|
// in function scopes.
|
|
|
|
Init Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
type Constant struct {
|
|
|
|
token.Position;
|
|
|
|
Type Type;
|
|
|
|
Value Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// A block represents a definition block in which a name may not be
|
|
|
|
// defined more than once.
|
|
|
|
type block struct {
|
|
|
|
// The block enclosing this one, including blocks in other
|
|
|
|
// scopes.
|
|
|
|
outer *block;
|
|
|
|
// The nested block currently being compiled, or nil.
|
|
|
|
inner *block;
|
|
|
|
// The Scope containing this block.
|
|
|
|
scope *Scope;
|
|
|
|
// The Variables, Constants, and Types defined in this block.
|
|
|
|
defs map[string] Def;
|
|
|
|
// The index of the first variable defined in this block.
|
|
|
|
// This must be greater than the index of any variable defined
|
|
|
|
// in any parent of this block within the same Scope at the
|
|
|
|
// time this block is entered.
|
|
|
|
offset int;
|
|
|
|
// The number of Variables defined in this block.
|
|
|
|
numVars int;
|
2009-09-03 17:20:49 -06:00
|
|
|
// If global, do not allocate new vars and consts in
|
|
|
|
// the frame; assume that the refs will be compiled in
|
|
|
|
// using defs[name].Init.
|
|
|
|
global bool;
|
2009-08-28 19:25:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// A Scope is the compile-time analogue of a Frame, which captures
|
|
|
|
// some subtree of blocks.
|
|
|
|
type Scope struct {
|
|
|
|
// The root block of this scope.
|
|
|
|
*block;
|
|
|
|
// The maximum number of variables required at any point in
|
|
|
|
// this Scope. This determines the number of slots needed in
|
|
|
|
// Frame's created from this Scope at run-time.
|
|
|
|
maxVars int;
|
|
|
|
}
|
|
|
|
|
2009-07-29 12:57:46 -06:00
|
|
|
func (b *block) enterChild() *block {
|
2009-08-24 14:48:16 -06:00
|
|
|
if b.inner != nil && b.inner.scope == b.scope {
|
2009-07-29 12:57:46 -06:00
|
|
|
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");
|
|
|
|
}
|
2009-08-24 14:48:16 -06:00
|
|
|
if b.outer.scope == b.scope {
|
|
|
|
if b.outer.inner != b {
|
|
|
|
log.Crash("Already exited block");
|
|
|
|
}
|
|
|
|
if b.inner != nil && b.inner.scope == b.scope {
|
|
|
|
log.Crash("Exit of parent block without exit of child block");
|
|
|
|
}
|
2009-07-29 12:57:46 -06:00
|
|
|
}
|
|
|
|
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 {
|
2009-08-24 14:48:16 -06:00
|
|
|
if b.inner != nil && b.inner.scope == b.scope {
|
2009-07-29 12:57:46 -06:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-07-31 16:51:27 -06:00
|
|
|
func (b *block) DefineVar(name string, pos token.Position, t Type) (*Variable, Def) {
|
|
|
|
if prev, ok := b.defs[name]; ok {
|
|
|
|
return nil, prev;
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
2009-09-03 17:20:49 -06:00
|
|
|
v := b.defineSlot(t, false);
|
2009-07-31 16:51:27 -06:00
|
|
|
v.Position = pos;
|
|
|
|
b.defs[name] = v;
|
|
|
|
return v, nil;
|
2009-07-27 14:01:23 -06:00
|
|
|
}
|
|
|
|
|
2009-09-03 17:20:49 -06:00
|
|
|
func (b *block) DefineTemp(t Type) *Variable {
|
|
|
|
return b.defineSlot(t, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *block) defineSlot(t Type, temp bool) *Variable {
|
2009-08-24 14:48:16 -06:00
|
|
|
if b.inner != nil && b.inner.scope == b.scope {
|
2009-07-29 12:57:46 -06:00
|
|
|
log.Crash("Failed to exit child block before defining variable");
|
|
|
|
}
|
2009-09-02 18:15:43 -06:00
|
|
|
index := -1;
|
2009-09-03 17:20:49 -06:00
|
|
|
if !b.global || temp {
|
2009-09-02 18:15:43 -06:00
|
|
|
index = b.offset+b.numVars;
|
|
|
|
b.numVars++;
|
2009-09-03 17:20:49 -06:00
|
|
|
if index >= b.scope.maxVars {
|
2009-09-02 18:15:43 -06:00
|
|
|
b.scope.maxVars = index+1;
|
|
|
|
}
|
2009-07-29 12:57:46 -06:00
|
|
|
}
|
2009-09-02 18:15:43 -06:00
|
|
|
v := &Variable{token.Position{}, index, t, nil};
|
2009-07-15 12:59:13 -06:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2009-09-04 13:55:25 -06:00
|
|
|
func (b *block) DefineConst(name string, pos token.Position, t Type, v Value) (*Constant, Def) {
|
|
|
|
if prev, ok := b.defs[name]; ok {
|
|
|
|
return nil, prev;
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
2009-07-31 16:51:27 -06:00
|
|
|
c := &Constant{pos, t, v};
|
2009-07-29 12:57:46 -06:00
|
|
|
b.defs[name] = c;
|
2009-09-04 13:55:25 -06:00
|
|
|
return c, nil;
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
|
|
|
|
2009-07-31 16:51:27 -06:00
|
|
|
func (b *block) DefineType(name string, pos token.Position, t Type) Type {
|
2009-07-29 12:57:46 -06:00
|
|
|
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-08-28 15:39:12 -06:00
|
|
|
nt := &NamedType{pos, name, nil, true, make(map[string] Method)};
|
2009-08-05 12:49:51 -06:00
|
|
|
if t != nil {
|
2009-08-28 15:39:12 -06:00
|
|
|
nt.Complete(t);
|
2009-08-05 12:49:51 -06:00
|
|
|
}
|
2009-07-29 12:57:46 -06:00
|
|
|
b.defs[name] = nt;
|
2009-07-27 14:01:23 -06:00
|
|
|
return nt;
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
|
|
|
|
2009-09-02 18:15:43 -06:00
|
|
|
func (b *block) Lookup(name string) (bl *block, level int, def Def) {
|
2009-07-29 12:57:46 -06:00
|
|
|
for b != nil {
|
|
|
|
if d, ok := b.defs[name]; ok {
|
2009-09-02 18:15:43 -06:00
|
|
|
return b, 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-09-02 18:15:43 -06:00
|
|
|
return nil, 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-09-03 17:20:49 -06:00
|
|
|
return outer.child(s.maxVars);
|
2009-07-21 14:31:23 -06:00
|
|
|
}
|
|
|
|
|
2009-08-28 19:25:56 -06:00
|
|
|
/*
|
|
|
|
* Frames
|
|
|
|
*/
|
|
|
|
|
|
|
|
type Frame struct {
|
|
|
|
Outer *Frame;
|
|
|
|
Vars []Value;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|