Skip to content

Commit 0661f6f

Browse files
committed
Accept arrays in Spin2 struct declarations
1 parent af97be5 commit 0661f6f

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

frontends/spin/spin.y

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,15 @@ FixupList(AST *list)
226226
return origlist;
227227
}
228228

229+
// possibly create an array type
230+
AST *
231+
MaybeArrayType(AST *basetype, AST *size)
232+
{
233+
if (!size) return basetype;
234+
235+
return MakeArrayType(basetype, size);
236+
}
237+
229238
// declare a Spin structure
230239
static void
231240
SpinDeclareStruct(AST *ident, AST *defs)
@@ -1075,26 +1084,33 @@ structlist:
10751084
{ $$ = AddToList($1, CommentedListHolder($4)); }
10761085
;
10771086

1087+
optarray:
1088+
'[' expr ']'
1089+
{ $$ = $2; }
1090+
| /* nothing */
1091+
{ $$ = NULL; }
1092+
;
1093+
10781094
structitem:
1079-
SP_IDENTIFIER
1080-
{ $$ = NewAST(AST_DECLARE_VAR, NULL, $1); }
1081-
| SP_BYTE SP_IDENTIFIER
1095+
SP_IDENTIFIER optarray
1096+
{ $$ = NewAST(AST_DECLARE_VAR, MaybeArrayType(NULL, $2), $1); }
1097+
| SP_BYTE SP_IDENTIFIER optarray
10821098
{
1083-
$$ = NewAST(AST_DECLARE_VAR, ast_type_byte, $2);
1099+
$$ = NewAST(AST_DECLARE_VAR, MaybeArrayType(ast_type_byte, $3), $2);
10841100
}
1085-
| SP_WORD SP_IDENTIFIER
1101+
| SP_WORD SP_IDENTIFIER optarray
10861102
{
1087-
$$ = NewAST(AST_DECLARE_VAR, ast_type_word, $2);
1103+
$$ = NewAST(AST_DECLARE_VAR, MaybeArrayType(ast_type_word, $3), $2);
10881104
}
1089-
| SP_LONG SP_IDENTIFIER
1105+
| SP_LONG SP_IDENTIFIER optarray
10901106
{
1091-
$$ = NewAST(AST_DECLARE_VAR, NULL, $2);
1107+
$$ = NewAST(AST_DECLARE_VAR, MaybeArrayType(NULL, $3), $2);
10921108
}
1093-
| SP_TYPENAME SP_IDENTIFIER
1109+
| SP_TYPENAME SP_IDENTIFIER optarray
10941110
{
10951111
AST *name = $2;
10961112
AST *typname = $1;
1097-
$$ = NewAST(AST_DECLARE_VAR, typname, name);
1113+
$$ = NewAST(AST_DECLARE_VAR, MaybeArrayType(typname, $3), name);
10981114
}
10991115
;
11001116

0 commit comments

Comments
 (0)