From d0a00c86841469434bc9cf302d4a687403223cac Mon Sep 17 00:00:00 2001 From: Moroseui <157774170+Moroseui@users.noreply.github.com> Date: Fri, 28 Mar 2025 13:58:45 -0400 Subject: [PATCH] Update grammar.y with Annie's changes --- src/grammar.y | 131 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 111 insertions(+), 20 deletions(-) diff --git a/src/grammar.y b/src/grammar.y index 7ff44da..c9da70b 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -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), $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), $1)))), $4, NULL); + }R_PAREN ASSIGN sblock + | ID { + TableNode *node = table_lookup(getAncestor(cur), $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), $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, $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, $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),$1),$3,NULL);} - ; + id_or_types COLON ID {printf("ID/TYPE: %s, ID: %s\n", $1, $3) ; CreateEntry(cur,table_lookup(getAncestor(cur),$1),$3,NULL); } + ; id_or_types: ID {printf("string of id in id_or_type is %s\n",$1);} {$$ = $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, $1); + printf("%s\n", $1); + if (node == NULL) { + printf("could not find %s in current scope\n", $1); + node = table_lookup(getAncestor(cur), $1); + if (node != NULL && getAdInfoType(node) == TYPE_FUNCTION_DECLARATION) { + if (getAsKeyword(node)) { + node = table_lookup(getAncestor(cur), getType(node)); + $$ = getRecLength(getParameter(node)); + } else { + $$ = 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) { + $$ = getNumArrDim(node); + } else { + $$ = getName(node); + } +} +| assignable ablock {if ($1 != $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 {$$ = $2;} R_PAREN ; argument_list: - expression COMMA argument_list - | expression +expression COMMA argument_list {$$ = $3 + 1;} +| expression {$$ = 1;} ;