Added some notes and fixed the bp

This commit is contained in:
Meyer Simon
2025-05-05 17:15:52 -04:00
parent 1c5b7de5fd
commit 0446b0ae6b
3 changed files with 88 additions and 47 deletions

View File

@ -133,12 +133,12 @@ prototype:
include_list: include_list:
include_statement include_list include_statement include_list
| include_statement | include_statement
; ;
include_statement: include_statement:
INCLUDE C_STRING INCLUDE C_STRING
; ;
@ -214,17 +214,17 @@ definition:
if(type_of_param_type == TYPE_FUNCTION_TYPE){ if(type_of_param_type == TYPE_FUNCTION_TYPE){
CreateEntry(cur, TYPE_FUNCTION_DECLARATION, parameter,NULL, getAdInfo(parameter)); CreateEntry(cur, TYPE_FUNCTION_DECLARATION, parameter,NULL, getAdInfo(parameter));
// throw_error(ERROR_TYPE, "Duplicate defination of parameter in function definition."); // throw_error(ERROR_TYPE, "Duplicate defination of parameter in function definition.");
} }
if(type_of_param_type == TYPE_ARRAY_TYPE){ if(type_of_param_type == TYPE_ARRAY_TYPE){
CreateEntry(cur, TYPE_ARRAY, parameter,NULL, getAdInfo(parameter)); CreateEntry(cur, TYPE_ARRAY, parameter,NULL, getAdInfo(parameter));
//throw_error(ERROR_TYPE, "Duplicate defination of parameter in function definition."); //throw_error(ERROR_TYPE, "Duplicate defination of parameter in function definition.");
} }
if(type_of_param_type == TYPE_PRIMITIVE_TYPE){ if(type_of_param_type == TYPE_PRIMITIVE_TYPE){
CreateEntry(cur, TYPE_PRIMITIVE, parameter,NULL, getAdInfo(parameter))==undefined; CreateEntry(cur, TYPE_PRIMITIVE, parameter,NULL, getAdInfo(parameter))==undefined;
// throw_error(ERROR_TYPE, "Duplicate defination of parameter in function definition.");} // throw_error(ERROR_TYPE, "Duplicate defination of parameter in function definition.");}
} }
} }
} else { } else {
@ -248,13 +248,13 @@ definition:
printdebug("undefined type of parameter inside record"); printdebug("undefined type of parameter inside record");
CreateEntry(cur,type_of_param_type, undefined, NULL, NULL); CreateEntry(cur,type_of_param_type, undefined, NULL, NULL);
//throw_error(ERROR_TYPE, "Duplicate defination of parameter in function definition."); //throw_error(ERROR_TYPE, "Duplicate defination of parameter in function definition.");
} else { } else {
if(type_of_param_type == TYPE_FUNCTION_DECLARATION){ if(type_of_param_type == TYPE_FUNCTION_DECLARATION){
printdebug("function declaration of parameter inside record"); printdebug("function declaration of parameter inside record");
CreateEntry(cur, TYPE_FUNCTION_DECLARATION, getTypeEntry(entry),NULL, getAdInfo(entry)); CreateEntry(cur, TYPE_FUNCTION_DECLARATION, getTypeEntry(entry),NULL, getAdInfo(entry));
//throw_error(ERROR_TYPE, "Duplicate defination of parameter in function definition."); //throw_error(ERROR_TYPE, "Duplicate defination of parameter in function definition.");
} }
if(type_of_param_type == TYPE_ARRAY){ if(type_of_param_type == TYPE_ARRAY){
printdebug("array type of parameter inside record"); printdebug("array type of parameter inside record");
@ -271,7 +271,7 @@ definition:
printdebug("record type of parameter inside record"); printdebug("record type of parameter inside record");
CreateEntry(cur, TYPE_RECORD, getTypeEntry(entry),NULL, getAdInfo(entry)); CreateEntry(cur, TYPE_RECORD, getTypeEntry(entry),NULL, getAdInfo(entry));
//throw_error(ERROR_TYPE, "Duplicate defination of parameter in function definition.");} //throw_error(ERROR_TYPE, "Duplicate defination of parameter in function definition.");}
} }
/*printdebug("creating entry of type %s for function", getType(entry)); /*printdebug("creating entry of type %s for function", getType(entry));
CreateEntry(cur, getTypeEntry(entry), "undefined", NULL);*/ CreateEntry(cur, getTypeEntry(entry), "undefined", NULL);*/
@ -304,7 +304,7 @@ function_declaration:
FUNCTION ID COLON ID FUNCTION ID COLON ID
{ {
if(getAdInfoType(table_lookup(cur, $4))==TYPE_FUNCTION_TYPE){ if(getAdInfoType(table_lookup(cur, $4))==TYPE_FUNCTION_TYPE){
//printf("%s\n",$2); //printf("%s\n",$1);
//printf("%s\n",getName(table_lookup(cur, $4))); //printf("%s\n",getName(table_lookup(cur, $4)));
if (CreateEntry(cur,TYPE_FUNCTION_DECLARATION, table_lookup(cur, $4), $2, CreateFunctionDeclarationInfo(-1, false,NULL)) == undefined) { if (CreateEntry(cur,TYPE_FUNCTION_DECLARATION, table_lookup(cur, $4), $2, CreateFunctionDeclarationInfo(-1, false,NULL)) == undefined) {
throw_error(ERROR_TYPE, "Duplicate defination of function in function declaration"); throw_error(ERROR_TYPE, "Duplicate defination of function in function declaration");
@ -489,7 +489,7 @@ declaration:
if(CreateEntry(cur,d,(TableNode*)$1,$3,NULL) == undefined){ if(CreateEntry(cur,d,(TableNode*)$1,$3,NULL) == undefined){
throw_error(ERROR_TYPE, "Duplicate defination of function in declaration list"); throw_error(ERROR_TYPE, "Duplicate defination of function in declaration list");
} }
} }
else if(d == TYPE_ARRAY_TYPE){ else if(d == TYPE_ARRAY_TYPE){
@ -620,19 +620,27 @@ compound_statement:
S_Push(TrueList, S_Init(), 0); S_Push(TrueList, S_Init(), 0);
S_Push(FalseList, S_Init(), 0); S_Push(FalseList, S_Init(), 0);
}expression R_PAREN THEN { }expression R_PAREN THEN {
emit_label(label_gen()); int l = label_gen();
emit_conditional_jump(E_IF_X_TRUE, l, tn_or_const(NODE, $4));
Stack * t = S_Peek(TrueList);
S_Push(t, current, 1);
emit_goto(0);
t = S_Peek(FalseList);
S_Push(t, current, 1);
emit_label(l);
emit_backpatch(S_Pop(TrueList), getLabel(current)); emit_backpatch(S_Pop(TrueList), getLabel(current));
} sblock ELSE { } sblock ELSE {
// NOTE we are not going back to // NOTE we are not going back to
int l = label_gen(); int l = label_gen();
emit_backpatch(S_Pop(FalseList), l); emit_backpatch(S_Pop(FalseList), l);
S_Push(FalseList, S_Init(), 0); S_Push(stack, S_Init(), 0);
emit_goto(0); emit_goto(0);
S_Push(S_Peek(FalseList), current, 1); S_Push(S_Peek(stack), current, 1);
emit_label(l); emit_label(l);
} sblock { } sblock {
int l = label_gen(); int l = label_gen();
emit_backpatch(S_Pop(FalseList), l); emit_backpatch(S_Pop(stack), l);
emit_label(l); emit_label(l);
if ($8 == undefined && $11 != undefined) { if ($8 == undefined && $11 != undefined) {
$$ = $11; $$ = $11;
@ -665,21 +673,22 @@ compound_statement:
simple_statement: simple_statement:
assignable ASSIGN expression assignable{
S_Push(TrueList, S_Init(), 0);
S_Push(FalseList, S_Init(), 0);
} ASSIGN expression
{ {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
emit_label(label_gen()); emit_label(label_gen());
emit_backpatch(S_Pop(TrueList), getLabel(current)); emit_backpatch(S_Pop(TrueList), getLabel(current));
emit_backpatch(S_Pop(FalseList), getLabel(current)); emit_backpatch(S_Pop(FalseList), getLabel(current));
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
printdebug("simple statement"); printdebug("simple statement");
TableNode* node; TableNode* node;
if((getAdInfoType((getTypeEntry((TableNode*)$1))) == TYPE_FUNCTION_TYPE)|| if((getAdInfoType((getTypeEntry((TableNode*)$1))) == TYPE_FUNCTION_TYPE)||
(getAdInfoType((getTypeEntry((TableNode*)$1))) == TYPE_ARRAY_TYPE)|| (getAdInfoType((getTypeEntry((TableNode*)$1))) == TYPE_ARRAY_TYPE)||
(getAdInfoType((getTypeEntry((TableNode*)$1))) == TYPE_RECORD_TYPE)|| (getAdInfoType((getTypeEntry((TableNode*)$1))) == TYPE_RECORD_TYPE)||
(getAdInfoType((getTypeEntry((TableNode*)$1))) == TYPE_PRIMITIVE_TYPE)){ (getAdInfoType((getTypeEntry((TableNode*)$1))) == TYPE_PRIMITIVE_TYPE)){
node = ((TableNode*)$1); node = ((TableNode*)$1);
} else { } else {
//printf("%d\n",getAdInfoType((getTypeEntry((TableNode*)$1)))); //printf("%d\n",getAdInfoType((getTypeEntry((TableNode*)$1))));
@ -689,28 +698,22 @@ simple_statement:
//printf("%s\n\n", getType(getTypeEntry((TableNode*)$3))); //printf("%s\n\n", getType(getTypeEntry((TableNode*)$3)));
node = undefined; node = undefined;
} }
if(getAdInfoType(node) == getAdInfoType((TableNode*)$4)){
emit_assignment($1, tn_or_const(NODE, $4));
if(getAdInfoType(node) == getAdInfoType((TableNode*)$3)){ printdebug("%s[☺] Passed type check; %s = %s", COLOR_GREEN, getType(node), getType((TableNode*)$4));
emit_assignment($1, tn_or_const(NODE, $3)); } else if (getTypeEntry(getTypeEntry(node)) == arrayprim && getTypeEntry((TableNode*)$4) == addr) {
printdebug("%s[☺] Passed type check; %s = %s", COLOR_GREEN, getType(node), getType((TableNode*)$3)); emit_assignment($1, tn_or_const(NODE, $4));
} else if (getTypeEntry(getTypeEntry(node)) == arrayprim && getTypeEntry((TableNode*)$3) == addr) { printdebug("%s[☺] Passed type check; %s = %s", COLOR_GREEN, getType(node), getType((TableNode*)$4));
emit_assignment($1, tn_or_const(NODE, $3)); } else if (getTypeEntry(getTypeEntry(node)) == recprime && getTypeEntry((TableNode*)$4) == addr) {
printdebug("%s[☺] Passed type check; %s = %s", COLOR_GREEN, getType(node), getType((TableNode*)$3)); emit_assignment($1, tn_or_const(NODE, $4));
} else if (getTypeEntry(getTypeEntry(node)) == recprime && getTypeEntry((TableNode*)$3) == addr) { printdebug("%s[☺] Passed type check; %s = %s", COLOR_GREEN, getType(node), getType((TableNode*)$4));
emit_assignment($1, tn_or_const(NODE, $3)); } else {
printdebug("%s[☺] Passed type check; %s = %s", COLOR_GREEN, getType(node), getType((TableNode*)$3));
}
else {
//printf("%d\n",getAdInfoType((TableNode*)$1)); //printf("%d\n",getAdInfoType((TableNode*)$1));
//printf("%d\n",getAdInfoType((TableNode*)$3)); //printf("%d\n",getAdInfoType((TableNode*)$3));
//printf("%d\n",getAdInfoType((TableNode*)$1)); //printf("%d\n",getAdInfoType((TableNode*)$1));
//printf("%d\n",getAdInfoType((TableNode*)$3)); //printf("%d\n",getAdInfoType((TableNode*)$3));
throw_error(ERROR_TYPE, "Assignable Assign Expression - Object %s of type %s != Object %s of type %s", getName(node), getType(node), getName((TableNode*)$3), getType((TableNode*)$3)); throw_error(ERROR_TYPE, "Assignable Assign Expression - Object %s of type %s != Object %s of type %s", getName(node), getType(node), getName((TableNode*)$4), getType((TableNode*)$4));
} }
$$ = undefined; $$ = undefined;
} }
@ -1020,9 +1023,10 @@ expression:
char* temp = temp_var_gen(); char* temp = temp_var_gen();
TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, boo, temp, NULL); TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, boo, temp, NULL);
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
emit_label(label_gen()); // emit_label(label_gen());
emit_backpatch(S_Pop(TrueList), getLabel(current)); // emit_backpatch(S_Pop(TrueList), getLabel(current));
emit_binary_op(E_LESS_THAN, node, tn_or_const(NODE,$1), tn_or_const(NODE,$3)); emit_binary_op(E_LESS_THAN, node, tn_or_const(NODE,$1), tn_or_const(NODE,$3));
/*
emit_conditional_jump(E_IF_X_TRUE, 0, tn_or_const(NODE, node)); emit_conditional_jump(E_IF_X_TRUE, 0, tn_or_const(NODE, node));
Stack * t = S_Peek(TrueList); Stack * t = S_Peek(TrueList);
if(t==NULL){ if(t==NULL){
@ -1037,6 +1041,7 @@ expression:
S_Push(FalseList, t, 1); S_Push(FalseList, t, 1);
} }
S_Push(t, current, 1); S_Push(t, current, 1);
*/
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
printdebug("less than expression"); printdebug("less than expression");
if(getTypeEntry((TableNode*)$1) == getTypeEntry((TableNode*)$3) && getTypeEntry((TableNode*)$1)==integ) { if(getTypeEntry((TableNode*)$1) == getTypeEntry((TableNode*)$3) && getTypeEntry((TableNode*)$1)==integ) {
@ -1054,9 +1059,8 @@ expression:
char* temp = temp_var_gen(); char* temp = temp_var_gen();
TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, boo, temp, NULL); TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, boo, temp, NULL);
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
emit_label(label_gen());
emit_backpatch(S_Pop(TrueList), getLabel(current));
emit_binary_op(E_EQUAL_TO, node, tn_or_const(NODE,$1), tn_or_const(NODE,$3)); emit_binary_op(E_EQUAL_TO, node, tn_or_const(NODE,$1), tn_or_const(NODE,$3));
/*
emit_conditional_jump(E_IF_X_TRUE, 0, tn_or_const(NODE, node)); emit_conditional_jump(E_IF_X_TRUE, 0, tn_or_const(NODE, node));
Stack * t = S_Peek(TrueList); Stack * t = S_Peek(TrueList);
if(t==NULL){ if(t==NULL){
@ -1071,6 +1075,9 @@ expression:
S_Push(FalseList, t, 1); S_Push(FalseList, t, 1);
} }
S_Push(t, current, 1); S_Push(t, current, 1);
*/
// emit_label(label_gen());
// emit_backpatch(S_Pop(TrueList), getLabel(current));
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
printdebug("equals check expression"); printdebug("equals check expression");
if(getTypeEntry((TableNode*)$1) == getTypeEntry((TableNode*)$3) && getTypeEntry((TableNode*)$1) != undefined) { if(getTypeEntry((TableNode*)$1) == getTypeEntry((TableNode*)$3) && getTypeEntry((TableNode*)$1) != undefined) {
@ -1089,6 +1096,7 @@ expression:
} }
| L_PAREN expression R_PAREN | L_PAREN expression R_PAREN
// TODO: We need to check if we need to backpatch here.
{ {
printdebug("paren expression. current type is %s",getType((TableNode*)$2)); printdebug("paren expression. current type is %s",getType((TableNode*)$2));
$$=$2; $$=$2;
@ -1143,7 +1151,7 @@ expression:
} }
cur = getParent(cur); cur = getParent(cur);
/*TableNode * t = getFirstEntry(cur); /*TableNode * t = getFirstEntry(cur);
TableNode * n = look_up(cur, $2); TableNode * n = look_up(cur, $2);
@ -1419,6 +1427,7 @@ assignable:
char* temp = temp_var_gen(); char* temp = temp_var_gen();
int t = 6; int t = 6;
//emission //emission
// arr._1 ....
$$ = CreateEntry(cur,t, integ, temp, NULL); $$ = CreateEntry(cur,t, integ, temp, NULL);
} }
} }
@ -1452,6 +1461,7 @@ assignable:
} }
TableNode* node = CreateEntry(cur,t, type, temp, NULL); TableNode* node = CreateEntry(cur,t, type, temp, NULL);
// getElementOffset(rec, ID)
//NOTE ADD ASSIGNMENT EMIT HERE (MIGHT NEED TO PUSH TO STACK) //NOTE ADD ASSIGNMENT EMIT HERE (MIGHT NEED TO PUSH TO STACK)
//emit_field_access(char* node, char* record, $3) //emit_field_access(char* node, char* record, $3)
$$=node; $$=node;

View File

@ -12,15 +12,12 @@ bar (r,s) := {
} }
entry (arg) := { entry (arg) := {
[ integer: result ; rec: w] [ integer: r ; integer: s; Boolean: x]
if ( result = result ) then { x := (r < s) & x;
if ( result < w.y ) then { if ( r < s ) then {
result := 8; r := 5;
} else {
result := 9;
}(* *)
} else { } else {
result := bar('c', 7); r := 7;
} }
return 0; return 0;
} }

View File

@ -0,0 +1,34 @@
(* TEST: [-asc -tc -cg -ir] *)
#include "std.alpha"
type M : string -> integer
function entry : M
type fib: integer -> integer
function Fib : fib
Fib(i) := {
[ Boolean: a ; Boolean: b ; Boolean: c]
if( i = 0 ) then {
return 7;
} else {
i := i;
}
b := b | (a & c);
b := 2 < 3;
if(i = 1) then {
return 1;
} else {
return i + Fib(i - 1);
}
}
entry (arg) := {
[ integer: x; integer: y ]
x := 2;
x := Fib(2);
y := printInteger(Fib(2));
return 1;
}