I added some more backpatching and some more files for testing

This commit is contained in:
Meyer Simon
2025-05-04 17:43:08 -04:00
parent 9d98b75bc0
commit accb21c78a
8 changed files with 113 additions and 18 deletions

View File

@ -609,7 +609,14 @@ compound_statement:
simple_statement: simple_statement:
assignable ASSIGN expression assignable ASSIGN expression
{ printdebug("simple statement"); {
// ----------------------------------------------------------------------------
emit_label(label_gen());
emit_backpatch(S_Pop(TrueList), getLabel(current));
emit_backpatch(S_Pop(FalseList), getLabel(current));
// ----------------------------------------------------------------------------
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)||
@ -691,7 +698,7 @@ ablock:
argument_list: argument_list:
expression{ expression{
TableNode* arg = CreateEntry(cur, getAdInfoType((TableNode*)$1), getTypeEntry((TableNode*)$1), arg_var_gen(), NULL); TableNode * arg = CreateEntry(cur, getAdInfoType((TableNode*)$1), getTypeEntry((TableNode*)$1), arg_var_gen(), NULL);
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// this is emitting the param withthe wrong TableNode // this is emitting the param withthe wrong TableNode
// We need to fiture out how to get the right one. // We need to fiture out how to get the right one.
@ -838,7 +845,11 @@ expression:
| expression AND expression | expression AND expression
{ {
char* temp = temp_var_gen();
TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, boo, temp, NULL);
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
uint_least8_t b = 0;
emit_assignment(node, tn_or_const(BOOLEAN,&b));
emit_label(label_gen()); emit_label(label_gen());
emit_backpatch(S_Pop(TrueList), getLabel(current)); emit_backpatch(S_Pop(TrueList), getLabel(current));
emit_conditional_jump(E_IF_X_FALSE, 0, tn_or_const(NODE, $1)); emit_conditional_jump(E_IF_X_FALSE, 0, tn_or_const(NODE, $1));
@ -864,7 +875,8 @@ expression:
S_Push(FalseList, t, 1); S_Push(FalseList, t, 1);
} }
S_Push(t, current, 1); S_Push(t, current, 1);
b = 1;
emit_assignment(node, tn_or_const(BOOLEAN,&b));
emit_goto(0); emit_goto(0);
t = S_Peek(TrueList); t = S_Peek(TrueList);
if(t==NULL){ if(t==NULL){
@ -875,8 +887,6 @@ expression:
S_Merge(FalseList); S_Merge(FalseList);
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
printdebug("AND"); printdebug("AND");
char* temp = temp_var_gen();
TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, boo, temp, NULL);
if((getTypeEntry((TableNode*)$1)==getTypeEntry((TableNode*)$3)) && getTypeEntry((TableNode*)$1) == boo) { if((getTypeEntry((TableNode*)$1)==getTypeEntry((TableNode*)$3)) && getTypeEntry((TableNode*)$1) == boo) {
//emit_binary_op(E_AND,node,tn_or_const(NODE,$1),tn_or_const(NODE,$3)); //emit_binary_op(E_AND,node,tn_or_const(NODE,$1),tn_or_const(NODE,$3));
$$ = node; $$ = node;
@ -888,7 +898,11 @@ expression:
| expression OR expression | expression OR expression
{ {
char* temp = temp_var_gen();
TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, boo, temp, NULL);
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
uint_least8_t b = 1;
emit_assignment(node, tn_or_const(BOOLEAN,&b));
emit_label(label_gen()); emit_label(label_gen());
emit_backpatch(S_Pop(TrueList), getLabel(current)); emit_backpatch(S_Pop(TrueList), getLabel(current));
emit_conditional_jump(E_IF_X_TRUE, 0, tn_or_const(NODE, $1)); emit_conditional_jump(E_IF_X_TRUE, 0, tn_or_const(NODE, $1));
@ -914,6 +928,8 @@ expression:
S_Push(TrueList, t, 1); S_Push(TrueList, t, 1);
} }
S_Push(t, current, 1); S_Push(t, current, 1);
b = 0;
emit_assignment(node, tn_or_const(BOOLEAN,&b));
emit_goto(0); emit_goto(0);
t = S_Peek(FalseList); t = S_Peek(FalseList);
if(t==NULL){ if(t==NULL){
@ -925,8 +941,6 @@ expression:
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
printdebug("OR"); printdebug("OR");
if((getTypeEntry((TableNode*)$1)==getTypeEntry((TableNode*)$3)) && getTypeEntry((TableNode*)$1) == boo) { if((getTypeEntry((TableNode*)$1)==getTypeEntry((TableNode*)$3)) && getTypeEntry((TableNode*)$1) == boo) {
char* temp = temp_var_gen();
TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, boo, temp, NULL);
$$ = node; $$ = node;
} else { } else {
$$=undefined; $$=undefined;
@ -1014,9 +1028,59 @@ expression:
} }
// TODO: We need to type check this. // TODO: We need to type check this.
| RESERVE ID {$$ = undefined; } | RESERVE ID {
char* temp = temp_var_gen();
TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, addr, temp, NULL);
TableNode * n = look_up(cur, $2);
if(getAdInfoType(n) != TYPE_RECORD){
throw_error(ERROR_TYPE, "Invalid Reserve expression with object %s of type %s.",
getName((TableNode*)n), getType((TableNode*)n));
$$=undefined;
}
int v = getRecTotal(getTypeEntry(n));
emit_reserve(node, tn_or_const(INTEGER, &v));
$$ = node;
}
| RELEASE ID {$$ = undefined; } | RELEASE ID {$$ = undefined; }
| RESERVE ID L_PAREN argument_list R_PAREN {$$ = undefined; } | RESERVE ID {
cur = CreateScope(cur, -1,-1);
} L_PAREN argument_list R_PAREN {
char* temp = temp_var_gen();
TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, addr, temp, NULL);
int a = S_Size(S_Peek(stack)) + 1;
emit_push_all(S_Peek(stack));
S_Pop(stack);
emit_function_call(node, a, tn_or_const(NODE, $2));
$$ = node;
/*
TableNode * t = getFirstEntry(cur);
TableNode * n = look_up(cur, $2);
if(getAdInfoType(n) == TYPE_ARRAY){
int array_dims = getNumArrDim(getTypeEntry(n));
if ($5 != array_dims) {
throw_error(ERROR_SYNTAX, "expected %d dimensions for this array but got %d", array_dims, $5);
}else{
while(t != NULL && t != undefined && getName(t)[0] != '&'){
t = getNextEntry(t);
}
if(getTypeEntry(t) != integ){
throw_error(ERROR_TYPE, "Arg for an array is not of type integer");
}
t = getNextEntry(t);
while(t != NULL && t != undefined && getName(t)[0] != '&'){
while(getTypeEntry(t) != integ)(arg_given != NULL && getName(arg_given)[0]!='&'){
arg_given = getNextEntry(arg_given);
}
if(getTypeEntry(arg_given) != getTypeEntry(param_arg_type)){
throw_error(ERROR_TYPE, "expected type %s expression as argument of a record in function call but got type %s", getType(param_arg_type), getType(arg_given));
}
arg_given = getNextEntry(arg_given);
param_arg_type = getNextEntry(param_arg_type);
}
}
}
*/
}
| RELEASE ID L_PAREN argument_list R_PAREN | RELEASE ID L_PAREN argument_list R_PAREN
{ {
int d = getAdInfoType((TableNode*)$2); int d = getAdInfoType((TableNode*)$2);

View File

@ -309,6 +309,8 @@ void emit_return(TNodeOrConst * value){
} }
void emit_reserve(TableNode * result, TNodeOrConst * size){ void emit_reserve(TableNode * result, TNodeOrConst * size){
// this needs to change
// we need to take a int
emit_parameter(size); emit_parameter(size);
emit_function_call(result, 1, tn_or_const(NODE, look_up(cur, "reserve"))); emit_function_call(result, 1, tn_or_const(NODE, look_up(cur, "reserve")));
} }

View File

@ -0,0 +1,11 @@
type main: string -> integer
function entry: main
type t: 3 -> integer
entry (arg) := {
[ t:a]
a := reserve a(1, 3, 4);
return 0;
}

View File

@ -14,7 +14,7 @@ bar (r,s) := {
*) *)
entry (arg) := { entry (arg) := {
[ Boolean:x ; Boolean:y ; Boolean:z ; Boolean:t] [ Boolean:x ; Boolean:y ; Boolean:z ; Boolean:t]
if ( ( x < y ) (* < ( z & t )*)) then { if ( ( x & y ) ) then {
(* (*
if ( x<y & !(x=y) ) then { if ( x<y & !(x=y) ) then {
@ -23,7 +23,7 @@ entry (arg) := {
} }
*) *)
z := 9; z := x < y;
} else { } else {
y := true; (* bar('c', 7); *) y := true; (* bar('c', 7); *)
} }

View File

@ -12,7 +12,7 @@ bar (r,s) := {
entry (arg) := { entry (arg) := {
[ Boolean:x ; Boolean:y ; Boolean:z ; Boolean:t] [ Boolean:x ; Boolean:y ; Boolean:z ; Boolean:t]
while ( !( x & y ) (* | ( z | t ) *) ) { while ( ( x | y ) (* | ( z | t ) *) ) {
(* (*
if ( ( x < y ) & ( z = t ) ) then { if ( ( x < y ) & ( z = t ) ) then {
y := z < t; y := z < t;
@ -20,7 +20,7 @@ entry (arg) := {
t := z = t; t := z = t;
} }
*) *)
y := true; (* bar('c', 7); *) y := t < z; (* bar('c', 7); *)
} }
(* x := x & y; *) (* x := x & y; *)
return 0; return 0;

View File

@ -0,0 +1,18 @@

View File

@ -3,7 +3,7 @@ function test: main
test (a) := { test (a) := {
[Boolean: b; integer: x; integer: y] [Boolean: b; integer: x; integer: y]
x := 1; character x := 1;
y := 2; y := 2;
b := x < y; b := x < y;
return 1; return 1;