aboutsummaryrefslogtreecommitdiff
path: root/src/parse.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/parse.c')
-rw-r--r--src/parse.c98
1 files changed, 98 insertions, 0 deletions
diff --git a/src/parse.c b/src/parse.c
new file mode 100644
index 0000000..fb147be
--- /dev/null
+++ b/src/parse.c
@@ -0,0 +1,98 @@
1/// src/parse.h
2//
3// Copyright (C) 2026 by Martin Michalec <martin@michalec.dev>
4//
5// SPDX-License-Identifier: MIT
6//
7/// Commentary:
8//
9/// Code:
10
11#include <stdlib.h>
12#include <stdio.h>
13#include <fcntl.h>
14#include <unistd.h>
15
16#include "utils.h"
17#include "arena.h"
18#include "sv.h"
19#include "rs274ngc_lexer.h"
20#include "rs274ngc_cst.h"
21
22#define INDENTATION 2
23
24void
25print_cst (struct arena *temp, struct rs274ngc_lexer *lexer, struct rs274ngc_cst_branch *root, size_t alignment)
26{
27 void *bottom = ARENA_COPY (temp, root);
28 ARENA_COPY (temp, alignment);
29 while (temp->tail->free != bottom) {
30 alignment = *(size_t *) arena_pop (temp, sizeof (alignment));
31 void *node = *(void **) arena_pop (temp, sizeof (node ));
32 ASSERT (node);
33 if (RS274NGC_CST_NODE_KIND (node) < RS274NGC_CST_TERMINAL_COUNT) { // leaf
34 struct rs274ngc_cst_leaf *leaf = &RS274NGC_CST_NODE_PTR (node)->leaf;
35 struct location location = find_location (lexer->sv, leaf->sv);
36 print_location (lexer->filename, location, alignment, "- leaf: %s",
37 rs274ngc_cst_node_kind_names[RS274NGC_CST_NODE_KIND (node)]);
38 } else { // branch
39 struct rs274ngc_cst_branch *branch = &RS274NGC_CST_NODE_PTR (node)->branch;
40 if (1 || RS274NGC_CST_NODE_KIND_EQ (node, LINE)) {
41 struct location location = find_location (lexer->sv, branch->sv);
42 print_location (lexer->filename, location, alignment, "- branch: %s",
43 rs274ngc_cst_node_kind_names[RS274NGC_CST_NODE_KIND (node)]);
44 }
45 alignment += INDENTATION;
46 for (size_t i = branch->count; i > 0; --i) {
47 ASSERT (branch->children[i - 1]);
48 ARENA_COPY (temp, branch->children[i - 1]);
49 ARENA_COPY (temp, alignment);
50 }
51 }
52 }
53}
54
55int
56main (int argc, char *argv[])
57{
58 progname = argv[0];
59
60 const char *filename;
61 int fd = 0;
62 if (argc == 1) {
63 filename = "-";
64 } else if (argc == 2) {
65 filename = argv[1];
66 fd = open (filename, O_RDONLY);
67 } else {
68 DIE ("too many arguments provided");
69 }
70
71 struct arena a = {0};
72 struct arena t = {0};
73
74 struct sv rs274ngc = read_until_eof (&a, fd);
75 if (fd) close (fd);
76
77 int base_alignment = strlen (filename) + 15;
78
79 rs274ngc_lexer_compile_regexes ();
80 struct rs274ngc_lexer lexer = rs274ngc_lexer_from_sv (rs274ngc, filename);
81
82 struct rs274ngc_cst_parser parser = {
83 .lexer = &lexer,
84 .output = &a,
85 .temporary = &t,
86 };
87 struct rs274ngc_cst_branch *program = &RS274NGC_CST_PARSE (parser, PROGRAM)->branch;
88 // ensure no memory leaked
89 ASSERT (t.head == t.tail );
90 ASSERT (t.head->free == t.head->begin);
91 printf ("Memory used %lu\n", arena_used (parser.output));
92
93 print_cst (&t, &lexer, program, base_alignment);
94
95 return EXIT_SUCCESS;
96}
97
98/// src/parse.h ends here