@@ -90,7 +90,12 @@ public TypeDictionary createAST(String src) {
9090
9191
9292 private void populateDefaultTypes (Map <String ,SONType > types ) {
93+ // Pre-create int, [int] and *[int] types
9394 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
9499 for (SONType t : SONType .gather ()) {
95100 types .put (t .str (), t );
96101 }
@@ -145,26 +150,31 @@ private void createSONStructType(Map<String, SONType> structTypes, String typeNa
145150 Type type = typeStruct .getField (name ); // FIXME
146151 fs .push (new Field (name ,getSONType (structTypes ,type ),_code .getALIAS (),false ));
147152 }
153+ // A Struct type may have been created before because of
154+ // reference from itself; in which case we need to update that
148155 SONType fref = structTypes .get (typeName );
149156 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
151161 ts ._fields = fs .asAry ();
152162 }
153- else throw new CompilerException ("" );
163+ else throw new CompilerException ("Expected struct type " + typeName + " but got " + fref );
154164 }
155165 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 );
157169 }
158- // SONTypeStruct ts = SONTypeStruct.make(typeName, fs.asAry());
159- // TYPES.put(typeName, SONTypeMemPtr.make(ts));
160170 }
161171
162172 private String getSONTypeName (Type type ) {
163173 if (type instanceof Type .TypeFunction typeFunction ) {
164174 return typeFunction .name ;
165175 }
166176 else if (type instanceof Type .TypeArray typeArray ) {
167- return typeArray .name () ;
177+ return "*[" + getSONTypeName ( typeArray .getElementType ()) + "]" ;
168178 }
169179 else if (type instanceof Type .TypeStruct typeStruct ) {
170180 return "*" + typeStruct .name ;
@@ -183,36 +193,25 @@ else if (type instanceof Type.TypeNullable typeNullable) {
183193 }
184194
185195 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 ());
188197 if (t != null ) return t ;
189198 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 ;
204208 }
205209 else if (type instanceof Type .TypeArray typeArray ) {
210+ // A reference to array in EeZee means
211+ // *array in SoN
206212 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 );
216215 structTypes .put (typeArray .name (), ptr ); // Array type name is not same as ptr str()
217216 return ptr ;
218217 }
@@ -223,11 +222,11 @@ else if (type instanceof Type.TypeNullable typeNullable) {
223222 if (ptr1 .nullable ())
224223 ptr = ptr1 ;
225224 else
226- ptr = new SONTypeMemPtr ((byte )3 ,ptr1 ._obj );
225+ ptr = SONTypeMemPtr . make ((byte )3 ,ptr1 ._obj );
227226 }
228227 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 );
231230 return ptr ;
232231 }
233232 else if (type instanceof Type .TypeVoid ) {
@@ -241,13 +240,13 @@ else if (type instanceof Type.TypeVoid) {
241240
242241 // TODO because first two slots are MEM and RPC
243242 private int REGNUM = 2 ;
244- private void setVarIds (Scope scope , ScopeNode scopeNode , FunNode fun ) {
243+ private void defineScopedVars (Scope scope , ScopeNode scopeNode , FunNode fun ) {
245244 for (Symbol symbol : scope .getLocalSymbols ()) {
246245 if (symbol instanceof Symbol .VarSymbol varSymbol ) {
247246 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 () );
251250 Node init = null ;
252251
253252 if (varSymbol instanceof Symbol .ParameterSymbol ) {
@@ -256,9 +255,6 @@ private void setVarIds(Scope scope, ScopeNode scopeNode, FunNode fun) {
256255 scopeNode .define (makeVarName (varSymbol ), sonType , false , init );
257256 }
258257 }
259- for (Scope childScope : scope .children ) {
260- setVarIds (childScope , scopeNode , fun );
261- }
262258 }
263259
264260 private void generateFunction (Symbol .FunctionTypeSymbol functionTypeSymbol ) {
@@ -338,7 +334,8 @@ private ReturnNode generateFunctionBody(Symbol.FunctionTypeSymbol functionTypeSy
338334 _scope .mem (mem );
339335 // All args, "as-if" called externally
340336 AST .FuncDecl funcDecl = (AST .FuncDecl ) functionTypeSymbol .functionDecl ;
341- setVarIds (funcDecl .scope ,_scope ,fun );
337+ REGNUM = 2 ;
338+ defineScopedVars (funcDecl .scope ,_scope ,fun );
342339
343340 // Parse the body
344341 Node last = compileStatement (funcDecl .block );
@@ -556,12 +553,12 @@ private Node newStruct( SONTypeStruct obj, Node size) {
556553 private Node compileNewExpr (AST .NewExpr newExpr ) {
557554 Type type = newExpr .type ;
558555 if (type instanceof Type .TypeArray typeArray ) {
559- SONTypeMemPtr tarray = (SONTypeMemPtr ) TYPES .get (getSONTypeName ( typeArray ));
556+ SONTypeMemPtr tarray = (SONTypeMemPtr ) TYPES .get (typeArray . name ( ));
560557 return newArray (tarray ._obj ,ZERO );
561558 }
562559 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 )));
565562 }
566563 else
567564 throw new CompilerException ("Unexpected type: " + type );
@@ -701,7 +698,7 @@ private Node compileCallExpr(AST.CallExpr callExpr) {
701698 }
702699
703700 private String makeVarName (Symbol .VarSymbol varSymbol ) {
704- return varSymbol .name + "$" + varSymbol .regNumber ;
701+ return varSymbol .name ; // + "$" + varSymbol.regNumber;
705702 }
706703
707704 private Node compileStatement (AST .Stmt statement ) {
@@ -955,9 +952,12 @@ private Node compileReturn(AST.ReturnStmt returnStmt) {
955952
956953 private Node compileBlock (AST .BlockStmt block ) {
957954 Node last = ZERO ;
955+ _scope .push (ScopeNode .Kind .Block );
956+ defineScopedVars (block .scope , _scope , _fun );
958957 for (AST .Stmt stmt : block .stmtList ) {
959958 last = compileStatement (stmt );
960959 }
960+ _scope .pop ();
961961 return last ;
962962 }
963963
0 commit comments