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
|
#include <stdio.h>
#include <stddef.h>
#include "io/file.h"
#include "cli/args.h"
#include "lexer/lexer.h"
#include "parser/parser.h"
int main(int argc, char** argv)
{
struct Args args;
enum ParseResult result = parse_args(argc, argv, &args);
if (result == PARSE_EXIT)
return 0;
if (result == PARSE_ERROR)
return 1;
struct File source;
if (!read_file(args.input_path, &source))
return 1;
struct Lexer lexer = create_lexer(source.data);
struct Program program;
if (!parse_program(&lexer, &program))
{
free_program(&program);
free_file(&source);
return 1;
}
for (size_t i = 0; i < program.const_count; i += 1)
{
struct ConstDecl decl = program.consts[i];
printf("const %.*s = %.*s\n",
(int)decl.name.length, decl.name.start,
(int)decl.value.length, decl.value.start);
}
for (size_t i = 0; i < program.data_count; i += 1)
{
struct DataDecl decl = program.data_decls[i];
printf("data %.*s = %.*s\n",
(int)decl.name.length, decl.name.start,
(int)decl.value.length, decl.value.start);
}
for (size_t i = 0; i < program.proc_count; i += 1)
{
struct ProcDecl proc = program.procs[i];
printf("proc %.*s(", (int)proc.name.length, proc.name.start);
for (size_t p = 0; p < proc.param_count; p += 1)
{
struct Param param = proc.params[p];
printf("%s%.*s: %.*s", p == 0 ? "" : ", ",
(int)param.name.length, param.name.start,
(int)param.reg.length, param.reg.start);
}
printf(")\n");
}
free_program(&program);
free_file(&source);
}
|