change the type of immediate number value from int to double

This commit is contained in:
Yuichi Nishiwaki 2013-10-15 20:05:12 +09:00
parent 02d52723ac
commit af9dd45e65
8 changed files with 27 additions and 34 deletions

View File

@ -3,7 +3,7 @@
enum pic_instruction { enum pic_instruction {
OP_PUSHNIL, OP_PUSHNIL,
OP_PUSHI, OP_PUSHNUM,
OP_PUSHUNDEF, OP_PUSHUNDEF,
OP_GREF, OP_GREF,
OP_GSET, OP_GSET,
@ -16,6 +16,7 @@ enum pic_instruction {
struct pic_code { struct pic_code {
enum pic_instruction insn; enum pic_instruction insn;
union { union {
double f;
int i; int i;
struct pic_pair *gvar; struct pic_pair *gvar;
} u; } u;

View File

@ -3,7 +3,7 @@
enum pic_vtype { enum pic_vtype {
PIC_VTYPE_NIL, PIC_VTYPE_NIL,
PIC_VTYPE_INT, PIC_VTYPE_FLOAT,
PIC_VTYPE_UNDEF, PIC_VTYPE_UNDEF,
PIC_VTYPE_HEAP PIC_VTYPE_HEAP
}; };
@ -12,14 +12,14 @@ typedef struct {
enum pic_vtype type; enum pic_vtype type;
union { union {
void *data; void *data;
int i; double f;
} u; } u;
} pic_value; } pic_value;
enum pic_tt { enum pic_tt {
/* immediate */ /* immediate */
PIC_TT_NIL, PIC_TT_NIL,
PIC_TT_INT, PIC_TT_FLOAT,
PIC_TT_UNDEF, PIC_TT_UNDEF,
/* heap */ /* heap */
PIC_TT_PAIR, PIC_TT_PAIR,
@ -56,9 +56,9 @@ enum pic_tt pic_type(pic_value);
pic_value pic_nil_value(); pic_value pic_nil_value();
pic_value pic_undef_value(); pic_value pic_undef_value();
pic_value pic_obj_value(void *); 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) #define pic_nil_p(v) (pic_type(v) == PIC_TT_NIL)

View File

@ -74,7 +74,7 @@ pic_eval(pic_state *pic, pic_value obj, struct pic_env *env)
/* not implemented */ /* not implemented */
} }
} }
case PIC_TT_INT: case PIC_TT_FLOAT:
case PIC_TT_NIL: { case PIC_TT_NIL: {
return obj; return obj;
} }

View File

@ -19,10 +19,10 @@ struct parser_control {
} }
%token tLPAREN tRPAREN tDOT %token tLPAREN tRPAREN tDOT
%token <datum> tSYMBOL tINT %token <datum> tSYMBOL tNUMBER
%type <datum> datum simple_datum symbol compound_datum %type <datum> datum simple_datum symbol compound_datum
%type <datum> number integer list list_tail %type <datum> number list list_tail
%% %%
@ -55,14 +55,7 @@ symbol
; ;
number number
: integer : tNUMBER
;
integer
: tINT
{
$$ = $1;
}
; ;
compound_datum compound_datum
@ -106,7 +99,6 @@ pic_parse(pic_state *pic, const char *str)
struct parser_control p; struct parser_control p;
p.pic = pic; p.pic = pic;
p.value = pic_int_value(42);
yy_scan_string(str); yy_scan_string(str);
yyparse(&p); yyparse(&p);

View File

@ -19,7 +19,7 @@ struct parser_control {
[ \t\n\r] /* skip whitespace */ [ \t\n\r] /* skip whitespace */
"(" return tLPAREN; "(" return tLPAREN;
")" return tRPAREN; ")" 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; } [a-z0-9A-Z]+ { yylval.datum = pic_intern_cstr(p->pic, yytext); return tSYMBOL; }
%% %%

View File

@ -8,8 +8,8 @@ pic_type(pic_value v)
switch (v.type) { switch (v.type) {
case PIC_VTYPE_NIL: case PIC_VTYPE_NIL:
return PIC_TT_NIL; return PIC_TT_NIL;
case PIC_VTYPE_INT: case PIC_VTYPE_FLOAT:
return PIC_TT_INT; return PIC_TT_FLOAT;
case PIC_VTYPE_UNDEF: case PIC_VTYPE_UNDEF:
return PIC_TT_UNDEF; return PIC_TT_UNDEF;
case PIC_VTYPE_HEAP: case PIC_VTYPE_HEAP:
@ -38,12 +38,12 @@ pic_obj_value(void *ptr)
} }
pic_value pic_value
pic_int_value(int i) pic_float_value(double f)
{ {
pic_value v; pic_value v;
v.type = PIC_VTYPE_INT; v.type = PIC_VTYPE_FLOAT;
v.u.i = i; v.u.f = f;
return v; return v;
} }

View File

@ -100,8 +100,8 @@ print_irep(pic_state *pic, struct pic_irep *irep)
case OP_PUSHNIL: case OP_PUSHNIL:
puts("OP_PUSHNIL"); puts("OP_PUSHNIL");
break; break;
case OP_PUSHI: case OP_PUSHNUM:
printf("OP_PUSHI\t%d\n", irep->code[i].u.i); printf("OP_PUSHNUM\t%f\n", irep->code[i].u.f);
break; break;
case OP_PUSHUNDEF: case OP_PUSHUNDEF:
puts("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; break;
} }
} }
case PIC_TT_INT: { case PIC_TT_FLOAT: {
irep->code[irep->clen].insn = OP_PUSHI; irep->code[irep->clen].insn = OP_PUSHNUM;
irep->code[irep->clen].u.i = pic_int(obj); irep->code[irep->clen].u.f = pic_float(obj);
irep->clen++; irep->clen++;
break; break;
} }
@ -283,8 +283,8 @@ pic_run(pic_state *pic, struct pic_proc *proc, pic_value args)
PUSH(pic_nil_value()); PUSH(pic_nil_value());
NEXT; NEXT;
} }
CASE(OP_PUSHI) { CASE(OP_PUSHNUM) {
PUSH(pic_int_value(pc->u.i)); PUSH(pic_float_value(pc->u.f));
NEXT; NEXT;
} }
CASE(OP_PUSHUNDEF) { CASE(OP_PUSHUNDEF) {
@ -322,7 +322,7 @@ pic_run(pic_state *pic, struct pic_proc *proc, pic_value args)
pic_value a, b; pic_value a, b;
a = POP(); a = POP();
b = POP(); b = POP();
PUSH(pic_int_value(pic_int(a) + pic_int(b))); PUSH(pic_float_value(pic_float(a) + pic_float(b)));
NEXT; NEXT;
} }
CASE(OP_STOP) { CASE(OP_STOP) {

View File

@ -21,8 +21,8 @@ pic_debug(pic_state *pic, pic_value obj)
case PIC_TT_SYMBOL: case PIC_TT_SYMBOL:
printf("%s", pic_symbol_ptr(obj)->name); printf("%s", pic_symbol_ptr(obj)->name);
break; break;
case PIC_TT_INT: case PIC_TT_FLOAT:
printf("%d", pic_int(obj)); printf("%f", pic_float(obj));
break; break;
case PIC_TT_UNDEF: case PIC_TT_UNDEF:
printf("#<undef>"); printf("#<undef>");