aboutsummaryrefslogtreecommitdiff
path: root/src/parser.y
blob: 07afbe7887e8cabdf9696af507e8aed3b3f5c2c7 (plain) (blame)
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
%{
#include <ctype.h>
#include <err.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#include "lexer.h"
#include "parser.h"
#include "wrapper.h"

static ast_t mkunop(int, ast_t);
static ast_t mkbinop(int, ast_t, ast_t);
static void yyerror(const char *);

extern const char *current_file;
%}

%code requires {
#include "pinocchio.h"
}

%union {
	char   ch;
	ast_t  ast;
	asts_t asts;
}

%define parse.error verbose
%locations

/* Very important that NOT is the first token declared!  Code depends on it. */

%start input
%token NOT AND OR XOR IMPL
%token EQUIV OPAR CPAR EOL
%token<ch> IDENT
%type<ast> expr
%type<asts> line exprs

%left OR
%left AND
%left XOR
%left IMPL EQUIV
%nonassoc NOT

%%

input:
	  %empty
	| input line {
		if ($2.len > 0)
			astprocess($2);
	}
	;

line:
	  EOL       { $$.len = 0; }
	| exprs eol { $$ = $1; }
	;

exprs:
	  expr {
		$$.len = 1;
		$$.cap = 8;
		$$.buf = xmalloc(sizeof(*$$.buf) * $$.cap);
		$$.buf[0] = $1;
	}
	| exprs '|' expr {
		$$ = $1;
		if ($$.len == $$.cap) {
			$$.cap *= 2;
			$$.buf = xrealloc($$.buf, sizeof(*$$.buf) * $$.cap);
		}
		$$.buf[$$.len++] = $3;
	}
	;

expr:
	  IDENT {
		$$.eqn = xmalloc(sizeof(eqn_t));
		$$.eqn->type = IDENT;
		$$.eqn->ch = $1;
		$$.vars = UINT64_C(1) << (islower($1) ? $1-'a'+26 : $1-'A');
	}
	| NOT expr        { $$ = mkunop(NOT,  $2);       }
	| OPAR expr CPAR  { $$ = mkunop(OPAR, $2);       }
	| expr AND   expr { $$ = mkbinop(AND,   $1, $3); }
	| expr OR    expr { $$ = mkbinop(OR,    $1, $3); }
	| expr XOR   expr { $$ = mkbinop(XOR,   $1, $3); }
	| expr IMPL  expr { $$ = mkbinop(IMPL,  $1, $3); }
	| expr EQUIV expr { $$ = mkbinop(EQUIV, $1, $3); }
	;

eol: EOL | YYEOF;

%%

ast_t
mkunop(int op, ast_t rhs)
{
	ast_t a = {
		.eqn  = xmalloc(sizeof(eqn_t)),
		.vars = rhs.vars,
	};
	a.eqn->type = op;
	a.eqn->rhs = rhs.eqn;
	return a;
}

ast_t
mkbinop(int op, ast_t lhs, ast_t rhs)
{
	ast_t a = {
		.eqn  = xmalloc(sizeof(eqn_t)),
		.vars = lhs.vars | rhs.vars,
	};
	a.eqn->type = op;
	a.eqn->lhs = lhs.eqn;
	a.eqn->rhs = rhs.eqn;
	return a;
}

void
yyerror(const char *s)
{
	user_error("%s:%d: %s", current_file, yylloc.first_line, s);
}