-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathashp4c.c
141 lines (126 loc) · 3.43 KB
/
ashp4c.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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h> /* exit */
#include "foundation.h"
#include "frontend.h"
#include "ashp4c.h"
typedef struct CmdlineArg {
char* name;
char* value;
struct CmdlineArg* next_arg;
} CmdlineArg;
static void
read_source(Arena* storage, char* filename, SourceText* source_text/*out*/)
{
FILE* f_stream;
char* text;
f_stream = fopen(filename, "rb");
if (!f_stream) {
error("Could not open file '%s'.", filename);
}
fseek(f_stream, 0, SEEK_END);
int text_size = ftell(f_stream);
fseek(f_stream, 0, SEEK_SET);
text = arena_malloc(storage, (text_size + 1)*sizeof(char));
fread(text, sizeof(char), text_size, f_stream);
text[text_size] = '\0';
fclose(f_stream);
source_text->text = text;
source_text->text_size = text_size;
source_text->filename = filename;
}
static CmdlineArg*
find_unnamed_arg(CmdlineArg* args)
{
CmdlineArg* unnamed_arg = 0;
CmdlineArg* arg;
arg = args;
while (arg) {
if (!arg->name) {
unnamed_arg = arg;
break;
}
arg = arg->next_arg;
}
return unnamed_arg;
}
#if 0
static CmdlineArg*
find_named_arg(char* name, CmdlineArg* args)
{
CmdlineArg* named_arg = 0;
CmdlineArg* arg = args;
while (arg) {
if (arg->name && cstr_match(name, arg->name)) {
named_arg = arg;
break;
}
arg = arg->next_arg;
}
return named_arg;
}
#endif
static CmdlineArg*
parse_cmdline_args(Arena* storage, int arg_count, char* args[])
{
CmdlineArg* arg_list = 0;
CmdlineArg *prev_arg, *cmdline_arg;
CmdlineArg sentinel_arg = {};
char* raw_arg;
if (arg_count <= 1) {
return arg_list;
}
prev_arg = &sentinel_arg;
int i = 1;
while (i < arg_count) {
cmdline_arg = arena_malloc(storage, sizeof(CmdlineArg));
if (cstr_start_with(args[i], "-")) {
raw_arg = args[i] + 1; /* skip the `-` prefix */
cmdline_arg->name = raw_arg;
} else {
cmdline_arg->value = args[i];
}
prev_arg->next_arg = cmdline_arg;
prev_arg = cmdline_arg;
i += 1;
}
arg_list = sentinel_arg.next_arg;
return arg_list;
}
int
main(int arg_count, char* args[])
{
CmdlineArg* cmdline, *filename;
SourceText source_text = {0};
Arena storage = {0}, scratch_storage = {0};
Ast* program;
Array* tokens;
Scope* root_scope;
Array* type_array;
Map* scope_map, *decl_map;
Map* type_env, *potype_map;
reserve_memory(500*KILOBYTE);
cmdline = parse_cmdline_args(&storage, arg_count, args);
filename = find_unnamed_arg(cmdline);
if (!filename) {
printf("<filename> is required.\n");
exit(1);
}
read_source(&scratch_storage, filename->value, &source_text);
tokens = tokenize(&storage, &source_text);
program = parse(&storage, source_text.filename, tokens, &root_scope);
arena_free(&scratch_storage);
drypass(source_text.filename, program);
builtin_methods(&storage, source_text.filename, program);
scope_map = scope_hierarchy(&storage, source_text.filename, program, root_scope);
decl_map = name_bind(&storage, source_text.filename, program, root_scope,
scope_map, &type_array);
type_env = declared_types(&storage, source_text.filename, program, root_scope,
type_array, scope_map, decl_map);
potype_map = potential_types(&storage, source_text.filename, program, root_scope,
scope_map, decl_map, type_env);
select_type(&storage, source_text.filename, program, root_scope,
type_array, scope_map, decl_map, type_env, potype_map);
arena_free(&storage);
return 0;
}