2024-09-23 04:39:35 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "parse.h"
|
|
|
|
#include "ast.h"
|
2024-09-24 07:03:52 -07:00
|
|
|
#include "compile.h"
|
2024-09-23 04:39:35 -07:00
|
|
|
|
2024-09-27 04:50:47 -07:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2024-09-23 04:39:35 -07:00
|
|
|
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);
|
2024-09-24 07:03:52 -07:00
|
|
|
COMPILE_FORMAT = COMPILE_FORMAT_ASM;
|
|
|
|
CompileEnvironment *env = make_compile_environment();
|
2024-09-27 04:50:47 -07:00
|
|
|
register_native_defuns(env);
|
2024-09-24 07:03:52 -07:00
|
|
|
AstErrorList *ast_errs;
|
|
|
|
CompileError *comp_errs;
|
2024-09-23 04:39:35 -07:00
|
|
|
while (!token_stream_is_eof(stream)) {
|
2024-09-24 07:03:52 -07:00
|
|
|
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);
|
2024-09-23 04:39:35 -07:00
|
|
|
ast_error_list_free_one(err);
|
|
|
|
}
|
2024-09-24 07:03:52 -07:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2024-09-23 04:39:35 -07:00
|
|
|
destroy_ast_node(node);
|
|
|
|
}
|
2024-09-24 07:03:52 -07:00
|
|
|
destroy_compile_environment(env);
|
2024-09-23 04:39:35 -07:00
|
|
|
destroy_token_stream(stream);
|
|
|
|
ast_deinit_parser();
|
|
|
|
return 0;
|
|
|
|
}
|