Commit 816e3da2 authored by Austin Clements's avatar Austin Clements

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
parent 3fc7cfd5
......@@ -136,3 +136,5 @@ type Frame struct {
Scope *Scope;
Vars []Value;
}
func (f *Frame) Get(s *Scope, index int) Value
This diff is collapsed.
......@@ -52,3 +52,10 @@ func (s *Scope) Lookup(name string) (Def, *Scope) {
}
return nil, nil;
}
func (f *Frame) Get(s *Scope, index int) Value {
for f.Scope != s {
f = f.Outer;
}
return f.Vars[index];
}
......@@ -59,6 +59,8 @@ func (boolType) String() string {
return "bool";
}
func (t *boolType) value(v bool) BoolValue
type uintType struct {
commonType;
......
......@@ -32,6 +32,11 @@ func (v *boolV) Set(x bool) {
*v = boolV(x);
}
func (t *boolType) value(v bool) BoolValue {
res := boolV(v);
return &res;
}
/*
* Uint
*/
......@@ -145,8 +150,8 @@ func (v *uintptrV) Set(x uint64) {
}
func (t *uintType) value(v uint64) UintValue {
// TODO(austin) This executes are run-time, even though
// virtually all of the logic can be done at type-check time.
// TODO(austin) The 'value' methods are only used for
// testing right now. Get rid of them.
// TODO(austin) Deal with named types
switch Type(t) {
case Uint8Type:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment