diff --git a/Makefile b/Makefile index 195424b..a6baef6 100644 --- a/Makefile +++ b/Makefile @@ -19,6 +19,9 @@ parser.tab.c parser.tab.h: parser.y expression.h %.o: %.c $(CC) $(CFLAGS) -c $< +native-test: native-test.c lex.yy.c parser.tab.c expression.c + gcc -std=gnu99 -Wall -pedantic $^ -o $@ $(LFLAGS) + .PHONY: clean clean: rm -rf *.o {lex.yy,parser.tab}.{c,h} webdice.js diff --git a/native-test.c b/native-test.c new file mode 100644 index 0000000..82d2bd5 --- /dev/null +++ b/native-test.c @@ -0,0 +1,55 @@ +#include +#include +#include "expression.h" +#include "parser.tab.h" +#include "lex.yy.h" + +void console_print (const char* str){ + fprintf(stderr,"%s\n",str); +} + +void output_print (const char* str){ + printf("%s\n",str); +} + +int yyerror(void* scanner, void* lvalp, char* str){ + console_print(str); + return 0; +} + +void 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); +} + +int main(){ + webdice("cenas"); + webdice("1d4"); + webdice("5"); + return 0; +}