-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhoney.ne
121 lines (102 loc) · 2.18 KB
/
honey.ne
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
@{%
const lexer= require("./lexer")
%}
@lexer lexer
prog
-> _ml statements _ml
{%
(data)=>{
return data[1];
}
%}
statements
-> statement (__lb_ statement):*
{%
(data) => {
const repeated = data[1];
const restStatements = repeated.map(chunks => chunks[1]);
return [data[0], ...restStatements];
}
%}
statement
-> var_assignment {% id %}
| fun_call {% id %}
| %comment {% id %}
var_assignment
-> %identifier _ "=" _ expr
{%
(data)=>{
return{
type: "var_assignment",
var_name: data[0],
value: data[4]
}
}
%}
fun_call
-> %identifier _ "(" _ml (arg_list _ml):? ")"
{%
(data)=>{
return{
type: "fun_call",
fun_name: data[0],
arguments:data[4] ? data[4][0] : []
}
}
%}
arg_list
-> expr
{%
(data)=>{
return [data[0]];
}
%}
| arg_list __ml expr
{%
(data)=>{
return [...data[0], data[2]];
}
%}
expr
-> %string {% id %}
| %number {% id %}
| %identifier {% id %}
| fun_call {% id %}
| lambda {% id %}
lambda -> "(" _ (param_list _):? ")" _ "=>" _ml lambda_body
{%
(data) => {
return {
type: "lambda",
parameters: data[2] ? data[2][0] : [],
body: data[7]
}
}
%}
param_list
-> %identifier (__ %identifier):*
{%
(data) => {
const repeatedPieces = data[1];
const restParams = repeatedPieces.map(piece => piece[1]);
return [data[0], ...restParams];
}
%}
lambda_body
-> expr
{%
(data) => {
return [data[0]];
}
%}
| "{" __lb_ statements __lb_ "}"
{%
(data) => {
return data[2];
}
%}
__lb_ -> (_ %NL):+ _
_ml -> (%WS | %NL):*
__ml -> (%WS | %NL):+
_ -> %WS:*
__ -> %WS:+