Skip to content

Commit

Permalink
This is unbelievable. It works so beautifully. I am done
Browse files Browse the repository at this point in the history
with switch!
  • Loading branch information
eudisd committed Apr 25, 2011
1 parent 1a7892f commit 2e48dfc
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 11 deletions.
Binary file modified bin/c
Binary file not shown.
Binary file modified examples/bin/c
Binary file not shown.
Binary file modified examples/bin/cvm
Binary file not shown.
Binary file modified src/compiler/c
Binary file not shown.
42 changes: 33 additions & 9 deletions src/compiler/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -399,23 +399,38 @@ void Case()

if(tk == TK_CASE){
match("case");


/* The result of E() is pushed onto the stack right now, we must save it then
compare down the chain */

Instruction inst_hole, inst_dup, inst_junk, inst_eql;

/* The dup happens before the E() */
printf("%d: dup \n", code_count);

inst_dup.opcode = OP_DUP;
inst_dup.operand.i = 0;
code[code_count] = inst_dup;
code_count++;

TYPE t = E();
if(t != 'C' && t != 'I'){
fprintf(stderr, "Error in switch statement! Case must be integer constant!");
exit(EXIT_FAILURE);
}
match(":");

/* The result of E() is pushed onto the stack right now, we must save it then
compare down the chain */

Instruction inst_hole;

printf("%d: dup \n", code_count);
/* generate code for equality test here */

code_count++;// There is a dup here
printf("%d: eql \n", code_count);
inst_eql.opcode = OP_EQL;
inst_eql.operand.i = 0;
code[code_count] = inst_eql;
code_count++;

printf("%d: jfalse 0 (Dummy Hole) \n", code_count);


/* Since this is the first case, we don't lookup the
to see if there already a case hole */
Expand All @@ -424,11 +439,20 @@ void Case()
inst_hole.operand.i = 0;
code[code_count] = inst_hole;
case_hole = code_count;

code_count++;

Statements();


code[case_hole].operand.i = code_count; /* Fill in hole before we are done in this case */

code[case_hole].operand.i = code_count;
/* Pop junk off the stack */
printf("%d: pop (Empty) \n", code_count);

inst_junk.opcode = OP_POPEMPTY;
inst_junk.operand.i = 0;
code[code_count] = inst_junk;
code_count++;

Case();
}
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/test.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

switch(1){
switch(2){
case 1:
printf (1);
case 2:
Expand Down
2 changes: 1 addition & 1 deletion src/cvm/cvm.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ void run(char *program)
break;
case OP_EQL:
stack[sp - 2].i = stack[sp - 2].i == stack[sp - 1].i;
break;
sp--;
break;
case OP_NEG:
stack[sp - 1].i = -stack[sp - 1].i;
Expand Down

0 comments on commit 2e48dfc

Please sign in to comment.