stage1 完成ast 定义sema
This commit is contained in:
@@ -1,8 +1,19 @@
|
||||
/* splc0.c — SPL compiler CLI */
|
||||
/* splc0.c — SPL compiler CLI (stage 1, 引导用)
|
||||
*
|
||||
* splc0 --dump tokens|ast|all <file> dump 前端产物
|
||||
* splc0 <in> <out> 编译 (阶段 B 实现)
|
||||
*/
|
||||
#define __SCC_LOG_IMPL_IMPORT_SRC__
|
||||
#include "../stage0/include/utils.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "spl_ast.h"
|
||||
#include "spl_lexer.h"
|
||||
#include "spl_tok.h"
|
||||
|
||||
static char *read_file(const char *path, long *out_len) {
|
||||
FILE *f = fopen(path, "rb");
|
||||
if (!f) {
|
||||
@@ -24,19 +35,68 @@ static char *read_file(const char *path, long *out_len) {
|
||||
return buf;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc < 2) {
|
||||
fprintf(stderr, "Usage: splc0 [--dump <flags>] <in> [out]\n");
|
||||
static const char *const tok_type_names[] = {
|
||||
#define X(name, enum_name, dummy) #enum_name,
|
||||
KEYWORD_TABLE
|
||||
#undef X
|
||||
#define X(name, enum_name, dummy) #enum_name,
|
||||
TOKEN_TABLE
|
||||
#undef X
|
||||
};
|
||||
|
||||
static void dump_tokens(const char *src, const char *fname) {
|
||||
spl_tok_vec_t toks = spl_lex(src, fname);
|
||||
printf("tokens got (%zu)\n", toks.size);
|
||||
for (usize i = 0; i < toks.size; i++) {
|
||||
const spl_tok_t *t = &toks.data[i];
|
||||
printf("[%s] %.*s (%zu:%zu)\n", tok_type_names[t->type], (int)t->len, t->lexeme, t->line,
|
||||
t->col);
|
||||
}
|
||||
vec_free(toks);
|
||||
}
|
||||
|
||||
static void dump_ast(const char *src, const char *fname) {
|
||||
spl_tok_vec_t toks = spl_lex(src, fname);
|
||||
spl_ast_t ast;
|
||||
spl_ast_init(&ast, &toks);
|
||||
spl_ast_prase(&ast);
|
||||
spl_ast_valid(&ast);
|
||||
spl_ast_dump(&ast, ast.root);
|
||||
spl_ast_drop(&ast);
|
||||
}
|
||||
|
||||
static int cmd_dump(const char *flags, const char *path) {
|
||||
long len;
|
||||
char *src = read_file(path, &len);
|
||||
if (!src)
|
||||
return 1;
|
||||
}
|
||||
if (strcmp(argv[1], "--dump") == 0)
|
||||
// return cmd_dump(argc - 2, argv + 2);
|
||||
return 0;
|
||||
if (strcmp(argv[1], "--help") == 0) {
|
||||
printf("splc0 <in> <out> compile\n");
|
||||
printf("splc0 --dump <f> <file> dump: tokens,cst,ast,ir,mcode,all\n");
|
||||
return 0;
|
||||
}
|
||||
// return cmd_compile(argc - 1, argv + 1);
|
||||
int do_tokens = strstr(flags, "tokens") != NULL || strcmp(flags, "all") == 0;
|
||||
int do_ast = strstr(flags, "ast") != NULL || strcmp(flags, "all") == 0;
|
||||
if (do_tokens)
|
||||
dump_tokens(src, path);
|
||||
if (do_ast)
|
||||
dump_ast(src, path);
|
||||
free(src);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc < 2) {
|
||||
LOG_FATAL("Usage: splc0 [--dump <flags>] <in> [out]\n");
|
||||
return 1;
|
||||
}
|
||||
if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0) {
|
||||
LOG_INFO("splc0 <in> <out> compile (.spl -> .sir, 阶段 B)\n");
|
||||
LOG_INFO("splc0 --dump <flags> <file> dump: tokens,ast,all\n");
|
||||
return 0;
|
||||
}
|
||||
if (strcmp(argv[1], "--dump") == 0) {
|
||||
if (argc < 4) {
|
||||
LOG_INFO("splc0: --dump need <flags> <file>\n");
|
||||
return 1;
|
||||
}
|
||||
return cmd_dump(argv[2], argv[3]);
|
||||
}
|
||||
LOG_FATAL("splc0: compile todo\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user