@@ -17,6 +17,10 @@ static void emit_puts_call(FILE *out, const AstExpr *expr, const FunctionTable *
1717static void emit_printf_args (FILE * out , const AstExpr * expr , const FunctionTable * functions );
1818static void emit_string_literal_n (FILE * out , const char * value , size_t length );
1919static void emit_string_literal (FILE * out , const char * value );
20+ static void emit_array_declaration (FILE * out , const AstStmt * stmt , const FunctionTable * functions , int indent );
21+ static void emit_array_literal_expr (FILE * out , const AstExprList * elements , const FunctionTable * functions );
22+ static void emit_array_index (FILE * out , const AstExpr * expr , const FunctionTable * functions );
23+ static void emit_array_default_value (FILE * out , TypeKind type );
2024static const FunctionSignature * lookup_signature (const FunctionTable * functions , const char * name );
2125static const char * binary_op_token (AstBinaryOp op );
2226static void emit_indent (FILE * out , int indent );
@@ -153,21 +157,36 @@ static void emit_statement(FILE *out, const AstStmt *stmt, const FunctionTable *
153157 emit_block (out , & stmt -> data .block , functions , signature , indent , 1 );
154158 break ;
155159 case STMT_DECL :
156- emit_indent (out , indent );
157- fprintf (out , "local %s" , stmt -> data .decl .name );
158- if (stmt -> data .decl .init )
160+ if (stmt -> data .decl .is_array )
159161 {
160- fputs (" = " , out );
161- emit_expression_expected (out , stmt -> data .decl .init , functions , stmt -> data .decl .type );
162+ emit_array_declaration (out , stmt , functions , indent );
163+ }
164+ else
165+ {
166+ emit_indent (out , indent );
167+ fprintf (out , "local %s" , stmt -> data .decl .name );
168+ if (stmt -> data .decl .init )
169+ {
170+ fputs (" = " , out );
171+ emit_expression_expected (out , stmt -> data .decl .init , functions , stmt -> data .decl .type );
172+ }
173+ fputc ('\n' , out );
162174 }
163- fputc ('\n' , out );
164175 break ;
165176 case STMT_ASSIGN :
166177 emit_indent (out , indent );
167178 fprintf (out , "%s = " , stmt -> data .assign .name );
168179 emit_expression_expected (out , stmt -> data .assign .value , functions , stmt -> data .assign .type );
169180 fputc ('\n' , out );
170181 break ;
182+ case STMT_ARRAY_ASSIGN :
183+ emit_indent (out , indent );
184+ fprintf (out , "%s[" , stmt -> data .array_assign .name );
185+ emit_array_index (out , stmt -> data .array_assign .index , functions );
186+ fputs ("] = " , out );
187+ emit_expression_expected (out , stmt -> data .array_assign .value , functions , stmt -> data .array_assign .element_type );
188+ fputc ('\n' , out );
189+ break ;
171190 case STMT_EXPR :
172191 if (stmt -> data .expr )
173192 {
@@ -256,6 +275,9 @@ static void emit_expression_raw(FILE *out, const AstExpr *expr, const FunctionTa
256275 case EXPR_STRING_LITERAL :
257276 emit_string_literal (out , expr -> data .string_literal );
258277 break ;
278+ case EXPR_ARRAY_LITERAL :
279+ emit_array_literal_expr (out , & expr -> data .array_literal .elements , functions );
280+ break ;
259281 case EXPR_IDENTIFIER :
260282 fputs (expr -> data .identifier , out );
261283 break ;
@@ -308,6 +330,21 @@ static void emit_expression_raw(FILE *out, const AstExpr *expr, const FunctionTa
308330 case EXPR_CALL :
309331 emit_call (out , expr , functions );
310332 break ;
333+ case EXPR_SUBSCRIPT :
334+ if (expr -> data .subscript .array && expr -> data .subscript .array -> kind == EXPR_IDENTIFIER )
335+ {
336+ emit_expression_raw (out , expr -> data .subscript .array , functions );
337+ }
338+ else
339+ {
340+ fputc ('(' , out );
341+ emit_expression_raw (out , expr -> data .subscript .array , functions );
342+ fputc (')' , out );
343+ }
344+ fputc ('[' , out );
345+ emit_array_index (out , expr -> data .subscript .index , functions );
346+ fputc (']' , out );
347+ break ;
311348 }
312349}
313350
@@ -459,6 +496,112 @@ static void emit_string_literal(FILE *out, const char *value)
459496 emit_string_literal_n (out , value , strlen (value ));
460497}
461498
499+ static void emit_array_declaration (FILE * out , const AstStmt * stmt , const FunctionTable * functions , int indent )
500+ {
501+ if (!stmt )
502+ {
503+ return ;
504+ }
505+ const AstExpr * init = stmt -> data .decl .array_init ;
506+ emit_indent (out , indent );
507+ fprintf (out , "local %s = {" , stmt -> data .decl .name );
508+ size_t emitted = 0 ;
509+ int first = 1 ;
510+ if (init && init -> kind == EXPR_ARRAY_LITERAL )
511+ {
512+ const AstExprList * elements = & init -> data .array_literal .elements ;
513+ for (size_t i = 0 ; i < elements -> count ; ++ i )
514+ {
515+ if (first )
516+ {
517+ fputc (' ' , out );
518+ first = 0 ;
519+ }
520+ else
521+ {
522+ fputs (", " , out );
523+ }
524+ emit_expression_expected (out , elements -> items [i ], functions , stmt -> data .decl .type );
525+ emitted ++ ;
526+ }
527+ }
528+ for (; emitted < stmt -> data .decl .array_size ; ++ emitted )
529+ {
530+ if (first )
531+ {
532+ fputc (' ' , out );
533+ first = 0 ;
534+ }
535+ else
536+ {
537+ fputs (", " , out );
538+ }
539+ emit_array_default_value (out , stmt -> data .decl .type );
540+ }
541+ fputs (" }\n" , out );
542+ }
543+
544+ static void emit_array_literal_expr (FILE * out , const AstExprList * elements , const FunctionTable * functions )
545+ {
546+ fputc ('{' , out );
547+ int first = 1 ;
548+ if (elements )
549+ {
550+ for (size_t i = 0 ; i < elements -> count ; ++ i )
551+ {
552+ if (first )
553+ {
554+ fputc (' ' , out );
555+ first = 0 ;
556+ }
557+ else
558+ {
559+ fputs (", " , out );
560+ }
561+ emit_expression_raw (out , elements -> items [i ], functions );
562+ }
563+ }
564+ if (first )
565+ {
566+ fputc (' ' , out );
567+ }
568+ fputs (" }" , out );
569+ }
570+
571+ static void emit_array_index (FILE * out , const AstExpr * expr , const FunctionTable * functions )
572+ {
573+ if (expr && expr -> kind == EXPR_INT_LITERAL )
574+ {
575+ fprintf (out , "%lld" , expr -> data .int_value + 1 );
576+ return ;
577+ }
578+ fputc ('(' , out );
579+ emit_expression_raw (out , expr , functions );
580+ fputs (" + 1)" , out );
581+ }
582+
583+ static void emit_array_default_value (FILE * out , TypeKind type )
584+ {
585+ switch (type )
586+ {
587+ case TYPE_INT :
588+ fputs ("0" , out );
589+ break ;
590+ case TYPE_FLOAT :
591+ fputs ("0.0" , out );
592+ break ;
593+ case TYPE_BOOL :
594+ fputs ("false" , out );
595+ break ;
596+ case TYPE_STRING :
597+ emit_string_literal (out , "" );
598+ break ;
599+ default :
600+ fputs ("nil" , out );
601+ break ;
602+ }
603+ }
604+
462605static int emit_builtin_expr_statement (FILE * out , const AstExpr * expr , const FunctionTable * functions , int indent )
463606{
464607 if (!expr || expr -> kind != EXPR_CALL )
0 commit comments