111 lines
2.6 KiB
C
111 lines
2.6 KiB
C
#include "ast.h"
|
|
#include "collection.h"
|
|
#include "parse.h"
|
|
#include "resolve.h"
|
|
#include "sema.h"
|
|
#include <assert.h>
|
|
#include <stddef.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
int main(int argc, char* argv[])
|
|
{
|
|
if (argc < 2) {
|
|
fprintf(stderr, "error: no filename\n");
|
|
return EXIT_FAILURE;
|
|
}
|
|
char* filename = argv[1];
|
|
char* text = read_textfile(filename);
|
|
if (!text) {
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
printf("--- text ---\n%s\n", text);
|
|
|
|
{
|
|
|
|
Arena tok_arena;
|
|
arena_init(&tok_arena, 4096);
|
|
|
|
Preproc* preproc
|
|
= preproc_new(strdup(filename), strdup(text), &tok_arena);
|
|
|
|
TokStream tokstream;
|
|
tokstream_init(&tokstream, preproc, &tok_arena);
|
|
|
|
printf("--- tokens ---\n");
|
|
|
|
Loc loc = { 1, 1 };
|
|
Tok tok;
|
|
while ((tok = tokstream_next(&tokstream)).ty != Tok_Eof) {
|
|
// printf("%s\t%s\n", tokty_str(tok.ty), tok.text);
|
|
Loc tok_loc = tok.loc;
|
|
if (loc.line > tok_loc.line) {
|
|
loc.line = 0;
|
|
}
|
|
while (loc.line < tok_loc.line) {
|
|
printf("\n");
|
|
loc.line += 1;
|
|
loc.col = 1;
|
|
}
|
|
while (loc.col < tok_loc.col) {
|
|
printf(" ");
|
|
loc.col += 1;
|
|
}
|
|
printf("%s", tok.text);
|
|
loc.line = tok_loc.line;
|
|
loc.col = tok_loc.col + strlen(tok.text);
|
|
}
|
|
fputs("\n", stdout);
|
|
|
|
preproc_free(preproc);
|
|
arena_deinit(&tok_arena);
|
|
}
|
|
|
|
{
|
|
Arena tok_arena;
|
|
arena_init(&tok_arena, 4096);
|
|
|
|
Preproc* preproc = preproc_new(strdup(filename), text, &tok_arena);
|
|
|
|
TokStream tokstream;
|
|
tokstream_init(&tokstream, preproc, &tok_arena);
|
|
|
|
Sema* sema = sema_new();
|
|
|
|
Arena ast_arena;
|
|
arena_init(&ast_arena, 4096);
|
|
|
|
printf("--- ast ---\n");
|
|
|
|
Parser* parser = parser_new(&tokstream, sema, &ast_arena);
|
|
|
|
// Arena sym_arena;
|
|
// arena_init(&sym_arena, 4096);
|
|
// Resolver* resolver = resolver_new(&sym_arena);
|
|
|
|
AstStmt* stmt;
|
|
while (!parser_done(parser)) {
|
|
stmt = parser_parse_stmt(parser);
|
|
|
|
ast_stmt_print(stdout, stmt, 0);
|
|
|
|
printf("\n");
|
|
|
|
// resolver_resolve_top_stmt(resolver, stmt);
|
|
}
|
|
|
|
// resolver_free(resolver);
|
|
|
|
parser_free(parser);
|
|
preproc_free(preproc);
|
|
arena_deinit(&tok_arena);
|
|
|
|
arena_deinit(&ast_arena);
|
|
// arena_deinit(&sym_arena);
|
|
|
|
sema_free(sema);
|
|
}
|
|
}
|