From af9dd45e65afc2d025fb3b4bed5032537310d76c Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Tue, 15 Oct 2013 20:05:12 +0900 Subject: [PATCH] change the type of immediate number value from int to double --- include/picrin/irep.h | 3 ++- include/picrin/value.h | 10 +++++----- src/eval.c | 2 +- src/parse.y | 14 +++----------- src/scan.l | 2 +- src/value.c | 10 +++++----- src/vm.c | 16 ++++++++-------- src/write.c | 4 ++-- 8 files changed, 27 insertions(+), 34 deletions(-) diff --git a/include/picrin/irep.h b/include/picrin/irep.h index 14378424..f870d54f 100644 --- a/include/picrin/irep.h +++ b/include/picrin/irep.h @@ -3,7 +3,7 @@ enum pic_instruction { OP_PUSHNIL, - OP_PUSHI, + OP_PUSHNUM, OP_PUSHUNDEF, OP_GREF, OP_GSET, @@ -16,6 +16,7 @@ enum pic_instruction { struct pic_code { enum pic_instruction insn; union { + double f; int i; struct pic_pair *gvar; } u; diff --git a/include/picrin/value.h b/include/picrin/value.h index a85d6660..2bca6f0a 100644 --- a/include/picrin/value.h +++ b/include/picrin/value.h @@ -3,7 +3,7 @@ enum pic_vtype { PIC_VTYPE_NIL, - PIC_VTYPE_INT, + PIC_VTYPE_FLOAT, PIC_VTYPE_UNDEF, PIC_VTYPE_HEAP }; @@ -12,14 +12,14 @@ typedef struct { enum pic_vtype type; union { void *data; - int i; + double f; } u; } pic_value; enum pic_tt { /* immediate */ PIC_TT_NIL, - PIC_TT_INT, + PIC_TT_FLOAT, PIC_TT_UNDEF, /* heap */ PIC_TT_PAIR, @@ -56,9 +56,9 @@ enum pic_tt pic_type(pic_value); pic_value pic_nil_value(); pic_value pic_undef_value(); pic_value pic_obj_value(void *); -pic_value pic_int_value(int); +pic_value pic_float_value(double); -#define pic_int(v) ((v).u.i) +#define pic_float(v) ((v).u.f) #define pic_nil_p(v) (pic_type(v) == PIC_TT_NIL) diff --git a/src/eval.c b/src/eval.c index 637f1c8b..2d1926ab 100644 --- a/src/eval.c +++ b/src/eval.c @@ -74,7 +74,7 @@ pic_eval(pic_state *pic, pic_value obj, struct pic_env *env) /* not implemented */ } } - case PIC_TT_INT: + case PIC_TT_FLOAT: case PIC_TT_NIL: { return obj; } diff --git a/src/parse.y b/src/parse.y index e30b3e3c..bff7e832 100644 --- a/src/parse.y +++ b/src/parse.y @@ -19,10 +19,10 @@ struct parser_control { } %token tLPAREN tRPAREN tDOT -%token tSYMBOL tINT +%token tSYMBOL tNUMBER %type datum simple_datum symbol compound_datum -%type number integer list list_tail +%type number list list_tail %% @@ -55,14 +55,7 @@ symbol ; number - : integer -; - -integer - : tINT - { - $$ = $1; - } + : tNUMBER ; compound_datum @@ -106,7 +99,6 @@ pic_parse(pic_state *pic, const char *str) struct parser_control p; p.pic = pic; - p.value = pic_int_value(42); yy_scan_string(str); yyparse(&p); diff --git a/src/scan.l b/src/scan.l index a3300a0e..8b4aba98 100644 --- a/src/scan.l +++ b/src/scan.l @@ -19,7 +19,7 @@ struct parser_control { [ \t\n\r] /* skip whitespace */ "(" return tLPAREN; ")" return tRPAREN; -[1-9][0-9]* { yylval.datum = pic_int_value(atoi(yytext)); return tINT; } +[1-9][0-9]* { yylval.datum = pic_float_value(atoi(yytext)); return tNUMBER; } [a-z0-9A-Z]+ { yylval.datum = pic_intern_cstr(p->pic, yytext); return tSYMBOL; } %% diff --git a/src/value.c b/src/value.c index fe8c996e..9b8becfd 100644 --- a/src/value.c +++ b/src/value.c @@ -8,8 +8,8 @@ pic_type(pic_value v) switch (v.type) { case PIC_VTYPE_NIL: return PIC_TT_NIL; - case PIC_VTYPE_INT: - return PIC_TT_INT; + case PIC_VTYPE_FLOAT: + return PIC_TT_FLOAT; case PIC_VTYPE_UNDEF: return PIC_TT_UNDEF; case PIC_VTYPE_HEAP: @@ -38,12 +38,12 @@ pic_obj_value(void *ptr) } pic_value -pic_int_value(int i) +pic_float_value(double f) { pic_value v; - v.type = PIC_VTYPE_INT; - v.u.i = i; + v.type = PIC_VTYPE_FLOAT; + v.u.f = f; return v; } diff --git a/src/vm.c b/src/vm.c index 2c47063b..5a1c29d3 100644 --- a/src/vm.c +++ b/src/vm.c @@ -100,8 +100,8 @@ print_irep(pic_state *pic, struct pic_irep *irep) case OP_PUSHNIL: puts("OP_PUSHNIL"); break; - case OP_PUSHI: - printf("OP_PUSHI\t%d\n", irep->code[i].u.i); + case OP_PUSHNUM: + printf("OP_PUSHNUM\t%f\n", irep->code[i].u.f); break; case OP_PUSHUNDEF: puts("OP_PUSHUNDEF"); @@ -190,9 +190,9 @@ pic_gen(pic_state *pic, struct pic_irep *irep, pic_value obj, struct pic_env *en break; } } - case PIC_TT_INT: { - irep->code[irep->clen].insn = OP_PUSHI; - irep->code[irep->clen].u.i = pic_int(obj); + case PIC_TT_FLOAT: { + irep->code[irep->clen].insn = OP_PUSHNUM; + irep->code[irep->clen].u.f = pic_float(obj); irep->clen++; break; } @@ -283,8 +283,8 @@ pic_run(pic_state *pic, struct pic_proc *proc, pic_value args) PUSH(pic_nil_value()); NEXT; } - CASE(OP_PUSHI) { - PUSH(pic_int_value(pc->u.i)); + CASE(OP_PUSHNUM) { + PUSH(pic_float_value(pc->u.f)); NEXT; } CASE(OP_PUSHUNDEF) { @@ -322,7 +322,7 @@ pic_run(pic_state *pic, struct pic_proc *proc, pic_value args) pic_value a, b; a = POP(); b = POP(); - PUSH(pic_int_value(pic_int(a) + pic_int(b))); + PUSH(pic_float_value(pic_float(a) + pic_float(b))); NEXT; } CASE(OP_STOP) { diff --git a/src/write.c b/src/write.c index a6ca251c..2aaa84b8 100644 --- a/src/write.c +++ b/src/write.c @@ -21,8 +21,8 @@ pic_debug(pic_state *pic, pic_value obj) case PIC_TT_SYMBOL: printf("%s", pic_symbol_ptr(obj)->name); break; - case PIC_TT_INT: - printf("%d", pic_int(obj)); + case PIC_TT_FLOAT: + printf("%f", pic_float(obj)); break; case PIC_TT_UNDEF: printf("#");