-
Notifications
You must be signed in to change notification settings - Fork 4
/
Apl.lhs
346 lines (293 loc) · 11.5 KB
/
Apl.lhs
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
\documentclass[12pt]{article}
\usepackage{verbatim}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{latexsym}
\usepackage{fontspec}
%\setmainfont[Ligatures=TeX]{DejaVu}
\setmainfont[Ligatures=TeX]{Linux Libertine O}
\newfontfamily\mathfont{Asana Math}
\newfontfamily\fallbackfont{DejaVu Sans Mono}
\usepackage{newunicodechar}
\newunicodechar{⋈}{\fallbackfont ⋈}
\newunicodechar{⨝}{\fallbackfont ⨝}
\newunicodechar{∣}{\fallbackfont ∣}
\newunicodechar{☠}{\fallbackfont ☠}
\newunicodechar{⌈}{\mathfont ⌈}
\newunicodechar{⌊}{\mathfont ⌊}
\newunicodechar{<}{\mathfont <}
\newunicodechar{≤}{\mathfont ≤}
\newunicodechar{=}{\mathfont =}
\newunicodechar{≥}{\mathfont ≥}
\newunicodechar{>}{\mathfont >}
\newunicodechar{≠}{\mathfont ≠}
\newfontfamily\mathfont{Asana Math}
\newfontfamily\aplfont{DejaVu Sans Mono}
%\newfontfamily\aplfont{APL385}
% \include{format-apl}
%format α = "\alpha"
%format ω = "\omega"
%format f1 = "f_{1}"
%format f2 = "f_{2}"
% %format /: = "\text{ /}_2\text{ }"
%format ⍳. = "\text{ ⍳ }"
%format ⍳: = "\text{ ⍳}_2\ "
%format ⋆. = "\text{ ⋆ }"
%format ⋆: = "\text{ ⋆}_2\ "
%format ⍴ = "\text{ ⍴ }"
%format ⍴. = "\text{ ⍴ }"
%format ⍴: = "\text{ ⍴}_2\text{ }"
%format ⌽. = "\text{ ⌽ }"
%format ⌽: = "\text{ ⌽}_2\text{ }"
%format ⌈. = "\text{ ⌈ }"
%format ⌈: = "\text{ ⌈}_2\text{ }"
%format ⌊. = "\text{ ⌊ }"
%format ⌊: = "\text{ ⌊}_2\text{ }"
%format ∈. = "\text{ ∈ }"
%format ∈: = "\text{ ∈}_2\text{ }"
%format ⍟. = "\text{ ⍟ }"
%format ⍟: = "\text{ ⍟}_2\text{ }"
%format ○. = "\text{ ○ }"
%format ○: = "\text{ ○}_2\text{ }"
%format ↑. = "\text{ ↑ }"
%format ↑: = "\text{ ↑}_2\text{ }"
%format ↓. = "\text{ ↓ }"
%format ↓: = "\text{ ↓}_2\text{ }"
%format ^. = "\text{ , }"
%format ^: = "\text{ ,}_2\text{ }"
%format ∣. = "\text{ | }"
%format ∣: = "\text{ |}_2\text{ }"
%auto-gen
%format ⌶. = "\text{ ⌶ }"
%format ⌶: = "\text{ ⌶}_2\text{ }"
%format ⌷. = "\text{ ⌷ }"
%format ⌷: = "\text{ ⌷}_2\text{ }"
%format ⌸. = "\text{ ⌸ }"
%format ⌸: = "\text{ ⌸}_2\text{ }"
%format ⌹. = "\text{ ⌹ }"
%format ⌹: = "\text{ ⌹}_2\text{ }"
%format ⌺. = "\text{ ⌺ }"
%format ⌺: = "\text{ ⌺}_2\text{ }"
%format ⌻. = "\text{ ⌻ }"
%format ⌻: = "\text{ ⌻}_2\text{ }"
%format ⌼. = "\text{ ⌼ }"
%format ⌼: = "\text{ ⌼}_2\text{ }"
%format ⌽. = "\text{ ⌽ }"
%format ⌽: = "\text{ ⌽}_2\text{ }"
%format ⌾. = "\text{ ⌾ }"
%format ⌾: = "\text{ ⌾}_2\text{ }"
%format ⌿. = "\text{ ⌿ }"
%format ⌿: = "\text{ ⌿}_2\text{ }"
%format ⍀. = "\text{ ⍀ }"
%format ⍀: = "\text{ ⍀}_2\text{ }"
%format ⍁. = "\text{ ⍁ }"
%format ⍁: = "\text{ ⍁}_2\text{ }"
%format ⍂. = "\text{ ⍂ }"
%format ⍂: = "\text{ ⍂}_2\text{ }"
%format ⍃. = "\text{ ⍃ }"
%format ⍃: = "\text{ ⍃}_2\text{ }"
%format ⍄. = "\text{ ⍄ }"
%format ⍄: = "\text{ ⍄}_2\text{ }"
%format ⍅. = "\text{ ⍅ }"
%format ⍅: = "\text{ ⍅}_2\text{ }"
%format ⍆. = "\text{ ⍆ }"
%format ⍆: = "\text{ ⍆}_2\text{ }"
%format ⍇. = "\text{ ⍇ }"
%format ⍇: = "\text{ ⍇}_2\text{ }"
%format ⍈. = "\text{ ⍈ }"
%format ⍈: = "\text{ ⍈}_2\text{ }"
%format ⍉. = "\text{ ⍉ }"
%format ⍉: = "\text{ ⍉}_2\text{ }"
%format ⍊. = "\text{ ⍊ }"
%format ⍊: = "\text{ ⍊}_2\text{ }"
%format ⍋. = "\text{ ⍋ }"
%format ⍋: = "\text{ ⍋}_2\text{ }"
%format ⍌. = "\text{ ⍌ }"
%format ⍌: = "\text{ ⍌}_2\text{ }"
%format ⍍. = "\text{ ⍍ }"
%format ⍍: = "\text{ ⍍}_2\text{ }"
%format ⍎. = "\text{ ⍎ }"
%format ⍎: = "\text{ ⍎}_2\text{ }"
%format ⍏. = "\text{ ⍏ }"
%format ⍏: = "\text{ ⍏}_2\text{ }"
%format ⍐. = "\text{ ⍐ }"
%format ⍐: = "\text{ ⍐}_2\text{ }"
%format ⍑. = "\text{ ⍑ }"
%format ⍑: = "\text{ ⍑}_2\text{ }"
%format ⍒. = "\text{ ⍒ }"
%format ⍒: = "\text{ ⍒}_2\text{ }"
%format ⍓. = "\text{ ⍓ }"
%format ⍓: = "\text{ ⍓}_2\text{ }"
%format ⍔. = "\text{ ⍔ }"
%format ⍔: = "\text{ ⍔}_2\text{ }"
%format ⍕. = "\text{ ⍕ }"
%format ⍕: = "\text{ ⍕}_2\text{ }"
%format ⍖. = "\text{ ⍖ }"
%format ⍖: = "\text{ ⍖}_2\text{ }"
%format ⍗. = "\text{ ⍗ }"
%format ⍗: = "\text{ ⍗}_2\text{ }"
%format ⍘. = "\text{ ⍘ }"
%format ⍘: = "\text{ ⍘}_2\text{ }"
%format ⍙. = "\text{ ⍙ }"
%format ⍙: = "\text{ ⍙}_2\text{ }"
%format ⍚. = "\text{ ⍚ }"
%format ⍚: = "\text{ ⍚}_2\text{ }"
%format ⍛. = "\text{ ⍛ }"
%format ⍛: = "\text{ ⍛}_2\text{ }"
%format ⍜. = "\text{ ⍜ }"
%format ⍜: = "\text{ ⍜}_2\text{ }"
%format ⍝. = "\text{ ⍝ }"
%format ⍝: = "\text{ ⍝}_2\text{ }"
%format ⍞. = "\text{ ⍞ }"
%format ⍞: = "\text{ ⍞}_2\text{ }"
%format ⍟. = "\text{ ⍟ }"
%format ⍟: = "\text{ ⍟}_2\text{ }"
%format ⍠. = "\text{ ⍠ }"
%format ⍠: = "\text{ ⍠}_2\text{ }"
%format ⍡. = "\text{ ⍡ }"
%format ⍡: = "\text{ ⍡}_2\text{ }"
%format ⍢. = "\text{ ⍢ }"
%format ⍢: = "\text{ ⍢}_2\text{ }"
%format ⍣. = "\text{ ⍣ }"
%format ⍣: = "\text{ ⍣}_2\text{ }"
%format ⍤. = "\text{ ⍤ }"
%format ⍤: = "\text{ ⍤}_2\text{ }"
%format ⍥. = "\text{ ⍥ }"
%format ⍥: = "\text{ ⍥}_2\text{ }"
%format ⍦. = "\text{ ⍦ }"
%format ⍦: = "\text{ ⍦}_2\text{ }"
%format ⍧. = "\text{ ⍧ }"
%format ⍧: = "\text{ ⍧}_2\text{ }"
%format ⍨. = "\text{ ⍨ }"
%format ⍨: = "\text{ ⍨}_2\text{ }"
%format ⍩. = "\text{ ⍩ }"
%format ⍩: = "\text{ ⍩}_2\text{ }"
%format ⍪. = "\text{ ⍪ }"
%format ⍪: = "\text{ ⍪}_2\text{ }"
%format ⍫. = "\text{ ⍫ }"
%format ⍫: = "\text{ ⍫}_2\text{ }"
%format ⍬. = "\text{ ⍬ }"
%format ⍬: = "\text{ ⍬}_2\text{ }"
%format ⍭. = "\text{ ⍭ }"
%format ⍭: = "\text{ ⍭}_2\text{ }"
%format ⍮. = "\text{ ⍮ }"
%format ⍮: = "\text{ ⍮}_2\text{ }"
%format ⍯. = "\text{ ⍯ }"
%format ⍯: = "\text{ ⍯}_2\text{ }"
%format ⍰. = "\text{ ⍰ }"
%format ⍰: = "\text{ ⍰}_2\text{ }"
%format ⍱. = "\text{ ⍱ }"
%format ⍱: = "\text{ ⍱}_2\text{ }"
%format ⍲. = "\text{ ⍲ }"
%format ⍲: = "\text{ ⍲}_2\text{ }"
%format ⍳. = "\text{ ⍳ }"
%format ⍳: = "\text{ ⍳}_2\text{ }"
%format ⍴. = "\text{ ⍴ }"
%format ⍴: = "\text{ ⍴}_2\text{ }"
%format ⍵. = "\text{ ⍵ }"
%format ⍵: = "\text{ ⍵}_2\text{ }"
%format ⍶. = "\text{ ⍶ }"
%format ⍶: = "\text{ ⍶}_2\text{ }"
%format ⍷. = "\text{ ⍷ }"
%format ⍷: = "\text{ ⍷}_2\text{ }"
%format ⍸. = "\text{ ⍸ }"
%format ⍸: = "\text{ ⍸}_2\text{ }"
%format ⍹. = "\text{ ⍹ }"
%format ⍹: = "\text{ ⍹}_2\text{ }"
%format ⍺. = "\text{ ⍺ }"
%format ⍺: = "\text{ ⍺}_2\text{ }"
%include lhs2TeX.sty
%include polycode.fmt
\input{unicode-char-apl}
%format `union` = "\cup"
%format alpha = "\alpha"
%format gamma = "\gamma"
%format delta = "\delta"
%format capGamma = "\Gamma"
%format tau = "\tau"
%format tau1 = "\tau_{1}"
%format tau2 = "\tau_{2}"
%format tau11 = "\tau_{11}"
%format tau12 = "\tau_{12}"
%format t12 = "t_{12}"
%format t1 = "t_{1}"
%format t1' = "t_{1}^{\prime}"
%format t2 = "t_{2}"
%format t2' = "t_{2}^{\prime}"
%format t3 = "t_{3}"
%format nv1 = "nv_{1}"
\title{APL Interpreter\\☠ ☠}
\author{\textsc{M. Barney}}
\date{\today}
\begin{document}
\maketitle
\section{Introduction}
This is a Haskell implementation of an APL interpreter. The paper, and code, is divided up into several sections, where I discuss issues in implementation, design decisions, and give function specifications.
\section{Inputting APL}
APL is famous for its non-standard character set, of which a few are:
\begin{center}
⌶ ⌷ ⌸ ⌹ ⌺ ⌻ ⌼ ⌽ ⌾\\
⌿ ⍀ ⍁ ⍂ ⍃ ⍄ ⍅ ⍆ ⍇\\
⍈ ⍉ ⍊ ⍋ ⍌ ⍍ ⍎ ⍏ ⍐\\
⍑ ⍒ ⍓ ⍔ ⍕ ⍖ ⍗ ⍘ ⍙\\
⍚ ⍛ ⍜ ⍝ ⍞ ⍟ ⍠ ⍡ ⍢\\
⍣ ⍤ ⍥ ⍦ ⍧ ⍨ ⍩ ⍪ ⍫\\
⍬ ⍭ ⍮ ⍯ ⍰ ⍱ ⍲ ⍳ ⍴\\
⍵ ⍶ ⍷ ⍸ ⍹ ⍺ ← → ⋆
\end{center}
Before beginning to write an APL parser, interpreter, etc., I had to first decide on how to even \emph{input} APL. My eventual decision was to write an X11 xkb symbol file. For the uninitiated this is a text file for GNU/Linux Xorg distributions which allow the user to specify different keyboard input methods.
If you select a different keyboard setting via a GUI in gnome or KDE, it is modifying or referencing this file.
This was probably the most tedious part of my project --- it took a substantial amount of time to look up the symbols unicode hexadecimal value; to make sure it was the correct symbol (Unicode is huge, and sometimes symbols look almost the same); and to manually type it into the keyboard configuration.
A typical line in an X11 keyboard configuration looks as follows:
\begin{verbatim}
key <AD10> { [ p, P, U22C6, U235F ] };
\end{verbatim}
In this case, the keyboard key for `p' (and uppercase `P' with a standard shift) is enhanced with two more possibilities via an {\tt ISO\_level3\_Shift}. This key is sometimes the right alt key (also known as altgr) on many keyboards, but can be remapped to anything.
Mine is the menu key because I don't even know what that key is for anyway.
Assuming my keyboard configuration has been loaded\footnotemark, in our example above, holding down the {\tt ISO\_level3\_Shift} and pressing the `p' key will result in the symbol `⋆' (the APL function for exponentiation). Similarly, holding a regular shift and {\tt ISO\_level3\_Shift} and finally the `p' key will result in `⍟' (the APL function for logarithm).\footnotetext{In GNU/Linux, from the commandline, this can usually be accomplished by the command: {\tt setxkbmap -layout us -variant apl}}
Wasn't that fun?
%include Interface.lhs
%include Functions.lhs
%include Parser.lhs
\section{Main}
This is the main module which starts a simple REPL loop that scans, parses and evaluates basic APL expressions, while catching exceptions raised during evaluation.
\begin{comment}
\begin{code}
{- пиздец всему фашизму -}
{-# LANGUAGE
OverloadedStrings,
FlexibleInstances,
ForeignFunctionInterface #-}
module Main where
import Control.Monad
import Foreign.C
import System.Random
import System.IO
import Control.Exception
import Data.Char (isSpace)
import Interface
import Functions
import Parser
\end{code}
\end{comment}
\begin{code}
simpleIgnoreLoop = do
putStr "<λ> "
hFlush stdout
expr <- getLine
if (expr == [] || (and $ map isSpace expr)) then
simpleIgnoreLoop
else
do
let tokens = scan expr
result <- try (evaluate (eval tokens))::
IO (Either SomeException (APL Float))
case result of
Left errorMesg -> print errorMesg
Right answer -> print answer
simpleIgnoreLoop
main = do
putStrLn "Astoundingly Profound Language Interpreter, version 0.1"
simpleIgnoreLoop
\end{code}
\end{document}