Repository navigation
Expand file tree
/
Copy pathcodit.compiler.js
More file actions
138 lines (112 loc) · 3.5 KB
/
Copy pathcodit.compiler.js
File metadata and controls
138 lines (112 loc) · 3.5 KB
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
var Codit = {
functions: {},
cFunctions: {},
uFunctions: {},
scope: {},
currentLine: -1,
operators: ['+', '-', '*', '/'],
ifOperators: ['==','>','<','!=', '<=', '>='],
loopOperators: ['until', 'as long as'],
events: ["clicked", "hovered", "keydowned"]
};
Codit.compile = function (code, options) {
this.code = code;
this.lines = code.split("\n");
for (this.currentLine = 0; this.currentLine < this.lines.length; this.currentLine++) {
this.query(this.lines[this.currentLine]);
lastChar = this.lines[this.currentLine].substr(this.lines[this.currentLine].length-1);
if(lastChar == ':') {
stillFunction = true;
Codit.currentLine++;
while(stillFunction) {
if(Codit.lines[Codit.currentLine].indexOf(";") > -1) {
stillFunction = false;
} else {
Codit.currentLine++;
}
}
}
};
}
Codit.query = function (line) {
this.command = line.split(" ")[0].replace(" ",'');
this.parameters = line.substr(line.indexOf(" ")+1);
if(this.command.length > 1)this.functions[this.command](this.parameters);
}
Codit.execute = function (code, options) {
try {
Codit.compile(code, options);
} catch (error) {
parsed = Codit.parseError(error);
Codit.output(parsed[0], parsed[1]);
}
}
Codit.value = function (content) {
if(content.substr(0,1) === "@") {
content = this.scope[content.substr(1)];
}
if(parseInt(content) > 0 || parseInt(content) <= 0) {
content = parseInt(content);
} else {
content = content.replace("\\", "");
}
return content;
}
Codit.phrase = function (content) {
bits = content.split(" ");
result = "";
for (var bit = 0; bit < bits.length; bit++) {
word = bits[bit];
if(Codit.operators.indexOf(word) > -1) {
temp = Codit.value(bits[bit-1]) + Codit.value(bits[bit]) + Codit.value(bits[bit+1]);
temp = eval(temp);
result += temp;
} else if((Codit.operators.indexOf(bits[bit-1]) < 0) && (Codit.operators.indexOf(bits[bit]) < 0) && (Codit.operators.indexOf(bits[bit+1]) < 0)){
result += Codit.value(word);
}
if(result.toString() == result && bit != bits.length-1)result += " ";
};
return result;
}
Codit.evalize = function (content) {
if(parseInt(content) >=0 || parseInt(content) < 0) {
return content;
} else {
return " '"+content+"' ";
}
}
Codit.cFunction = function (func, name, command) {
this.func = func;
this.name = name;
this.command = command;
Codit.functions[this.command] = this.func;
Codit.cFunctions[this.command] = this;
}
Codit.output = function (content, tip) {
if(typeof tip !== 'undefined')content += " \n"+tip;
alert(content);
}
Codit.input = function (content) {
return prompt(content);
}
Codit.error = function (content, tip) {
if(typeof tip !== "undefined") {
content += "<small>("+tip+")</small>";
}
throw content;
}
Codit.parseError = function(text) {
if(text == "TypeError: Cannot read property 'replace' of undefined") {
return ["Browser error - expected value", "The variable name might be wrong or there's a space at the end of the line"];
}
if(text == "TypeError: Cannot read property 'split' of undefined") {
return ["Browser error - wrong Codit syntax", "Check your between-commands, like `to` and `then`."];
}
if(text == "TypeError: undefined is not a function") {
return ["Browser error - could not find such a function", "Maybe the command is not defined?"];
}
/*if(text.indexOf(" is not defined") > -1) {
return ["Browser error - something is not defined with a value", "Check all your variables and make sure your numbers are numbers"];
}*/
return [text,'at line '+(Codit.currentLine+1)];
}