-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspecs.tex
More file actions
632 lines (581 loc) · 28.3 KB
/
specs.tex
File metadata and controls
632 lines (581 loc) · 28.3 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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[letterpaper, portrait, margin=1in]{geometry} % Page margins %
\usepackage{graphicx}
\usepackage{amsmath,amssymb}
\usepackage{forest}
% formatted code
\usepackage{xcolor}
\usepackage[defaultmono]{droidmono}
\usepackage{listings}
\usepackage{parskip}
\lstdefinelanguage{FSharp}%
{morekeywords={let, new, match, with, rec, open, module, namespace, type, of, member, %
and, for, while, true, false, in, do, begin, end, fun, function, return, yield, try, %
mutable, if, then, else, cloud, async, static, use, abstract, interface, inherit, finally },
otherkeywords={ let!, return!, do!, yield!, use!, var, from, select, where, order, by },
keywordstyle=\color{bluekeywords},
sensitive=true,
basicstyle=\ttfamily,
breaklines=true,
xleftmargin=\parindent,
aboveskip=\bigskipamount,
tabsize=4,
morecomment=[l][\color{greencomments}]{///},
morecomment=[l][\color{greencomments}]{//},
morecomment=[s][\color{greencomments}]{{(*}{*)}},
morestring=[b]",
showstringspaces=false,
literate={`}{\`}1,
stringstyle=\color{redstrings},
}
\lstdefinestyle{fssnake}{
language=python,
basicstyle=\ttfamily\small,
aboveskip={1.0\baselineskip},
belowskip={1.0\baselineskip},
columns=fixed,
extendedchars=true,
breaklines=true,
tabsize=4,
prebreak=\raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},
frame=lines,
showtabs=false,
showspaces=false,
showstringspaces=false,
morekeywords={let, pushf, popf, sublist, rand, sqrt},
keywordstyle=\color[rgb]{0.627,0.126,0.941},
commentstyle=\color[rgb]{0.133,0.545,0.133},
stringstyle=\color[rgb]{01,0,0},
numbers=left,
numberstyle=\small,
stepnumber=1,
numbersep=10pt,
captionpos=t,
escapeinside={\%*}{*)}
}
\lstdefinelanguage{BNF}{
sensitive=false, % keywords are not case-sensitive
morecomment=[l]{;}, % l is for line comment
morekeywords={},
morestring=[b]" % defines that strings are enclosed in double quotes
}
\lstdefinestyle{bnf}{
language=BNF,
basicstyle=\ttfamily\color[rgb]{0,0,0}\small,
aboveskip={1.0\baselineskip},
belowskip={1.0\baselineskip},
columns=fixed,
extendedchars=true,
breaklines=true,
tabsize=4,
prebreak=\raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},
frame=lines,
showtabs=false,
showspaces=false,
showstringspaces=false,
keywordstyle=\color[rgb]{0,0,0},
commentstyle=\color[rgb]{0.133,0.545,0.133},
stringstyle=\color[rgb]{1,0,0},
identifierstyle=\color[rgb]{0,0,1},
numbers=left,
numberstyle=\small,
stepnumber=1,
numbersep=10pt,
captionpos=t,
escapeinside={\%*}{*)}
}
\usepackage[shortlabels]{enumitem}
\usepackage{forest}
\usepackage{tabularx}
% Allow for newlines within tabularx cells
\newcommand{\specialcell}[2][c]{%
\begin{tabular}[#1]{@{}c@{}}#2\end{tabular}}
\usepackage{hyperref}
\title{F\#Snake Language Specification}
\author{Markus Feng}
\date{2018-12-09}
\begin{document}
\maketitle
\section{Introduction}
The goal of this language is to create a F\# implementation of a general purpose interpreted language heavily based on the Python programming language, yet with restricted functionality on the language. This language would have many features of existing general purpose imperative programming languages, including function definitions, function calls, lexical scoping, variables, control flow, operators, and types including \texttt{int}, \texttt{string}, \texttt{bool}, and \texttt{list}. \\
This problem needs its own programming language because it defines a specific set of restricted functionality. This means that it is easier to check the security and the correctness of the programs written in this language. The hope is that this language is a basic building block such that simple programs can be written in this language, and that features can be added to this language as needed to make it easier to write specific programs, while not losing its benefit of simplicity.
A demo of the project can be found at:\\ \url{http://assisstion.github.io/projects/cardgamelang/}.
\section{Design Principles}
The guiding design principle behind this design is powerful but simple and familiar. This would more likely ring true to programmers with previous experience in Python, who would find the syntax and semantics similar to that language. This language is powerful enough to be Turing complete (assuming infinite time and memory), as demonstrated by the fact that it is possible to implement a Rule 110 elementary cellular automata in this language.
\section{Examples}
The following example (which can be run by \texttt{dotnet run example-1.cgr}) is an implementation of a program to print out prime numbers:
\begin{lstlisting}[style=fssnake]
isprime(n):
if n < 2:
return false
for i in range(2, sqrt(n) + 1):
if n % i == 0:
return false
return true
main():
print("Print all prime numbers less than 100:")
for i in range(100):
if isprime(i):
print(i)
return 0
\end{lstlisting}
The following example (which can be run by \texttt{dotnet run example-2.cgr}) is an implementation of the ``Fizz-Buzz" program:
\begin{lstlisting}[style=fssnake]
main():
print("For numbers from 1 to 50, inclusive")
print("Print Fizz for numbers divisble by 3")
print("Print Buzz for numbers divisible by 5")
for i in range(1,51):
let b1 = i % 3 == 0
let b2 = i % 5 == 0
if b1 | b2:
let s = concat(str(i), ":")
if b1:
s = concat(s, " Fizz")
if b2:
s = concat(s, " Buzz")
print(s)
return 0
\end{lstlisting}
The following more complicated example (which can be run by \texttt{dotnet run example-3.cgr}) is an implementation of mergesort:
\begin{lstlisting}[style=fssnake]
split(l):
let splitted = []
let c = len(l)
pushf(splitted, sublist(l, c/2))
pushf(splitted, sublist(l, 0, c/2))
return splitted
merge(a, b):
if len(a) == 0:
return b
elif len(b) == 0:
return a
let av = popf(a)
let bv = popf(b)
if av <= bv:
pushf(b, bv)
let nl = merge(a, b)
pushf(nl, av)
return nl
else:
pushf(a, av)
let nl = merge(a, b)
pushf(nl, bv)
return nl
mergesort(l):
if len(l) <= 1:
return l
let splitted = split(l)
let left = mergesort(splitted[0])
let right = mergesort(splitted[1])
return merge(left, right)
randlist(length, maxval):
let l = []
for i in range(length):
pushf(l, rand(maxval))
return l
main():
print("Merge sorting a fixed list:")
let arr = [3,1,4,1,5,9,2,6]
print("Original:")
print(arr)
print("Sorted:")
print(mergesort(arr))
print("Merge sorting a random list:")
let arr2 = randlist(20, 1000)
print("Original:")
print(arr2)
print("Sorted:")
print(mergesort(arr2))
return 0
\end{lstlisting}
Other sample programs can be found in the \texttt{samples} subdirectory, and can be run by \texttt{dotnet run samples/<file>.cgr}. Most of these are programs to test that my language is working the way that it is intended.
\section{Language Concepts}
The user needs to understand basic imperative programming concepts to use this language. Because the structure of this language is similar to any other type of imperative language such as \texttt{python} or \texttt{java}, the language has variables and functions, control flow such as \texttt{if}/\texttt{else}/\texttt{for}/\texttt{while}, recursion, and types such as \texttt{int}, \texttt{boolean}, \texttt{string}, \texttt{list}. In this case, variables act as primitives, while methods, control flow, and complex objects act as combining forms. In addition, there is a concept of a \texttt{scope}, similar to that of a class. A \texttt{scope} contains a set of properties and functions, and can be initialized by calling a function of the scope name with no arguments. Note that like many other general purpose programming languages, there is usually multiple ways to solve a problem in this language, so not understanding some of the previous concepts does not prevent a user from being able to use this language.
\section{Syntax}
The goal is for the language to have a \texttt{python}-like whitespace based syntax. Therefore, indentation matters in such a language, so we use a \texttt{[<expr>+1]} to indicate that everything inside \texttt <expr> (other than newlines) is indented one additional time. In addition, any non-quoted "\#" character begins a comment, so that character and all successive characters of the same line are ignored. The BNF of the currently implemented grammar looks like the following:
\begin{lstlisting}[style=bnf]
<program> ::= <definition> <newlines> <definition> | <definition>
<newlines> ::= \n | <newlines> \n
<definition> ::= <fun_def> | <var_decl> | <named_scope>
<named_scope> ::= <id> : <newlines> [<program>+1]
<var_decl> ::= <id> | <assignment>
<fun_def> ::= <fun_header> <stmt_block>
<stmt_block> ::= : <newlines> [<expr_body>+1]
<fun_header> ::= <id> (<fun_args>)
<fun_args> ::= <id> | <fun_args>, <id>
<expr_body> ::= <stmt> <newlines> <stmt> | <stmt>
;Statements
<stmt> ::= <fun_call>
| <prop_fun_call>
| <complex_assignment>
| <let_stmt>
| <return_stmt>
| <if_else_block>
| <while_block>
| <for_block>
<complex_assignment> ::= <lvalue> <complex_eq> <expr>
<complex_eq> ::= <op>= | =
<let_stmt> ::= let <assignment>
<assignment> ::= <id> = <expr>
<return_stmt> ::= return <expr>
<if_else_block> ::= <if_part> | <if_part> <else_block>
<else_block> ::= <else_part> | <elif_block> <else_part>
<elif_block> ::= <elif_part> | <elif_part> <elif_block>
<if_part> ::= if <expr> <stmt_block>
<elif_part> ::= elif <expr> <stmt_block>
<else_part> :: else <stmt_block>
<while_block> ::= while <expr> <stmt_block>
<for_block> ::= for <id> in <expr> <stmt_block>
;Expressions
<expr> ::= <dot_expr> | <dot_expr> <op> <expr>
<dot_expr> ::= <array_expr> | <prop_acc> | <prop_fun_call>
<array_expr> ::= <consuming_expr> | <array_acc>
<array_acc> ::= <array_expr>[<expr>]
<consuming_expr> ::= <unary_expr>
| <fun_call>
| <this_literal>
| <bool_literal>
| <id>
| <num_literal>
| <parens_expr>
| <list_literal>
;Function calls and properties
<fun_call> ::= <id> () | <id> (<expr_args>)
<prop_fun_call> ::= <array_expr> . <fun_call>
<prop_acc> ::= <array_expr> . <id>
<expr_args> ::= <expr>, <expr_args> | <expr>
<lvalue> ::= <id>
| <array_acc>
| <prop_acc>
;Literals
<string_literal> ::= "" | "<chars>"
<num_literal> ::= <num_literal> <num> | <num>
<bool_literal> ::= true | false
<this_literal> ::= this
<parens_expr> ::= (<expr>)
<list_literal> ::= [] | [<expr_args>]
;Identifiers
<id> ::= <id> <idchar> | <idchar>
<idchar> ::= <loweralpha> | <upperalpha> | <num> | <underscore>
<loweralpha> ::= {a, b, ..., z}
<upperalpha> ::= {A, B, ..., Z}
<underscore> ::= _
<num> ::= num in {0, 1, ..., 9}
;Operators
<unary_expr> ::= <unary_op> <array_expr>
<unary_op> ::= {-, +, !}
<op> ::= {+, -, *, /, %, ==, !=, <=, >=, <, >, &, |}
;Strings
<stringchars> ::= <stringchar> <stringchars> | <stringchar>
<stringchar> ::= \ <char> | <nonquote>
<char> ::= {Unicode characters}
<nonquote> ::= {Unicode character that are not a double quote}
\end{lstlisting}
\section{Semantics}
The program is interpreted in order, line-by-line, with multi-line clusters forming a scope. Due to the inspiration from a \texttt{python}-like whitespace based syntax, indentation is used to define blocks, whereas line breaks are used to separate statements.
The interpreter will throw a syntax error if the result of the parsing is invalid, indicating the line number that the interpreter fails at. Currently, the interpreter does not support multi-line statements, so the same statement must be kept on a single line for parsing to succeed.
\subsection{Primitive Values}
\begin{center}
\begin{tabularx}{\linewidth}{|c|c|c|X|}
\hline
Syntax & Abstract Syntax & Type & Meaning \\
\hline
n & NumLiteral of int & ValInt & A primitive in the language that represents a 32-bit signed integer using the F\# Int32 type. \\
\hline
``str" & StringLiteral of string & ValString & A primitive in the language that represents a string using the F\# String type. \\
\hline
true/false & BoolLiteral of bool & ValBool & A primitive in the language that represents a boolean using the F\# bool type. \\
\hline
id & Identifier of string & ValReference & A primitive in the language that represents an identifier that can be assigned to and accessed later. \\
\hline
this & ThisLiteral & ValReference & A primitive in the language that, when called in an instance function, gives a reference to that instance, and fails otherwise. \\
\hline
\end{tabularx}
\end{center}
\clearpage
\subsection{Control flow}
\begin{center}
\begin{tabularx}{\linewidth}{|X|X|X|}
\hline
Syntax & Abstract Syntax & Meaning \\
\hline
if $pred$: stmts (elif $pred$: stmts)* (else: stmts)? & IfElseStmt of (Expr * Stmt list) * ((Expr * Stmt list) list) * Stmt list option & If the predicate is true, execute the first block of statements. Otherwise, successively go through each of the elif predicates. If any of those are true, execute that block of statements and stop. Finally if there is an else block and none of the previous blocks executed, execute the else block statements. \\
\hline
while $pred$ stmts & WhileStmt of Expr * Stmt list & Run the predicate. If it's true, execute the block of statments and repeat the process. \\
\hline
for $id$ in $list$ stmts & ForStmt of string * Expr * Stmt list & Evaluate the list expression, and execute the block once for each value in the list, setting a temporary variable $id$ to that value. \\
\hline
$fun$($args$) & FuncCallStmt of string * Expr list & Evaluate the arguments form left to right order (where args is a comma separated list of expressions), then look for a function with the name $fun$ and execute the function with the specified arguments. An exception will be thrown if the number of provided arguments is incorrect. \\
\hline
\end{tabularx}
\end{center}
\clearpage
\subsection{Accessors}
(Note that the left hand side of accessors can be any expression that evaluates to the list or object)
\begin{center}
\begin{tabularx}{\linewidth}{|X|X|X|}
\hline
Syntax & Abstract Syntax & Meaning \\
\hline
$arr$[$index$] & ArrayAccessor of Expr * string & Access the a zero-indexed list at the specified index, failing if the index is out of bounds or the object is not a list, or assigns to the list at the index (index must be a valid index).\\
\hline
$obj$.$property$ & PropertyAccessor of Expr * string & Access a property of an object, failing if the property does not exist, or assigns a property of an object (property does not already need to exist). \\
\hline
$obj$.$fun$($args$) & PropertyFunctionCall of Expr * string * Expr list & Evaluate the arguments form left to right order (where args is a comma separated list of expressions), then look for a function with the name $fun$ of the specified object and execute the function with the specified arguments. An exception will be thrown if the number of provided arguments is incorrect. \\
\hline
\end{tabularx}
\end{center}
\subsection{Composite Values}
\begin{center}
\begin{tabularx}{\linewidth}{|c|X|X|X|}
\hline
Syntax & Abstract Syntax & Type & Meaning \\
\hline
$[e_1, e_2, ..., e_n]$ & ListLiteral of Expr list & Value list & Represents a list of elements. Evaluating the list involves evaluating each individual expression in the list, and returning a lit containing the result of the evaluations. \\
\hline
$a\ \ [op]=\ b$ & AssignmentStmt of LValue * BinaryOp option * Expr & Stmt & Represents an assignment of a variable $a$ to a value $b$. If the optional $op$ is included, represents taking the value of $a$ and $b$ and applying $op$ to them, then storing the value to $a$. \\
\hline
let $a=b$ & LetStmt of string * Expr & Stmt & Represents an assignment of a newly created local variable $a$ to a value $b$. This newly created local variable can shadow and existing variable of the same name in an outer scope. \\
\hline
$expr$ & Expr & Expr & Represents an expression that can be evaluated to produce a value. \\
\hline
$stmt$ & Stmt & Stmt & Represents a statement that can be executed sequentially in program order. \\
\hline
$defn$ & Defn & Defn & Represents an upper level definition. A complete program is made up of Defns. \\
\hline
($expr$) & ParensExpr of Expr & Parenthases ensure that the expression inside is evaluated as a group. Use to override operator precedence. \\
\hline
$fun$($args$): $stmts$ & FunctionDefn of string * string list * Stmt list & ValFunc & Represents a function, defined by the function name (string), a list of arguments (string list), and a list of statements(Stmt) \\
\hline
$scopename$: $defns$ & ScopeDefn of string * Defn list & ValFunc & Represents a scope (the equivalent of a class). The scope is instantiated by calling a function $scopename()$, and each instance has unique properties. The created instance will have type ValReference. \\
\hline
\end{tabularx}
\end{center}
\subsection{Operators}
\begin{center}
\begin{tabularx}{\linewidth}{|c|c|c|X|}
\hline
Syntax & Input Type & Resulting Type & Meaning \\
\hline
$a + b$ & ValInt * ValInt & ValInt & Returns the sum of the input values. Note: does not work for string concatenation; use the \texttt{concat} builtin function instead. \\
\hline
$a - b$ & ValInt * ValInt & ValInt & Returns the difference between the first and second values. \\
\hline
$a * b$ & ValInt * ValInt & ValInt & Returns the product of the input values. \\
\hline
$a / b$ & ValInt * ValInt & ValInt & Returns the quotient of the first and second values. \\
\hline
$a \% b$ & ValInt * ValInt & ValInt & Returns the modulo of the first and the second values; (the modulo is the remainder after division). \\
\hline
$a == b$ & Value * Value & ValBool & Returns true if the inputs are equal, false otherwise. Works for the following Value types (the two inputs must be the same type): ValBool, ValInt, ValString, ValFunc, ValList (reference not structural equality), ValReference. \\
\hline
$a != b$ & Value * Value & ValBool & Returns false if the inputs are equal, true otherwise. In other words, equivalent to $!(a == b)$. \\
\hline
$a <= b$ & ValInt * ValInt & ValBool & Returns true if the first value is less than or equal to the second value, false otherwise. \\
\hline
$a >= b$ & ValInt * ValInt & ValBool & Returns true if the first value is greater than or equal to the second value, false otherwise. \\
\hline
$a < b$ & ValInt * ValInt & ValBool & Returns true if the first value is less than the second value, false otherwise. \\
\hline
$a > b$ & ValInt * ValInt & ValBool & Returns true if the first value is greater than the second value, false otherwise. \\
\hline
$a \& b$ & ValBool * ValBool & ValBool & Returns true if all inputs are true, false otherwise. \\
\hline
$a | b$ & ValBool * ValBool & ValBool & Returns true if any input is true, false otherwise. \\
\hline
$!a$ & ValBool & ValBool & Returns true the input is false, false otherwise. \\
\hline
$-a$ & ValInt & ValInt & Returns the additive inverse of the input value. Equivalent to $a * -1$. \\
\hline
$+a$ & ValInt & ValInt & Returns the input value. \\
\hline
\end{tabularx}
\end{center}
\clearpage
\subsection{Builtin Functions}
\begin{center}
\begin{tabularx}{\linewidth}{|c|c|c|X|}
\hline
Syntax & Input Type & Resulting Type & Meaning \\
\hline
$str(v)$ & Value & ValString & Returns the string representation of the value. \\
\hline
$print(v)$ & Value & ValNone & Prints the string representation of the value to the console, with a trailing newline. \\
\hline
$printraw(v)$ & Value & ValNone & Prints the string representation of the value to the console, with no trailing newline. \\
\hline
$input([v])$ & Value option & ValString & Reads input from standard input into a string. If an argument is specified, uses the string representation of that argument as a prompt for the input. \\
\hline
$sqrt(i)$ & ValInt & ValInt & Returns the floor of the square root of the input, failing on a negative input. \\
\hline
$pushf(l, v)$ & ValList * Value & ValNone & Pushes a value $v$ to the beginning of a list $l$. \\
\hline
$popf(l)$ & ValList & Value & Pops a value from the beginning of a list and returns it. \\
\hline
$concat(v1, v2)$ & \specialcell{ValListOrString *\\ ValListOrString} & ValListOrString & Concatenates two lists or two strings together. If lists are concatenated, does not modify the reference to the original list. \\
\hline
$len(v)$ & ValListOrString & ValInt & Returns the length of the input list or string. \\
\hline
$range([a], b)$ & \specialcell{ValInt option *\\ ValInt} & ValList & If there are two arguments, return a list of ValInts from $a$ inclusive to $b$ exclusive (the list contains all values from $a$ to $b-1$). If only one argument is specified, the first argument is implied to be zero. \\
\hline
$clone(l)$ & ValList & ValList & Returns a new list that is a copy of the input list. \\
\hline
$sublist(l, a, [b])$ & \specialcell{ValList *\\ ValInt option *\\ ValInt} & ValNone & Returns a new list that contains the elements of an existing list from index of the first argument (inclusive) to the index of the second argument (exclusive), or the end of the list if the second argument is omitted. \\
\hline
$reverse(v)$ & ValListOrString & ValListOrString & Reverses a list or string (modifying the current list), and returns it. \\
\hline
$rand(i)$ & ValInt & ValInt & Returns a random integer from 0 to $i-1$, inclusive. \\
\hline
$int(str)$ & ValString & ValInt & Parses the input string to its the integer value, fails if it cannot occur. It is recommended to use $tryInt$ first to verify if the string can be parsed to an integer. \\
\hline
$tryInt(str)$ & ValString & ValBool & Returns true if the string can be parsed to an int, and false otherwise. \\
\hline
\end{tabularx}
\end{center}
\begin{center}
\begin{tabularx}{\linewidth}{|c|c|c|X|}
\hline
$toCharList(s)$ & ValString & ValList & Returns the list of characters of a string (characters are represented by ValInts with their ASCII/Unicode representation). \\
\hline
$fromCharList(l)$ & ValList & ValString & Returns a string formed by a specified list of characters (characters are represented by ValInts with their ASCII/Unicode representation). \\
\hline
$charToStr(i)$ & ValInt & ValString & Returns the string representation of a single character (characters are represented by ValInts with their ASCII/Unicode representation). \\
\hline
$charAt(s, i)$ & \specialcell{ValString *\\ ValInt} & ValString & Returns the character at a specified index of a string (characters are represented by ValInts with their ASCII/Unicode representation). \\
\hline
\end{tabularx}
\end{center}
My program will be represented in an abstract syntax tree (AST) fashion in an algebraic data type. For example, the following code excerpt:
\begin{lstlisting}[style=fssnake]
playing_value(base, card):
if card.suit == base.suit:
return card.value
\end{lstlisting}
Would be stored in the following alegbraic data type structure, subject to change:
\begin{lstlisting}[style=fssnake]
Function("playing_value", ["base", "card"],
[IfStmt(
BinaryExpr(
BinaryOp(Eq)
LValue(PropertyAccessor("card", "suit"))
LValue(PropertyAccessor("base", "suit"))
)
[ReturnStmt(
LValue(PropertyAccessor("card", "value"))
)]
)]
)
\end{lstlisting}
\clearpage
This same code excerpt would be represented in the abstract syntax tree structure in the following form (ignoring indentation for now).
\begin{forest}
[fun\_def
[fun\_header
[id
[\texttt{playing\_value}]
]
[fun\_args
[id
[\texttt{base}]
]
[fun\_args
[id
[\texttt{card}]
]
]
]
]
[stmt\_block
[\texttt{:}]
[newlines
[\texttt{\textbackslash n}]
]
[expr\_body
[stmt
[if\_else\_block
[if\_part
[\texttt{if}]
[expr
[binary\_expr
[prop\_acc
[expr
[id
[\texttt{card}]
]
]
[\texttt{.}]
[id
[\texttt{suit}]
]
]
[op
[Eq
[\texttt{==}]
]
]
[prop\_acc
[expr
[id
[\texttt{base}]
]
]
[\texttt{.}]
[id
[\texttt{suit}]
]
]
]
]
[stmt\_block
[\texttt{:}]
[newlines
[\texttt{\textbackslash n}]
]
[expr\_body
[stmt
[return\_stmt
[\texttt{return}]
[expr
[prop\_acc
[expr
[id
[\texttt{card}]
]
]
[\texttt{.}]
[id
[\texttt{value}]
]
]
]
]
]
]
]
]
]
]
]
]
]
\end{forest}
This language supports lexical scoping and strong dynamic typing. Any error that occurs during execution will cause the program to halt with an error message describing the problem. The interpreter will run the main function of the global scope as the entry point, with an empty state.
\section{Remaining Work}
Currently, my project already achieves the goal of a general purpose programming language with various features. In addition, I already have a web interface/playground to test my project on, with working "command line" output.
As a stretch goal, I could try to implement more additional language features. Some of the potential features to include are the following:
\begin{itemize}
\item Higher order functions
\item Constructors
\item Escape characters in strings
\item Lambda expressions
\item Line-numbers during execution
\item Dictionaries
\item Multi-line statements/expressions
\item Floating point numbers
\item Multi-file support
\item External language support
\end{itemize}
Finally, though almost all programs written with my syntax can be parsed, like most general purpose programming languages, many end up being invalid programs when run. Therefore, implementing an "exception handling" system in my language could be another stretch goal for my project.
\end{document}