|
|
|
#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 = NULL;
|
|
|
|
struct expression_result result;
|
|
|
|
int length;
|
|
|
|
char* out_block;
|
|
|
|
|
|
|
|
yyscan_t scanner;
|
|
|
|
yylex_init(&scanner);
|
|
|
|
yy_scan_string (str, scanner);
|
|
|
|
|
|
|
|
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));
|
|
|
|
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);
|
|
|
|
free(result.text);
|
|
|
|
free_expression(root);
|
|
|
|
}else{
|
|
|
|
output_print("<p>Error!</p>");
|
|
|
|
}
|
|
|
|
yylex_destroy(scanner);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(){
|
|
|
|
|
|
|
|
webdice("1d4\n");
|
|
|
|
webdice("1d4 + 1d4 + 2d4\n");
|
|
|
|
webdice("cenas\n");
|
|
|
|
webdice("5\n");
|
|
|
|
return 0;
|
|
|
|
}
|