diff options
author | Austin Clements <aclements@csail.mit.edu> | 2009-07-27 13:01:23 -0700 |
---|---|---|
committer | Austin Clements <aclements@csail.mit.edu> | 2009-07-27 13:01:23 -0700 |
commit | 3aef0e90780731f844867bd8d86772e98b8ad583 (patch) | |
tree | f71e1ce1d782f4b76859763c402c654a1e79bc54 /usr/austin/eval/compiler.go | |
parent | 2fbe664ef546cf6c0fc72bb94099d637d29a204d (diff) | |
download | golang-3aef0e90780731f844867bd8d86772e98b8ad583.tar.gz |
Implement single-valued, non-variadic function literals and
function calling. Implement a type compiler and named types.
Implement a universal scope containing built-in named types,
and some built-in constants. Implement a simple virtual
machine for executing statements and single-valued return.
Fix many places that incorrectly dealt with named types. In
particular, the Type.Zero methods now use the type's bit count
to determine the appropriate value representation. As a
result, a bit count of 0 now means architecture-dependent and
bounded types use unsafe.Sizeof to determine the correct
bounds. Previously, the bounds on a 32-bit machine would have
been wrong.
Eliminated Type.compatible, since the implementation is
equivalent for all types. Added Type.rep that shallowly
strips named types. Replaced almost all uses of Type.literal
with Type.rep.
Fix implementation of assign-op's so it only evaluates the
left side once. As part of this, there is now a generic way
to separate out the effect and value of an expression.
R=rsc
APPROVED=rsc
DELTA=1530 (1244 added, 68 deleted, 218 changed)
OCL=32184
CL=32230
Diffstat (limited to 'usr/austin/eval/compiler.go')
-rw-r--r-- | usr/austin/eval/compiler.go | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/usr/austin/eval/compiler.go b/usr/austin/eval/compiler.go new file mode 100644 index 000000000..ab505dec1 --- /dev/null +++ b/usr/austin/eval/compiler.go @@ -0,0 +1,79 @@ +// 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"; + "fmt"; + "go/ast"; + "go/scanner"; + "go/token"; +) + + +type positioned interface { + Pos() token.Position; +} + + +// A compiler captures information used throughout an entire +// compilation. Currently it includes only the error handler. +// +// TODO(austin) This might actually represent package level, in which +// case it should be package compiler. +type compiler struct { + errors scanner.ErrorHandler; +} + +func (a *compiler) diagAt(pos positioned, format string, args ...) { + a.errors.Error(pos.Pos(), fmt.Sprintf(format, args)); +} + +type FuncDecl struct +func (a *compiler) compileFunc(scope *Scope, decl *FuncDecl, body *ast.BlockStmt) (func (f *Frame) Func) +type exprCompiler struct +func (a *compiler) compileExpr(scope *Scope, expr ast.Expr, constant bool) *exprCompiler +func (a *compiler) compileType(scope *Scope, typ ast.Expr) Type +func (a *compiler) compileFuncType(scope *Scope, typ *ast.FuncType) *FuncDecl + +func (a *compiler) compileArrayLen(scope *Scope, expr ast.Expr) (int64, bool) + + +type codeBuf struct +// A funcCompiler captures information used throughout the compilation +// of a single function body. +type funcCompiler struct { + *compiler; + outVars []*Variable; + // Whether the out variables are named. This affects what + // kinds of return statements are legal. + outVarsNamed bool; + *codeBuf; + err bool; +} + + +// A blockCompiler captures information used throughout the compilation +// of a single block within a function. +type blockCompiler struct { + *funcCompiler; + scope *Scope; + returned bool; +} + +func (a *blockCompiler) compileBlock(body *ast.BlockStmt) + + +// An exprContext stores information used throughout the compilation +// of a single expression. It does not embed funcCompiler because +// expressions can appear at top level. +// +// TODO(austin) Rename exprCompiler to exprNodeCompiler and rename +// this to exprCompiler. +type exprContext struct { + *compiler; + scope *Scope; + constant bool; +} |