diff --git a/Makefile b/Makefile index 7f13f8db..0aab4000 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,12 @@ +CC=gcc + all: build run build: cd src; \ yacc -d parse.y; \ lex scan.l - gcc -Wall -o bin/picrin -I./include -lreadline src/main.c src/state.c src/gc.c src/pair.c src/port.c src/symbol.c src/value.c src/y.tab.c src/lex.yy.c src/bool.c src/vm.c src/init.c src/number.c + $(CC) -Wall -o bin/picrin -I./include -lreadline src/main.c src/state.c src/gc.c src/pair.c src/port.c src/symbol.c src/value.c src/y.tab.c src/lex.yy.c src/bool.c src/vm.c src/init.c src/number.c clean: rm -f src/y.tab.c src/y.tab.h src/lex.yy.c diff --git a/src/main.c b/src/main.c index d068904d..88ff9ea8 100644 --- a/src/main.c +++ b/src/main.c @@ -17,13 +17,17 @@ main() { pic_state *pic; char code[CODE_MAX_LENGTH] = "", line[LINE_MAX_LENGTH]; - char last_char, *read_line, *prompt; - int char_index; + char *read_line, *prompt; pic_value v; struct pic_proc *proc; int ai; bool r; +#if ! PIC_ENABLE_READLINE + char last_char; + int char_index; +#endif + pic = pic_open(); ai = pic_gc_arena_preserve(pic); @@ -89,9 +93,11 @@ main() pic_gc_arena_restore(pic, ai); } +#if ! PIC_ENABLE_READLINE eof: puts(""); goto exit; +#endif overflow: puts("** [fatal] line input overflow"); diff --git a/src/parse.y b/src/parse.y index 8c1176d7..1864e620 100644 --- a/src/parse.y +++ b/src/parse.y @@ -25,7 +25,7 @@ struct parser_control { %token tSYMBOL tNUMBER tBOOLEAN %type datum simple_datum symbol compound_datum -%type number list list_data +%type number boolean list list_data %% @@ -111,7 +111,7 @@ incomplete_data %% -int +void yyerror(struct parser_control *p, const char *msg) { puts(msg); diff --git a/src/scan.l b/src/scan.l index 790cf19d..ad3c46fc 100644 --- a/src/scan.l +++ b/src/scan.l @@ -7,11 +7,13 @@ struct parser_control { pic_state *pic; pic_value value; + bool incomp; }; #define YY_DECL int yylex (struct parser_control *p) %} +%option noinput %option nounput /* boolean */ diff --git a/src/state.c b/src/state.c index 5fee9658..71d1f9ac 100644 --- a/src/state.c +++ b/src/state.c @@ -43,7 +43,7 @@ pic_open() pic->icapa = PIC_IREP_SIZE; /* globals */ - pic->globals = (struct pic_value *)malloc(sizeof(pic_value) * PIC_GLOBALS_SIZE); + pic->globals = (pic_value *)malloc(sizeof(pic_value) * PIC_GLOBALS_SIZE); pic->glen = 0; pic->gcapa = PIC_GLOBALS_SIZE; diff --git a/src/value.c b/src/value.c index 52aca41a..7fa303d2 100644 --- a/src/value.c +++ b/src/value.c @@ -1,6 +1,7 @@ #include +#include -#include "picrin.h" +#include "picrin/value.h" enum pic_tt pic_type(pic_value v) @@ -9,15 +10,18 @@ pic_type(pic_value v) case PIC_VTYPE_NIL: return PIC_TT_NIL; case PIC_VTYPE_TRUE: + return PIC_TT_BOOL; case PIC_VTYPE_FALSE: return PIC_TT_BOOL; - case PIC_VTYPE_FLOAT: - return PIC_TT_FLOAT; case PIC_VTYPE_UNDEF: return PIC_TT_UNDEF; + case PIC_VTYPE_FLOAT: + return PIC_TT_FLOAT; case PIC_VTYPE_HEAP: return ((struct pic_object *)v.u.data)->tt; } + /* logic flaw (suppress warnings gcc will emit) */ + abort(); } pic_value diff --git a/src/vm.c b/src/vm.c index 97c81621..a54c0c5d 100644 --- a/src/vm.c +++ b/src/vm.c @@ -143,6 +143,9 @@ print_irep(pic_state *pic, struct pic_irep *irep) printf("## irep %p [clen = %zd, ccapa = %zd]\n", irep, irep->clen, irep->ccapa); for (i = 0; i < irep->clen; ++i) { switch (irep->code[i].insn) { + case OP_POP: + puts("OP_POP"); + break; case OP_PUSHNIL: puts("OP_PUSHNIL"); break; @@ -375,6 +378,11 @@ pic_gen(pic_state *pic, struct pic_irep *irep, pic_value obj, struct pic_env *en irep->clen++; break; } + case PIC_TT_PROC: + case PIC_TT_UNDEF: + case PIC_TT_PORT: { + pic_raise(pic, "invalid expression given"); + } } } @@ -431,7 +439,7 @@ pic_gen_lambda(pic_state *pic, pic_value obj, struct pic_env *env) pic_free(pic, inner_env); #if VM_DEBUG - printf("LAMBDA_%d:\n", pic->ilen); + printf("LAMBDA_%zd:\n", pic->ilen); print_irep(pic, irep); puts(""); #endif @@ -476,6 +484,7 @@ pic_codegen(pic_state *pic, pic_value obj, struct pic_env *env) #define PUSH(v) (*++pic->sp = (v)) #define POP() (*pic->sp--) +#define POPN(i) ((void)(pic->sp-=i)) #define PUSHCI() (++pic->ci) #define POPCI() (pic->ci--) @@ -506,7 +515,7 @@ pic_run(pic_state *pic, struct pic_proc *proc, pic_value args) VM_LOOP { CASE(OP_POP) { - POP(); + POPN(1); NEXT; } CASE(OP_PUSHNIL) {