-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDef.g
More file actions
174 lines (148 loc) · 4.72 KB
/
Def.g
File metadata and controls
174 lines (148 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
tree grammar Def;
options {
tokenVocab = Sneakers;
ASTLabelType = SneakersAST;
filter = true;
}
@header { package sneakers; }
@members {
SymbolTable symtab;
Scope currentScope;
public Def(TreeNodeStream input, SymbolTable symtab) {
this(input);
this.symtab = symtab;
currentScope = symtab.globals;
}
private void print(String s) {
//System.out.println(s);
}
}
// END: header
topdown
: enterBlock
//| enterMethod
| enterClass
| varDeclaration
| atoms
;
bottomup
: exitBlock
| exitMethod
| exitClass
;
// S C O P E S
enterBlock
: BLOCK
{
print("enter block");
print("PUSH SCOPE");
currentScope = new LocalScope(currentScope);
} // push scope
;
exitBlock
: BLOCK
{
print("OUT OF BLOCK");
print("locals: "+currentScope);
print("POP SCOPE");
currentScope = currentScope.getEnclosingScope(); // pop scope
}
;
// START: class
enterClass
: ^('class' name=TYPEID .*)
{ // def class but leave superclass blank until ref phase
print("line "+$name.getLine()+
": def class "+$name.text);
// record scope in AST for next pass
ClassSymbol cs = new ClassSymbol($name.text,currentScope,null);
cs.def = $name; // point from symbol table into AST
$name.symbol = cs; // point from AST into symbol table
currentScope.define(cs); // def class in current scope
print("PUSH SCOPE");
currentScope = cs; // set current scope to class scope
}
;
// END: class*/
exitClass
: CLASSDEF
{
print("OUT OF CLASS");
print("members: "+currentScope);
print("POP SCOPE");
currentScope = currentScope.getEnclosingScope(); // pop scope
}
;
exitMethod
: FNDECL
{
print("OUT OF METHOD");
print("args: "+currentScope);
print("POP SCOPE");
currentScope = currentScope.getEnclosingScope(); // pop arg scope
}
;
// START: atoms
/** Set scope for any identifiers in expressions or assignments */
atoms
@init {SneakersAST t = (SneakersAST)input.LT(1);}
: {t.hasAncestor(EXPR)||t.hasAncestor(ASSIGN)}? ('this'|ID|TYPEID)
{t.scope = currentScope;}
;
//END: atoms
// START: var
varDeclaration // global, parameter, or local variable
: ^('=' ID ^(FNDECL TYPEID .*))
{
print("line "+$ID.getLine()+": def "+$ID.text);
MethodSymbol ms = new MethodSymbol($ID.text,null,currentScope);
$TYPEID.scope = currentScope;
ms.def = $ID; // track AST location of def's ID
$ID.symbol = ms; // track in AST
currentScope.define(ms); // def method in globals
print("PUSH SCOPE");
currentScope = ms; // set current scope to method scope
}
| ^('=' ID ~(FNDECL)* )
{
print("line "+$ID.getLine()+": def "+$ID.text);
VariableSymbol vs = new VariableSymbol($ID.text,null);
vs.scope = currentScope;
vs.def = $ID; // track AST location of def's ID
$ID.symbol = vs; // track in AST
currentScope.define(vs);
}
| ^(FNPARAM ID TYPEID)
{
print("fn param line "+$ID.getLine()+": def "+$ID.text);
VariableSymbol vs = new VariableSymbol($ID.text,null);
$TYPEID.scope = currentScope;
vs.scope = currentScope;
vs.def = $ID; // track AST location of def's ID
$ID.symbol = vs; // track in AST
currentScope.define(vs);
}
| ^(FIELDDEF KEYWORD ^(~FNDECL .*))
{
print("class var line "+$KEYWORD.getLine()+": def "+$KEYWORD.text);
String text = $KEYWORD.text.substring(1);
VariableSymbol vs = new VariableSymbol(text, null);
vs.scope = currentScope;
vs.def = $KEYWORD; // track AST location of def's ID
$KEYWORD.symbol = vs; // track in AST
currentScope.define(vs);
}
| ^(METHODDEF KEYWORD ^(FNDECL .+))
{
print("class method line "+$KEYWORD.getLine()+": def "+$KEYWORD.text);
String text = $KEYWORD.text.substring(1);
MethodSymbol ms = new MethodSymbol(text,null,currentScope);
ms.scope = currentScope;
ms.def = $KEYWORD; // track AST location of def's ID
$KEYWORD.symbol = ms; // track in AST
currentScope.define(ms);
print("PUSH SCOPE");
currentScope = ms;
}
;
// END: field