forked from russlank/lang-forge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraw.lf
More file actions
204 lines (194 loc) · 6.12 KB
/
Copy pathdraw.lf
File metadata and controls
204 lines (194 loc) · 6.12 KB
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
%target c
%package draw
%semantic c mode reducer
%semantic c type Program draw_program *
%semantic c type Statements draw_statement_list *
%semantic c type StatementTail draw_statement_list *
%semantic c type Statement draw_statement *
%semantic c type CanvasStatement draw_statement *
%semantic c type BackgroundStatement draw_statement *
%semantic c type StyleStatement draw_statement *
%semantic c type DefinitionStatement draw_statement *
%semantic c type DrawStatement draw_statement *
%semantic c type FigureReference draw_figure_ref *
%semantic c type FigureBlock draw_figure_block *
%semantic c type Figures draw_statement_list *
%semantic c type FigureTail draw_statement_list *
%semantic c type Figure draw_statement *
%semantic c type Primitive draw_statement *
%semantic c type ColorValue draw_color *
%semantic c type Expr draw_expr *
%semantic c type ExprTail draw_binary_tail_list *
%semantic c type Term draw_expr *
%semantic c type TermTail draw_binary_tail_list *
%semantic c type Unary draw_expr *
%semantic c type Primary draw_expr *
%start Program
%token Canvas Background Stroke Fill None Width Draw RepDraw Circle Line Box Point Func Ident Number Color Plus Minus Star Slash LParen RParen Assign Comma Semi
%% lexer
DIGIT = [0-9];
HEX = [0-9A-Fa-f];
IDENT = [A-Za-z_] [A-Za-z0-9_]*;
NUMBER = DIGIT+ ( "." DIGIT+ )?;
COLOR = "#" HEX HEX HEX HEX HEX HEX;
LINE_COMMENT = "//" ( [1-9] | [11-12] | [14-127] )*;
BLOCK_COMMENT = "/" \* ( [1-41] | [43-127] )* \* "/";
"canvas" => token(Canvas);
"background" => token(Background);
"stroke" => token(Stroke);
"fill" => token(Fill);
"none" => token(None);
"width" => token(Width);
"draw" => token(Draw);
"repdraw" => token(RepDraw);
"circle" => token(Circle);
"line" => token(Line);
"box" => token(Box);
"point" => token(Point);
"sin" => token(Func);
"cos" => token(Func);
"tan" => token(Func);
"ln" => token(Func);
"sqrt" => token(Func);
"sqr" => token(Func);
"exp" => token(Func);
"=" => token(Assign);
"+" => token(Plus);
"-" => token(Minus);
"*" => token(Star);
"/" => token(Slash);
"(" => token(LParen);
")" => token(RParen);
"," => token(Comma);
";" => token(Semi);
COLOR => token(Color);
NUMBER => token(Number);
IDENT => token(Ident);
LINE_COMMENT => skip;
BLOCK_COMMENT => skip;
[1-32]+ => skip;
%% parser
Program : statements=Statements
{c: program}
;
Statements : head=Statement tail=StatementTail
{c: statements}
;
StatementTail : Semi head=Statement tail=StatementTail
{c: statementTail.more}
| Semi
{c: statementTail.empty}
| %empty
{c: statementTail.empty}
;
Statement : value=CanvasStatement
{c: pass}
| value=BackgroundStatement
{c: pass}
| value=StyleStatement
{c: pass}
| value=DefinitionStatement
{c: pass}
| value=DrawStatement
{c: pass}
| value=Primitive
{c: pass}
;
CanvasStatement : Canvas width=Expr Comma height=Expr
{c: canvas}
;
BackgroundStatement : Background color=ColorValue
{c: background}
;
StyleStatement : Stroke color=ColorValue
{c: stroke}
| Fill color=ColorValue
{c: fill}
| Fill None
{c: fill.none}
| Width value=Expr
{c: width}
;
DefinitionStatement : name=Ident Assign value=Expr
{c: assign}
| name=Ident Assign figure=FigureBlock
{c: defineFigure}
;
DrawStatement : Draw target=FigureReference
{c: draw}
| RepDraw count=Expr target=FigureReference
{c: repdraw}
;
FigureReference : name=Ident
{c: figureRef.named}
| figure=FigureBlock
{c: figureRef.inline}
;
FigureBlock : LParen statements=Figures RParen
{c: figureBlock}
;
Figures : head=Figure tail=FigureTail
{c: figures}
;
FigureTail : Semi head=Figure tail=FigureTail
{c: figureTail.more}
| Semi
{c: figureTail.empty}
| %empty
{c: figureTail.empty}
;
Figure : value=StyleStatement
{c: pass}
| value=DefinitionStatement
{c: pass}
| value=DrawStatement
{c: pass}
| value=Primitive
{c: pass}
;
Primitive : Point x=Expr Comma y=Expr
{c: primitive.point}
| Line x1=Expr Comma y1=Expr Comma x2=Expr Comma y2=Expr
{c: primitive.line}
| Box x1=Expr Comma y1=Expr Comma x2=Expr Comma y2=Expr
{c: primitive.box}
| Circle cx=Expr Comma cy=Expr Comma radius=Expr
{c: primitive.circle}
;
ColorValue : literal=Color
{c: color}
;
Expr : left=Term tail=ExprTail
{c: expr}
;
ExprTail : Plus right=Term tail=ExprTail
{c: exprTail.add}
| Minus right=Term tail=ExprTail
{c: exprTail.subtract}
| %empty
{c: exprTail.empty}
;
Term : left=Unary tail=TermTail
{c: term}
;
TermTail : Star right=Unary tail=TermTail
{c: termTail.multiply}
| Slash right=Unary tail=TermTail
{c: termTail.divide}
| %empty
{c: termTail.empty}
;
Unary : Minus operand=Unary
{c: unary.negate}
| value=Primary
{c: expr.pass}
;
Primary : token=Number
{c: number}
| name=Ident
{c: variable}
| function=Func LParen argument=Expr RParen
{c: call}
| LParen value=Expr RParen
{c: group}
;