Browse Source

Attempt to debug various faults

main
raffitz 5 years ago
parent
commit
fbf41d75c4
  1. 6
      Makefile
  2. 18
      js-api.c
  3. 1
      lexer.l
  4. 13
      native-test.c
  5. 2
      parser.y

6
Makefile

@ -11,16 +11,16 @@ webdice.js: js-api.c expression.o lex.yy.o parser.tab.o
$(CC) $^ $(CFLAGS) -o $@ $(LFLAGS) $(CC) $^ $(CFLAGS) -o $@ $(LFLAGS)
lex.yy.c lex.yy.h: lexer.l parser.tab.h expression.h lex.yy.c lex.yy.h: lexer.l parser.tab.h expression.h
flex --header-file=lex.yy.h $< flex --debug --header-file=lex.yy.h $<
parser.tab.c parser.tab.h: parser.y expression.h parser.tab.c parser.tab.h: parser.y expression.h
bison -d $< bison --debug -d $<
%.o: %.c %.o: %.c
$(CC) $(CFLAGS) -c $< $(CC) $(CFLAGS) -c $<
native-test: native-test.c lex.yy.c parser.tab.c expression.c native-test: native-test.c lex.yy.c parser.tab.c expression.c
gcc -std=gnu99 -Wall -pedantic $^ -o $@ $(LFLAGS) gcc -m32 -std=gnu99 -Wall -pedantic $^ -o $@ $(LFLAGS) -g
.PHONY: clean .PHONY: clean
clean: clean:

18
js-api.c

@ -20,21 +20,24 @@ int yyerror(void* scanner, void* lvalp, char* str){
} }
void EMSCRIPTEN_KEEPALIVE webdice(char* str){ void EMSCRIPTEN_KEEPALIVE webdice(char* str){
union expression* root; union expression* root = NULL;
struct expression_result result; struct expression_result result;
int length; int length;
char* in_block;
char* out_block; char* out_block;
console_print("A"); yydebug = 1;
in_block = (char*) malloc((2 + strlen(str)) * sizeof(char));
sprintf(in_block,"%s\n",str);
yyscan_t scanner; yyscan_t scanner;
console_print("B");
yylex_init(&scanner); yylex_init(&scanner);
console_print("C"); yy_scan_string (in_block, scanner);
yy_scan_string (str, scanner);
console_print("D");
if(yyparse((void*) scanner, &root) == 0){ yyparse((void*) scanner, &root);
if(root != NULL){
result = resolve(root); result = resolve(root);
length = strlen(result.text) + 4 * ceill(log10l(result.max)) + 100; length = strlen(result.text) + 4 * ceill(log10l(result.max)) + 100;
out_block = (char*) malloc(length * sizeof(char)); out_block = (char*) malloc(length * sizeof(char));
@ -46,5 +49,6 @@ void EMSCRIPTEN_KEEPALIVE webdice(char* str){
}else{ }else{
output_print("<p>Error!</p>"); output_print("<p>Error!</p>");
} }
free(in_block);
yylex_destroy(scanner); yylex_destroy(scanner);
} }

1
lexer.l

@ -3,7 +3,6 @@
#include <stdio.h> #include <stdio.h>
#include "expression.h" #include "expression.h"
#include "parser.tab.h" #include "parser.tab.h"
#define _POSIX_C_SOURCE
%} %}

13
native-test.c

@ -32,7 +32,7 @@ void webdice(char* str){
yy_scan_string (str, scanner); yy_scan_string (str, scanner);
console_print("D"); console_print("D");
if(yyparse((void*) scanner, &root) == 0){ if(yyparse((void*) scanner, &root) == 0 && root != NULL){
result = resolve(root); result = resolve(root);
length = strlen(result.text) + 4 * ceill(log10l(result.max)) + 100; length = strlen(result.text) + 4 * ceill(log10l(result.max)) + 100;
out_block = (char*) malloc(length * sizeof(char)); out_block = (char*) malloc(length * sizeof(char));
@ -48,8 +48,13 @@ void webdice(char* str){
} }
int main(){ int main(){
webdice("cenas");
webdice("1d4"); #ifdef YYDEBUG
webdice("5"); yydebug = 1;
#endif
webdice("1d4\n");
webdice("cenas\n");
webdice("5\n");
return 0; return 0;
} }

2
parser.y

@ -41,7 +41,7 @@ expression: expression WEBDICE_TOKEN_ADD expression { $$ = webdice_add($1,$3); }
; ;
lines: lines:
| lines WEBDICE_TOKEN_EOL { (*root) = NULL; } | lines WEBDICE_TOKEN_EOL {}
| lines expression WEBDICE_TOKEN_EOL { (*root) = $2; } | lines expression WEBDICE_TOKEN_EOL { (*root) = $2; }
; ;

Loading…
Cancel
Save