Not working

This commit is contained in:
Meyer Simon
2025-04-30 21:55:36 -04:00
parent cd4393d052
commit 2b07464f84
6 changed files with 143 additions and 28 deletions

View File

@ -31,6 +31,6 @@ int offset;
int currentsp;
CGNode *cgList;
extern Stack* s;
Stack* stack;
Stack* TrueList;
Stack* FalseList;

View File

@ -270,7 +270,7 @@ definition:
}
//printf("Ending ID: %s\n", $1);
//printf("Ending Type: %s\n", getType(table_lookup(getAncestor(cur), $1)));
} idlist R_PAREN ASSIGN sblock
}
;
@ -367,6 +367,7 @@ idlist:
sblock:
L_BRACE
{
// emit_label(label_gen());
if (getLine(cur) != 0) {
cur = CreateScope(cur,@1.first_line,@1.first_column);
printdebug("Created a new scope");
@ -554,19 +555,15 @@ compound_statement statement_list {
compound_statement:
WHILE L_PAREN
{
if (!TrueList) {TrueList = S_Init();}
if (!FalseList) {FalseList = S_Init();}
S_Push(TrueList, S_Init());
S_Push(FalseList, S_Init());
} expression R_PAREN {
emit_label(label_gen());
backpatch(S_Pop(TrueList), getLabel(current));
} sblock {
$$ = $7;
emit_label(label_gen());
backpatch(S_Pop(FalseList), getLabel(current));
}
| IF L_PAREN expression R_PAREN THEN {emit_label(label_gen());} sblock ELSE {emit_label(label_gen());} sblock {
| IF L_PAREN expression R_PAREN THEN {emit_label(label_gen());}
sblock ELSE {emit_label(label_gen());}
sblock {
/*
*/
if ($7 == undefined && $10 != undefined) {
@ -657,9 +654,11 @@ rec_op:
ablock:
L_PAREN argument_list R_PAREN
L_PAREN {}
argument_list R_PAREN
{
$$ = $2;
// here
$$ = $3;
printdebug("ablock is %d", $$);
}
@ -669,13 +668,20 @@ ablock:
argument_list:
//NEED TO EMIT PARAMETERS HERE. MAYBE USE STACK STRUCTURE
expression{
TableNode* arg = CreateEntry(cur, getAdInfoType((TableNode*)$1), getTypeEntry((TableNode*)$1), arg_var_gen(), NULL);
// this emits params for function and arrays TODO: fix
emit_parameter(tn_or_const(NODE,arg));
//S_Push(stack,current);
//emit_detach();
Stack * t = S_Peek(stack);
if (stack == NULL){
stack = S_Init();
}
if(t == NULL){
t = S_Init();
S_Push(stack, t, 0);
}
S_Push(t, current, 1);
emit_detach();
//printdebug("[ARGUMENT_LIST] argument list is %d", $$);
}
COMMA argument_list
@ -686,8 +692,7 @@ argument_list:
{
TableNode* arg = CreateEntry(cur, getAdInfoType((TableNode*)$1), getTypeEntry((TableNode*)$1), arg_var_gen(), NULL);
emit_parameter(tn_or_const(NODE,arg));
//S_Push(stack,current);
//emit_detach();
emit_push_all(S_Pop(stack));
$$ = 1;
printdebug("[ARGUMENT_LIST] argument list is %d", $$);
}
@ -840,7 +845,7 @@ expression:
| expression LESS_THAN expression
{
emit_conditional_jump(E_LESS_THAN, 0, tn_or_const(NODE,$1), tn_or_const(NODE,$3));
S_Push(S_Peek(TrueList), current);
emit_goto(0);
printdebug("less than expression");
if(getTypeEntry((TableNode*)$1) == getTypeEntry((TableNode*)$3) && getTypeEntry((TableNode*)$1)==integ) {
char* temp = temp_var_gen();
@ -859,6 +864,7 @@ expression:
char* temp = temp_var_gen();
TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, boo, temp, NULL);
emit_conditional_jump(E_EQUAL_TO, 0, tn_or_const(NODE,$1), tn_or_const(NODE,$3));
emit_goto(0);
$$ = node;
} else {
@ -1008,7 +1014,16 @@ assignable:
t= TYPE_UNDEFINED;
throw_error(ERROR_TYPE, "Undefined type returned by function.");
}
// TODO: add from stack
// TODO: emit_function_call
TableNode* node = CreateEntry(cur,t, typeNode2, temp, NULL);
int a = 0;
/*
if(S_IsEmpty(stack)){
int a = S_Size(S_Peek(stack));
}
*/
emit_function_call(node, a, tn_or_const(NODE, $1));
$$ = node;
//NOTE ADD ASSIGNMENT EMIT HERE (MIGHT NEED TO PUSH TO STACK for function call)
printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", getName(typeNode2), getName((TableNode*)$1));
@ -1037,8 +1052,8 @@ assignable:
throw_error(ERROR_TYPE, "Undefined type stored in array.");
}
TableNode* node = CreateEntry(cur,t, typeNode2, temp, NULL);
//emit assign here
//emit_array_access(char* node, char* array, ...)
//TODO: emit assign here
//TODO: emit_array_access(char* node, char* array, ...)
$$ = node;
printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", getType((TableNode*)$1), getName((TableNode*)$1));
}

View File

@ -15,16 +15,17 @@ void S_Free(Stack *s){
free(s);
}
void S_Push(Stack * s, void *v) {
void S_Push(Stack * s, void *v, int i) {
__Node * n = calloc(1, sizeof(*n));
n->v = v;
n->next = s->n;
s->n = n;
s->w = i;
s->size = s->size + 1;
}
void * S_Pop(Stack *s) {
if (s->size == 0) {
if (s == NULL || !S_IsEmpty(s)) {
return NULL;
}
__Node * node = s->n;
@ -37,20 +38,23 @@ void * S_Pop(Stack *s) {
void * S_Peek(Stack *s){
if (!S_IsEmpty(s)) {
if (s == NULL || !S_IsEmpty(s)) {
return NULL;
}
return s->n->v;
}
bool S_IsEmpty(Stack *s){
if(!s->size) {
if(!s) {
return true;
}
return false;
}
int S_Size(Stack *s){
if (s == NULL || !S_IsEmpty(s)) {
return 0;
}
return s->size;
}
//_______________________________________________________________________
@ -64,6 +68,15 @@ char * temp = NULL;
otherwise make it next of current and set cur to your instruction.
*/
void emit_push_all(Stack * s){
for (Instruction * i = S_Pop(s); s; i = S_Pop(s)){
current->next = i;
i->prev = current;
current = i;
current->next = NULL;
}
}
void emit_detach(){
current = current->prev;
current->next = NULL;
@ -162,6 +175,12 @@ void emit_binary_op(
current->operand2 = arg2;
}
void emit_goto(int i){
emit_helper();
current->opcode = E_GOTO;
current->label = i;
}
void emit_unary_op(Op op, TableNode * result, TNodeOrConst * arg){
emit_helper();
current->opcode = op;
@ -257,8 +276,8 @@ void emit_function_call(
){
emit_helper();
current->opcode = E_CALL;
current->operand1 = tn_or_const(INTEGER, &param_count);
current->operand2 = name;
current->operand1 = name;
current->operand2 = tn_or_const(INTEGER, &param_count);
current->result = result;
}

View File

@ -22,11 +22,12 @@ typedef struct __Node {
typedef struct Stack {
__Node * n;
int w;
int size;
} Stack;
Stack * S_Init();
void S_Free(Stack *s);
void S_Push(Stack * s, void *v);
void S_Push(Stack * s, void *v, int i);
void * S_Pop(Stack *s);
void * S_Peek(Stack *s);
bool S_IsEmpty(Stack *s);

View File

@ -73,8 +73,8 @@ entry (arg) := {
w := reserve w;
w.x := 5;
w.y := 7;
result := bar1(w);
(* result := bar1(w); *)
result := bar2(5,7);
return 0;
}
}

View File

@ -0,0 +1,80 @@
type main: string -> integer
function entry: main
type rec: [integer: x; integer: y]
type T1: integer -> integer
type T2: rec -> integer
type llnode: [llnode: prev; integer: val; llnode: next]
type list: integer -> llnode
function foo : T1
function bar1 : T2
function bar2 : T2
function make_list : list
make_list (a) := {
[integer:orig_a; llnode: ret; llnode: curr; llnode: temp]
if (a < 0 | a = 0) then {
return null;
} else {
ret := reserve ret;
ret.prev := null;
ret.next := null;
ret.val := a;
while (0 < a) {
temp := reserve temp;
temp.prev := null;
temp.next := null;
temp.val := ret.val;
if (a = orig_a) then {
ret.next := temp;
temp.prev := ret;
curr := temp;
} else {
curr.next := temp;
temp.prev := curr;
curr := temp;
}
a := a - 1;
}
return ret;
}
}
foo (x) := {
return x * x;
}
bar1(a,b) := {
return a * b;
}
bar2(r,s) := {
if (r < s) then {
while (!(r < s)) {
r := r + 1;
}
} else {
[integer: x]
x := 0;
while (x < 10) {
r := r + s;
}
}
return r * s;
}
entry (arg) := {
[ integer: result ; rec: w; llnode: li; boolean: b]
li := make_list(6, 7);
result := foo(5);
w := reserve w;
w.x := 5;
w.y := 7;
result := bar1(w);
result := bar2(5,7);
return 'a';
}