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
|
%{
#include "ast.h"
#include "lexer.h"
#include <stdio.h>
struct Node* program_root = NULL;
void yyerror(const char* message);
%}
%union
{
char character;
struct Node* node;
}
%token TOK_COLONCOLON
%token TOK_EMIT
%token TOK_COMMIT
%token TOK_DECLTYPE
%token TOK_DIFFERENCE
%token TOK_SUM
%token TOK_PRODUCT
%token TOK_QUOTIENT
%token <character> TOK_CHAR
%type <node> statements statement type value
%%
program
: statements
{ program_root = $1; }
;
statements
: /* nothing */
{ $$ = NULL; }
| statements statement ';'
{ $$ = node_append($1, $2); }
;
statement
: TOK_COLONCOLON TOK_EMIT '<' type '>' TOK_COLONCOLON TOK_COMMIT
{ $$ = node_unary(STMT_EMIT, $4); }
;
type
: TOK_COLONCOLON TOK_DECLTYPE '(' value ')'
{ $$ = node_unary(TYPE_DECLTYPE, $4); }
;
value
: TOK_COLONCOLON TOK_DIFFERENCE '<' TOK_CHAR ',' TOK_CHAR '>'
{ $$ = node_binary(VALUE_DIFFERENCE, node_char($4), node_char($6)); }
| TOK_COLONCOLON TOK_SUM '<' value ',' value '>'
{ $$ = node_binary(VALUE_SUM, $4, $6); }
| TOK_COLONCOLON TOK_PRODUCT '<' value ',' value '>'
{ $$ = node_binary(VALUE_PRODUCT, $4, $6); }
| TOK_COLONCOLON TOK_QUOTIENT '<' value ',' value '>'
{ $$ = node_binary(VALUE_QUOTIENT, $4, $6); }
;
%%
void yyerror(const char* message)
{
if (!lexer_failed())
{
(void)fprintf(stderr, "line %d: %s, did you forget to commit\n", yylineno, message);
}
}
|