From 84c1156b9de8f3052b9ff5b61954ff8156a4c3c1 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Thu, 7 Nov 2013 12:17:37 +0900 Subject: [PATCH] add unary minus operator --- include/picrin/irep.h | 1 + src/vm.c | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/include/picrin/irep.h b/include/picrin/irep.h index b55b7ec0..00b53110 100644 --- a/include/picrin/irep.h +++ b/include/picrin/irep.h @@ -30,6 +30,7 @@ enum pic_opcode { OP_SUB, OP_MUL, OP_DIV, + OP_MINUS, OP_EQ, OP_LT, OP_LE, diff --git a/src/vm.c b/src/vm.c index cd450224..6a0392d2 100644 --- a/src/vm.c +++ b/src/vm.c @@ -260,7 +260,7 @@ pic_apply(pic_state *pic, struct pic_proc *proc, pic_value argv) &&L_OP_GREF, &&L_OP_GSET, &&L_OP_LREF, &&L_OP_LSET, &&L_OP_CREF, &&L_OP_CSET, &&L_OP_JMP, &&L_OP_JMPIF, &&L_OP_CALL, &&L_OP_TAILCALL, &&L_OP_RET, &&L_OP_LAMBDA, &&L_OP_CONS, &&L_OP_CAR, &&L_OP_CDR, &&L_OP_NILP, - &&L_OP_ADD, &&L_OP_SUB, &&L_OP_MUL, &&L_OP_DIV, + &&L_OP_ADD, &&L_OP_SUB, &&L_OP_MUL, &&L_OP_DIV, &&L_OP_MINUS, &&L_OP_EQ, &&L_OP_LT, &&L_OP_LE, &&L_OP_STOP }; #endif @@ -577,6 +577,21 @@ pic_apply(pic_state *pic, struct pic_proc *proc, pic_value argv) NEXT; } + CASE(OP_MINUS) { + pic_value n; + n = POP(); + if (pic_int_p(n)) { + PUSH(pic_int_value(-pic_int(n))); + } + else if (pic_float_p(n)) { + PUSH(pic_float_value(-pic_float(n))); + } + else { + pic->errmsg = "unary - got a non-number operand"; + } + NEXT; + } + #define DEFINE_COMP_OP(opcode, op) \ CASE(opcode) { \ pic_value a, b; \