aboutsummaryrefslogtreecommitdiff
path: root/src/rs274ngc_cst.c
blob: b2dd5821d5ad1e8cec82dca35f16c633e5a22c7e (plain)
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/// src/rs274ngc_cst.c --- RS274NGC Concrete Syntax Tree
//
// Copyright (C) 2026 by Martin Michalec <martin@michalec.dev>
//
// SPDX-License-Identifier: MIT
//
/// Commentary:
//
/// Code:

#include <stdbool.h>
#include <stdarg.h>
#include <stdio.h>
#include "utils.h"
#include "rs274ngc_cst.h"

#ifndef ARENA_ASSERT
#include <assert.h>
#define ASSERT assert
#else
#define ASSERT ARENA_ASSERT
#endif

#define X(SYM, ...) [RS274NGC_CST_NODE_KIND_##SYM] = #SYM,
const char *rs274ngc_cst_node_kind_names[RS274NGC_CST_NODE_KIND_COUNT] = {
  [RS274NGC_CST_NODE_KIND_INVALID    ] = "INVALID",
  [RS274NGC_CST_NODE_KIND_END_OF_LINE] = "END_OF_LINE",
  [RS274NGC_CST_NODE_KIND_END_OF_FILE] = "END_OF_FILE",
  RS274NGC_CST_NODE_KINDS ()
};
#undef X

static void
print_error (struct rs274ngc_lexer *lexer, const char *point,
             const char *format, ...)
{
  int row = 1;
  int col = 1;

  for (const char *head = lexer->sv.beg; head < point; ++head) {
    col += 1;
    if (*head == '\n') {
      row += 1;
      col = 1;
    }
  };

  fprintf (stderr, "%s:%d:%d: error: ", lexer->filename, row, col);

  va_list ap;
  va_start (ap, format);
  vfprintf (stderr, format, ap);
  va_end (ap);

  putc ('\n', stderr);
}

#define PARSE_BRANCH_NODE(P, B, K)                              \
  parse_branch_node (P, B, RS274NGC_CST_NODE_KIND_##K)
#define PARSE_BRANCH_NODE_IF(P, B, K, L)                                \
  (((P).lexer->lexeme.kind == RS274NGC_LEXEME_KIND_##L)              \
   ? parse_branch_node (P, B, RS274NGC_CST_NODE_KIND_##K) : false)
#define PARSE_BRANCH_NODE_IF_SELF(P, B, L)                              \
  (((P).lexer->lexeme.kind == RS274NGC_LEXEME_KIND_##L)              \
   ? parse_branch_node (P, B, RS274NGC_CST_NODE_KIND_##L) : false)
static bool
parse_branch_node (struct rs274ngc_cst_parser    parser,
                   struct rs274ngc_cst_branch   *branch,
                   enum   rs274ngc_cst_node_kind kind)
{
  union rs274ngc_cst_node *node = rs274ngc_cst_parse_node (parser, kind);

  if (node == NULL)
    return false;

  ARENA_COPY (parser.temporary, node);
  branch->count += 1;

  return true;
}

#define LEXEME_MATCHES(P, K) \
  ((P).lexer->lexeme.kind == RS274NGC_LEXEME_KIND_##K)

struct rs274ngc_cst_leaf *
rs274ngc_cst_parse_leaf (struct rs274ngc_cst_parser    parser,
                         enum   rs274ngc_cst_node_kind kind)
{
  ASSERT (kind < RS274NGC_CST_TERMINAL_COUNT);

  if (parser.lexer->lexeme.kind != (enum rs274ngc_lexeme_kind) kind)
    return NULL;

  struct rs274ngc_cst_leaf leaf = { .sv = parser.lexer->lexeme.sv };
  void *ptr = ARENA_COPY (parser.output, leaf);
  rs274ngc_lexer_next_lexeme (parser.lexer);

  return (struct rs274ngc_cst_leaf *) RS274NGC_CST_TAG_NODE (ptr, kind);
}

struct rs274ngc_cst_branch *
rs274ngc_cst_parse_branch (struct rs274ngc_cst_parser    parser,
                           enum   rs274ngc_cst_node_kind kind)
{
  ASSERT (kind >= RS274NGC_CST_TERMINAL_COUNT);
  ASSERT (kind <  RS274NGC_CST_NODE_KIND_COUNT);

  // printf ("Parsing: %s\n", rs274ngc_cst_node_kind_names[kind]);

  struct rs274ngc_cst_branch branch = {
    .sv.beg = parser.lexer->lexeme.sv.beg
  };

  const char *cursor = parser.lexer->cursor;
  struct arena_checkpoint children =
    arena_checkpoint (parser.temporary, ALIGNOF (void *) - 1);
  switch (kind) {
  case RS274NGC_CST_NODE_KIND_PROGRAM: {
    while (!LEXEME_MATCHES (parser, END_OF_FILE)) {
      if (PARSE_BRANCH_NODE (parser, &branch, LINE))
        continue;

      struct location location =
        find_location (parser.lexer->sv, parser.lexer->lexeme.sv);
      print_location (parser.lexer->filename, location, 0, "failed: " SV_FMT,
                      SV_FMTA (parser.lexer->lexeme.sv));
      for (;;) {
        rs274ngc_lexer_next_lexeme (parser.lexer);
        if (LEXEME_MATCHES (parser, END_OF_FILE)) break;
        if (LEXEME_MATCHES (parser, END_OF_LINE)) {
          rs274ngc_lexer_next_lexeme (parser.lexer);
          break;
        }
      }
    }
  } break;
  case RS274NGC_CST_NODE_KIND_LINE: {
    PARSE_BRANCH_NODE_IF      (parser, &branch, BLOCK_DELETE, SLASH);
    PARSE_BRANCH_NODE_IF_SELF (parser, &branch, LINE_NUMBER);
    while (!LEXEME_MATCHES (parser, END_OF_LINE) &&
           !LEXEME_MATCHES (parser, END_OF_FILE)) {
      if (!PARSE_BRANCH_NODE (parser, &branch, SEGMENT)) goto fail;
    } rs274ngc_lexer_next_lexeme (parser.lexer); // skip END_OF_LINE
  } break;
  case RS274NGC_CST_NODE_KIND_SEGMENT: {
    if (PARSE_BRANCH_NODE_IF (parser, &branch, WORD,              WORD_LETTER   )) break;
    if (PARSE_BRANCH_NODE_IF (parser, &branch, PARAMETER_SETTING, PARAMETER_SIGN)) break;
    if (PARSE_BRANCH_NODE    (parser, &branch, COMMENT                          )) break;
    goto fail;
  } break;
  case RS274NGC_CST_NODE_KIND_WORD: {
    if (!PARSE_BRANCH_NODE_IF_SELF (parser, &branch, WORD_LETTER)) goto fail;
    if (!PARSE_BRANCH_NODE         (parser, &branch, REAL_VALUE )) goto fail;
  } break;
  case RS274NGC_CST_NODE_KIND_PARAMETER_INDEX: {
    if (PARSE_BRANCH_NODE (parser, &branch, REAL_VALUE)) break;
    goto fail;
  } break;
  case RS274NGC_CST_NODE_KIND_REAL_VALUE: {
    if (PARSE_BRANCH_NODE    (parser, &branch, REAL_NUMBER                    )) break;
    if (PARSE_BRANCH_NODE    (parser, &branch, EXPRESSION                     )) break;
    if (PARSE_BRANCH_NODE_IF (parser, &branch, PARAMETER_VALUE, PARAMETER_SIGN)) break;
    if (PARSE_BRANCH_NODE    (parser, &branch, UNARY_COMBO                    )) break;
    goto fail;
  } break;
  case RS274NGC_CST_NODE_KIND_REAL_NUMBER: {
    if (!PARSE_BRANCH_NODE_IF_SELF (parser, &branch, MINUS))
      PARSE_BRANCH_NODE_IF_SELF (parser, &branch, PLUS);
    if (PARSE_BRANCH_NODE (parser, &branch, REAL_NUMBER_NO_SIGN)) break;
    goto fail;
  } break;
  case RS274NGC_CST_NODE_KIND_EXPRESSION: {
    if (!PARSE_BRANCH_NODE (parser, &branch, BRACKET_LEFT)) goto fail;
    if (!PARSE_BRANCH_NODE (parser, &branch, REAL_VALUE  )) goto fail;
    while (!LEXEME_MATCHES (parser, BRACKET_RIGHT)) {
      if (!PARSE_BRANCH_NODE (parser, &branch, BINARY_OPERATION)) goto fail;
      if (!PARSE_BRANCH_NODE (parser, &branch, REAL_VALUE      )) goto fail;
    }
    if (!PARSE_BRANCH_NODE (parser, &branch, BRACKET_RIGHT)) goto fail;
  } break;
  case RS274NGC_CST_NODE_KIND_COMMENT: {
    if (PARSE_BRANCH_NODE (parser, &branch, ORDINARY_COMMENT)) break;
    if (PARSE_BRANCH_NODE (parser, &branch, MESSAGE         )) break;
    goto fail;
  } break;
  case RS274NGC_CST_NODE_KIND_BINARY_OPERATION: {
    if (PARSE_BRANCH_NODE_IF_SELF (parser, &branch, POWER            )) break;
    if (PARSE_BRANCH_NODE_IF_SELF (parser, &branch, TIMES            )) break;
    if (PARSE_BRANCH_NODE_IF      (parser, &branch, DIVIDED_BY, SLASH)) break;
    if (PARSE_BRANCH_NODE_IF_SELF (parser, &branch, MODULO           )) break;
    if (PARSE_BRANCH_NODE_IF_SELF (parser, &branch, PLUS             )) break;
    if (PARSE_BRANCH_NODE_IF_SELF (parser, &branch, MINUS            )) break;
    if (PARSE_BRANCH_NODE_IF_SELF (parser, &branch, EXCLUSIVE_OR     )) break;
    if (PARSE_BRANCH_NODE_IF_SELF (parser, &branch, NON_EXCLUSIVE_OR )) break;
    if (PARSE_BRANCH_NODE_IF_SELF (parser, &branch, AND              )) break;
    goto fail;
  } break;
  case RS274NGC_CST_NODE_KIND_BLOCK_DELETE:
  case RS274NGC_CST_NODE_KIND_DIVIDED_BY: {
    if (PARSE_BRANCH_NODE (parser, &branch, SLASH)) break;
    goto fail;
  } break;
  case RS274NGC_CST_NODE_KIND_PARAMETER_VALUE: {
    if (!PARSE_BRANCH_NODE (parser, &branch, PARAMETER_SIGN)) goto fail;
    if (!PARSE_BRANCH_NODE (parser, &branch, REAL_VALUE    )) goto fail;
  } break;
  case RS274NGC_CST_NODE_KIND_UNARY_COMBO: {
    if (PARSE_BRANCH_NODE (parser, &branch, ORDINARY_UNARY_COMBO)) break;
    if (PARSE_BRANCH_NODE (parser, &branch, ARC_TANGENT_COMBO   )) break;
    goto fail;
  } break;
  case RS274NGC_CST_NODE_KIND_ORDINARY_UNARY_COMBO: {
    do {
      if (PARSE_BRANCH_NODE_IF_SELF (parser, &branch, ABSOLUTE_VALUE)) break;
      if (PARSE_BRANCH_NODE_IF_SELF (parser, &branch, ARC_COSINE    )) break;
      if (PARSE_BRANCH_NODE_IF_SELF (parser, &branch, ARC_SINE      )) break;
      if (PARSE_BRANCH_NODE_IF_SELF (parser, &branch, COSINE        )) break;
      if (PARSE_BRANCH_NODE_IF_SELF (parser, &branch, SINE          )) break;
      if (PARSE_BRANCH_NODE_IF_SELF (parser, &branch, E_RAISED_TO   )) break;
      if (PARSE_BRANCH_NODE_IF_SELF (parser, &branch, NATURAL_LOG_OF)) break;
      if (PARSE_BRANCH_NODE_IF_SELF (parser, &branch, FIX_DOWN      )) break;
      if (PARSE_BRANCH_NODE_IF_SELF (parser, &branch, FIX_UP        )) break;
      if (PARSE_BRANCH_NODE_IF_SELF (parser, &branch, ROUND         )) break;
      if (PARSE_BRANCH_NODE_IF_SELF (parser, &branch, SQUARE_ROOT   )) break;
      if (PARSE_BRANCH_NODE_IF_SELF (parser, &branch, TANGENT       )) break;
    } while (0);
    if (!PARSE_BRANCH_NODE (parser, &branch, EXPRESSION))
      goto fail;
  } break;
  case RS274NGC_CST_NODE_KIND_ARC_TANGENT_COMBO: {
    if (!PARSE_BRANCH_NODE_IF_SELF (parser, &branch, ARC_TANGENT )) goto fail;
    if (!PARSE_BRANCH_NODE    (parser, &branch, EXPRESSION       )) goto fail;
    if (!PARSE_BRANCH_NODE_IF (parser, &branch, DIVIDED_BY, SLASH)) goto fail;
    if (!PARSE_BRANCH_NODE    (parser, &branch, EXPRESSION       )) goto fail;
  } break;
  case RS274NGC_CST_NODE_KIND_PARAMETER_SETTING: {
    if (!PARSE_BRANCH_NODE_IF_SELF (parser, &branch, PARAMETER_SIGN )) goto fail;
    if (!PARSE_BRANCH_NODE         (parser, &branch, PARAMETER_INDEX)) goto fail;
    if (!PARSE_BRANCH_NODE_IF_SELF (parser, &branch, EQUAL_SIGN     )) goto fail;
    if (!PARSE_BRANCH_NODE         (parser, &branch, REAL_VALUE     )) goto fail;
  } break;
  default: return NULL;
  }

  if (branch.count == 0)
    return NULL;
  size_t children_size = branch.count*sizeof (*branch.children);
  struct rs274ngc_cst_branch *ptr =
    arena_reserve (parser.output, sizeof (branch) + children_size, ALIGNOF (*ptr) - 1);
  ASSERT (ptr != NULL);
  ARENA_COPY_FAST (parser.output, branch);
  void *other = arena_coalesce_from_into_fast (parser.temporary, children,
                                                parser.output, children_size);
  ptr->sv.end = RS274NGC_CST_NODE_PTR (ptr->children[ptr->count - 1])->sv.end;

  // struct location location = find_location (parser.lexer->sv, ptr->sv);
  // print_location (parser.lexer->filename, location, 0, "parsed: %s",
  //                 rs274ngc_cst_node_kind_names[kind]);

  return (void *) RS274NGC_CST_TAG_NODE (ptr, kind);

 fail:
  // printf ("Failed to parse: %s\n", rs274ngc_node_kind_names[kind]);
  parser.lexer->cursor = cursor;
  if (branch.count)
    arena_rollback_to (parser.temporary, children);
  return NULL;
}

union rs274ngc_cst_node *
rs274ngc_cst_parse_node (struct rs274ngc_cst_parser parser,
                         enum   rs274ngc_cst_node_kind kind)
{
  return (kind < RS274NGC_CST_TERMINAL_COUNT
          ? (void *) rs274ngc_cst_parse_leaf   (parser, kind)
          : (void *) rs274ngc_cst_parse_branch (parser, kind));
}

/// src/rs274ngc_cst.c ends here