stage1 修复设计问题 实现ir2vm
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
|
||||
#include "spl_ast.h"
|
||||
#include "spl_ast2ir.h"
|
||||
#include "spl_ir2vm.h"
|
||||
#include "spl_lexer.h"
|
||||
#include "spl_sema.h"
|
||||
#include "spl_tok.h"
|
||||
@@ -62,6 +63,11 @@ static void dump_ast(const char *src, const char *fname) {
|
||||
spl_ast_t ast;
|
||||
spl_ast_init(&ast, &toks);
|
||||
spl_ast_prase(&ast);
|
||||
if (ast.parsed < 0) {
|
||||
printf("parse failed, skip AST dump\n");
|
||||
spl_ast_drop(&ast);
|
||||
return;
|
||||
}
|
||||
spl_ast_valid(&ast);
|
||||
spl_ast_dump(&ast, ast.root);
|
||||
spl_ast_drop(&ast);
|
||||
@@ -72,6 +78,11 @@ static void dump_sema(const char *src, const char *fname) {
|
||||
spl_ast_t ast;
|
||||
spl_ast_init(&ast, &toks);
|
||||
spl_ast_prase(&ast);
|
||||
if (ast.parsed < 0) {
|
||||
printf("parse failed, skip sema dump\n");
|
||||
spl_ast_drop(&ast);
|
||||
return;
|
||||
}
|
||||
spl_ast_valid(&ast);
|
||||
spl_sema_t sema;
|
||||
spl_sema_init(&sema);
|
||||
@@ -108,6 +119,11 @@ static void dump_ir(const char *src, const char *fname) {
|
||||
spl_ast_t ast;
|
||||
spl_ast_init(&ast, &toks);
|
||||
spl_ast_prase(&ast);
|
||||
if (ast.parsed < 0) {
|
||||
printf("parse failed, skip IR\n");
|
||||
spl_ast_drop(&ast);
|
||||
return;
|
||||
}
|
||||
spl_ast_valid(&ast);
|
||||
spl_sema_t sema;
|
||||
spl_sema_init(&sema);
|
||||
@@ -152,6 +168,48 @@ static int cmd_dump(const char *flags, const char *path) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int compile_spl(const char *src, const char *fname, const char *outpath) {
|
||||
spl_tok_vec_t toks = spl_lex(src, fname);
|
||||
spl_ast_t ast;
|
||||
spl_ast_init(&ast, &toks);
|
||||
spl_ast_prase(&ast);
|
||||
if (ast.parsed < 0) {
|
||||
printf("parse failed, no output\n");
|
||||
spl_ast_drop(&ast);
|
||||
return 1;
|
||||
}
|
||||
spl_ast_valid(&ast);
|
||||
spl_sema_t sema;
|
||||
spl_sema_init(&sema);
|
||||
sema.ast = *
|
||||
spl_sema_run(&sema);
|
||||
spl_sema_check(&sema);
|
||||
if (sema.error_count) {
|
||||
printf("sema errors=%d, no output\n", sema.error_count);
|
||||
spl_sema_drop(&sema);
|
||||
spl_ast_drop(&ast);
|
||||
return 1;
|
||||
}
|
||||
spl_ast2ir_t a2ir;
|
||||
spl_ast2ir_init(&a2ir, &sema);
|
||||
spl_ast2ir_run(&a2ir);
|
||||
if (a2ir.err_count) {
|
||||
printf("ast2ir errors=%d, no output\n", a2ir.err_count);
|
||||
spl_ast2ir_drop(&a2ir);
|
||||
spl_sema_drop(&sema);
|
||||
spl_ast_drop(&ast);
|
||||
return 1;
|
||||
}
|
||||
spl_ir2vm_t ir2vm;
|
||||
spl_ir2vm_init(&ir2vm, &a2ir.ir, &sema.type);
|
||||
int rc = spl_ir2vm_run(&ir2vm, outpath);
|
||||
spl_ir2vm_drop(&ir2vm);
|
||||
spl_ast2ir_drop(&a2ir);
|
||||
spl_sema_drop(&sema);
|
||||
spl_ast_drop(&ast);
|
||||
return rc ? 1 : 0;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc < 2) {
|
||||
LOG_FATAL("Usage: splc0 [--dump <flags>] <in> [out]\n");
|
||||
@@ -174,6 +232,16 @@ int main(int argc, char **argv) {
|
||||
}
|
||||
return cmd_dump(argv[argi + 1], argv[argi + 2]);
|
||||
}
|
||||
LOG_FATAL("splc0: compile todo\n");
|
||||
return 1;
|
||||
/* splc0 <in> <out> */
|
||||
if (argc < argi + 2) {
|
||||
LOG_FATAL("Usage: splc0 <in> <out> or splc0 --dump <flags> <file>\n");
|
||||
return 1;
|
||||
}
|
||||
long len;
|
||||
char *src = read_file(argv[argi], &len);
|
||||
if (!src)
|
||||
return 1;
|
||||
int rc = compile_spl(src, argv[argi], argv[argi + 1]);
|
||||
free(src);
|
||||
return rc;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user