-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
149 lines (147 loc) · 5.41 KB
/
index.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
class Easy{
constructor(codelouieasy){
this.codelouieasy = codelouieasy
}
tokenize(){
const length = this.codelouieasy.length
let pos = 0
let tokens = []
const BUILT_IN_KEYWORDS = ["send", "var"]
const varChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_'
let line = 1
let column = 0
while(pos < length){
let currentChar = this.codelouieasy[pos]
if(currentChar === " "){
pos++
column++
continue
}else if(currentChar === "\n"){
line++
column = 0
pos++
continue
}else if(currentChar === '"'){
let res = ""
pos++
column++
while(this.codelouieasy[pos] !== '"' && pos < length){
res += this.codelouieasy[pos]
pos++
column++
}
if(this.codelouieasy[pos] !== '"'){
return {
error: `Unterminated string at line ${line} column ${column}`
}
}
pos++
column++
tokens.push({
type: "string",
value: res,
})
}else if(varChars.includes(currentChar)){
let res = currentChar
pos++
column++
while(varChars.includes(this.codelouieasy[pos]) && pos < length){
res += this.codelouieasy[pos]
pos++
column++
}
tokens.push({
type: BUILT_IN_KEYWORDS.includes(res) ? "keyword" : "keyword_custom",
value: res
})
}else if(currentChar === "="){
pos++
column++
tokens.push({
type: "operator",
value: "eq"
})
}else{
return {
error: `Unexpected character ${this.codelouieasy[pos]} at line ${line} column ${column}`
}
}
}
return {
error: false,
tokens
}
}
parse(tokens){
const len = tokens.length
let pos = 0
const vars = {}
while(pos < len){
const token = tokens[pos]
if(token.type === "keyword" && token.value === "send"){
if(!tokens[pos + 1]){
return console.log("Unexpected end of line, expected string")
}
let isVar = tokens[pos + 1].type === "keyword_custom"
let isString = tokens[pos + 1].type === "string"
if(!isString && !isVar){
return console.log(`Unexpected token ${tokens[pos + 1].type}, expected string`)
}
if(isVar){
if(!(tokens[pos + 1].value in vars)){
return console.log(`Undefined variable ${tokens[pos + 1].value}`)
}
console.log('\x1b[35m%s\x1b[0m', vars[tokens[pos + 1].value])
}else{
console.log('\x1b[35m%s\x1b[0m', tokens[pos + 1].value)
}
pos += 2
}else if(token.type === "keyword" && token.value === "var"){
const isCustomKW = tokens[pos + 1] && tokens[pos + 1].type === "keyword_custom"
if(!isCustomKW){
if(!tokens[pos + 1]){
return console.log("Unexpected end of line, expected variable name")
}
return console.log(`Unexpected token ${tokens[pos + 1].type}, expected variable name`)
}
const Name = tokens[pos + 1].value
const isEq = tokens[pos + 2] && tokens[pos + 2].type === "operator" && tokens[pos + 2].value === "eq"
if(!isEq){
if(!tokens[pos + 2]){
return console.log("Unexpected end of line, expected =")
}
return console.log(`Unexpected token ${tokens[pos + 1].type}, expected =`)
}
const isString = tokens[pos + 3] && tokens[pos + 3].type === "string"
if(!isString){
if(!tokens[pos + 3]){
return console.log("Unexpected end of line, expected string")
}
return console.log(`Unexpected token ${tokens[pos + 1].type}, expected string`)
}
if(Name in vars){
return console.log(`Variable ${Name} already exists`)
}
vars[Name] = tokens[pos + 3].value
pos += 4
}else{
return console.log(`Unexpected token ${token.type}`)
}
}
}
run(){
const{ tokens, error}=this.tokenize()
if(error){
return console.log(error)
}
this.parse(tokens)
}
}
const codelouieasy =
`
var text = "loui"
send text
send "Hello, World!"
`
const easy = new Easy(codelouieasy)
easy.run()