-
Notifications
You must be signed in to change notification settings - Fork 0
/
fizzylogo_globals.coffee
154 lines (128 loc) · 4.67 KB
/
fizzylogo_globals.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
149
150
151
152
153
154
Fizzylogo = {}
# export as a global, whether it's in node or in the browser
(exports ? this).Fizzylogo = Fizzylogo
# Debug logging flags
objectFindSignatureMakeCallDebug = false
listEvaluationsDebug = false
tokensDebug = false
tokenizerDebug = false
contextDebug = false
flClassesDebug = false
methodsExecutionDebug = false
ASSOCIATIVITY_RIGHT_TO_LEFT = true
rWorkspace = null
log = console.log
dir = console.dir
repeatFunctionContinuation = null
outerMostContext = null
DEBUG_STRINGIFICATION_CHECKS = false
if DEBUG_STRINGIFICATION_CHECKS
stringsTable_TO_CHECK_CONVERTIONS = {}
indentation = ->
#return " ".repeat(flContexts.length * 2)
return ""
# to disambiguate between when we are operating
# on JS arrays from when we are operating
# on fizzylogo lists.
Array::jsArrayPush = (element) ->
@push element
# variation of base64, generates valid IDs from
# an arbitrary string. Little known fact, javascript
# IDs can be of arbitrary length and can start with and have some pretty wild chars
# see https://stackoverflow.com/questions/1661197/what-characters-are-valid-for-javascript-variable-names
keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789Γಠ_'
ValidIDfromString = (input) ->
#log "ValidIDfromString encoding: " + input
if /^([A-Z_][0-9A-Z_$]*)$/gi.test input
#log "ValidIDfromString encoded as: " + input
return input
utf8_encode = (string) ->
string = string.replace(/\r\n/g, '\n')
utftext = ''
n = 0
while n < string.length
c = string.charCodeAt(n)
if c < 128
utftext += String.fromCharCode(c)
else if c > 127 and c < 2048
utftext += String.fromCharCode(c >> 6 | 192)
utftext += String.fromCharCode(c & 63 | 128)
else
utftext += String.fromCharCode(c >> 12 | 224)
utftext += String.fromCharCode(c >> 6 & 63 | 128)
utftext += String.fromCharCode(c & 63 | 128)
n++
utftext
output = ''
i = 0
input = utf8_encode(input)
while i < input.length
chr1 = input.charCodeAt(i++)
chr2 = input.charCodeAt(i++)
chr3 = input.charCodeAt(i++)
enc1 = chr1 >> 2
enc2 = (chr1 & 3) << 4 | chr2 >> 4
enc3 = (chr2 & 15) << 2 | chr3 >> 6
enc4 = chr3 & 63
if isNaN(chr2)
enc3 = enc4 = 64
else if isNaN(chr3)
enc4 = 64
output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4)
#log "ValidIDfromString encoded as: " + "$" + output
return "$" + output
StringFromValidID = (input) ->
if /^([A-Z_][0-9A-Z_$]*)$/gi.test input
return input
utf8_decode = (string) ->
output = ''
i = 0
charCode = 0
while i < string.length
charCode = string.charCodeAt(i)
if charCode < 128
output += String.fromCharCode(charCode)
i++
else if charCode > 191 and charCode < 224
output += String.fromCharCode((charCode & 31) << 6 | string.charCodeAt(i + 1) & 63)
i += 2
else
output += String.fromCharCode((charCode & 15) << 12 | (string.charCodeAt(i + 1) & 63) << 6 | string.charCodeAt(i + 2) & 63)
i += 3
output
input = input.replace(/[^ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789Γಠ_]/g, '')
input = input.replace(/^\$/, '')
#log "StringFromValidID decoding: " + input
output = ''
i = 0
while i < input.length
d = keyStr.indexOf(input.charAt(i++))
e = keyStr.indexOf(input.charAt(i++))
f = keyStr.indexOf(input.charAt(i++))
g = keyStr.indexOf(input.charAt(i++))
a = d << 2 | e >> 4
b = (e & 15) << 4 | f >> 2
c = (f & 3) << 6 | g
output += String.fromCharCode(a)
if f != 64
output += String.fromCharCode(b)
if g != 64
output += String.fromCharCode(c)
return utf8_decode output
sortFirstArrayAccordingToSecond = (targetData, refData) ->
# create an array of indices [0, 1, ... targetData.length] and
# sort those specularly to refData
indices = [0...targetData.length]
# Sort array of indices according to the reference data.
indices.sort (indexA, indexB) ->
if refData[indexA] < refData[indexB]
#log "refData[indexA] < refData[indexB] " + refData[indexA] + " " + refData[indexB]
return -1
else if refData[indexA] > refData[indexB]
#log "refData[indexA] > refData[indexB] " + refData[indexA] + " " + refData[indexB]
return 1
#log "refData[indexA] = refData[indexB] " + refData[indexA] + " " + refData[indexB]
0
# Map array of indices to corresponding values of the target array.
return indices.map (index) -> targetData[index]
allClasses = []