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 @@ -11,16 +11,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 --header-file=lex.yy.h $<
flex --debug --header-file=lex.yy.h $<
parser.tab.c parser.tab.h: parser.y expression.h
bison -d $<
bison --debug -d $<
%.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)
gcc -m32 -std=gnu99 -Wall -pedantic $^ -o $@ $(LFLAGS) -g
.PHONY: clean
clean:

18
js-api.c

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

1
lexer.l

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

13
native-test.c

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

2
parser.y

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

Loading…
Cancel
Save