@@ -90,7 +90,12 @@ public TypeDictionary createAST(String src) {
90
90
91
91
92
92
private void populateDefaultTypes (Map <String ,SONType > types ) {
93
+ // Pre-create int, [int] and *[int] types
93
94
types .put (typeDictionary .INT .name (), SONTypeInteger .BOT );
95
+ var intArrayType = SONTypeStruct .makeAry (SONTypeInteger .U32 , _code .getALIAS (), SONTypeInteger .BOT , _code .getALIAS ());
96
+ var ptrIntArrayType = SONTypeMemPtr .make (intArrayType );
97
+ types .put ("[" + typeDictionary .INT .name () + "]" , ptrIntArrayType );
98
+ // Also get the types created by default
94
99
for (SONType t : SONType .gather ()) {
95
100
types .put (t .str (), t );
96
101
}
@@ -145,26 +150,31 @@ private void createSONStructType(Map<String, SONType> structTypes, String typeNa
145
150
Type type = typeStruct .getField (name ); // FIXME
146
151
fs .push (new Field (name ,getSONType (structTypes ,type ),_code .getALIAS (),false ));
147
152
}
153
+ // A Struct type may have been created before because of
154
+ // reference from itself; in which case we need to update that
148
155
SONType fref = structTypes .get (typeName );
149
156
if (fref != null ) {
150
- if (fref instanceof SONTypeStruct ts ) {
157
+ if (fref instanceof SONTypeMemPtr ptr &&
158
+ ptr ._obj instanceof SONTypeStruct ts ) {
159
+ assert ts ._fields .length == 0 ;
160
+ // Add the fields to the existing type
151
161
ts ._fields = fs .asAry ();
152
162
}
153
- else throw new CompilerException ("" );
163
+ else throw new CompilerException ("Expected struct type " + typeName + " but got " + fref );
154
164
}
155
165
else {
156
- structTypes .put (typeName , new SONTypeStruct (typeName , fs .asAry ()));
166
+ var ts = SONTypeStruct .make (typeName , fs .asAry ());
167
+ var ptr = SONTypeMemPtr .make ((byte )2 ,ts );
168
+ structTypes .put (typeName ,ptr );
157
169
}
158
- // SONTypeStruct ts = SONTypeStruct.make(typeName, fs.asAry());
159
- // TYPES.put(typeName, SONTypeMemPtr.make(ts));
160
170
}
161
171
162
172
private String getSONTypeName (Type type ) {
163
173
if (type instanceof Type .TypeFunction typeFunction ) {
164
174
return typeFunction .name ;
165
175
}
166
176
else if (type instanceof Type .TypeArray typeArray ) {
167
- return typeArray .name () ;
177
+ return "*[" + getSONTypeName ( typeArray .getElementType ()) + "]" ;
168
178
}
169
179
else if (type instanceof Type .TypeStruct typeStruct ) {
170
180
return "*" + typeStruct .name ;
@@ -183,36 +193,25 @@ else if (type instanceof Type.TypeNullable typeNullable) {
183
193
}
184
194
185
195
private SONType getSONType (Map <String , SONType > structTypes , Type type ) {
186
- String tname = getSONTypeName (type );
187
- SONType t = structTypes .get (tname );
196
+ SONType t = structTypes .get (type .name ());
188
197
if (t != null ) return t ;
189
198
if (type instanceof Type .TypeStruct ) {
190
- SONType existing = structTypes .get (type .name );
191
- SONTypeStruct ts ;
192
- if (existing instanceof SONTypeStruct tsExisting )
193
- ts = tsExisting ;
194
- else {
195
- ts = new SONTypeStruct (type .name , new Field [0 ]);
196
- structTypes .put (ts .str (), ts );
197
- }
198
- SONTypeMemPtr ptr = new SONTypeMemPtr ((byte )2 ,ts );
199
- if (type .name ().equals (ts .str ())) {
200
- structTypes .put (ptr .str (), ptr );
201
- return ptr ;
202
- }
203
- else throw new CompilerException ("Unexpected error" );
199
+ // For struct types in EeZee language a reference
200
+ // to T means *T in SoN
201
+ // Create SON struct type
202
+ SONTypeStruct ts = SONTypeStruct .make (type .name , new Field [0 ]);
203
+ // Now create *T
204
+ SONTypeMemPtr ptr = SONTypeMemPtr .make ((byte )2 ,ts );
205
+ // EeZee T maps to SoN *T
206
+ structTypes .put (type .name (), ptr );
207
+ return ptr ;
204
208
}
205
209
else if (type instanceof Type .TypeArray typeArray ) {
210
+ // A reference to array in EeZee means
211
+ // *array in SoN
206
212
SONType elementType = getSONType (structTypes ,typeArray .getElementType ());
207
- SONTypeStruct ts = null ;
208
- SONType existing = structTypes .get (typeArray .name ());
209
- if (existing instanceof SONTypeStruct tsExisting )
210
- ts = tsExisting ;
211
- else {
212
- ts = SONTypeStruct .makeArray (SONTypeInteger .U32 , _code .getALIAS (), elementType , _code .getALIAS ());
213
- structTypes .put (ts .str (), ts );
214
- }
215
- SONTypeMemPtr ptr = new SONTypeMemPtr ((byte )2 ,ts );
213
+ SONTypeStruct ts = SONTypeStruct .makeArray (SONTypeInteger .U32 , _code .getALIAS (), elementType , _code .getALIAS ());
214
+ SONTypeMemPtr ptr = SONTypeMemPtr .make ((byte )2 ,ts );
216
215
structTypes .put (typeArray .name (), ptr ); // Array type name is not same as ptr str()
217
216
return ptr ;
218
217
}
@@ -223,11 +222,11 @@ else if (type instanceof Type.TypeNullable typeNullable) {
223
222
if (ptr1 .nullable ())
224
223
ptr = ptr1 ;
225
224
else
226
- ptr = new SONTypeMemPtr ((byte )3 ,ptr1 ._obj );
225
+ ptr = SONTypeMemPtr . make ((byte )3 ,ptr1 ._obj );
227
226
}
228
227
else
229
- ptr = new SONTypeMemPtr ((byte )2 ,(SONTypeStruct ) baseType );
230
- structTypes .put (ptr . str (), ptr );
228
+ ptr = SONTypeMemPtr . make ((byte )2 ,(SONTypeStruct ) baseType );
229
+ structTypes .put (typeNullable . name (), ptr );
231
230
return ptr ;
232
231
}
233
232
else if (type instanceof Type .TypeVoid ) {
@@ -241,13 +240,13 @@ else if (type instanceof Type.TypeVoid) {
241
240
242
241
// TODO because first two slots are MEM and RPC
243
242
private int REGNUM = 2 ;
244
- private void setVarIds (Scope scope , ScopeNode scopeNode , FunNode fun ) {
243
+ private void defineScopedVars (Scope scope , ScopeNode scopeNode , FunNode fun ) {
245
244
for (Symbol symbol : scope .getLocalSymbols ()) {
246
245
if (symbol instanceof Symbol .VarSymbol varSymbol ) {
247
246
varSymbol .regNumber = REGNUM ++;
248
- String sonTypeName = getSONTypeName (varSymbol .type );
249
- SONType sonType = TYPES . get ( sonTypeName );
250
- if ( sonType == null ) throw new CompilerException ("Unknown SON Type " +sonTypeName );
247
+ SONType sonType = TYPES . get (varSymbol .type . name () );
248
+ if ( sonType == null )
249
+ throw new CompilerException ("Unknown SON Type " +varSymbol . type . name () );
251
250
Node init = null ;
252
251
253
252
if (varSymbol instanceof Symbol .ParameterSymbol ) {
@@ -256,9 +255,6 @@ private void setVarIds(Scope scope, ScopeNode scopeNode, FunNode fun) {
256
255
scopeNode .define (makeVarName (varSymbol ), sonType , false , init );
257
256
}
258
257
}
259
- for (Scope childScope : scope .children ) {
260
- setVarIds (childScope , scopeNode , fun );
261
- }
262
258
}
263
259
264
260
private void generateFunction (Symbol .FunctionTypeSymbol functionTypeSymbol ) {
@@ -338,7 +334,8 @@ private ReturnNode generateFunctionBody(Symbol.FunctionTypeSymbol functionTypeSy
338
334
_scope .mem (mem );
339
335
// All args, "as-if" called externally
340
336
AST .FuncDecl funcDecl = (AST .FuncDecl ) functionTypeSymbol .functionDecl ;
341
- setVarIds (funcDecl .scope ,_scope ,fun );
337
+ REGNUM = 2 ;
338
+ defineScopedVars (funcDecl .scope ,_scope ,fun );
342
339
343
340
// Parse the body
344
341
Node last = compileStatement (funcDecl .block );
@@ -556,12 +553,12 @@ private Node newStruct( SONTypeStruct obj, Node size) {
556
553
private Node compileNewExpr (AST .NewExpr newExpr ) {
557
554
Type type = newExpr .type ;
558
555
if (type instanceof Type .TypeArray typeArray ) {
559
- SONTypeMemPtr tarray = (SONTypeMemPtr ) TYPES .get (getSONTypeName ( typeArray ));
556
+ SONTypeMemPtr tarray = (SONTypeMemPtr ) TYPES .get (typeArray . name ( ));
560
557
return newArray (tarray ._obj ,ZERO );
561
558
}
562
559
else if (type instanceof Type .TypeStruct typeStruct ) {
563
- SONTypeStruct tstruct = (SONTypeStruct ) TYPES .get (typeStruct .name ());
564
- return newStruct (tstruct ,con (tstruct . offset (tstruct ._fields .length )));
560
+ SONTypeMemPtr tptr = (SONTypeMemPtr ) TYPES .get (typeStruct .name ());
561
+ return newStruct (tptr . _obj ,con (tptr . _obj . offset (tptr . _obj ._fields .length )));
565
562
}
566
563
else
567
564
throw new CompilerException ("Unexpected type: " + type );
@@ -701,7 +698,7 @@ private Node compileCallExpr(AST.CallExpr callExpr) {
701
698
}
702
699
703
700
private String makeVarName (Symbol .VarSymbol varSymbol ) {
704
- return varSymbol .name + "$" + varSymbol .regNumber ;
701
+ return varSymbol .name ; // + "$" + varSymbol.regNumber;
705
702
}
706
703
707
704
private Node compileStatement (AST .Stmt statement ) {
@@ -955,9 +952,12 @@ private Node compileReturn(AST.ReturnStmt returnStmt) {
955
952
956
953
private Node compileBlock (AST .BlockStmt block ) {
957
954
Node last = ZERO ;
955
+ _scope .push (ScopeNode .Kind .Block );
956
+ defineScopedVars (block .scope , _scope , _fun );
958
957
for (AST .Stmt stmt : block .stmtList ) {
959
958
last = compileStatement (stmt );
960
959
}
960
+ _scope .pop ();
961
961
return last ;
962
962
}
963
963
0 commit comments