add unary minus operator

This commit is contained in:
Yuichi Nishiwaki 2013-11-07 12:17:37 +09:00
parent ae376b4903
commit 84c1156b9d
2 changed files with 17 additions and 1 deletions

View File

@ -30,6 +30,7 @@ enum pic_opcode {
OP_SUB, OP_SUB,
OP_MUL, OP_MUL,
OP_DIV, OP_DIV,
OP_MINUS,
OP_EQ, OP_EQ,
OP_LT, OP_LT,
OP_LE, OP_LE,

View File

@ -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_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_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_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 &&L_OP_EQ, &&L_OP_LT, &&L_OP_LE, &&L_OP_STOP
}; };
#endif #endif
@ -577,6 +577,21 @@ pic_apply(pic_state *pic, struct pic_proc *proc, pic_value argv)
NEXT; 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) \ #define DEFINE_COMP_OP(opcode, op) \
CASE(opcode) { \ CASE(opcode) { \
pic_value a, b; \ pic_value a, b; \