Add /deps.sh, in /linapple remove full-screen mode to make it run properly on recent...
[applesoft_basic.git] / applesoft_basic.y
1 /*
2  * Copyright (C) 2022 Nick Downing <nick@ndcode.org>
3  * SPDX-License-Identifier: GPL-2.0-only
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the Free
7  * Software Foundation; version 2.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 51
16  * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 %{
20   import t_def
21   import sys
22 %}
23
24 %locations
25
26 %token DATA_TEXT
27 %token INT_LITERAL
28 %token FLOAT_LITERAL
29 %token OPERATOR_GE
30 %token OPERATOR_LE
31 %token OPERATOR_NE
32 %token REM_TEXT
33 %token STR_LITERAL
34 %token TOKEN_END 128
35 %token TOKEN_FOR 129
36 %token TOKEN_NEXT 130
37 %token TOKEN_DATA 131
38 %token TOKEN_INPUT 132
39 %token TOKEN_DEL 133
40 %token TOKEN_DIM 134
41 %token TOKEN_READ 135
42 %token TOKEN_GR 136
43 %token TOKEN_TEXT 137
44 %token TOKEN_PR_HASH 138
45 %token TOKEN_IN_HASH 139
46 %token TOKEN_CALL 140
47 %token TOKEN_PLOT 141
48 %token TOKEN_HLIN 142
49 %token TOKEN_VLIN 143
50 %token TOKEN_HGR2 144
51 %token TOKEN_HGR 145
52 %token TOKEN_HCOLOR_EQUAL 146
53 %token TOKEN_HPLOT 147
54 %token TOKEN_DRAW 148
55 %token TOKEN_XDRAW 149
56 %token TOKEN_HTAB 150
57 %token TOKEN_HOME 151
58 %token TOKEN_ROT_EQUAL 152
59 %token TOKEN_SCALE_EQUAL 153
60 %token TOKEN_SHLOAD 154
61 %token TOKEN_TRACE 155
62 %token TOKEN_NOTRACE 156
63 %token TOKEN_NORMAL 157
64 %token TOKEN_INVERSE 158
65 %token TOKEN_FLASH 159
66 %token TOKEN_COLOR_EQUAL 160
67 %token TOKEN_POP 161
68 %token TOKEN_VTAB 162
69 %token TOKEN_HIMEM_COLON 163
70 %token TOKEN_LOMEM_COLON 164
71 %token TOKEN_ONERR 165
72 %token TOKEN_RESUME 166
73 %token TOKEN_RECALL 167
74 %token TOKEN_STORE 168
75 %token TOKEN_SPEED_EQUAL 169
76 %token TOKEN_LET 170
77 %token TOKEN_GOTO 171
78 %token TOKEN_RUN 172
79 %token TOKEN_IF 173
80 %token TOKEN_RESTORE 174
81 %token TOKEN_AMPERSAND 175
82 %token TOKEN_GOSUB 176
83 %token TOKEN_RETURN 177
84 %token TOKEN_REM 178
85 %token TOKEN_STOP 179
86 %token TOKEN_ON 180
87 %token TOKEN_WAIT 181
88 %token TOKEN_LOAD 182
89 %token TOKEN_SAVE 183
90 %token TOKEN_DEF 184
91 %token TOKEN_POKE 185
92 %token TOKEN_PRINT 186
93 %token TOKEN_CONT 187
94 %token TOKEN_LIST 188
95 %token TOKEN_CLEAR 189
96 %token TOKEN_GET 190
97 %token TOKEN_NEW 191
98 %token TOKEN_TAB_LPAREN 192
99 %token TOKEN_TO 193
100 %token TOKEN_FN 194
101 %token TOKEN_SPC_LPAREN 195
102 %token TOKEN_THEN 196
103 %token TOKEN_AT 197
104 %token TOKEN_NOT 198
105 %token TOKEN_STEP 199
106 %token TOKEN_PLUS 200
107 %token TOKEN_MINUS 201
108 %token TOKEN_ASTERISK 202
109 %token TOKEN_SLASH 203
110 %token TOKEN_CARET 204
111 %token TOKEN_AND 205
112 %token TOKEN_OR 206
113 %token TOKEN_GREATER_THAN 207
114 %token TOKEN_EQUAL 208
115 %token TOKEN_LESS_THAN 209
116 %token TOKEN_SGN 210
117 %token TOKEN_INT 211
118 %token TOKEN_ABS 212
119 %token TOKEN_USR 213
120 %token TOKEN_FRE 214
121 %token TOKEN_SCRN_LPAREN 215
122 %token TOKEN_PDL 216
123 %token TOKEN_POS 217
124 %token TOKEN_SQR 218
125 %token TOKEN_RND 219
126 %token TOKEN_LOG 220
127 %token TOKEN_EXP 221
128 %token TOKEN_COS 222
129 %token TOKEN_SIN 223
130 %token TOKEN_TAN 224
131 %token TOKEN_ATN 225
132 %token TOKEN_PEEK 226
133 %token TOKEN_LEN 227
134 %token TOKEN_STR_DOLLAR 228
135 %token TOKEN_VAL 229
136 %token TOKEN_ASC 230
137 %token TOKEN_CHR_DOLLAR 231
138 %token TOKEN_LEFT_DOLLAR 232
139 %token TOKEN_RIGHT_DOLLAR 233
140 %token TOKEN_MID_DOLLAR 234
141 %token VARIABLE_NAME
142
143 %left TOKEN_OR
144 %left TOKEN_AND
145 %left TOKEN_LESS_THAN TOKEN_EQUAL TOKEN_GREATER_THAN OPERATOR_GE OPERATOR_LE OPERATOR_NE
146 %left TOKEN_PLUS TOKEN_MINUS
147 %left TOKEN_ASTERISK TOKEN_SLASH
148 %left TOKEN_CARET
149 %right UNARY TOKEN_NOT
150
151 %%
152
153 program
154   :
155   | program %space (?E{t_def.Line}INT_LITERAL statement_list) '\n'
156   | error '\n' {
157     yyerror('Syntax error\n')
158     yyerrok()
159   }
160   ;
161
162 statement_list
163   : statement_opt
164   | statement_list ':' statement_opt
165   ;
166
167 statement_opt
168   :
169   | %space (?E{t_def.StatementEnd}TOKEN_END)
170   | %space (?E{t_def.StatementFor}TOKEN_FOR VARIABLE_NAME TOKEN_EQUAL rvalue TOKEN_TO rvalue)
171   | %space (?E{t_def.StatementFor}TOKEN_FOR VARIABLE_NAME TOKEN_EQUAL rvalue TOKEN_TO rvalue TOKEN_STEP rvalue)
172   | %space (?E{t_def.StatementNext}TOKEN_NEXT)
173   | %space (?E{t_def.StatementNext}TOKEN_NEXT VARIABLE_NAME)
174   | %space (?E{t_def.StatementData}TOKEN_DATA data_item_list)
175   | %space (?E{t_def.StatementInput}TOKEN_INPUT lvalue)
176   | %space (?E{t_def.StatementInput}TOKEN_INPUT STR_LITERAL ';' lvalue)
177   | %space (?E{t_def.StatementDel}TOKEN_DEL)
178   | %space (?E{t_def.StatementDim}TOKEN_DIM dim_item_list)
179   | %space (?E{t_def.StatementRead}TOKEN_READ lvalue_list)
180   | %space (?E{t_def.StatementGr}TOKEN_GR)
181   | %space (?E{t_def.StatementText}TOKEN_TEXT)
182   | %space (?E{t_def.StatementPrHash}TOKEN_PR_HASH rvalue)
183   | %space (?E{t_def.StatementInHash}TOKEN_IN_HASH rvalue)
184   | %space (?E{t_def.StatementCall}TOKEN_CALL rvalue)
185   | %space (?E{t_def.StatementPlot}TOKEN_PLOT rvalue ',' rvalue)
186   | %space (?E{t_def.StatementHLin}TOKEN_HLIN rvalue ',' rvalue TOKEN_AT rvalue)
187   | %space (?E{t_def.StatementVLin}TOKEN_VLIN rvalue ',' rvalue TOKEN_AT rvalue)
188   | %space (?E{t_def.StatementHGR2}TOKEN_HGR2)
189   | %space (?E{t_def.StatementHGR}TOKEN_HGR)
190   | %space (?E{t_def.StatementHColorEqual}TOKEN_HCOLOR_EQUAL rvalue)
191   | %space (?E{t_def.StatementHPlot}TOKEN_HPLOT)
192   | %space (?E{t_def.StatementDraw}TOKEN_DRAW)
193   | %space (?E{t_def.StatementXDraw}TOKEN_XDRAW)
194   | %space (?E{t_def.StatementHTab}TOKEN_HTAB rvalue)
195   | %space (?E{t_def.StatementHome}TOKEN_HOME)
196   | %space (?E{t_def.StatementRotEqual}TOKEN_ROT_EQUAL rvalue)
197   | %space (?E{t_def.StatementScaleEqual}TOKEN_SCALE_EQUAL rvalue)
198   | %space (?E{t_def.StatementShLoad}TOKEN_SHLOAD)
199   | %space (?E{t_def.StatementTrace}TOKEN_TRACE)
200   | %space (?E{t_def.StatementNoTrace}TOKEN_NOTRACE)
201   | %space (?E{t_def.StatementNormal}TOKEN_NORMAL)
202   | %space (?E{t_def.StatementInverse}TOKEN_INVERSE)
203   | %space (?E{t_def.StatementFlash}TOKEN_FLASH)
204   | %space (?E{t_def.StatementColorEqual}TOKEN_COLOR_EQUAL rvalue)
205   | %space (?E{t_def.StatementPop}TOKEN_POP)
206   | %space (?E{t_def.StatementVTab}TOKEN_VTAB rvalue)
207   | %space (?E{t_def.StatementHimemColon}TOKEN_HIMEM_COLON rvalue)
208   | %space (?E{t_def.StatementLomemColon}TOKEN_LOMEM_COLON rvalue)
209   | %space (?E{t_def.StatementOnErr}TOKEN_ONERR TOKEN_GOTO INT_LITERAL)
210   | %space (?E{t_def.StatementResume}TOKEN_RESUME)
211   | %space (?E{t_def.StatementRecall}TOKEN_RECALL)
212   | %space (?E{t_def.StatementStore}TOKEN_STORE)
213   | %space (?E{t_def.StatementSpeedEqual}TOKEN_SPEED_EQUAL rvalue)
214   | %space (?E{t_def.StatementLet}lvalue TOKEN_EQUAL rvalue)
215   | %space (?E{t_def.StatementLet}TOKEN_LET lvalue TOKEN_EQUAL rvalue)
216   | %space (?E{t_def.StatementGoto}TOKEN_GOTO INT_LITERAL)
217   | %space (?E{t_def.StatementRun}TOKEN_RUN)
218   | %space (?E{t_def.StatementIf}TOKEN_IF rvalue TOKEN_THEN) statement_opt
219   | %space (?E{t_def.StatementIf}TOKEN_IF rvalue TOKEN_THEN) %space (?E{t_def.StatementGoto}INT_LITERAL)
220   | %space (?E{t_def.StatementRestore}TOKEN_RESTORE)
221   | %space (?E{t_def.StatementAmpersand}TOKEN_AMPERSAND)
222   | %space (?E{t_def.StatementGosub}TOKEN_GOSUB INT_LITERAL)
223   | %space (?E{t_def.StatementReturn}TOKEN_RETURN)
224   | %space (?E{t_def.StatementRem}TOKEN_REM REM_TEXT)
225   | %space (?E{t_def.StatementStop}TOKEN_STOP)
226   | %space (?E{t_def.StatementOnGoto}TOKEN_ON rvalue TOKEN_GOTO int_literal_list_opt)
227   | %space (?E{t_def.StatementOnGosub}TOKEN_ON rvalue TOKEN_GOSUB int_literal_list_opt)
228   | %space (?E{t_def.StatementWait}TOKEN_WAIT)
229   | %space (?E{t_def.StatementLoad}TOKEN_LOAD)
230   | %space (?E{t_def.StatementSave}TOKEN_SAVE)
231   | %space (?E{t_def.StatementDef}TOKEN_DEF)
232   | %space (?E{t_def.StatementPoke}TOKEN_POKE rvalue ',' rvalue)
233   | %space (?E{t_def.StatementPrint, semicolon = False}TOKEN_PRINT print_item_list0)
234   | %space (?E{t_def.StatementPrint, semicolon = True}TOKEN_PRINT print_item_list1)
235   | %space (?E{t_def.StatementCont}TOKEN_CONT)
236   | %space (?E{t_def.StatementList}TOKEN_LIST)
237   | %space (?E{t_def.StatementClear}TOKEN_CLEAR)
238   | %space (?E{t_def.StatementGet}TOKEN_GET lvalue)
239   | %space (?E{t_def.StatementNew}TOKEN_NEW)
240   ;
241
242 print_item_list0
243   :
244   | print_item_list0 print_item
245   | print_item_list1 print_item
246   ;
247
248 print_item_list1
249   : print_item_list0 ';'
250   | print_item_list1 ';'
251   ;
252
253 print_item
254   : rvalue
255   | %space (?E{t_def.RValueTabLParen}TOKEN_TAB_LPAREN rvalue ')')
256   ;
257
258 lvalue_list
259   : lvalue
260   | lvalue_list ',' lvalue
261   ;
262
263 int_literal_list_opt
264   :
265   | int_literal_list
266   ;
267
268 int_literal_list
269   : INT_LITERAL
270   | int_literal_list ',' INT_LITERAL
271   ;
272
273 data_item_list
274   : data_item
275   | data_item_list ',' data_item
276   ;
277
278 data_item
279   : (?E{t_def.DataText} %empty)
280   | DATA_TEXT
281   | STR_LITERAL
282   ;
283
284 dim_item_list
285   : dim_item
286   | dim_item_list ',' dim_item
287   ;
288
289 dim_item
290   : %space (?E{t_def.DimItem}VARIABLE_NAME '(' rvalue_list ')')
291   ;
292
293 rvalue_list
294   : rvalue
295   | rvalue_list ',' rvalue
296   ;
297
298 rvalue
299   : lvalue
300   | '(' rvalue ')'
301   | %space (?E{t_def.RValueStrLiteral}STR_LITERAL)
302   | %space (?E{t_def.RValueFloatLiteral}FLOAT_LITERAL)
303   | %space (?E{t_def.RValueIntLiteral}INT_LITERAL)
304   | %space (?E{t_def.RValueSpcLParen}TOKEN_SPC_LPAREN rvalue ')')
305   | %space (?E{t_def.RValueNot}TOKEN_NOT rvalue)
306   | %space (?E{t_def.RValueSign, sign = 1}TOKEN_PLUS rvalue) %prec UNARY
307   | %space (?E{t_def.RValueSign, sign = -1}TOKEN_MINUS rvalue) %prec UNARY
308   | %space (?E{t_def.RValueAdd}rvalue TOKEN_PLUS rvalue)
309   | %space (?E{t_def.RValueSubtract}rvalue TOKEN_MINUS rvalue)
310   | %space (?E{t_def.RValueMultiply}rvalue TOKEN_ASTERISK rvalue)
311   | %space (?E{t_def.RValueDivide}rvalue TOKEN_SLASH rvalue)
312   | %space (?E{t_def.RValuePower}rvalue TOKEN_CARET rvalue)
313   | %space (?E{t_def.RValueAnd}rvalue TOKEN_AND rvalue)
314   | %space (?E{t_def.RValueOr}rvalue TOKEN_OR rvalue)
315   | %space (?E{t_def.RValueGT}rvalue TOKEN_GREATER_THAN rvalue)
316   | %space (?E{t_def.RValueEqual}rvalue TOKEN_EQUAL rvalue)
317   | %space (?E{t_def.RValueLT}rvalue TOKEN_LESS_THAN rvalue)
318   | %space (?E{t_def.RValueGE}rvalue OPERATOR_GE rvalue)
319   | %space (?E{t_def.RValueLE}rvalue OPERATOR_LE rvalue)
320   | %space (?E{t_def.RValueNE}rvalue OPERATOR_NE rvalue)
321   | %space (?E{t_def.RValueSgn}TOKEN_SGN '(' rvalue ')')
322   | %space (?E{t_def.RValueInt}TOKEN_INT '(' rvalue ')')
323   | %space (?E{t_def.RValueAbs}TOKEN_ABS '(' rvalue ')')
324   | %space (?E{t_def.RValueUsr}TOKEN_USR '(' rvalue ')' ',' STR_LITERAL)
325   | %space (?E{t_def.RValueFre}TOKEN_FRE '(' rvalue ')')
326   | %space (?E{t_def.RValueScrnLParen}TOKEN_SCRN_LPAREN rvalue ',' rvalue ')')
327   | %space (?E{t_def.RValuePdl}TOKEN_PDL '(' rvalue ')')
328   | %space (?E{t_def.RValuePos}TOKEN_POS '(' rvalue ')')
329   | %space (?E{t_def.RValueSqr}TOKEN_SQR '(' rvalue ')')
330   | %space (?E{t_def.RValueRnd}TOKEN_RND '(' rvalue ')')
331   | %space (?E{t_def.RValueLog}TOKEN_LOG '(' rvalue ')')
332   | %space (?E{t_def.RValueExp}TOKEN_EXP '(' rvalue ')')
333   | %space (?E{t_def.RValueCos}TOKEN_COS '(' rvalue ')')
334   | %space (?E{t_def.RValueSin}TOKEN_SIN '(' rvalue ')')
335   | %space (?E{t_def.RValueTan}TOKEN_TAN '(' rvalue ')')
336   | %space (?E{t_def.RValueAtn}TOKEN_ATN '(' rvalue ')')
337   | %space (?E{t_def.RValuePeek}TOKEN_PEEK '(' rvalue ')')
338   | %space (?E{t_def.RValueLen}TOKEN_LEN '(' rvalue ')')
339   | %space (?E{t_def.RValueStrDollar}TOKEN_STR_DOLLAR '(' rvalue ')')
340   | %space (?E{t_def.RValueVal}TOKEN_VAL '(' rvalue ')')
341   | %space (?E{t_def.RValueAsc}TOKEN_ASC '(' rvalue ')')
342   | %space (?E{t_def.RValueChrDollar}TOKEN_CHR_DOLLAR '(' rvalue ')')
343   | %space (?E{t_def.RValueLeftDollar}TOKEN_LEFT_DOLLAR '(' rvalue ',' rvalue ')')
344   | %space (?E{t_def.RValueRightDollar}TOKEN_RIGHT_DOLLAR '(' rvalue ',' rvalue ')')
345   | %space (?E{t_def.RValueMidDollar}TOKEN_MID_DOLLAR '(' rvalue ',' rvalue ')')
346   | %space (?E{t_def.RValueMidDollar}TOKEN_MID_DOLLAR '(' rvalue ',' rvalue ',' rvalue ')')
347   ;
348
349 lvalue
350   : %space (?E{t_def.LValueVariable}VARIABLE_NAME)
351   | %space (?E{t_def.LValueArray}VARIABLE_NAME '(' rvalue_list ')')
352   ;
353 %%
354
355 def yyerror(s):
356   sys.stdout.write('{0:s}\n'.format(s))
357   sys.exit(1)