Browse Source

Add compiling code

Still does not work. Some words just return an integer overflow, others
just return error.
main
raffitz 5 years ago
parent
commit
36bbfc45b3
Signed by: raffitz
GPG Key ID: 0224483A6E6AC710
  1. 24
      Makefile
  2. 8
      expression.c
  3. 24
      index.html
  4. 50
      js-api.c
  5. 21
      lexer.l
  6. 7
      parser.y

24
Makefile

@ -0,0 +1,24 @@ @@ -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

8
expression.c

@ -68,7 +68,7 @@ struct expression_result resolve(union expression* root){ @@ -68,7 +68,7 @@ struct expression_result resolve(union expression* root){
result.expected = (*root).constant.value;
result.actual = (*root).constant.value;
result.text = (char*) malloc(length * sizeof(char));
sprintf(result.text, "%d",(*root).constant.value);
sprintf(result.text, "%lld",(*root).constant.value);
break;
case WEBDICE_ROLL:
size = (*root).roll.count * ceill(15+log10l((*root).roll.count * (*root).roll.type));
@ -82,11 +82,11 @@ struct expression_result resolve(union expression* root){ @@ -82,11 +82,11 @@ struct expression_result resolve(union expression* root){
values[i] = 1 + (rand() % (*root).roll.type);
total += values[i];
if (i == 0){
sprintf(result.text,"<span title=\"%dd%d=%d,", (*root).roll.count, (*root).roll.type, values[i]);
sprintf(result.text,"<span title=\"%lldd%lld=%lld,", (*root).roll.count, (*root).roll.type, values[i]);
}else if (j == 1){
sprintf(result.text,"%s%d\">%d</span>", result.text, values[i], total);
sprintf(result.text,"%s%lld\">%lld</span>", result.text, values[i], total);
}else{
sprintf(result.text,"%s%d,", result.text, values[i]);
sprintf(result.text,"%s%lld,", result.text, values[i]);
}
}
free(values);

24
index.html

@ -0,0 +1,24 @@ @@ -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>

50
js-api.c

@ -0,0 +1,50 @@ @@ -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);
}

21
lexer.l

@ -3,27 +3,34 @@ @@ -3,27 +3,34 @@
#include <stdio.h>
#include "expression.h"
#include "parser.tab.h"
#define _POSIX_C_SOURCE
%}
%option reentrant
%option bison-bridge
%option nounput
%option noinput
%option noyywrap
%%
"\n" { return WEBDICE_TOKEN_EOL; }
-?[0-9]+ {
int64_t aux;
sscanf(yytext,"%ld",&aux);
yylval.exp.constant.id = WEBDICE_CONST;
yylval.exp.constant.value = aux;
sscanf(yytext,"%lld",&aux);
(*(*yylval).exp).constant.id = WEBDICE_CONST;
(*(*yylval).exp).constant.value = aux;
return WEBDICE_TOKEN_CONST;
}
-?[0-9]+d[0-9]+ {
int64_t count,type;
sscanf(yytext,"%ldd%ld",&count,&type);
yylval.exp.roll.id = WEBDICE_ROLL;
yylval.exp.roll.count = count;
yylval.exp.roll.type = type;
sscanf(yytext,"%lldd%lld",&count,&type);
(*(*yylval).exp).roll.id = WEBDICE_ROLL;
(*(*yylval).exp).roll.count = count;
(*(*yylval).exp).roll.type = type;
return WEBDICE_TOKEN_ROLL;
}
"(" { return WEBDICE_TOKEN_LPAREN; }

7
parser.y

@ -1,12 +1,15 @@ @@ -1,12 +1,15 @@
%{
#include <stdlib.h>
#include "expression.h"
int yylex();
int yyerror(char*);
int yyerror(void* scanner, void* lvalp, char* str);
%}
%parse-param {union expression** root};
%pure-parser
%lex-param {void *scanner}
%parse-param {void *scanner} {union expression** root}
%union {
union expression* exp;

Loading…
Cancel
Save