-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
grammar.BNF
117 lines (77 loc) · 4.01 KB
/
grammar.BNF
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
<start> ::= <package> <includes> <parent_items> | <empty>
<package> ::= PACKAGE IDENTIFIER | <empty> ;; usually not required for small programs or runnable scripts; but maybe required when a library or package is defined
<includes> ::= <includes> <include> | <empty> ;; to include third party libraries
<include> ::= USE <identifier_dots>
| USE STRING
<identifier_dots> ::= <identifier_dots> "." IDENTIFIER
| IDENTIFIER
<parent_items> ::= <parent_item> <parent_items>
<parent_item> ::= <function>
| <global_define>
<global_define> ::= <global_struct>
| <global_variable> ;; global variables might be allowed in some special conditions
<global_variable> ::= <datatype> IDENTIFIER
| <datatype> IDENTIFIER "=" <expression_const>
<global_struct> ::= STRUCT IDENTIFIER "{" <struct_vars> "}"
<struct_vars> ::= <datatype> IDENTIFIER
| <datatype> IDENTIFIER "=" <expression_const>
<function> ::= <function_prototype> IDENTIFIER <function_arguments> <block>
<function_prototype> ::= <datatype> | <empty> ;; default return type is I32 (int, 32bit) e.g: `main {}` and default return value is 0
<function_arguments> ::= <function_arguments> "," <function_argument>
<datatype_primary> :: = I8
| I16
| I32
| I64
| F32
| F64
| U8
| U16
| U32
| U64
| STRING
| CHAR ;; Unlike the C language 'char' data type, it can store UTF8 characters. (e.g.: "ش")
<datatype> ::= <datatype_primary>
| <datatype_primary> "[" "]"
| IDENTIFIER ;; to declare user defined data types like a struct
<function_argument> ::= <datatype> IDENTIFIER
<block> ::= "{" <statements> "}"
<statements> ::= <statements> <statement>
<statement> ::= <statement_return>
| <statement_expression>
| <statement_if>
| <statement_for>
| <statement_while>
| <statement_dowhile>
| <statement_repeat>
| <statement_switch>
| <statement_print>
| <statement_printnl>
| <statement_printerr>
| <statement_printerrnl>
<statement_repeat> ::= REPEAT NUMBER <block> ;; for loop from 0 to j {...}
| REPEAT NUMBER TO NUMBER <block> ;; for loop from i to j {...}
<statement_print> ::= "_" <expressions> ;; in C: `printf("%1%2%3",...)`
<statement_printnl> ::= "__" <expressions> ;; in C: `printf("%1\n%2\n%3\n",...)`
<statement_printerr>::= "!_" <expressions> ;; in C: `fprintf(stderr, "%1%2%3",...)`
<statement_printerrnl>::= "!__" <expressions> ;; in C: `fprintf(stderr, "%1\n%2\n%3\n",...)`
<expressions> ::= <expressions> "," <expression>
| <expression>
<statement_if> ::= IF <expression> <block>
| IF <expression> <block> ELSE <statement_if>
| IF <expression> <block> ELSE <block>
<statement_switch> ::= SWITCH "{" <switch_cases> "}"
<switch_cases> ::= <switch_cases> <switch_case>
<switch_case> ::= CASE <expression_const> ":" <statements>
<statement_for> ::= FOR <expression> <block>
| FOR IDENTIFIER IN <expression_const> <block> ;; e.g.: in Python: `for item in array:`
<statement_while>::= WHILE <expression> <block>
<statement_dowhile>::= DO <block> while <expression>
<statement_return> ::= RETURN <expressions> ;; returning multiple values from a function is allowed
<expression_const> :== <expression>
<expression> ::= <term> "+" <expression>
| <term>
<term> ::= <factor> "*" <term>
| <factor>
<factor> ::= "(" <expression> ")"
| <const>
<const> ::= NUMBER | STRING | IDENTIFIER