diff options
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | Makefile | 14 | ||||
| -rw-r--r-- | src/builtins.c | 8 | ||||
| -rw-r--r-- | src/interp.c | 14 | ||||
| -rw-r--r-- | src/main.c | 10 | ||||
| -rw-r--r-- | src/parser.c | 8 |
6 files changed, 50 insertions, 6 deletions
@@ -10,3 +10,5 @@ fibo.lisp debug.lisp perf.data perf.data.old +web-* +index.html @@ -1,13 +1,16 @@ mode ?= debug cc = gcc -includes = -I3rdparty/algds/build/include/ + +includes = -DWITHREADLINE -I3rdparty/algds/build/include/ +#includes = -I3rdparty/algds/build/include/ + 3rdlibs = 3rdparty/algds/build/lib/libalgds.a ldflags = -lm -lreadline ifeq ($(mode), debug) cflags = $(includes) \ -g \ -fsanitize=address -else +else ifeq ($(mode), debug) cflags = $(includes) -g -flto -O2 endif @@ -19,6 +22,13 @@ tests_bin=$(tests:.c=.bin) all: bamboo-lisp +web: web-bamboo-lisp.js + +web-bamboo-lisp.js: $(src) + -rm web-* + emcc -I3rdparty/algds/build/include $(src) 3rdparty/algds/src/*.c -o web-bamboo-lisp.js \ + -s EXPORTED_FUNCTIONS="['_print_lisp_error', '_malloc', '_free', '_new_interp', '_lisp_to_string', _Interp_eval_string]" -s WASM=1 -s EXPORTED_RUNTIME_METHODS="['stringToUTF8', 'UTF8ToString']" + install: bamboo-lisp sudo cp bamboo-lisp /usr/local/bin/bamboo diff --git a/src/builtins.c b/src/builtins.c index 950c2c7..9dcf497 100644 --- a/src/builtins.c +++ b/src/builtins.c @@ -8,12 +8,12 @@ #include <math.h> SExpRef builtin_charp(Interp *interp, SExpRef args) { - if (LENGTH(args) != 1) return new_error(interp, "char?: arg num error."); + if (LENGTH(args) != 1) return new_error(interp, "char?: arg num error.\n"); return new_boolean(interp, VALTYPE(CAR(args)) == kCharSExp); } SExpRef builtin_char_eq(Interp *interp, SExpRef args) { - if (LENGTH(args) != 2) return new_error(interp, "char=: arg num error."); + if (LENGTH(args) != 2) return new_error(interp, "char=: arg num error.\n"); if (VALTYPE(CAR(args)) != kCharSExp || VALTYPE(CADR(args)) != kCharSExp) { return new_error(interp, "char=: type error.\n"); } @@ -23,7 +23,7 @@ SExpRef builtin_char_eq(Interp *interp, SExpRef args) { } SExpRef builtin_char_gt(Interp *interp, SExpRef args) { - if (LENGTH(args) != 2) return new_error(interp, "char>: arg num error."); + if (LENGTH(args) != 2) return new_error(interp, "char>: arg num error.\n"); if (VALTYPE(CAR(args)) != kCharSExp || VALTYPE(CADR(args)) != kCharSExp) { return new_error(interp, "char>: type error.\n"); } @@ -33,7 +33,7 @@ SExpRef builtin_char_gt(Interp *interp, SExpRef args) { } SExpRef builtin_char_lt(Interp *interp, SExpRef args) { - if (LENGTH(args) != 2) return new_error(interp, "char<: arg num error."); + if (LENGTH(args) != 2) return new_error(interp, "char<: arg num error.\n"); if (VALTYPE(CAR(args)) != kCharSExp || VALTYPE(CADR(args)) != kCharSExp) { return new_error(interp, "char<: type error.\n"); } diff --git a/src/interp.c b/src/interp.c index 9749b6d..5a4c9b6 100644 --- a/src/interp.c +++ b/src/interp.c @@ -41,6 +41,19 @@ HASH_TABLE_IMPL(SExpRef, SExpRef); #define UNBOUND ((SExpRef){-1}) +// for wasm +Interp *new_interp() { + Interp *ret = malloc(sizeof(Interp)); + Interp_init(ret); + return ret; +} + +// for wasm +void print_lisp_error(Interp *interp, SExpRef err) { + if (VALTYPE(err) != kErrSignal) return; + fprintf(stderr, "Error: %s", REF(err)->str); +} + void Interp_init(Interp *self) { self->recursion_depth = 0; self->gensym_cnt = 42; @@ -213,6 +226,7 @@ void Interp_init(Interp *self) { } } + SExpRef Interp_eval_string(Interp *interp, const char * str) { Parser_set_string(interp->parser, str); SExpRef sexp, ret; @@ -24,16 +24,26 @@ int main(int argc, char **argv) { mainret = -1; goto end; } } +#ifdef WITHREADLINE Parser_set_readline(interp.parser); +#else + Parser_set_file(interp.parser, stdin); +#endif SExpRef sexp, res; ParseResult parse_result; while (1) { +#ifndef WITHREADLINE + printf(">>> "); + fflush(stdout); +#endif parse_result = parse_sexp(interp.parser); if (parse_result.errmsg != NULL) { if (Parser_peek(interp.parser) == EOF) goto end; fprintf(stderr, "Parsing error: %s", parse_result.errmsg); +#ifdef WITHREADLINE free((void*)interp.parser->string); Parser_set_readline(interp.parser); +#endif continue; } diff --git a/src/parser.c b/src/parser.c index 6982198..d8fda82 100644 --- a/src/parser.c +++ b/src/parser.c @@ -4,8 +4,10 @@ #include <stdlib.h> #include <stdarg.h> +#ifdef WITHREADLINE #include <readline/readline.h> #include <readline/history.h> +#endif #include "sexp.h" @@ -83,6 +85,7 @@ void Parser_set_file(Parser *parser, FILE *fp) { parser->fp = fp; } +#ifdef WITHREADLINE void Parser_set_readline(Parser *parser) { stifle_history(100); parser->parse_type = kParseReadline; @@ -90,6 +93,7 @@ void Parser_set_readline(Parser *parser) { parser->str_cursor = NULL; parser->readline_eof = false; } +#endif int Parser_getchar(Parser *ctx) { @@ -100,6 +104,7 @@ int Parser_getchar(Parser *ctx) { return ret; } else if (ctx->parse_type == kParseFile) { return fgetc(ctx->fp); +#ifdef WITHREADLINE } else if (ctx->parse_type == kParseReadline) { if (ctx->readline_eof) return EOF; if (ctx->string == NULL) { @@ -127,6 +132,7 @@ int Parser_getchar(Parser *ctx) { int c = *ctx->str_cursor; ctx->str_cursor++; return c; +#endif } return EOF; } @@ -141,6 +147,7 @@ int Parser_peek(Parser *ctx) { if (ret == EOF) return EOF; ungetc(ret, ctx->fp); return ret; +#ifdef WITHREADLINE } else if (ctx->parse_type == kParseReadline) { if (ctx->readline_eof) return EOF; if (ctx->string == NULL) { @@ -158,6 +165,7 @@ int Parser_peek(Parser *ctx) { } int c = *ctx->str_cursor; return c; +#endif } return EOF; } |
