From 36bbfc45b383163498bb53a5fcb37dd998980f61 Mon Sep 17 00:00:00 2001 From: raffitz Date: Wed, 13 Mar 2019 00:02:49 +0000 Subject: [PATCH] Add compiling code Still does not work. Some words just return an integer overflow, others just return error. --- Makefile | 24 ++++++++++++++++++++++++ expression.c | 8 ++++---- index.html | 24 ++++++++++++++++++++++++ js-api.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ lexer.l | 21 ++++++++++++++------- parser.y | 7 +++++-- 6 files changed, 121 insertions(+), 13 deletions(-) create mode 100644 Makefile create mode 100644 index.html create mode 100644 js-api.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..195424b --- /dev/null +++ b/Makefile @@ -0,0 +1,24 @@ +# Flags +CC = emcc +JSFLAGS += -Oz -s "EXTRA_EXPORTED_RUNTIME_METHODS=['lengthBytesUTF8','stringToUTF8']" -s "EXPORTED_FUNCTIONS=['_malloc','_free']" -s WASM=1 -s EXIT_RUNTIME=1 +CFLAGS += $(JSFLAGS) -std=gnu99 -Wall -pedantic +LFLAGS += -lm + +.PHONY: all +all: webdice.js + +webdice.js: js-api.c expression.o lex.yy.o parser.tab.o + $(CC) $^ $(CFLAGS) -o $@ $(LFLAGS) + +lex.yy.c lex.yy.h: lexer.l parser.tab.h expression.h + flex --header-file=lex.yy.h $< + +parser.tab.c parser.tab.h: parser.y expression.h + bison -d $< + +%.o: %.c + $(CC) $(CFLAGS) -c $< + +.PHONY: clean +clean: + rm -rf *.o {lex.yy,parser.tab}.{c,h} webdice.js diff --git a/expression.c b/expression.c index e5c353f..a79606d 100644 --- a/expression.c +++ b/expression.c @@ -68,7 +68,7 @@ struct expression_result resolve(union expression* root){ result.expected = (*root).constant.value; result.actual = (*root).constant.value; result.text = (char*) malloc(length * sizeof(char)); - sprintf(result.text, "%d",(*root).constant.value); + sprintf(result.text, "%lld",(*root).constant.value); break; case WEBDICE_ROLL: size = (*root).roll.count * ceill(15+log10l((*root).roll.count * (*root).roll.type)); @@ -82,11 +82,11 @@ struct expression_result resolve(union expression* root){ values[i] = 1 + (rand() % (*root).roll.type); total += values[i]; if (i == 0){ - sprintf(result.text,"%d", result.text, values[i], total); + sprintf(result.text,"%s%lld\">%lld", result.text, values[i], total); }else{ - sprintf(result.text,"%s%d,", result.text, values[i]); + sprintf(result.text,"%s%lld,", result.text, values[i]); } } free(values); diff --git a/index.html b/index.html new file mode 100644 index 0000000..728dd3a --- /dev/null +++ b/index.html @@ -0,0 +1,24 @@ + + + + + + + +
+
+ + +
+ + diff --git a/js-api.c b/js-api.c new file mode 100644 index 0000000..5d68901 --- /dev/null +++ b/js-api.c @@ -0,0 +1,50 @@ +#include "emscripten.h" +#include +#include +#include "expression.h" +#include "parser.tab.h" +#include "lex.yy.h" + +EM_JS(void, console_print, (const char* str), { + console.log(UTF8ToString(str)); +}) + +EM_JS(void, output_print, (const char* str), { + var div = document.getElementById('output'); + div.innerHTML = UTF8ToString(str) + div.innerHTML; +}) + +int yyerror(void* scanner, void* lvalp, char* str){ + console_print(str); + return 0; +} + +void EMSCRIPTEN_KEEPALIVE webdice(char* str){ + union expression* root; + struct expression_result result; + int length; + char* out_block; + + console_print("A"); + + yyscan_t scanner; + console_print("B"); + yylex_init(&scanner); + console_print("C"); + yy_scan_string (str, scanner); + console_print("D"); + + if(yyparse((void*) scanner, &root) == 0){ + result = resolve(root); + length = strlen(result.text) + 4 * ceill(log10l(result.max)) + 100; + out_block = (char*) malloc(length * sizeof(char)); + sprintf(out_block,"

%s = %Lf

", result.text, result.min, result.expected, result.max, result.actual); + + output_print(out_block); + + free(out_block); + }else{ + output_print("

Error!

"); + } + yylex_destroy(scanner); +} diff --git a/lexer.l b/lexer.l index 62489f5..db80f53 100644 --- a/lexer.l +++ b/lexer.l @@ -3,27 +3,34 @@ #include #include "expression.h" #include "parser.tab.h" +#define _POSIX_C_SOURCE %} +%option reentrant +%option bison-bridge +%option nounput +%option noinput +%option noyywrap + %% "\n" { return WEBDICE_TOKEN_EOL; } -?[0-9]+ { int64_t aux; - sscanf(yytext,"%ld",&aux); - yylval.exp.constant.id = WEBDICE_CONST; - yylval.exp.constant.value = aux; + sscanf(yytext,"%lld",&aux); + (*(*yylval).exp).constant.id = WEBDICE_CONST; + (*(*yylval).exp).constant.value = aux; return WEBDICE_TOKEN_CONST; } -?[0-9]+d[0-9]+ { int64_t count,type; - sscanf(yytext,"%ldd%ld",&count,&type); - yylval.exp.roll.id = WEBDICE_ROLL; - yylval.exp.roll.count = count; - yylval.exp.roll.type = type; + sscanf(yytext,"%lldd%lld",&count,&type); + (*(*yylval).exp).roll.id = WEBDICE_ROLL; + (*(*yylval).exp).roll.count = count; + (*(*yylval).exp).roll.type = type; return WEBDICE_TOKEN_ROLL; } "(" { return WEBDICE_TOKEN_LPAREN; } diff --git a/parser.y b/parser.y index bbaff25..e8491f0 100644 --- a/parser.y +++ b/parser.y @@ -1,12 +1,15 @@ %{ +#include #include "expression.h" int yylex(); -int yyerror(char*); +int yyerror(void* scanner, void* lvalp, char* str); %} -%parse-param {union expression** root}; +%pure-parser +%lex-param {void *scanner} +%parse-param {void *scanner} {union expression** root} %union { union expression* exp;