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
|
# Simple interpreter
An interpreter of a simple artificial programming language written in Python 3.
Here you'll find a brief description of the language and the implementation.
## Simple language
This software is an interpreter of a relatively simple programming language
designed by me.
### Data types
Simple language supports integer numbers, floating-point numbers and the
Boolean data type.
Literal integer numbers are comprised of a sequence of digits.
Please note that negative integer number literals are not supported just yet.
Floating-point number literals follow a bit more complicated format, with
scientific notation support and stuff.
At the moment negative floating-point numbers literals are not supported
either.
The Boolean data type has two literal values: `True` and `False`.
### Variables
Named memory locations are called variables.
Numbers can be stored in memory by assigning them to variables using the
assignment operator `:=`.
A variable can be used anywhere a number can be used.
x := 1;
answer_to_everything := 42;
y := x;
### Arithmetic expressions
Numbers can be computed from complex arithmetic expressions.
Simple language supports addition, subtraction, multiplication, and division
with grouping using parentheses.
An arithmetic expression can be used anywhere a number can be used.
microseconds := 1000000 * seconds;
### Input/output
Simple language provides basic output facilities using the `print` statement.
print 60 * 3.14 / 180;
print days_per_year * 24;
Only `print`ing numbers is supported at the moment.
### Control flow
Simple language supports conditional control flow using the `if` operator.
When executed, the `if` operator executes its body statement if it's condition
evaluates to the true Boolean value.
never_printed := 0;
if (False)
print never_printed;
Boolean values can be computed from complex logical expressions.
Simple language supports conjunction (`&&`) and disjunction (`||`) of logical
expressions, as well as comparing them using the equality (`==`) and inequality
(`!=`) operators.
Arguably not the most useful feature at the moment, but I am working on it.
A logical expression can be used anywhere a Boolean value can be used.
if (True && False == False)
days := 366;
Please note that the conditional operators `&&` and `||` have the same
precedence right now.
### Compound statement
A compound statement (or a block) is a sequence of statements grouped together
inside a pair of curly braces (`{` and `}`).
When executed, a block executes its statements sequentially.
A block can be used anywhere a statement can be used.
if (True) {
days := 366;
hours := days * 24;
minutes := hours * 60;
seconds := minutes * 60;
}
### Language grammar
The language grammar written in the Extended Backus‒Naur Form (EBNF) (as
described in [the corresponding Wikipedia article]
(https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_Form))
is as follows:
program = { statement } ;
statement = empty_statement
| block
| assignment
| print_statement
| if_statement ;
empty_statement = ";" ;
block = "{" , { statement } , "}" ;
assignment = identifier , ":=" , arithmetic_expression , ";" ;
print_statement = "print" , arithmetic_expression , ";" ;
arithmetic_expression = arithmetic_term , { "+" , arithmetic_term
| "-" , arithmetic_term } ;
arithmetic_term = arithmetic_factor , { "*" , arithmetic_factor
| "/" , arithmetic_factor } ;
arithmetic_factor = identifier
| number
| "(" , arithmetic_expression , ")" ;
number = integer_number | floating_point_number ;
integer_number = digit , { digit } ;
digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ;
(* Valid floating-point number literals: "1e1", "1e+1", "1e-1", ".1", "1.".
Invalid floating-point number literals: "1e", ".e1". *)
floating_point_number = integer_number , exponent
| integer_number , "." , { digit } , [ exponent ] ;
| { digit } , "." , integer_number , [ exponent ] ;
exponent = ( "e" | "E" ) , [ "+" | "-" ] , integer_number ;
identifier = ( LETTER | "_" ) , { LETTER | digit | "_" } ;
(* LETTER is a symbol Python considers a letter under the current locale
like "a", "b", "c", "X", "Y", "Z", etc. *)
if_statement = "if" , "(" , logical_expression , ")" , statement ;
logical_expression = logical_term , { "&&" , logical_term
| "||" , logical_term } ;
logical_term = logical_factor , [ "==" , logical_factor
| "!=" , logical_factor ] ;
logical_factor = "True"
| "False"
| "(" , logical_expression , ")" ;
## Interpreter design
This implementation follows the conventional interpreter design principles
(more or less).
### Lexer
The *lexer* represents the contents of a source file as a sequence of *tokens*.
Token examples include identifiers (like `x` and `foo`), literal values (either
numeric like `42` and `3.14` or Boolean like `True`), parentheses (`(` and
`)`), arithmetic operation signs (`+`, `*`), etc.
The lexer is implemented in `src/lexer.py`.
### Parser
The *parser* builds a program tree according to the rules described in the
language grammar.
Each tree node processes its children accordingly.
For example, a node representing addition of two numbers must have exactly
two children.
When executed, this node evaluates its children and adds the two values.
+
/ \
/ \
1 2
Each of the children in the example above might in turn be represented by a
complex subtree.
For example the right-side operand of the addition might be a result of
multiplicating two numbers.
+
/ \
/ \
/ \
1 *
/ \
/ \
2 3
The `if` statement also has two children (its condition and body), but executes
its body only after making sure the condition evaluates to the true Boolean
value.
if
/ \
------- -------
/ \
&& :=
/ \ / \
/ \ / \
/ \ days 366
/ \
True False
The parser is implemented in `src/parser.py`.
## Usage
To use this software, you need to be able to run Python 3 scripts.
To execute a script written in Simple language, pass the path to the script
to `src/parser.py`.
You can also pass the path to a script to `src/lexer.py` to examine the tokens
the script gets separated into.
## License
This project, including all of the files and their contents, is licensed under
terms of the MIT License.
See [LICENSE.txt] for details.
[LICENSE.txt]: LICENSE.txt
|