change the type of immediate number value from int to double
This commit is contained in:
parent
02d52723ac
commit
af9dd45e65
|
@ -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;
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
14
src/parse.y
14
src/parse.y
|
@ -19,10 +19,10 @@ struct parser_control {
|
|||
}
|
||||
|
||||
%token tLPAREN tRPAREN tDOT
|
||||
%token <datum> tSYMBOL tINT
|
||||
%token <datum> tSYMBOL tNUMBER
|
||||
|
||||
%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
|
||||
: 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);
|
||||
|
|
|
@ -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; }
|
||||
|
||||
%%
|
||||
|
|
10
src/value.c
10
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;
|
||||
}
|
||||
|
||||
|
|
16
src/vm.c
16
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) {
|
||||
|
|
|
@ -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("#<undef>");
|
||||
|
|
Loading…
Reference in New Issue