/
index.coffee
150 lines (134 loc) · 3.52 KB
/
index.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
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
sope = {}
functions = []
window.parseParens = (code) ->
ret = []
code = code.replace(/\(/g, "[").replace /\)/g, "]"
code = code.replace /(\w)\s(\w)/g, "$1\", \"$2"
code = code.replace /^(\w)/g, "\"$1"
code = code.replace /(\w)$/g, "$1\""
code = code.replace /\[(\w)/g, "[\"$1"
code = code.replace /(\w)\]/g, "$1\"]"
code = code.replace /(\w)\s\[/g, "$1\", ["
code = code.replace /\]\s(\w)/g, "], \"$1"
code = code.replace /\]\s*\[/g, "], ["
ret = JSON.parse code
console.log ret
ret
replaceQuotes = (txt) ->
txt = txt.replace /\\"/g, '\\x22'
#"
txt = txt.replace /(\"[^\"]*[^\\]\")/g, (a, b) ->
#"
return a.replace(/\n/g, '\\x0A').replace(/\n/g, '\\x0D').replace(/\x20/g, '\\x20')
txt = txt.replace /\\\n/g, ' '
txt
compile = (code) ->
code = replaceQuotes code
get = (name) ->
if _.isNumeric(name)
scope.so = name
else if _.s(name, 0, 1) != "'"
scope.so = scope[name]
else
scope.so = _.s(name, 1)
return scope.so
functions = []
scope = {}
code = code.split "\n"
newcode = []
for line in code
if line.length > 0
newcode.push line
code = newcode
for line, index in code
line = line.split " "
func = line[0]
if func.length is 0 then continue
args = _.s line, 1
# you could turn `'val` to scope["val"] here
# right now you are doing it at run time
if func == "label"
scope[get(args[0])] = index
else
functions.push """
function() {
instructionSet.#{func}("#{args.join("\", \"")}");
}
"""
"""
instructionSet = {
'set' : function(name, val) {
scope.so = scope[this.get(name)] = this.get(val)
return scope.so
},
'label': function(name) {
return this.set(name, scope.pc+1);
},
'log': function(name) {
scope.so = this.get(name)
console.log(scope.so)
return scope.so
},
'get' : function(name) {
if (_.isNumeric(name)) {
scope.so = name
} else if (_.s(name, 0, 1) !== "'") {
scope.so = scope[name]
} else {
scope.so = _.s(name, 1)
}
return scope.so
},
'add' : function(a, b) {
scope.so = (this.get(a) - 0) + (this.get(b) - 0)
return scope.so
},
goto: function(line) {
scope.set_pc = this.get(line)
},
compare: function(a,b) {
return scope.so = this.get(a) == this.get(b)
},
ifgoto: function (cond, line) {
if (this.get(cond)) {
this.goto(line)
}
},
exit: function() {
scope.__close__ = true;
},
def: function() {
}
}
var scope = #{JSON.stringify(scope)};
functions = [\n#{functions.join(',\n')}]
scope.pc = 0
scope.last_pc = 0
scope.set_pc = -1
for (var j=0; j<100; j++) {
if (scope.pc >= functions.length || scope.__close__ == true) {
console.log(scope);
break;
}
functions[scope.pc](scope);
scope.second_last_pc = scope.last_pc
scope.last_pc = scope.pc
scope.pc++
if (scope.set_pc != -1) {
scope.pc = scope.set_pc
scope.set_pc = -1
}
}
"""
$(document).ready () ->
$('#code').persistValue()
compileCode = () ->
code = $('#code').val()
compiled = compile code
$('#output1').val compiled
eval compiled
$("#compile").click compileCode
$(document.body).bind "keydown", (e) ->
if e.keyCode is 116 #f5
e.preventDefault()
compileCode()