-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunholy.js
197 lines (185 loc) · 7.58 KB
/
unholy.js
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
const fs = require('fs')
const path = require('path')
class unholy {
constructor(code) {
this.code = code
this.vars = []
}
tokenize() {
const length = this.code.length
let pos = 0
let tokens = []
const BUILT_IN_KEYWORDS = ["systemutilsunholycoutputtext", "systemutilsunholycaddnumbers", "systemutilsunholycsubtractnumbers", "systemutilsunholycsetvariable", "systemutilsunholycgetvariable", "systemutilsunholycmultiplynumbers", "systemutilsunholycdividenumbers"]
const varChars='abcdefghijklmnopqrstuvwxyz~'
const numbers='1234567890'
while (pos < length) {
let currentChar = this.code[pos]
// if its "space" or newline then continue
if(currentChar === "\n") {
return console.log("NEWLINES ARE NOT ALLOWED IN UNHOLY C --")
}
if(currentChar === "_" || currentChar === "|" || currentChar === "\r") {
pos++
continue
} else if (currentChar === '*') {
// if char is * then it's a string
let res = ""
pos++
while(this.code[pos] !== '*' && this.code[pos] !== '\n' && pos < length) {
res += this.code[pos]
pos++
}
if (this.code[pos] !== '*') {
return {
error: 'didnt finish the string'
}
}
pos++
tokens.push({
type: "string",
value: res
})
} else if (varChars.includes(currentChar)) {
let res = currentChar
pos++
while (varChars.includes(this.code[pos]) && pos < length) {
res += this.code[pos]
pos++
}
if (!BUILT_IN_KEYWORDS.includes(res)) {
return {
error: `lol what is ${res}`
}
}
// add keyword to tokens
tokens.push({
type: "keyword",
value: res
})
} else if (numbers.includes(currentChar)) {
let res = currentChar
pos++
while (numbers.includes(this.code[pos]) && pos < length) {
res += this.code[pos]
pos++
}
tokens.push({
type: "number",
value: res
})
} else { // person who wrote code is dumb
return {
error: `Unexpected character at ${this.code[pos]}`
}
}
}
return {
error: false,
tokens
}
}
parse(tokens){
const len = tokens.length
let pos = 0
while(pos < len) {
const token = tokens[pos]
if(token.type === "keyword" && token.value === "systemutilsunholycoutputtext") {
if(!tokens[pos + 1]) {
return console.log("unexpected EOL, expected a string")
}
let isString = tokens[pos + 1].type === "string"
if(!isString) {
return console.log(`expected a string ? u are stupid`)
}
console.log('\x1b[35m%s\x1b[0m', tokens[pos + 1].value) // horror
pos += 2 // add 2 because the check after print keyword is already checked
} else if (token.type === "keyword" && token.value === "systemutilsunholycaddnumbers") {
if (!tokens[pos + 1]) {
return console.log("Unexpected EOL, expected a number")
}
let isNum = tokens[pos + 1].type === "number"
if(!isNum) {
return console.log(`expected number`)
}
console.log('\x1b[35m%s\x1b[0m', (Number(tokens[pos + 1].value[0]) + Number(tokens[pos+1].value[1])).toString())
pos += 2
} else if (token.type === "keyword" && token.value === "systemutilsunholycsubtractnumbers") {
if (!tokens[pos + 1]) {
return console.log("Unexpected EOL, expected a number")
}
let isNum = tokens[pos + 1].type === "number"
if(!isNum) {
return console.log(`expected number`)
}
console.log('\x1b[35m%s\x1b[0m', (Number(tokens[pos + 1].value[0]) - Number(tokens[pos+1].value[1])).toString())
pos += 2
} else if (token.type === "keyword" && token.value === "systemutilsunholycsetvariable") {
if (!tokens[pos + 1]) {
return console.log("Unexpected EOL, expected a string")
}
let isString = tokens[pos + 1].type === "string"
if(!isString) {
return console.log(`expected string`)
}
this.vars.push({
name: `${tokens[pos + 1].value[0]}`,
value: `${tokens[pos + 1].value.slice(1)}`
})
pos += 2
} else if (token.type === "keyword" && token.value === "systemutilsunholycgetvariable") {
if (!tokens[pos + 1]) {
return console.log("Unexpected EOL, expected a string")
}
let isString = tokens[pos + 1].type === "string"
if(!isString) {
return console.log(`expected string`)
}
for(let i=0; i<this.vars.length; i++) {
if(this.vars[i].name.toString() == tokens[pos + 1].value) {
return console.log('\x1b[35m%s\x1b[0m', this.vars[i].value)
} else {
continue;
}
}
console.log("???????")
pos += 2
} else if (token.type === "keyword" && token.value === "systemutilsunholycmultiplynumbers") {
if (!tokens[pos + 1]) {
return console.log("Unexpected EOL, expected a number")
}
let isNum = tokens[pos + 1].type === "number"
if(!isNum) {
return console.log(`expected number`)
}
console.log('\x1b[35m%s\x1b[0m', (Number(tokens[pos + 1].value[0]) * Number(tokens[pos+1].value[1])).toString())
pos += 2
} else if (token.type === "keyword" && token.value === "systemutilsunholycmultiplynumbers") {
if (!tokens[pos + 1]) {
return console.log("Unexpected EOL, expected a number")
}
let isNum = tokens[pos + 1].type === "number"
if(!isNum) {
return console.log(`expected number`)
}
console.log('\x1b[35m%s\x1b[0m', (Number(tokens[pos + 1].value[0]) / Number(tokens[pos+1].value[1])).toString())
pos += 2
} else {
return console.log(`Unexpected token ${token.type}`)
}
}
}
run() {
const {
tokens,
error
} = this.tokenize()
if (error) {
console.log(error)
return
}
this.parse(tokens)
}
}
const code = fs.readFileSync(path.join(__dirname, 'index.unholyc'), 'utf8').toString()
const coderunner= new unholy(code)
coderunner.run()