You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.4 KiB
61 lines
1.4 KiB
#include "emscripten.h" |
|
#include <string.h> |
|
#include <stdlib.h> |
|
#include <time.h> |
|
#include <math.h> |
|
#include "expression.h" |
|
#include "parser.tab.h" |
|
#include "lex.yy.h" |
|
|
|
EM_JS(void, console_print, (const char* str), { |
|
console.log(UTF8ToString(str)); |
|
}) |
|
|
|
EM_JS(void, output_print, (const char* str), { |
|
var div = document.getElementById('output'); |
|
div.innerHTML = UTF8ToString(str) + div.innerHTML; |
|
}) |
|
|
|
int yyerror(void* scanner, void* lvalp, char* str){ |
|
console_print(str); |
|
return 0; |
|
} |
|
|
|
void EMSCRIPTEN_KEEPALIVE webdice(char* str){ |
|
union expression* root = NULL; |
|
struct expression_result result; |
|
int length; |
|
char* in_block; |
|
char* out_block; |
|
static char seeded = 0; |
|
|
|
if (!seeded){ |
|
seeded = 1; |
|
srand(time(NULL)); |
|
} |
|
|
|
in_block = (char*) malloc((2 + strlen(str)) * sizeof(char)); |
|
sprintf(in_block,"%s\n",str); |
|
|
|
yyscan_t scanner; |
|
yylex_init(&scanner); |
|
yy_scan_string (in_block, scanner); |
|
|
|
yyparse((void*) scanner, &root); |
|
if(root != NULL){ |
|
result = resolve(root); |
|
length = strlen(result.text) + 4 * ceill(10 + log10l(result.max)) + 100; |
|
out_block = (char*) malloc(length * sizeof(char)); |
|
sprintf(out_block,"<p>%s = <span title=\"min: %Lg, expected: %Lg, max: %Lg\">%Lg</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>"); |
|
} |
|
free(in_block); |
|
yylex_destroy(scanner); |
|
}
|
|
|