simple-lisp/bootstrap/main.c

58 lines
1.8 KiB
C

#include <stdio.h>
#include "parse.h"
#include "ast.h"
#include "compile.h"
static const char NATIVE_DEFUNS[] =
"(defun native-write (bytes-or-string &key stream start end))";
static void register_native_defuns(CompileEnvironment *env) {
FILE *in = fmemopen((char *) NATIVE_DEFUNS, sizeof(NATIVE_DEFUNS) - 1, "r");
TokenStream *stream = make_token_stream(in);
while (!token_stream_is_eof(stream)) {
AstNode *node = ast_next_toplevel(stream, NULL);
if (node) {
load_toplevel_definition(env, node, NULL);
}
destroy_ast_node(node);
}
destroy_token_stream(stream);
}
int main(int argc, const char **argv) {
ast_init_parser();
FILE *file = fopen("bootstrap/test.sl", "r");
if (!file) {
perror("fopen");
}
TokenStream *stream = make_token_stream(file);
COMPILE_FORMAT = COMPILE_FORMAT_ASM;
CompileEnvironment *env = make_compile_environment();
register_native_defuns(env);
AstErrorList *ast_errs;
CompileError *comp_errs;
while (!token_stream_is_eof(stream)) {
AstNode *node = ast_next_toplevel(stream, &ast_errs);
while (ast_errs) {
AstErrorList *err = ast_error_list_pop(&ast_errs);
ast_format_error(err, "test.sl", stderr);
ast_error_list_free_one(err);
}
if (node) {
ssize_t ninst = byte_compile_form(env, node, stdout, &comp_errs);
while (comp_errs) {
CompileError *err = compile_error_pop(&comp_errs);
compile_format_error(err, "test.sl", stderr);
compile_error_free_one(err);
}
}
destroy_ast_node(node);
}
destroy_compile_environment(env);
destroy_token_stream(stream);
ast_deinit_parser();
return 0;
}