forked from solstice/solstice
Start work on WASM compatibility, lambda fix
This commit is contained in:
81
src/wasm_main.c
Normal file
81
src/wasm_main.c
Normal file
@@ -0,0 +1,81 @@
|
||||
#include <emscripten.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <setjmp.h>
|
||||
|
||||
#include "lexer/lexer.h"
|
||||
#include "typeparser/typeparser.h"
|
||||
#include "parser/parser.h"
|
||||
#include "codegen/codegen.h"
|
||||
|
||||
#include <groundvm.h>
|
||||
|
||||
|
||||
static char out_buf[65536];
|
||||
static int out_pos = 0;
|
||||
|
||||
// Defined by Ground
|
||||
void wasm_print(const char* str);
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
const char* solstice_run(char* source) {
|
||||
out_pos = 0;
|
||||
out_buf[0] = '\0';
|
||||
|
||||
// 1. Lex
|
||||
ResultType(SolsLexer, charptr) lexer = createLexer(source);
|
||||
if (lexer.error) {
|
||||
snprintf(out_buf, sizeof(out_buf),
|
||||
"[lex error] %s", lexer.as.error);
|
||||
return out_buf;
|
||||
}
|
||||
ResultType(Nothing, charptr) lexed = lex(&lexer.as.success);
|
||||
if (lexed.error) {
|
||||
snprintf(out_buf, sizeof(out_buf), "%s", lexed.as.error);
|
||||
return out_buf;
|
||||
}
|
||||
|
||||
// 2. Type parse
|
||||
ResultType(SolsTokens, charptr) typed = addTypeInfo(&lexer.as.success.output);
|
||||
if (typed.error) {
|
||||
snprintf(out_buf, sizeof(out_buf), "%s", typed.as.error);
|
||||
return out_buf;
|
||||
}
|
||||
|
||||
// 3. Parse
|
||||
ResultType(SolsParser, charptr) parser = createSolsParser(&typed.as.success);
|
||||
if (parser.error) {
|
||||
snprintf(out_buf, sizeof(out_buf),
|
||||
"[parse error] %s", parser.as.error);
|
||||
return out_buf;
|
||||
}
|
||||
ResultType(Nothing, charptr) parsed = parse(&parser.as.success);
|
||||
if (parsed.error) {
|
||||
snprintf(out_buf, sizeof(out_buf), "%s", parsed.as.error);
|
||||
return out_buf;
|
||||
}
|
||||
|
||||
// 4. Codegen
|
||||
SolsScope scope = {
|
||||
.variables = NULL,
|
||||
.tmpCounter = 0,
|
||||
.returnType = createSolsType(STT_INT).as.success
|
||||
};
|
||||
ResultType(GroundProgram, charptr) codegen =
|
||||
generateCode(&parser.as.success.output, &scope);
|
||||
if (codegen.error) {
|
||||
snprintf(out_buf, sizeof(out_buf), "%s", codegen.as.error);
|
||||
return out_buf;
|
||||
}
|
||||
|
||||
// 5. Run
|
||||
GroundValue retval = groundRunProgram(&codegen.as.success);
|
||||
if (out_pos == 0) {
|
||||
// Program produced no output — report exit code
|
||||
snprintf(out_buf, sizeof(out_buf),
|
||||
"[exited with code %d]",
|
||||
retval.type == INT ? retval.data.intVal : 0);
|
||||
}
|
||||
|
||||
return out_buf;
|
||||
}
|
||||
Reference in New Issue
Block a user