// Nesting levels
expr_lev int; // 0 = control clause level, 1 = expr inside ()'s
scope_lev int; // 0 = global scope, 1 = function scope of global functions, etc.
+
+ // Scopes
+ top_scope *Globals.Scope;
};
// ----------------------------------------------------------------------------
-// Support functions
+// Elementary support
+
+func unimplemented() {
+ panic("unimplemented");
+}
+
+
+func unreachable() {
+ panic("unreachable");
+}
+
+
+func assert(pred bool) {
+ if !pred {
+ panic("assertion failed");
+ }
+}
+
+
+// ----------------------------------------------------------------------------
+// Parsing support
func (P *Parser) PrintIndent() {
for i := P.indent; i > 0; i-- {
}
+// ----------------------------------------------------------------------------
+// Scopes
+
+func (P *Parser) OpenScope() {
+ P.top_scope = Globals.NewScope(P.top_scope);
+}
+
+
+func (P *Parser) CloseScope() {
+ P.top_scope = P.top_scope.parent;
+}
+
+
+func (P *Parser) Lookup(ident string) *Globals.Object {
+ for scope := P.top_scope; scope != nil; scope = scope.parent {
+ obj := scope.Lookup(ident);
+ if obj != nil {
+ return obj;
+ }
+ }
+ return nil;
+}
+
+
+func (P *Parser) DeclareInScope(scope *Globals.Scope, x *AST.Expr, kind int) {
+ if P.scope_lev < 0 {
+ panic("cannot declare objects in other packages");
+ }
+ obj := x.obj;
+ assert(x.tok == Scanner.IDENT && obj.kind == Object.NONE);
+ obj.kind = kind;
+ obj.pnolev = P.scope_lev;
+ if scope.Lookup(obj.ident) != nil {
+ P.Error(obj.pos, `"` + obj.ident + `" is declared already`);
+ return; // don't insert it into the scope
+ }
+ scope.Insert(obj);
+}
+
+
+// Declare a comma-separated list of idents or a single ident.
+func (P *Parser) Declare(p *AST.Expr, kind int) {
+ for p.tok == Scanner.COMMA {
+ P.DeclareInScope(P.top_scope, p.x, kind);
+ p = p.y;
+ }
+ P.DeclareInScope(P.top_scope, p, kind);
+}
+
+
// ----------------------------------------------------------------------------
// AST support
func (P *Parser) ParseIdentList() *AST.Expr {
P.Trace("IdentList");
+ var last *AST.Expr;
x := P.ParseIdent();
- for first := true; P.tok == Scanner.COMMA; {
+ for P.tok == Scanner.COMMA {
pos := P.pos;
P.Next();
y := P.ParseIdent();
- if first {
+ if last == nil {
x = P.NewExpr(pos, Scanner.COMMA, x, y);
- first = false;
+ last = x;
} else {
- x.y = P.NewExpr(pos, Scanner.COMMA, x.y, y);
+ last.y = P.NewExpr(pos, Scanner.COMMA, last.y, y);
+ last = last.y;
}
}
func (P *Parser) ParseFunctionType() *AST.Type {
P.Trace("FunctionType");
+ P.OpenScope();
+ P.scope_lev++;
+
t := AST.NewType(P.pos, Scanner.LPAREN);
t.list = P.ParseParameters(true).list; // TODO find better solution
t.end = P.pos;
t.elt = P.ParseResult();
+ P.scope_lev--;
+ P.CloseScope();
+
P.Ecart();
return t;
}
P.Expect(Scanner.INTERFACE);
if P.tok == Scanner.LBRACE {
P.Next();
+ P.OpenScope();
+ P.scope_lev++;
+
t.list = array.New(0);
for P.tok == Scanner.IDENT {
P.ParseMethodSpec(t.list);
}
}
t.end = P.pos;
+
+ P.scope_lev--;
+ P.CloseScope();
P.Expect(Scanner.RBRACE);
}
P.Expect(Scanner.STRUCT);
if P.tok == Scanner.LBRACE {
P.Next();
+ P.OpenScope();
+ P.scope_lev++;
+
t.list = array.New(0);
for P.tok != Scanner.RBRACE && P.tok != Scanner.EOF {
P.ParseVarDeclList(t.list, false);
}
P.OptSemicolon();
t.end = P.pos;
+
+ P.scope_lev--;
+ P.CloseScope();
P.Expect(Scanner.RBRACE);
}
P.Trace("Block");
P.Expect(Scanner.LBRACE);
+ P.OpenScope();
+
slist = P.ParseStatementList();
end = P.pos;
+
+ P.CloseScope();
P.Expect(Scanner.RBRACE);
P.opt_semi = true;
singles = false;
}
- for first := true; P.tok != Scanner.RBRACE && P.tok != Scanner.EOF; {
+ var last *AST.Expr;
+ for P.tok != Scanner.RBRACE && P.tok != Scanner.EOF {
y := P.ParseExpression(0);
if singles {
}
}
- if first {
+ if last == nil {
x = P.NewExpr(pos, Scanner.COMMA, x, y);
+ last = x;
} else {
- x.y = P.NewExpr(pos, Scanner.COMMA, x.y, y);
+ last.y = P.NewExpr(pos, Scanner.COMMA, last.y, y);
+ last = last.y;
}
if P.tok == Scanner.COMMA {
func (P *Parser) ParseIfStat() *AST.Stat {
P.Trace("IfStat");
+ P.OpenScope();
s := P.ParseControlClause(Scanner.IF);
s.block, s.end = P.ParseBlock();
if P.tok == Scanner.ELSE {
}
s.post = s1;
}
+ P.CloseScope();
P.Ecart();
return s;
func (P *Parser) ParseForStat() *AST.Stat {
P.Trace("ForStat");
+ P.OpenScope();
s := P.ParseControlClause(Scanner.FOR);
s.block, s.end = P.ParseBlock();
+ P.CloseScope();
P.Ecart();
return s;
func (P *Parser) ParseSwitchStat() *AST.Stat {
P.Trace("SwitchStat");
+ P.OpenScope();
s := P.ParseControlClause(Scanner.SWITCH);
s.block = array.New(0);
P.Expect(Scanner.LBRACE);
s.end = P.pos;
P.Expect(Scanner.RBRACE);
P.opt_semi = true;
+ P.CloseScope();
P.Ecart();
return s;
P.Expect(Scanner.STRING); // use Expect() error handling
}
+ if d.ident != nil {
+ P.Declare(d.ident, Object.PACKAGE);
+ }
+
P.Ecart();
return d;
}
P.Next();
d.val = P.ParseExpressionList();
}
+
+ P.Declare(d.ident, Object.CONST);
P.Ecart();
return d;
}
}
+ P.Declare(d.ident, Object.VAR);
+
P.Ecart();
return d;
}
func (P *Parser) ParseProgram() *AST.Program {
P.Trace("Program");
+ P.OpenScope();
p := AST.NewProgram(P.pos);
P.Expect(Scanner.PACKAGE);
p.ident = P.ParseIdent();
- p.decls = array.New(0);
- for P.tok == Scanner.IMPORT {
- p.decls.Push(P.ParseDecl(false, Scanner.IMPORT));
- P.OptSemicolon();
- }
-
- if !P.deps {
- for P.tok != Scanner.EOF {
- p.decls.Push(P.ParseDeclaration());
+ // package body
+ { P.OpenScope();
+ p.decls = array.New(0);
+ for P.tok == Scanner.IMPORT {
+ p.decls.Push(P.ParseDecl(false, Scanner.IMPORT));
P.OptSemicolon();
}
+ if !P.deps {
+ for P.tok != Scanner.EOF {
+ p.decls.Push(P.ParseDeclaration());
+ P.OptSemicolon();
+ }
+ }
+ P.CloseScope();
}
p.comments = P.comments;
+ P.CloseScope();
P.Ecart();
return p;
type State struct {
// setup
err Scanner.ErrorHandler;
-
- // state
- level int;
- top_scope *Globals.Scope;
}
}
-// ----------------------------------------------------------------------------
-// Scopes
-
-func (s *State) OpenScope() {
- s.top_scope = Globals.NewScope(s.top_scope);
-}
-
-
-func (s *State) CloseScope() {
- s.top_scope = s.top_scope.parent;
-}
-
-
-func (s *State) Lookup(ident string) *Globals.Object {
- for scope := s.top_scope; scope != nil; scope = scope.parent {
- obj := scope.Lookup(ident);
- if obj != nil {
- return obj;
- }
- }
- return nil;
-}
-
-
-func (s *State) DeclareInScope(scope *Globals.Scope, obj *Globals.Object) {
- if s.level > 0 {
- panic("cannot declare objects in other packages");
- }
- obj.pnolev = s.level;
- if scope.Lookup(obj.ident) != nil {
- s.Error(obj.pos, `"` + obj.ident + `" is declared already`);
- return; // don't insert it into the scope
- }
- scope.Insert(obj);
-}
-
-
-func (s *State) Declare(obj *Globals.Object) {
- s.DeclareInScope(s.top_scope, obj);
-}
-
-
-// ----------------------------------------------------------------------------
-// Common productions
-
-func (s *State) DeclareIdent(ident *AST.Expr, kind int, typ *AST.Type) {
- // ident is either a comma-separated list or a single ident
- switch ident.tok {
- case Scanner.IDENT:
- obj := Globals.NewObject(ident.pos, kind, ident.obj.ident);
- s.Declare(obj);
- case Scanner.COMMA:
- s.DeclareIdent(ident.x, kind, typ);
- s.DeclareIdent(ident.y, kind, typ);
- default:
- unreachable();
- }
-}
-
-
// ----------------------------------------------------------------------------
func (s *State) CheckType() {
// single declaration
switch d.tok {
case Scanner.IMPORT:
- assert(d.ident == nil || d.ident.tok == Scanner.IDENT);
- if d.ident != nil {
- s.DeclareIdent(d.ident, d.tok, d.typ);
- } else {
- }
-
case Scanner.EXPORT:
- // TODO
-
case Scanner.CONST:
- s.DeclareIdent(d.ident, d.tok, d.typ);
-
case Scanner.VAR:
- s.DeclareIdent(d.ident, d.tok, d.typ);
-
case Scanner.TYPE:
- assert(d.ident.tok == Scanner.IDENT);
- // types may be forward-declared
- obj := s.Lookup(d.ident.obj.ident);
- if obj != nil {
- // TODO check if proper forward-declaration
-
- } else {
- s.DeclareIdent(d.ident, d.tok, d.typ);
- }
-
case Scanner.FUNC:
- assert(d.ident.tok == Scanner.IDENT);
- if d.typ.key != nil {
- // method
- // TODO
- } else {
- // functions may be forward-declared
- obj := s.Lookup(d.ident.obj.ident);
- if obj != nil {
- // TODO check if proper forward-declaration
-
- } else {
- s.DeclareIdent(d.ident, d.tok, d.typ);
- }
- }
-
default:
unreachable();
}
func (s *State) CheckProgram(p *AST.Program) {
- s.OpenScope();
-
- { s.OpenScope();
- for i := 0; i < p.decls.Len(); i++ {
- s.CheckDeclaration(p.decls.At(i).(*AST.Decl));
- }
- s.CloseScope();
+ for i := 0; i < p.decls.Len(); i++ {
+ s.CheckDeclaration(p.decls.At(i).(*AST.Decl));
}
-
- s.CloseScope();
}