-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
199 lines (182 loc) · 5.09 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#define AST_VISITOR_IMPLEMENTATION
#include "ast.h"
#include "parse.h"
#include <assert.h>
#include "visitor.h"
// #include "interpreter.h"
#include "traverse.h"
#include "compiler.h"
#include "vm.h"
#ifndef _WIN32
#include <unistd.h>
#endif
#include "string_table.h"
static char *read_text_file(const char *path)
{
FILE *fp = fopen(path, "r");
if(!fp)
return NULL;
long n = 0;
fseek(fp, 0, SEEK_END);
n = ftell(fp);
char *data = calloc(1, n + 1);
rewind(fp);
fread(data, 1, n, fp);
fclose(fp);
return data;
}
static bool node_fn(ASTNode *n, void *ctx)
{
if(n->type != AST_FILE_REFERENCE)
return false;
Parser *parser = ctx;
snprintf(parser->string, parser->max_string_length, "%s", n->ast_file_reference_data.file);
// for(char *p = parser->string; *p; p++)
// if(*p == '\\')
// *p = '/';
Allocator allocator = arena_allocator(parser->perm);
hash_trie_upsert(parser->file_references, parser->string, &allocator, false);
return false;
}
int compile_file(const char *path,
const char *data,
CompiledFile *cf,
Arena *perm,
Arena scratch,
StringTable *strtab,
int flags,
HashTrie *globals)
{
if(!data)
return 1;
Allocator perm_allocator = arena_allocator(perm);
jmp_buf jmp;
if(setjmp(jmp))
{
// printf("[ERROR] Out of memory!\n");
return 1;
}
Instruction *instructions = new(&scratch, Instruction, MAX_INSTRUCTIONS);
Compiler compiler = { 0 };
compiler.globals = globals;
compiler.instructions = instructions;
compiler.instruction_count = 0;
compiler.arena = &scratch;
compiler.strings = strtab;
compiler.jmp = &jmp;
compiler.flags = flags;
// char path[512];
// snprintf(path, sizeof(path), "%s/%s.gsc", base_path, input_file);
// for(char *p = path; *p; p++)
// *p = *p == '\\' ? '/' : *p;
// unsigned char *data = read_text_file(path);
// if(!data)
// {
// printf("Can't read '%s'\n", path);
// return 1;
// }
compiler.source = data;
compiler.path = path;
char string[16384];
Stream s = { 0 };
StreamBuffer sb = { 0 };
init_stream_from_buffer(&s, &sb, (unsigned char*)data, strlen(data) + 1);
Lexer l = { 0 };
lexer_init(&l, &s);
l.flags |= LEXER_FLAG_PRINT_SOURCE_ON_ERROR;
// l.flags |= LEXER_FLAG_TOKENIZE_WHITESPACE;
l.jmp = &jmp;
Parser parser = { 0 };
parser.verbose = false;
// Allocator allocator = arena_allocator(&scratch);
// parser.allocator = &allocator;
parser.string = string;
parser.max_string_length = sizeof(string);
parser.lexer = &l;
parser.perm = perm;
parser.temp = &scratch;
parser.file_references = &cf->file_references;
parser.includes = &cf->includes;
HashTrie ast_functions;
hash_trie_init(&ast_functions);
lexer_step(parser.lexer, &parser.token);
parse(&parser, path, &ast_functions, globals);
for(HashTrieNode *it = ast_functions.head; it; it = it->next)
{
ASTFunction *func = it->value;
traverse((ASTNode*)func, node_fn, &parser);
int local_count = 0;
CompiledFunction *compfunc = new(perm, CompiledFunction, 1);
compfunc->file = cf;
compfunc->parameter_count = func->parameter_count;
compfunc->instruction_count = compile_function(&compiler, perm, scratch, func, &local_count, compfunc);
compfunc->local_count = local_count;
compfunc->instructions = new(perm, Instruction, compfunc->instruction_count);
memcpy(compfunc->instructions, instructions, sizeof(Instruction) * compfunc->instruction_count);
HashTrieNode *entry = hash_trie_upsert(&cf->functions, func->name, &perm_allocator, false);
entry->value = compfunc;
compfunc->name = entry->key;
}
// for(HashTrieNode *it = cf->includes.head; it; it = it->next)
// {
// printf("INCLUDE:%s\n", it->key);
// getchar();
// }
// printf("scratch %f MiB\n", arena_available_mib(&scratch));
// getchar();
return 0;
}
// void compile()
// {
// // Compile all functions
// // getchar();
// VM *vm = perm_allocator.malloc(perm_allocator.ctx, sizeof(VM));
// vm_init(vm, &perm_allocator, &strtab);
// vm->flags = VM_FLAG_NONE;
// if(verbose)
// vm->flags |= VM_FLAG_VERBOSE;
// vm->jmp = &jmp;
// vm->ctx = program;
// vm->func_lookup = vm_func_lookup;
// compiler_free(&compiler);
// }
// void run()
// {
// void register_c_functions(VM *vm);
// register_c_functions(vm);
// VMFunction *cf = get_function(get_file(input_file), "main");
// if(cf)
// {
// // dump_instructions(&compiler, cf->instructions);
// vm_call_function_thread(vm, input_file, "main", 0, NULL);
// while(1)
// {
// float dt = 1.f / 20.f;
// if(!vm_run_threads(vm, dt))
// break;
// debug_serialize(vm);
// debug_update();
// usleep(20000);
// }
// }
// else
// {
// printf("can't find %s::%s\n", input_file, "main");
// }
// // dump_program(program);
// // interp.program = program;
// vm_cleanup(vm);
// for(HashTrieNode *it = compiled_files.head; it; it = it->next)
// {
// CompiledFile *cf = it->value;
// for(HashTrieNode *it_fun = cf->functions.head; it_fun; it_fun = it_fun->next)
// {
// VMFunction *vmf = it_fun->value;
// buf_free(vmf->instructions);
// free(vmf);
// }
// free(cf);
// }
// // call_function(&interp, "maps/moscow", "main", 0);
// // printf("%f MB", get_memory_usage_kb() / 1000.f);
// }