From 3f78cfe0ad9fb1f922122ce2dcc388e12f226d74 Mon Sep 17 00:00:00 2001 From: raffitz Date: Sat, 23 Mar 2019 17:42:08 +0000 Subject: [PATCH] Fix some bugs, still have some --- Makefile | 8 +++++--- expression.c | 39 +++++++++++++++++++++++++++++++-------- expression.h | 3 +++ lexer.l | 7 ++----- native-test.c | 14 ++++---------- 5 files changed, 45 insertions(+), 26 deletions(-) diff --git a/Makefile b/Makefile index 5d41656..7e74129 100644 --- a/Makefile +++ b/Makefile @@ -3,6 +3,8 @@ 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 +DEBUG = +#DEBUG += --debug .PHONY: all all: webdice.js @@ -11,16 +13,16 @@ 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 --debug --header-file=lex.yy.h $< + flex ${DEBUG} --header-file=lex.yy.h $< parser.tab.c parser.tab.h: parser.y expression.h - bison --debug -d $< + bison ${DEBUG} -d $< %.o: %.c $(CC) $(CFLAGS) -c $< native-test: native-test.c lex.yy.c parser.tab.c expression.c - gcc -m32 -std=gnu99 -Wall -pedantic $^ -o $@ $(LFLAGS) -g + gcc -m32 -std=gnu99 -Wall -pedantic $^ -o $@ $(LFLAGS) -g -O0 .PHONY: clean clean: diff --git a/expression.c b/expression.c index a79606d..496516c 100644 --- a/expression.c +++ b/expression.c @@ -5,7 +5,7 @@ #include "expression.h" union expression* webdice_const(int64_t value){ - union expression* aux = (union expression*) malloc(sizeof(union expression*)); + union expression* aux = (union expression*) malloc(sizeof(union expression)); (*aux).constant.id = WEBDICE_CONST; (*aux).constant.value = value; @@ -14,7 +14,7 @@ union expression* webdice_const(int64_t value){ } union expression* webdice_roll(int64_t count, int64_t roll){ - union expression* aux = (union expression*) malloc(sizeof(union expression*)); + union expression* aux = (union expression*) malloc(sizeof(union expression)); (*aux).roll.id = WEBDICE_ROLL; (*aux).roll.count = count; @@ -24,7 +24,7 @@ union expression* webdice_roll(int64_t count, int64_t roll){ } union expression* webdice_binary(enum binary_type type, union expression* left, union expression* right){ - union expression* aux = (union expression*) malloc(sizeof(union expression*)); + union expression* aux = (union expression*) malloc(sizeof(union expression)); (*aux).binary.id = WEBDICE_BINARY; (*aux).binary.operation = type; @@ -54,7 +54,7 @@ struct expression_result resolve(union expression* root){ struct expression_result result,left,right; int i, j; - int64_t *values; + int64_t *values = NULL; int64_t total; long double size; int64_t length = 1; @@ -75,21 +75,30 @@ struct expression_result resolve(union expression* root){ length += (int64_t) size; result.min = (*root).roll.count; result.max = (*root).roll.count * (*root).roll.type; - result.expected = (*root).roll.count * ((*root).roll.type + 1) / 2; + result.expected = (*root).roll.count * ((*root).roll.type + 1.0) / 2.0; result.text = (char*) malloc(length * sizeof(char)); + fprintf(stderr,"COUNT: %lld\n\n",(*root).roll.count); values = (int64_t*) malloc((*root).roll.count * sizeof(int64_t)); + fprintf(stderr,"ALLOC'D %X\n\n",values); for (i = 0, j = (*root).roll.count, total = 0; i < (*root).roll.count; i++, j--){ values[i] = 1 + (rand() % (*root).roll.type); total += values[i]; - if (i == 0){ + if (i == 0 && j == 1){ + sprintf(result.text,"%lld", (*root).roll.count, (*root).roll.type, values[i], total); + }else if (i == 0){ sprintf(result.text,"%lld", result.text, values[i], total); + sprintf(&(result.text[strlen(result.text)]),"%lld\">%lld", values[i], total); }else{ - sprintf(result.text,"%s%lld,", result.text, values[i]); + sprintf(&(result.text[strlen(result.text)]),"%lld,", values[i]); } } + fprintf(stderr,"FREE'D %X\n\n",values); + fprintf(stderr,"FREE'D %X\n\n",values); + fprintf(stderr,"FREE'D %X\n\n",values); + fflush(stderr); free(values); + fprintf(stderr,"FREE'D %X\n\n",values); result.actual = total; break; case WEBDICE_BINARY: @@ -148,3 +157,17 @@ struct expression_result resolve(union expression* root){ } return result; } + +void free_expression(union expression* root){ + switch((*root).id.id){ + case WEBDICE_BINARY: + free_expression((*root).binary.left); + free_expression((*root).binary.right); + case WEBDICE_CONST: + case WEBDICE_ROLL: + default: + free(root); + break; + } + return; +} diff --git a/expression.h b/expression.h index 0a422fe..fe8fff9 100644 --- a/expression.h +++ b/expression.h @@ -64,4 +64,7 @@ union expression* webdice_sub(union expression* left, union expression* right); union expression* webdice_binary(enum binary_type type, union expression* left, union expression* right); struct expression_result resolve(union expression* root); + +void free_expression(union expression* root); + #endif diff --git a/lexer.l b/lexer.l index 0a91ed1..0b3a76e 100644 --- a/lexer.l +++ b/lexer.l @@ -19,17 +19,14 @@ int64_t aux; sscanf(yytext,"%lld",&aux); - (*(*yylval).exp).constant.id = WEBDICE_CONST; - (*(*yylval).exp).constant.value = aux; + (*yylval).exp = webdice_const(aux); return WEBDICE_TOKEN_CONST; } -?[0-9]+d[0-9]+ { int64_t count,type; sscanf(yytext,"%lldd%lld",&count,&type); - (*(*yylval).exp).roll.id = WEBDICE_ROLL; - (*(*yylval).exp).roll.count = count; - (*(*yylval).exp).roll.type = type; + (*yylval).exp = webdice_roll(count,type); return WEBDICE_TOKEN_ROLL; } "(" { return WEBDICE_TOKEN_LPAREN; } diff --git a/native-test.c b/native-test.c index d4dabc1..d2dba69 100644 --- a/native-test.c +++ b/native-test.c @@ -18,19 +18,14 @@ int yyerror(void* scanner, void* lvalp, char* str){ } void webdice(char* str){ - union expression* root; + union expression* root = NULL; 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 && root != NULL){ result = resolve(root); @@ -41,6 +36,8 @@ void webdice(char* str){ output_print(out_block); free(out_block); + free(result.text); + free_expression(root); }else{ output_print("

Error!

"); } @@ -49,11 +46,8 @@ void webdice(char* str){ int main(){ -#ifdef YYDEBUG - yydebug = 1; -#endif - webdice("1d4\n"); + webdice("1d4 + 1d4 + 2d4\n"); webdice("cenas\n"); webdice("5\n"); return 0;