1
0
mirror of https://github.com/golang/go synced 2024-11-23 05:50:05 -07:00
go/usr/austin/eval/scope.go
Austin Clements 816e3da26d Make Value always represent an l-value and never a generic
container for values.

Instead of having one evaluator function that returns a
generic Value, there is now an evaluator function for each
generalized type that simply returns a native type.

The compiler is more type-safe now because there are almost no
type conversions at evaluation time and it's impossible to
invoke a nil evaluator function during evaluation.  This also
makes ideals and pointers really clean.

As an added bonus, expression evaluation should be faster
because it doesn't require heap allocation for every
intermediate value, type switches, or lots of conversions to
and from Value.  It also involves fewer function calls.

R=rsc
APPROVED=rsc
DELTA=431  (280 added, 115 deleted, 36 changed)
OCL=31705
CL=31709
2009-07-15 17:56:17 -07:00

62 lines
1.1 KiB
Go

// 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";
)
func NewRootScope() *Scope {
return &Scope{nil, make(map[string] Def), 0};
}
func (s *Scope) Fork() *Scope {
return &Scope{s, make(map[string] Def), 0};
}
func (s *Scope) DefineVar(name string, t Type) *Variable {
if _, ok := s.defs[name]; ok {
return nil;
}
v := &Variable{s.numVars, t};
s.numVars++;
s.defs[name] = v;
return v;
}
func (s *Scope) DefineConst(name string, v Value) *Constant {
if _, ok := s.defs[name]; ok {
return nil;
}
c := &Constant{v.Type(), v};
s.defs[name] = c;
return c;
}
func (s *Scope) DefineType(name string, t Type) bool {
if _, ok := s.defs[name]; ok {
return false;
}
s.defs[name] = t;
return true;
}
func (s *Scope) Lookup(name string) (Def, *Scope) {
for s != nil {
if d, ok := s.defs[name]; ok {
return d, s;
}
s = s.outer;
}
return nil, nil;
}
func (f *Frame) Get(s *Scope, index int) Value {
for f.Scope != s {
f = f.Outer;
}
return f.Vars[index];
}