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.
51 lines
1.2 KiB
51 lines
1.2 KiB
5 years ago
|
#include "emscripten.h"
|
||
|
#include <string.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;
|
||
|
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);
|
||
|
}
|