-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.ebnf
201 lines (158 loc) · 6.89 KB
/
test.ebnf
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
(* TODO -
*
* Remove pointers.
* Add either optional types or nullable types.
* Are sets required?
* Make everything immutable unless specified?
* Add unsafe?
* Add module definitions? These are in the ProgInOberon.pdf
* Add child modules.
* Add generics.
* Add lambdas.
* Add first class/pure functions?
*)
(* A source file can either be a main function/entry point to the program or a library unit/module.
* Each unit can import modules and each should be passed to the compiler.
* A main function may or may not require command line arguments, i.e. an embedded systems program probably won't, an
* OS kernel would, a program run on the command line would.
*)
compilation_unit = function_decl (* TODO - Should this be a special rule? *)
| module
;
(* Wondering whether it's worth modules having an interface?
* e.g.
* [import_list]
* "interface" qualified_identifier "is"
* decl_sequence
* "end" qualified_identifier ";"
*
* "module" qualified_identifier ["implements" qualified_identifier {"," qualified_identifier}] "is"
*
* This would allow for implementations specific to various configurations, but that could also be done with a
* specification/body split, as Ada does?
*
* I'm wanting a traits system as well, which would allow traits to also fit in there somewhere, maybe with
* another keyword?
*)
module = [import_list]
"module" qualified_identifier "is"
decl_sequence
["begin"
statements]
"end" qualified_identifier ";"
;
(* Allows to redefine module names, i.e. import System.OS as OS; *)
import_list = "import" qualified_identifier ["as" identifier] { ","
qualified_identifier ["as" identifier] } ";"
;
(* TODO - This seems to enable the keywords without the decl's. *)
decl_sequence = { "constant" { constant_decl ";" }
| "type" { type_decl ";" }
| "var" { var_decl ";" } }
{ function_decl ";"
| forward_decl ";" }
;
constant_decl = identifier_def ":=" constant_expr ;
type_decl = identifier_def "is" type ;
(* TODO - Add default initialisation. *)
var_decl = identifier_list ":" type ;
function_decl = "function" [receiver] identifier_def [formal_params] "is"
decl_sequence
["begin"
statements]
"end" identifier ";"
;
(* TODO - Surely it's possible to do without forward declarations, like Go? *)
forward_decl = "function" "^" [receiver] identifier_def [formal_params] ;
formal_params = "(" [formal_parameter {";" formal_parameter}] ")" [":" qualified_identifier] ;
formal_parameter = ["var"] identifier {"," identifier} ":" type ;
receiver = "(" ["var"] identifier ":" identifier ")" ;
(* TODO - Arrays
*
* Need bounds and index types.
* Ranges.
*)
type = qualified_identifier
| "array" [constant_expr {"," constant_expr}] "of" type
| "record" ["("qualified_identifier")"]
field_list {";" field_list}
"end"
| "pointer" "to" type
| "function" [formal_params]
;
field_list = [identifier_list ":" type] ;
statements = statement ";" {statement ";"} ;
statement = assignment_stmt
| function_call
| if_statement
| case_statement
| while_loop
| repeat_loop
| for_loop
| general_loop
| with_statement
| "exit"
| "return" [expression]
;
assignment_stmt = designator ":=" expression ;
function_call = designator ["(" [expr_list] ")"] ;
if_statement = "if" expression "then"
statements
{"elsif" expression "then"
statements}
["else"
statements]
"end" ;
case_statement = "case" expression "of"
case {"|" case}
["else"
statements]
"end" ;
case = [case_labels {"," case_labels} ":"
statements] ;
case_labels = constant_expr [".." constant_expr] ;
while_loop = "while" expression "loop"
statements
"end" ;
(* TODO - Repeat loops can be implemented with an exit statement with an expression *)
repeat_loop = "loop"
statements
"until" expression ;
(* TODO - Add ranges *)
for_loop = "for" identifier ":=" expression "to" expression ["by" constant_expr] "loop"
statements
"end" ;
general_loop = "loop"
statements
"end" ;
with_statement = "with" guard "do"
statements {"|" guard "do"
statements}
["else"
statements]
"end"
;
guard = qualified_identifier ":" qualified_identifier ;
constant_expr = expression ;
expression = simple_expr [relation simple_expr] ;
simple_expr = ["+" | "-"] term {add_op term} ;
term = factor {mul_op factor} ;
factor = designator ["(" [expr_list] ")"]
| number
| character
| string
| "null"
| set
| "(" expression ")"
| "not" factor
;
set = "{" [element {"," element}] "}" ;
element = expression [".." expression] ;
relation = "=" | "!=" | "<" | "<=" | ">" | ">=" | "in" | "is" ;
add_op = "+" | "-" | "or" ;
mul_op = "*" | "/" | "div" | "mod" | "and" ;
designator = qualified_identifier {"." identifier | "[" expr_list "]" | " ^ " | "(" qualified_identifier ")"} ;
expr_list = expression {"," expression} ;
identifier_list = identifier_def {"," identifier_def} ;
qualified_identifier = [identifier "."] identifier ;
identifier_def = identifier [" * " | " - "] ;