Browse Source

Fix some bugs, still have some

main
raffitz 5 years ago
parent
commit
3f78cfe0ad
Signed by: raffitz
GPG Key ID: 0224483A6E6AC710
  1. 8
      Makefile
  2. 39
      expression.c
  3. 3
      expression.h
  4. 7
      lexer.l
  5. 14
      native-test.c

8
Makefile

@ -3,6 +3,8 @@ CC = emcc @@ -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 @@ -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:

39
expression.c

@ -5,7 +5,7 @@ @@ -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){ @@ -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){ @@ -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){ @@ -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){ @@ -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,"<span title=\"%lldd%lld=%lld\">%lld</span>", (*root).roll.count, (*root).roll.type, values[i], total);
}else if (i == 0){
sprintf(result.text,"<span title=\"%lldd%lld=%lld,", (*root).roll.count, (*root).roll.type, values[i]);
}else if (j == 1){
sprintf(result.text,"%s%lld\">%lld</span>", result.text, values[i], total);
sprintf(&(result.text[strlen(result.text)]),"%lld\">%lld</span>", 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){ @@ -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;
}

3
expression.h

@ -64,4 +64,7 @@ union expression* webdice_sub(union expression* left, union expression* right); @@ -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

7
lexer.l

@ -19,17 +19,14 @@ @@ -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; }

14
native-test.c

@ -18,19 +18,14 @@ int yyerror(void* scanner, void* lvalp, char* str){ @@ -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){ @@ -41,6 +36,8 @@ void webdice(char* str){
output_print(out_block);
free(out_block);
free(result.text);
free_expression(root);
}else{
output_print("<p>Error!</p>");
}
@ -49,11 +46,8 @@ void webdice(char* str){ @@ -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;

Loading…
Cancel
Save