Browse Source
Still does not work. Some words just return an integer overflow, others just return error.main
raffitz
5 years ago
6 changed files with 121 additions and 13 deletions
@ -0,0 +1,24 @@ |
|||||||
|
# Flags
|
||||||
|
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
|
||||||
|
|
||||||
|
.PHONY: all |
||||||
|
all: webdice.js |
||||||
|
|
||||||
|
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 --header-file=lex.yy.h $<
|
||||||
|
|
||||||
|
parser.tab.c parser.tab.h: parser.y expression.h |
||||||
|
bison -d $<
|
||||||
|
|
||||||
|
%.o: %.c |
||||||
|
$(CC) $(CFLAGS) -c $<
|
||||||
|
|
||||||
|
.PHONY: clean |
||||||
|
clean: |
||||||
|
rm -rf *.o {lex.yy,parser.tab}.{c,h} webdice.js
|
@ -0,0 +1,24 @@ |
|||||||
|
<html> |
||||||
|
<head> |
||||||
|
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> |
||||||
|
<script src="webdice.js"></script> |
||||||
|
<script> |
||||||
|
function send(){ |
||||||
|
var input = document.getElementById('inputBox').value; |
||||||
|
var size = lengthBytesUTF8(input) + 1; |
||||||
|
var pointer = _malloc(size); |
||||||
|
stringToUTF8(input,pointer,size); |
||||||
|
_webdice(pointer); |
||||||
|
_free(pointer); |
||||||
|
inputBox.value = ''; |
||||||
|
} |
||||||
|
</script> |
||||||
|
</head> |
||||||
|
<body> |
||||||
|
<div id="output"></div> |
||||||
|
<form action="javascript:void(0);"> |
||||||
|
<input type="text" id="inputBox" onkeydown="if (event.keyCode == 13) send()"> |
||||||
|
<input type="button" onclick="send()" value="Submit"> |
||||||
|
</form> |
||||||
|
</body> |
||||||
|
</html> |
@ -0,0 +1,50 @@ |
|||||||
|
#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); |
||||||
|
} |
Loading…
Reference in new issue