2009-07-27 14:01:23 -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 (
|
|
|
|
"fmt";
|
|
|
|
"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;
|
2009-08-27 12:21:52 -06:00
|
|
|
numErrors int;
|
|
|
|
silentErrors int;
|
2009-07-27 14:01:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *compiler) diagAt(pos positioned, format string, args ...) {
|
|
|
|
a.errors.Error(pos.Pos(), fmt.Sprintf(format, args));
|
2009-08-27 12:21:52 -06:00
|
|
|
a.numErrors++;
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *compiler) numError() int {
|
|
|
|
return a.numErrors + a.silentErrors;
|
2009-07-27 14:01:23 -06:00
|
|
|
}
|
|
|
|
|
2009-08-28 19:25:56 -06:00
|
|
|
// The universal scope
|
|
|
|
func newUniverse() *Scope {
|
|
|
|
sc := &Scope{nil, 0};
|
|
|
|
sc.block = &block{
|
2009-09-03 17:20:49 -06:00
|
|
|
offset: 0,
|
2009-08-28 19:25:56 -06:00
|
|
|
scope: sc,
|
2009-09-03 17:20:49 -06:00
|
|
|
global: true,
|
2009-08-28 19:25:56 -06:00
|
|
|
defs: make(map[string] Def)
|
|
|
|
};
|
|
|
|
return sc;
|
|
|
|
}
|
|
|
|
var universe *Scope = newUniverse();
|
|
|
|
|
2009-07-27 14:01:23 -06:00
|
|
|
|
2009-08-27 12:21:52 -06:00
|
|
|
// TODO(austin) These can all go in stmt.go now
|
2009-07-30 15:39:27 -06:00
|
|
|
type label struct {
|
|
|
|
name string;
|
|
|
|
desc string;
|
|
|
|
// The PC goto statements should jump to, or nil if this label
|
|
|
|
// cannot be goto'd (such as an anonymous for loop label).
|
|
|
|
gotoPC *uint;
|
|
|
|
// The PC break statements should jump to, or nil if a break
|
|
|
|
// statement is invalid.
|
|
|
|
breakPC *uint;
|
|
|
|
// The PC continue statements should jump to, or nil if a
|
|
|
|
// continue statement is invalid.
|
|
|
|
continuePC *uint;
|
|
|
|
// The position where this label was resolved. If it has not
|
|
|
|
// been resolved yet, an invalid position.
|
|
|
|
resolved token.Position;
|
|
|
|
// The position where this label was first jumped to.
|
|
|
|
used token.Position;
|
|
|
|
}
|
|
|
|
|
2009-07-27 14:01:23 -06:00
|
|
|
// A funcCompiler captures information used throughout the compilation
|
|
|
|
// of a single function body.
|
|
|
|
type funcCompiler struct {
|
|
|
|
*compiler;
|
2009-07-27 18:32:35 -06:00
|
|
|
fnType *FuncType;
|
2009-07-27 14:01:23 -06:00
|
|
|
// Whether the out variables are named. This affects what
|
|
|
|
// kinds of return statements are legal.
|
|
|
|
outVarsNamed bool;
|
|
|
|
*codeBuf;
|
2009-07-30 15:39:27 -06:00
|
|
|
flow *flowBuf;
|
|
|
|
labels map[string] *label;
|
2009-07-27 14:01:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// A blockCompiler captures information used throughout the compilation
|
|
|
|
// of a single block within a function.
|
|
|
|
type blockCompiler struct {
|
|
|
|
*funcCompiler;
|
2009-07-29 12:57:46 -06:00
|
|
|
block *block;
|
2009-07-30 15:39:27 -06:00
|
|
|
// The label of this block, used for finding break and
|
|
|
|
// continue labels.
|
|
|
|
label *label;
|
2009-07-28 15:37:06 -06:00
|
|
|
// The blockCompiler for the block enclosing this one, or nil
|
|
|
|
// for a function-level block.
|
|
|
|
parent *blockCompiler;
|
2009-07-27 14:01:23 -06:00
|
|
|
}
|