This commit is contained in:
Scarlett
2025-04-08 16:01:36 -04:00
parent 87659ebf46
commit a4dc3d90be
5 changed files with 82 additions and 25 deletions

View File

@ -326,14 +326,14 @@ sblock:
dblock:
L_BRACKET
{if(getLine(cur)==0){
setLineNumber(cur, @1.first_line);
setColumnNumber(cur,@1.first_line);}
else{
cur = CreateScope(cur,@1.first_line,@1.first_column);
{
if (getLine(cur) == 0) {
setLineNumber(cur, @1.first_line);
setColumnNumber(cur,@1.first_line);
} else{
cur = CreateScope(cur,@1.first_line,@1.first_column); // <----- What is this?
}
}
}
declaration_list R_BRACKET;
@ -391,22 +391,45 @@ compound_statement:
simple_statement:
assignable ASSIGN expression
{
if(strcmp(getName((TableNode*)$1), getName((TableNode*)$3)) == 0) {
printdebug("Passed standard type check; assignable = expression");
} else if((strcmp(getType((TableNode*)$1), "array") == 0) && (strcmp(getName((TableNode*)$3), "address") == 0)) {
printdebug("%s[☺] Passed array type check; %s = %s", COLOR_GREEN, getName((TableNode*)$1), getName((TableNode*)$3));
} else if((strcmp(getType((TableNode*)$1), "record") == 0) && (strcmp(getName((TableNode*)$3), "address") == 0)) {
printdebug("%s[☺] Passed address type check; %s = %s", COLOR_GREEN, getName((TableNode*)$1), getName((TableNode*)$3));
} else if((strcmp(getType((TableNode*)$1), "function type primitive") == 0) && (strcmp(getName((TableNode*)$3), "address") == 0)) {
printdebug("%s[☺] Passed function type primitive type check; %s = %s", COLOR_GREEN, getName((TableNode*)$1), getName((TableNode*)$3));
// } else if () {
bool passCheck = false;
TableNode * left = (TableNode*)$1;
TableNode * right = (TableNode*)$3;
printTableNode((TableNode*)$1);
printTableNode((TableNode*)$3);
// } else if(strcmp(getType(table_lookup(cur, $1)), getType(table_lookup(cur, $3))) == 0) {
// printdebug("%s[] Passed double lookup type check; %s = %s", COLOR_GREEN, $1, $3);
} else {
if (strcmp(getType(right), "primitive") == 0) {
if (strcmp((getType(left)),(getName(right))) == 0) {
printdebug("%s[☺] Passed primitive type check; %s = %s", COLOR_GREEN, getName(left), getName(right));
passCheck = true;
}
}
if(strcmp(getName(left), getName(right)) == 0) {
printdebug("Passed standard type check; assignable = expression");
passCheck = true;
}
if((strcmp(getType(left), "array") == 0) && (strcmp(getName(right), "address") == 0)) {
printdebug("%s[☺] Passed array type check; %s = %s", COLOR_GREEN, getName(left), getName(right));
passCheck = true;
}
if((strcmp(getType(left), "record") == 0) && (strcmp(getName(right), "address") == 0)) {
printdebug("%s[☺] Passed address type check; %s = %s", COLOR_GREEN, getName(left), getName(right));
passCheck = true;
}
if((strcmp(getType(left), "function type primitive") == 0) && (strcmp(getName(right), "address") == 0)) {
printdebug("%s[☺] Passed function type primitive type check; %s = %s", COLOR_GREEN, getName(left), getName(right));
passCheck = true;
}
// Type check fails:
if (!passCheck) {
printdebug("%s[TYPE ERROR] %sMismatch at %sline %d and column %d%s", COLOR_ORANGE, COLOR_WHITE, COLOR_YELLOW, @2.first_line, @2.first_column, COLOR_WHITE);
printdebug(" - Invalid types %s$1: %s and $3: %s%s", COLOR_YELLOW, getType((TableNode*)$1), getType((TableNode*)$3), COLOR_WHITE);
printdebug(" - %sgetType for address: %s", COLOR_YELLOW, getType((TableNode*)$1));
printdebug(" - Invalid types %s$1: %s and $3: %s%s", COLOR_YELLOW, getType(left), getType(right), COLOR_WHITE);
printdebug(" - %sgetType for address: %s", COLOR_YELLOW, getType(left));
}
}
@ -624,6 +647,8 @@ assignable:
//char *funtype = getType(look_up(cur, $1));
printdebug("%s", getType(look_up(cur, getName((TableNode*)$1))));
TableNode * typeNode = table_lookup(getAncestor(cur), getType((TableNode*)$1));
TableNode *param = getParameter(typeNode);
printTableNode(param);

View File

@ -913,7 +913,7 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) {
if (table->Parent_Scope == NULL) {
fprintf(file_ptr, "%-*s:%-*s:%-*s:%-*s:%-*s:\n",
col_widths[0], " NAME",
col_widths[0], "NAME",
col_widths[1], " SCOPE",
col_widths[2], " PARENT",
col_widths[3], " TYPE",
@ -965,7 +965,11 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) {
if (parentScopeNum == 0) {
st_fprint(file_ptr, entry->theName, currentScopeNum, -100, " Primitive", primAdInfo);
} else {
st_fprint(file_ptr, entry->theName, currentScopeNum, parentScopeNum, " Primitive", primAdInfo);
printdebug("%sTHIS ONE", COLOR_RED);
printTableNode(entry);
char *primType = (char *)malloc(sizeof(getType(entry) + 1));
sprintf(primType, " %s", getType(entry));
st_fprint(file_ptr, entry->theName, currentScopeNum, parentScopeNum, primType, primAdInfo);
}
}

View File

@ -1,8 +1,10 @@
type main: string -> integer
type rec: [integer: rec_x; integer: rec_y]
function entry: main
entry (arg) := {
[integer:x]
x := 3 + 2 * 8;
[integer: arg_x; integer: arg_y; rec: arg_record; Boolean: arg_bool]
arg_x := 3 + 2 * 8;
return 0;
}

View File

@ -31,6 +31,9 @@ test (arg) := {
b := b & 1;
b := 1 & b;
b := 1 = 1;
b := 1 = b;
return 0;

View File

@ -0,0 +1,23 @@
(*
Testing the following type checks:
- integer : primitive
- character : primitive
- boolean : primitive
- address (not included, special case)
*)
type main: string -> integer
function entry: main
entry (arg) := {
[integer: i; address: add; character: char; Boolean: bool]
i := 3 + 2 * 8;
char := 'a';
bool := true;
return 0;
}