-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlang.bnf
147 lines (122 loc) · 3.03 KB
/
lang.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
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
/* Lexical Parser */
!whitespace : '\t' | '\n' | '\r' | ' ' ;
!lineComment : '/' '/' { . } '\n' ;
!blockComment : '/' '*' { . | '*' } '*' '/' ;
_letter : 'A'-'Z' | 'a'-'z' | '_' ;
_digit : '0'-'9' ;
_alpha : _letter | _digit ;
string_literal : '"' {_alpha | ' ' | '!' | '?' }'"' ;
int : '0' | '1'-'9' {_digit} ;
/* keywords */
func : 'f' 'u' 'n' 'c' ;
let : 'l' 'e' 't' ;
if : 'i' 'f' ;
else : 'e' 'l' 's' 'e' ;
return : 'r' 'e' 't' 'u' 'r' 'n' ;
true : 't' 'r' 'u' 'e' ;
false : 'f' 'a' 'l' 's' 'e' ;
and : 'a' 'n' 'd' ;
or : 'o' 'r' ;
ident : _letter {_alpha} ;
/* punctuation */
plus : '+' ;
minus : '-' ;
mul : '*' ;
div : '/' ;
eq : '=' '=' ;
neq : '!' '=' ;
atmost : '<' '=' ;
lt : '<' ;
atleast : '>' '=' ;
gt : '>' ;
lbrace : '{' ;
rbrace : '}' ;
assign : '=' ;
lparen : '(' ;
rparen : ')' ;
comma : ',' ;
semicolon : ';' ;
/* Syntactic Parsr */
<<
import (
"github.com/aniketp/meego/src/ast"
)
>>
Program
: Functions Statements << ast.NewProgram($0, $1) >>
;
Functions
: Functions Function << ast.AppendStatement($0, $1) >>
| empty << ast.NewStatementList() >>
;
Function
: func ident lparen FormalArgs rparen ident StatementBlock << ast.NewFunctionStatement($1, $3, $5, $6) >>
;
Statements
: Statements Statement << ast.AppendStatement($0, $1) >>
| empty << ast.NewStatementList() >>
;
StatementBlock
: lbrace Statements rbrace << ast.NewBlockStatement($1) >>
;
Statement
: if Expression StatementBlock IfStatement << ast.NewIfStatement($1, $2, $3) >>
| ident assign Expression semicolon << ast.NewAssignStatement($0, $2) >>
| let ident assign Expression semicolon << ast.NewIdentInit($1, $3) >>
| Expression semicolon << ast.NewExpressionStatement($0) >>
| return Expression semicolon << ast.NewReturnStatement($1) >>
;
IfStatement
: else StatementBlock << $1, nil >>
| empty
;
Expression
: Expression Operation Term << ast.NewInfixExpression($0, $2, $1) >>
| Term
;
Operation
: plus
| minus
| atmost
| atleast
| lt
| gt
| eq
| neq
| and
| or
;
Term
: Term mul Factor << ast.NewInfixExpression($0, $2, $1) >>
| Term div Factor << ast.NewInfixExpression($0, $2, $1) >>
| string_literal << ast.NewStringLiteral($0) >>
| Bool << ast.NewBoolExpression($0) >>
| Factor
;
Factor
: lparen Expression rparen << $1, nil >>
| int << ast.NewIntegerLiteral($0) >>
| ident << ast.NewIdentExpression($0) >>
| ident lparen Args rparen << ast.NewFunctionCall($0, $2) >>
| error
;
Bool
: true << true, nil >>
| false << false, nil >>
;
Args
: Expression ArgsList << ast.AppendArgs($0, $1) >>
| empty
;
ArgsList
: ArgsList comma Expression << ast.AppendArgs($2, $0) >>
| empty << ast.NewArg() >>
;
FormalArgs
: ident ident FormalArgsList << ast.AppendFormalArgs($2, $0, $1) >>
| empty
;
FormalArgsList
: FormalArgsList comma ident ident << ast.AppendFormalArgs($0, $2, $3) >>
| empty << ast.NewFormalArg() >>
;