raffitz
5 years ago
2 changed files with 58 additions and 0 deletions
@ -0,0 +1,55 @@ |
|||||||
|
#include <string.h> |
||||||
|
#include <math.h> |
||||||
|
#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,"<p>%s = <span title=\"min: %Lf, expected: %Lf, max: %Lf\">%Lf</span></p>", result.text, result.min, result.expected, result.max, result.actual); |
||||||
|
|
||||||
|
output_print(out_block); |
||||||
|
|
||||||
|
free(out_block); |
||||||
|
}else{ |
||||||
|
output_print("<p>Error!</p>"); |
||||||
|
} |
||||||
|
yylex_destroy(scanner); |
||||||
|
} |
||||||
|
|
||||||
|
int main(){ |
||||||
|
webdice("cenas"); |
||||||
|
webdice("1d4"); |
||||||
|
webdice("5"); |
||||||
|
return 0; |
||||||
|
} |
Loading…
Reference in new issue