-
Notifications
You must be signed in to change notification settings - Fork 2
/
gram.y
155 lines (141 loc) · 1.61 KB
/
gram.y
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
%term IF FOR IN
%term WORD REDIR
%left IF FOR LOAD
%left ANDAND OROR
%left '|'
%{
package main
import (
"github.com/lufia/qsh/ast"
"github.com/lufia/qsh/build"
)
%}
%union{
tree *ast.Node
}
%type<tree> line block body cmdsa cmdsan assign redir
%type<tree> cmd simple first word comword words
%type<tree> WORD REDIR
%%
stmt:
{
return 1
}
| line '\n'
{
if *flagDebug {
ast.Dump($1)
}
build.Compile($1)
return 0
}
line:
cmd
| cmdsa line
{
$$ = ast.New(ast.LIST, $1, $2)
}
body:
cmd
| cmdsan body
{
$$ = ast.New(ast.LIST, $1, $2)
}
cmdsa:
cmd ';'
| cmd '&'
{
$$ = ast.Async($1)
}
cmdsan:
cmdsa
| cmd '\n'
block:
'{' body '}'
{
$$ = ast.Block($2)
}
assign:
first '=' word
{
$$ = ast.Assign($1, $3)
}
cmd:
{
$$ = nil
}
| IF block block
{
$$ = ast.New(ast.IF, $2, $3)
}
| FOR word IN words block
{
p := ast.New(ast.LIST, $2, $4)
$$ = ast.New(ast.FOR, p, $5)
}
| LOAD word
{
$$ = ast.Load($2)
}
| simple
{
$$ = ast.Simple($1)
}
| cmd ANDAND cmd
{
$$ = ast.New(ast.ANDAND, $1, $3)
}
| cmd OROR cmd
{
$$ = ast.New(ast.OROR, $1, $3)
}
| cmd '|' cmd
{
$$ = ast.New(ast.PIPE, $1, $3)
}
| assign
simple:
first
| simple word
{
$$ = ast.New(ast.LIST, $1, $2)
}
| simple redir
{
$$ = ast.New(ast.LIST, $1, $2)
}
redir:
REDIR word
{
$$ = ast.Redirect($1, $2)
}
first:
comword
word:
comword
comword:
'$' word
{
$$ = ast.Var($2)
}
| '(' words ')'
{
$$ = ast.Tuple($2)
}
| '$' '{' words '}'
{
$$ = ast.Module($3)
}
| WORD
words:
{
$$ = nil
}
| words word
{
if $1 == nil {
$$ = $2
} else {
$$ = ast.New(ast.LIST, $1, $2)
}
}