|
From: Keith J. <bu...@us...> - 2003-08-08 11:38:24
|
Update of /cvsroot/cup-language/cup/src/vm
In directory sc8-pr-cvs1:/tmp/cvs-serv6306/vm
Modified Files:
debug.c execute.c
Log Message:
Completed the ADD opcode, the println() function will now work.
Index: debug.c
===================================================================
RCS file: /cvsroot/cup-language/cup/src/vm/debug.c,v
retrieving revision 1.12
retrieving revision 1.13
diff -C2 -r1.12 -r1.13
*** debug.c 6 Aug 2003 19:06:47 -0000 1.12
--- debug.c 8 Aug 2003 11:38:20 -0000 1.13
***************
*** 10,17 ****
"NOP", "PUSH", "SET", "ADD",
"SUB", "MUL", "DIV", "MOD",
! "NEG", "NOT", "EQ", "JMP",
! "JMPF", "JMPT", "JMPR", "RET",
! "BUILTIN", "ARG", "POP", "VAR",
! "PUSHSCOPE", "POPSCOPE", "EXIT"
};
--- 10,17 ----
"NOP", "PUSH", "SET", "ADD",
"SUB", "MUL", "DIV", "MOD",
! "NEG", "NOT", "EQ", "NEQ",
! "JMP", "JMPF", "JMPT", "JMPR",
! "RET", "BUILTIN", "ARG", "POP",
! "VAR", "PUSHSCOPE", "POPSCOPE", "EXIT"
};
Index: execute.c
===================================================================
RCS file: /cvsroot/cup-language/cup/src/vm/execute.c,v
retrieving revision 1.16
retrieving revision 1.17
diff -C2 -r1.16 -r1.17
*** execute.c 7 Aug 2003 17:09:57 -0000 1.16
--- execute.c 8 Aug 2003 11:38:20 -0000 1.17
***************
*** 11,14 ****
--- 11,15 ----
void cupvm_NOT(CUPVM *);
void cupvm_EQ(CUPVM *);
+ void cupvm_ADD(CUPVM *);
void cupvm_SET(CUPVM *);
void cupvm_ARG(CUPVM *, unsigned long);
***************
*** 53,56 ****
--- 54,60 ----
break;
case CUPOP_ADD:
+ cupvm_ADD(vm);
+ vm->pc++;
+ break;
case CUPOP_SUB:
case CUPOP_MUL:
***************
*** 471,473 ****
--- 475,544 ----
if (!cupvm_value_push(vm, ret))
cupvm_tb(vm, "cupvm_EQ()", "failed to push value");
+ }
+
+ void cupvm_ADD(CUPVM *vm)
+ {
+ CUPVALUE *v1, *v2, *ret;
+
+ v2 = cupvm_value_pop(vm);
+ if (!v2)
+ {
+ cupvm_tb(vm, "cupvm_EQ()", "failed to pop value 2");
+ return;
+ }
+ v2->references--;
+ v1 = cupvm_value_pop(vm);
+ if (!v1)
+ {
+ cupvm_tb(vm, "cupvm_EQ()", "failed to pop value 1");
+ if (!v2->references)
+ cupvm_value_free(v2);
+ return;
+ }
+ v1->references--;
+
+ if (v1->type != v2->type)
+ ret = NULL;
+ else
+ {
+ switch (v1->type)
+ {
+ case CUPTYPE_STRING:
+ ret = cupvm_value_new();
+ ret->references = 1;
+ ret->type = CUPTYPE_STRING;
+ ret->data.val_string = dstrdup(v1->data.val_string, TYPE_CUPVM);
+ ret->data.val_string = drealloc(ret->data.val_string,
+ strlen(v1->data.val_string) + strlen(v2->data.val_string) + 1);
+ strcat(ret->data.val_string, v2->data.val_string);
+ break;
+ case CUPTYPE_INTEGER:
+ ret = cupvm_value_new();
+ ret->references = 1;
+ ret->type = CUPTYPE_INTEGER;
+ ret->data.val_integer = v1->data.val_integer + v2->data.val_integer;
+ break;
+ case CUPTYPE_FLOAT:
+ ret = cupvm_value_new();
+ ret->references = 1;
+ ret->type = CUPTYPE_FLOAT;
+ ret->data.val_float = v1->data.val_float + v2->data.val_float;
+ break;
+ default:
+ ret = NULL;
+ break;
+ }
+ }
+
+ if (!v1->references)
+ cupvm_value_free(v1);
+ if (!v2->references)
+ cupvm_value_free(v2);
+ if (ret)
+ {
+ if (!cupvm_value_push(vm, ret))
+ cupvm_tb(vm, "cupvm_ADD()", "failed to push value");
+ }
+ else
+ cupvm_tb(vm, "cupvm_ADD()", "unable to add types");
}
|