Browse Source

Add random initialisation and improve frontend

main
raffitz 5 years ago
parent
commit
2c5e4c97e1
Signed by: raffitz
GPG Key ID: 0224483A6E6AC710
  1. 24
      index.html
  2. 11
      js-api.c

24
index.html

@ -3,6 +3,7 @@ @@ -3,6 +3,7 @@
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script src="webdice.js"></script>
<script>
var previous = "";
function send(){
var input = document.getElementById('inputBox').value;
var size = lengthBytesUTF8(input) + 1;
@ -12,13 +13,28 @@ @@ -12,13 +13,28 @@
_free(pointer);
inputBox.value = '';
}
function keypress(code){
switch(code){
case 13:
previous = document.getElementById('inputBox').value;
send();
break;
case 38:
document.getElementById('inputBox').value = previous;
break;
default:
break;
}
}
</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">
<div id="container" style="position: absolute; bottom: 7em; left: 1em; top: 1em; right: 1em; font-family: sans-serif; overflow-y: scroll; ">
<div id="output"></div>
</div>
<form style="position: absolute; left: 1em; bottom: 1em; right: 1em; " action="javascript:void(0);">
<input style="width: 100%;" type="text" id="inputBox" onkeydown="keypress(event.keyCode)">
<input style="width: 100%;" type="button" onclick="keypress(13)" value="Submit">
</form>
</body>
</html>

11
js-api.c

@ -1,5 +1,7 @@ @@ -1,5 +1,7 @@
#include "emscripten.h"
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "expression.h"
#include "parser.tab.h"
@ -25,13 +27,16 @@ void EMSCRIPTEN_KEEPALIVE webdice(char* str){ @@ -25,13 +27,16 @@ void EMSCRIPTEN_KEEPALIVE webdice(char* str){
int length;
char* in_block;
char* out_block;
static char seeded = 0;
yydebug = 1;
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);
@ -46,6 +51,8 @@ void EMSCRIPTEN_KEEPALIVE webdice(char* str){ @@ -46,6 +51,8 @@ void EMSCRIPTEN_KEEPALIVE webdice(char* str){
output_print(out_block);
free(out_block);
free(result.text);
free_expression(root);
}else{
output_print("<p>Error!</p>");
}

Loading…
Cancel
Save