It seems to be working but I need to ckeck again

This commit is contained in:
Meyer Simon
2025-05-03 19:29:38 -04:00
parent 4380d214bb
commit 31bc69d52b
6 changed files with 181 additions and 27 deletions

View File

@ -578,23 +578,8 @@ compound_statement:
S_Push(TrueList, S_Init(), 0);
S_Push(FalseList, S_Init(), 0);
}expression R_PAREN THEN {
emit_conditional_jump(E_IF_X_TRUE, 0, tn_or_const(NODE, $4));
Stack * t = S_Peek(TrueList);
if(t==NULL){
t = S_Init();
S_Push(TrueList, t, 1);
}
S_Push(t, current, 1);
emit_goto(0);
t = S_Peek(FalseList);
if(t==NULL){
t = S_Init();
S_Push(FalseList, t, 1);
}
S_Push(t, current, 1);
emit_label(label_gen());
emit_backpatch(S_Pop(TrueList), getLabel(current));
} sblock ELSE {
// NOTE we are not going back to
int l = label_gen();
@ -868,11 +853,47 @@ expression:
| expression AND expression
{
// ----------------------------------------------------------------------------
emit_conditional_jump(E_IF_X_TRUE, 0, tn_or_const(NODE, $1));
Stack * t1 = S_Peek(TrueList);
if(t1==NULL){
t1 = S_Init();
S_Push(TrueList, t1, 1);
}
S_Push(t1, current, 1);
emit_goto(0);
t1 = S_Peek(FalseList);
if(t1==NULL){
t1 = S_Init();
S_Push(FalseList, t1, 1);
}
S_Push(t1, current, 1);
emit_label(label_gen());
emit_backpatch(S_Pop(TrueList), getLabel(current));
emit_conditional_jump(E_IF_X_TRUE, 0, tn_or_const(NODE, $3));
Stack * t = S_Peek(TrueList);
if(t==NULL){
t = S_Init();
S_Push(TrueList, t, 1);
}
S_Push(t, current, 1);
emit_goto(0);
t = S_Peek(FalseList);
if(t==NULL){
t = S_Init();
S_Push(FalseList, t, 1);
}
S_Push(t, current, 1);
S_Merge(FalseList);
emit_label(label_gen());
emit_backpatch(S_Pop(TrueList), getLabel(current));
// ----------------------------------------------------------------------------
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) {
char* temp = temp_var_gen();
TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, boo, temp, NULL);
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;
} else {
$$=undefined;
@ -882,11 +903,47 @@ expression:
| expression OR expression
{
// ----------------------------------------------------------------------------
emit_conditional_jump(E_IF_X_TRUE, 0, tn_or_const(NODE, $1));
Stack * t1 = S_Peek(TrueList);
if(t1==NULL){
t1 = S_Init();
S_Push(TrueList, t1, 1);
}
S_Push(t1, current, 1);
emit_goto(0);
t1 = S_Peek(FalseList);
if(t1==NULL){
t1 = S_Init();
S_Push(FalseList, t1, 1);
}
S_Push(t1, current, 1);
emit_label(label_gen());
emit_backpatch(S_Pop(FalseList), getLabel(current));
emit_conditional_jump(E_IF_X_TRUE, 0, tn_or_const(NODE, $3));
Stack * t = S_Peek(TrueList);
if(t==NULL){
t = S_Init();
S_Push(TrueList, t, 1);
}
S_Push(t, current, 1);
emit_goto(0);
t = S_Peek(FalseList);
if(t==NULL){
t = S_Init();
S_Push(FalseList, t, 1);
}
S_Push(t, current, 1);
S_Merge(TrueList);
// This is very inportant.
// With OR W DO NOT BP THE TRUELIST Only with AND
// ----------------------------------------------------------------------------
printdebug("OR");
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);
emit_binary_op(E_OR,node,tn_or_const(NODE,$1),tn_or_const(NODE,$3));
// emit_binary_op(E_OR,node,tn_or_const(NODE,$1),tn_or_const(NODE,$3));
$$ = node;
} else {
$$=undefined;
@ -896,11 +953,10 @@ expression:
| expression LESS_THAN expression
{
// ----------------------------------------------------------------------------
char* temp = temp_var_gen();
TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, boo, temp, NULL);
// ----------------------------------------------------------------------------
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));
Stack * t = S_Peek(TrueList);
if(t==NULL){
@ -915,6 +971,9 @@ expression:
S_Push(FalseList, t, 1);
}
S_Push(t, current, 1);
// emit_label(label_gen());
// emit_backpatch(S_Pop(TrueList), getLabel(current));
/*
*/
// ----------------------------------------------------------------------------
printdebug("less than expression");
@ -930,13 +989,11 @@ expression:
| expression EQUAL_TO expression
{
printdebug("equals check expression");
if(getTypeEntry((TableNode*)$1) == getTypeEntry((TableNode*)$3) && getTypeEntry((TableNode*)$1) != undefined) {
char* temp = temp_var_gen();
TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, boo, temp, NULL);
// ----------------------------------------------------------------------------
emit_binary_op(E_EQUAL_TO, node, tn_or_const(NODE,$1), tn_or_const(NODE,$3));
/*
emit_conditional_jump(E_EQUAL_TO, 0, tn_or_const(NODE,$1), tn_or_const(NODE,$3));
emit_conditional_jump(E_IF_X_TRUE, 0, tn_or_const(NODE, node));
Stack * t = S_Peek(TrueList);
if(t==NULL){
t = S_Init();
@ -950,7 +1007,12 @@ expression:
S_Push(FalseList, t, 1);
}
S_Push(t, current, 1);
*/
// emit_label(label_gen());
// emit_backpatch(S_Pop(TrueList), getLabel(current));
// ----------------------------------------------------------------------------
printdebug("equals check expression");
if(getTypeEntry((TableNode*)$1) == getTypeEntry((TableNode*)$3) && getTypeEntry((TableNode*)$1) != undefined) {
// emit_binary_op(E_EQUAL_TO, node, tn_or_const(NODE,$1), tn_or_const(NODE,$3));
$$ = node;
} else {

View File

@ -58,7 +58,16 @@ int S_Size(Stack *s){
return s->size;
}
void S_Merge(Stack *s1, Stack *s2){
void S_Merge(Stack *list){
Stack* s1 = S_Pop(list);
Stack* s2 = S_Peek(list);
if(s1 == NULL){
return;
}
if(s2 == NULL){
S_Push(list, s1, 0);
return;
}
for (Instruction * i = S_Pop(s1); i; i = S_Pop(s1)){
S_Push(s2, i, 1);
}

View File

@ -32,6 +32,7 @@ void * S_Pop(Stack *s);
void * S_Peek(Stack *s);
bool S_IsEmpty(Stack *s);
int S_Size(Stack *s);
void S_Merge(Stack *list);
//______________________________________________________________________________________________
typedef union TNConstUnion TNConstUnion;

View File

@ -0,0 +1,26 @@
type rec: [character: x; integer: y]
type T2: rec -> integer
type main: string -> integer
function entry: main
function bar: T2
bar (r,s) := {
return 0;
}
entry (arg) := {
[ Boolean:x ; Boolean:y ; Boolean:z ; Boolean:t]
if ( !( x < y ) & ( z & t ) ) then {
(* if ( result < w.y ) then {
result := 8;
} else {
result := 9;
} *)
z := t;
} else {
y := true; (* bar('c', 7); *)
}
return 0;
}

View File

@ -0,0 +1,29 @@
type rec: [character: x; integer: y]
type T2: rec -> integer
type main: string -> integer
function entry: main
function bar: T2
bar (r,s) := {
return 0;
}
entry (arg) := {
[ Boolean:x ; Boolean:y ; Boolean:z ; Boolean:t]
if ( y & z | t ) then {
(* if ( ( x < y ) & ( z = t ) ) then {
y := z < t;
t:= 0;
} else {
t := z = t;
z := 1;
} *)
t := true;
} else {
y := true; (* bar('c', 7); *)
}
(* x := x & y; *)
return 0;
}

View File

@ -0,0 +1,27 @@
type rec: [character: x; integer: y]
type T2: rec -> integer
type main: string -> integer
function entry: main
function bar: T2
bar (r,s) := {
return 0;
}
entry (arg) := {
[ Boolean:x ; Boolean:y ; Boolean:z ; Boolean:t]
while ( !( x & y ) (* | ( z | t ) *) ) {
(*
if ( ( x < y ) & ( z = t ) ) then {
y := z < t;
} else {
t := z = t;
}
*)
y := true; (* bar('c', 7); *)
}
(* x := x & y; *)
return 0;
}