Not working
This commit is contained in:
@ -31,6 +31,6 @@ int offset;
|
|||||||
int currentsp;
|
int currentsp;
|
||||||
CGNode *cgList;
|
CGNode *cgList;
|
||||||
|
|
||||||
extern Stack* s;
|
Stack* stack;
|
||||||
Stack* TrueList;
|
Stack* TrueList;
|
||||||
Stack* FalseList;
|
Stack* FalseList;
|
||||||
|
@ -270,7 +270,7 @@ definition:
|
|||||||
}
|
}
|
||||||
//printf("Ending ID: %s\n", $1);
|
//printf("Ending ID: %s\n", $1);
|
||||||
//printf("Ending Type: %s\n", getType(table_lookup(getAncestor(cur), $1)));
|
//printf("Ending Type: %s\n", getType(table_lookup(getAncestor(cur), $1)));
|
||||||
} idlist R_PAREN ASSIGN sblock
|
}
|
||||||
|
|
||||||
;
|
;
|
||||||
|
|
||||||
@ -367,6 +367,7 @@ idlist:
|
|||||||
sblock:
|
sblock:
|
||||||
L_BRACE
|
L_BRACE
|
||||||
{
|
{
|
||||||
|
// emit_label(label_gen());
|
||||||
if (getLine(cur) != 0) {
|
if (getLine(cur) != 0) {
|
||||||
cur = CreateScope(cur,@1.first_line,@1.first_column);
|
cur = CreateScope(cur,@1.first_line,@1.first_column);
|
||||||
printdebug("Created a new scope");
|
printdebug("Created a new scope");
|
||||||
@ -554,19 +555,15 @@ compound_statement statement_list {
|
|||||||
compound_statement:
|
compound_statement:
|
||||||
WHILE L_PAREN
|
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 {
|
} expression R_PAREN {
|
||||||
emit_label(label_gen());
|
emit_label(label_gen());
|
||||||
backpatch(S_Pop(TrueList), getLabel(current));
|
|
||||||
} sblock {
|
} sblock {
|
||||||
$$ = $7;
|
$$ = $7;
|
||||||
emit_label(label_gen());
|
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) {
|
if ($7 == undefined && $10 != undefined) {
|
||||||
@ -657,9 +654,11 @@ rec_op:
|
|||||||
|
|
||||||
|
|
||||||
ablock:
|
ablock:
|
||||||
L_PAREN argument_list R_PAREN
|
L_PAREN {}
|
||||||
|
argument_list R_PAREN
|
||||||
{
|
{
|
||||||
$$ = $2;
|
// here
|
||||||
|
$$ = $3;
|
||||||
printdebug("ablock is %d", $$);
|
printdebug("ablock is %d", $$);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -669,13 +668,20 @@ ablock:
|
|||||||
|
|
||||||
|
|
||||||
argument_list:
|
argument_list:
|
||||||
//NEED TO EMIT PARAMETERS HERE. MAYBE USE STACK STRUCTURE
|
|
||||||
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 emits params for function and arrays TODO: fix
|
// this emits params for function and arrays TODO: fix
|
||||||
emit_parameter(tn_or_const(NODE,arg));
|
emit_parameter(tn_or_const(NODE,arg));
|
||||||
//S_Push(stack,current);
|
Stack * t = S_Peek(stack);
|
||||||
//emit_detach();
|
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", $$);
|
//printdebug("[ARGUMENT_LIST] argument list is %d", $$);
|
||||||
}
|
}
|
||||||
COMMA argument_list
|
COMMA argument_list
|
||||||
@ -686,8 +692,7 @@ argument_list:
|
|||||||
{
|
{
|
||||||
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);
|
||||||
emit_parameter(tn_or_const(NODE,arg));
|
emit_parameter(tn_or_const(NODE,arg));
|
||||||
//S_Push(stack,current);
|
emit_push_all(S_Pop(stack));
|
||||||
//emit_detach();
|
|
||||||
$$ = 1;
|
$$ = 1;
|
||||||
printdebug("[ARGUMENT_LIST] argument list is %d", $$);
|
printdebug("[ARGUMENT_LIST] argument list is %d", $$);
|
||||||
}
|
}
|
||||||
@ -840,7 +845,7 @@ expression:
|
|||||||
| expression LESS_THAN expression
|
| expression LESS_THAN expression
|
||||||
{
|
{
|
||||||
emit_conditional_jump(E_LESS_THAN, 0, tn_or_const(NODE,$1), tn_or_const(NODE,$3));
|
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");
|
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) {
|
||||||
char* temp = temp_var_gen();
|
char* temp = temp_var_gen();
|
||||||
@ -859,6 +864,7 @@ 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_conditional_jump(E_EQUAL_TO, 0, 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_goto(0);
|
||||||
$$ = node;
|
$$ = node;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
@ -1008,7 +1014,16 @@ assignable:
|
|||||||
t= TYPE_UNDEFINED;
|
t= TYPE_UNDEFINED;
|
||||||
throw_error(ERROR_TYPE, "Undefined type returned by function.");
|
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);
|
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;
|
$$ = node;
|
||||||
//NOTE ADD ASSIGNMENT EMIT HERE (MIGHT NEED TO PUSH TO STACK for function call)
|
//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));
|
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.");
|
throw_error(ERROR_TYPE, "Undefined type stored in array.");
|
||||||
}
|
}
|
||||||
TableNode* node = CreateEntry(cur,t, typeNode2, temp, NULL);
|
TableNode* node = CreateEntry(cur,t, typeNode2, temp, NULL);
|
||||||
//emit assign here
|
//TODO: emit assign here
|
||||||
//emit_array_access(char* node, char* array, ...)
|
//TODO: emit_array_access(char* node, char* array, ...)
|
||||||
$$ = node;
|
$$ = node;
|
||||||
printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", getType((TableNode*)$1), getName((TableNode*)$1));
|
printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", getType((TableNode*)$1), getName((TableNode*)$1));
|
||||||
}
|
}
|
||||||
|
@ -15,16 +15,17 @@ void S_Free(Stack *s){
|
|||||||
free(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));
|
__Node * n = calloc(1, sizeof(*n));
|
||||||
n->v = v;
|
n->v = v;
|
||||||
n->next = s->n;
|
n->next = s->n;
|
||||||
s->n = n;
|
s->n = n;
|
||||||
|
s->w = i;
|
||||||
s->size = s->size + 1;
|
s->size = s->size + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void * S_Pop(Stack *s) {
|
void * S_Pop(Stack *s) {
|
||||||
if (s->size == 0) {
|
if (s == NULL || !S_IsEmpty(s)) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
__Node * node = s->n;
|
__Node * node = s->n;
|
||||||
@ -37,20 +38,23 @@ void * S_Pop(Stack *s) {
|
|||||||
|
|
||||||
|
|
||||||
void * S_Peek(Stack *s){
|
void * S_Peek(Stack *s){
|
||||||
if (!S_IsEmpty(s)) {
|
if (s == NULL || !S_IsEmpty(s)) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return s->n->v;
|
return s->n->v;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool S_IsEmpty(Stack *s){
|
bool S_IsEmpty(Stack *s){
|
||||||
if(!s->size) {
|
if(!s) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int S_Size(Stack *s){
|
int S_Size(Stack *s){
|
||||||
|
if (s == NULL || !S_IsEmpty(s)) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
return s->size;
|
return s->size;
|
||||||
}
|
}
|
||||||
//_______________________________________________________________________
|
//_______________________________________________________________________
|
||||||
@ -64,6 +68,15 @@ char * temp = NULL;
|
|||||||
otherwise make it next of current and set cur to your instruction.
|
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(){
|
void emit_detach(){
|
||||||
current = current->prev;
|
current = current->prev;
|
||||||
current->next = NULL;
|
current->next = NULL;
|
||||||
@ -162,6 +175,12 @@ void emit_binary_op(
|
|||||||
current->operand2 = arg2;
|
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){
|
void emit_unary_op(Op op, TableNode * result, TNodeOrConst * arg){
|
||||||
emit_helper();
|
emit_helper();
|
||||||
current->opcode = op;
|
current->opcode = op;
|
||||||
@ -257,8 +276,8 @@ void emit_function_call(
|
|||||||
){
|
){
|
||||||
emit_helper();
|
emit_helper();
|
||||||
current->opcode = E_CALL;
|
current->opcode = E_CALL;
|
||||||
current->operand1 = tn_or_const(INTEGER, ¶m_count);
|
current->operand1 = name;
|
||||||
current->operand2 = name;
|
current->operand2 = tn_or_const(INTEGER, ¶m_count);
|
||||||
current->result = result;
|
current->result = result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,11 +22,12 @@ typedef struct __Node {
|
|||||||
|
|
||||||
typedef struct Stack {
|
typedef struct Stack {
|
||||||
__Node * n;
|
__Node * n;
|
||||||
|
int w;
|
||||||
int size;
|
int size;
|
||||||
} Stack;
|
} Stack;
|
||||||
Stack * S_Init();
|
Stack * S_Init();
|
||||||
void S_Free(Stack *s);
|
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_Pop(Stack *s);
|
||||||
void * S_Peek(Stack *s);
|
void * S_Peek(Stack *s);
|
||||||
bool S_IsEmpty(Stack *s);
|
bool S_IsEmpty(Stack *s);
|
||||||
|
@ -73,8 +73,8 @@ entry (arg) := {
|
|||||||
w := reserve w;
|
w := reserve w;
|
||||||
w.x := 5;
|
w.x := 5;
|
||||||
w.y := 7;
|
w.y := 7;
|
||||||
result := bar1(w);
|
(* result := bar1(w); *)
|
||||||
result := bar2(5,7);
|
result := bar2(5,7);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
80
tests/sprint2/test/sp2_llnode_bad.alpha
Normal file
80
tests/sprint2/test/sp2_llnode_bad.alpha
Normal 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';
|
||||||
|
}
|
Reference in New Issue
Block a user