-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.coffee
executable file
·55 lines (53 loc) · 1.46 KB
/
main.coffee
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
#!/usr/bin/env coffee
define = require('amdefine')(module) if typeof define != 'function'
define(['readline', './byohc', './byohc_parser'], \
( readline , byohc , byohc_parser )->
rl = readline.createInterface(
input: process.stdin,
output: process.stdout,
terminal: false
)
print_ir = (ir)->
next_indent = (env)-> {
indent: env.indent+' '
}
ops = {
function: (env)->
process.stdout.write "(lambda (#{@parameter.name})\n"
process.stdout.write "#{next_indent(env).indent}"
_print_ir(byohc.interpret(@expression), next_indent(env))
process.stdout.write ")"
apply: (env)->
@arg = byohc.interpret(@arg)
process.stdout.write "(apply \n"
process.stdout.write "#{next_indent(env).indent}"
_print_ir(@function, next_indent(env))
process.stdout.write "\n"
process.stdout.write "#{next_indent(env).indent}"
_print_ir(@arg, next_indent(env))
process.stdout.write ")"
variable: (env)->
if @value?
return _print_ir(@value, env)
else
process.stdout.write "#{@name}"
}
_print_ir = (ir, env)->
return ops[ir.type].call(ir, env)
_print_ir(ir, {indent: ''})
process.stdout.write "\n"
rl.on('line', (line)->
if line[0] != '#'
print = yes
if line[0] == '^'
print = no
line = line.substr(1)
ast = byohc_parser(line)
ir = byohc.ast_to_ir(ast)
#console.log JSON.stringify ir, null, 2
r = byohc.interpret(ir)
print_ir(r) if print
else
console.log line
)
)