-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathocaml.jison
205 lines (168 loc) · 4.45 KB
/
ocaml.jison
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
/* parses ocaml to JS */
/* lexical grammar */
%lex
%%
\s+ /* skip whitespace */
/* keywords */
"let" return 'LET';
"in" return 'IN';
"rec" return 'REC';
/* identifiers and literals */
[a-z][a-zA-Z0-9_']* return 'IDENTIFIER';
\d+(\.)(\d+)? return 'FLOAT';
\d+ return 'INTEGER';
\'[a-zA-Z]\' return 'CHAR';
\"[a-zA-Z]+\" return 'STRING';
/* operators */
"=" return '=';
"+." return '+.';
"-." return '-.';
"*." return '*.';
"/." return '/.';
"+" return '+';
"-" return '-';
"*" return '*';
"/" return '/';
"::" return '::';
"@" return '@';
"(" return '(';
")" return ')';
"[" return '[';
"]" return ']';
";" return ';';
/* end of input */
<<EOF>> return 'EOF';
/lex
/* operator precedence */
%right '='
%left '+' '-' '+.' '-.'
%left '*' '/' '*.' '/.'
/* I'm unsure if '@' is left or right associative,
but its precedence is definitely below '::' */
%left '@'
%right '::'
/* language grammar */
%%
// represents the entire ocaml 'program' given to transpile
input
// ocaml source code, terminated with EOF
: content EOF
{ return $1; }
;
// the actual text of the ocaml input
content
// recursive list of successive definitions (functions,
// vars, types, etc) representing an entire program
: definitions
// single expression followed by EOF
// (primarily for use in interpreter mode)
| expression
;
expression
// the simplest expression, an identifier or constant literal
: primitive_type
// mathematical expressions
| expression '+' expression
{ $$ = $1 + " + " + $3; }
| expression '-' expression
{ $$ = $1 + " - " + $3; }
| expression '*' expression
{ $$ = $1 + " * " + $3; }
| expression '/' expression
{ $$ = $1 + " / " + $3; }
| expression "+." expression
{ $$ = $1 + " + " + $3; }
| expression "-." expression
{ $$ = $1 + " - " + $3; }
| expression "*." expression
{ $$ = $1 + " * " + $3; }
| expression "/." expression
{ $$ = $1 + " / " + $3; }
// function call
| IDENTIFIER arguments
{ $$ = $1 + '(' + $2 + ')'; }
;
// the meat and potatoes of a real ocaml program
definition
: LET let_binding
{ $$ = "var " + $2; }
;
// used for variable and function assignment
let_binding
// var assignment
: IDENTIFIER '=' expression
{ $$ = $1 + " = " + $3; }
// function assignment (ocaml functions always
// have at least one arg)
| IDENTIFIER parameters '=' expression
{ $$ = function_def_str($1, $2, $4); }
| REC IDENTIFIER parameters '=' expression
{ $$ = function_def_str($2, $3, $5); }
;
// all primitive data types as well as variable names
primitive_type
: IDENTIFIER
| FLOAT
| INTEGER
| CHAR
| STRING
;
// a list literal (including cons and append)
list
// list literal
: '[' list_elements ']'
{ $$ = '[' + $2 + ']'; }
// list cons chain terminating in identifier
| primitive_type "::" IDENTIFIER
{ $$ = '[' + $1 + ']' + ".concat(" + $3 + ")"; }
// list cons
| primitive_type "::" list
{ $$ = '[' + $1 + ']' + ".concat(" + $3 + ")"; }
// list append
| list '@' list
{ $$ = $1 + ".concat(" + $3 + ")"; }
;
list_elements
: %empty
{ $$ = ""; }
| simple_expression
{ $$ = $1; }
| simple_expression ';' list_elements
{ $$ = $1 + ", " + $3; }
;
// what can be given to a function
argument
: primitive_type
| '(' expression ')'
{ $$ = $2; }
;
// RECURSIVELY DEFINED LISTS ===============
// recurisve list of definitions
definitions
// possible empty input, return empty string
: %empty
{ $$ = ""; }
| definition definitions
{ $$ = $1 + '\n' + $2; }
;
// list of parameter identifiers in function definition
parameters
: IDENTIFIER
| IDENTIFIER parameters
{ $$ = $1 + ', ' + $2; }
;
// list of expressions, used as args in function call
arguments
: argument
| argument arguments
{ $$ = $1 + ", " + $2; }
;
%%
// utils
var function_def_str = function(identifier, arg_list, val) {
return identifier + ' = ' +
'function(' + arg_list + ') {\n'
+ ' return ' + val + ';\n' +
'}'
;
}