Update grammar.y with Annie's changes

This commit is contained in:
Moroseui
2025-03-28 13:58:45 -04:00
committed by GitHub
parent 36694a2f4a
commit d0a00c8684

View File

@ -119,18 +119,58 @@ prototype:
L_PAREN EXTERNAL R_PAREN FUNCTION ID COLON ID;
definition:
TYPE ID COLON dblock
| TYPE ID COLON C_INTEGER ARROW ID
definition:
TYPE ID COLON {tn = CreateEntry(getAncestor(cur), recprime, $2, CreateRecordInfo(0, cur = CreateScope(cur, 0, 0)));
if (table_lookup(getAncestor(cur), $2) == NULL) {
printf("rec not found \n");
}
}dblock { setRecSize(table_lookup(getParent(cur), $2), getRecSize(cur));
cur = getParent(cur);}
| TYPE ID COLON C_INTEGER ARROW id_or_types { CreateEntry(cur, arrayprim, $2, CreateArrayInfo($4, look_up(cur, $6)));}
| function_declaration
| TYPE ID COLON id_or_types ARROW id_or_types {
CreateEntry(cur,funtypeprime,$2,CreateFunctionTypeInfo(table_lookup(cur,$4),table_lookup(cur,$6)));
}
| ID parameter ASSIGN sblock
}
| ID {
TableNode *node = table_lookup(getAncestor(cur), $<words>1);
if (node == NULL || getAdInfoType(node) != TYPE_FUNCTION_DECLARATION) {
printf("function not declared at line %d, column %d\n", @1.first_line, @1.first_column);
} else {
setStartLine(node, @1.first_line);
setAsKeyword(node, false);
}
cur = CreateScope(cur, 0, 0);
} L_PAREN ID {
CreateEntry(cur, getParameter(table_lookup(getAncestor(cur), getType(table_lookup(getAncestor(cur), $<words>1)))), $<words>4, NULL);
}R_PAREN ASSIGN sblock
| ID {
TableNode *node = table_lookup(getAncestor(cur), $<words>1);
if (node == NULL) {
printf("null check\n");
}
if (node == NULL || getAdInfoType(node) != TYPE_FUNCTION_DECLARATION) {
printf("function not declared at line %d, column %d\n", @1.first_line, @1.first_column);
} else {
setStartLine(node, @1.first_line);
setAsKeyword(node, false);
}
cur = CreateScope(cur, 0, 0);
}AS L_PAREN {
TableNode *parameter = getParameter(table_lookup(getAncestor(cur), getType(table_lookup(getAncestor(cur), $<words>1))));
if (parameter == NULL || getAdInfoType(parameter) != TYPE_RECORD) {
printf("function defined with as, but parameter is not a record at line %d, column %d\n", @1.first_line, @1.first_column);
} else {
for (TableNode* entry = getFirstEntry(getRecList(parameter)); entry!= NULL; entry = getNextEntry(entry)){
CreateEntry(cur, entry, NULL, NULL);
}
}
} idlist R_PAREN ASSIGN sblock
;
function_declaration:
FUNCTION ID COLON ID
| EXTERNAL FUNCTION ID COLON ID
FUNCTION ID COLON ID {CreateEntry(cur, look_up(cur, $4), $2, CreateFunctionDeclarationInfo(-1, false));}
| EXTERNAL FUNCTION ID COLON ID {CreateEntry(cur, look_up(cur, $5), $3, NULL);}
;
parameter:
@ -139,14 +179,34 @@ parameter:
;
idlist:
ID COMMA idlist
| ID
ID {
TableNode *entry = getFirstEntry(cur);
while (getName(entry) != NULL) {
entry = getNextEntry(entry);
}
if (getNextEntry(entry) == NULL) {
printf("too many parameters at line %d column %d\n", @1.first_line, @1.first_column);
}
addName(entry, $<words>1);
} COMMA idlist
| ID {
TableNode *entry = getFirstEntry(cur);
while (getName(entry) != NULL) {
entry = getNextEntry(entry);
}
if (getNextEntry(entry) != NULL) {
printf("too many parameters at line %d column %d\n", @1.first_line, @1.first_column);
}
addName(entry, $<words>1);
}
;
sblock:
L_BRACE {cur = CreateScope(cur,@1.first_line,@1.first_column);} statement_list {cur = getParent(cur);} R_BRACE
| L_BRACE {cur = CreateScope(cur,@1.first_line,@1.first_column);}
dblock {printf("seen sblock with dblock\n");} statement_list {cur = getParent(cur);} R_BRACE
L_BRACE {if (getLine(cur) != 0 && getColumn(cur))cur = CreateScope(cur,@1.first_line,@1.first_column);} statement_list {cur = getParent(cur);} R_BRACE
| L_BRACE {if (getLine(cur) != 0 && getColumn(cur))cur = CreateScope(cur,@1.first_line,@1.first_column);} dblock
{printf("seen sblock with dblock\n");} statement_list {cur = getParent(cur);} R_BRACE
;
dblock:
@ -158,8 +218,8 @@ declaration_list:
;
declaration:
id_or_types COLON ID {CreateEntry(cur,table_lookup(getAncestor(cur),$<words>1),$<words>3,NULL);}
;
id_or_types COLON ID {printf("ID/TYPE: %s, ID: %s\n", $<words>1, $<words>3) ; CreateEntry(cur,table_lookup(getAncestor(cur),$<words>1),$<words>3,NULL); }
;
id_or_types:
ID {printf("string of id in id_or_type is %s\n",$<words>1);} {$$ = $<words>1;}
@ -180,14 +240,45 @@ compound_statement:
;
simple_statement:
assignable ASSIGN expression
assignable ASSIGN expression {if(strcmp($1, $3) == 0){
} else {
printf("Mismatch at line %d and column%d\n", @2.first_line, @2.first_column);
}}
| RETURN expression
;
assignable:
ID
| assignable ablock
| assignable rec_op ID
ID {TableNode *node = table_lookup(cur, $<words>1);
printf("%s\n", $1);
if (node == NULL) {
printf("could not find %s in current scope\n", $<words>1);
node = table_lookup(getAncestor(cur), $<words>1);
if (node != NULL && getAdInfoType(node) == TYPE_FUNCTION_DECLARATION) {
if (getAsKeyword(node)) {
node = table_lookup(getAncestor(cur), getType(node));
$<integ>$ = getRecLength(getParameter(node));
} else {
$<integ>$ = 1;
}
}
else {
printf("assignable not defined as correct type at line %d, column %d\n", @1.first_line, @1.first_column);
}
} else if (getAdInfoType(node) == TYPE_ARRAY) {
$<integ>$ = getNumArrDim(node);
} else {
$<words>$ = getName(node);
}
}
| assignable ablock {if ($<integ>1 != $<integ>2) {
printf("invalid number of expressions in ablock at line %d, column %d\n", @1.first_line, @1.first_column);
}
$$ = getName(getReturn(look_up(cur, $1)));
}
| assignable rec_op ID {TableNode * tn; printf("this is assignable.id %s\n", getType(look_up(cur, $1)));if(NULL != (tn = table_lookup(getRecList(look_up(cur, $1)), $3)))
{$$ = "test";}
}
;
rec_op :
@ -271,12 +362,12 @@ expression:
ablock:
L_PAREN argument_list R_PAREN
L_PAREN argument_list {$<integ>$ = $<integ>2;} R_PAREN
;
argument_list:
expression COMMA argument_list
| expression
expression COMMA argument_list {$<integ>$ = $<integ>3 + 1;}
| expression {$<integ>$ = 1;}
;