-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcpcbasic.js
207 lines (182 loc) · 5.83 KB
/
cpcbasic.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
198
199
200
201
202
203
204
205
206
207
// cpcbasic.js - CPCBasic for the Browser
// (c) Marco Vieth, 2019
// https://benchmarko.github.io/CPCBasic/
//
/* globals Controller, Model, Utils, View */
"use strict";
var cpcconfig, cpcBasic;
cpcBasic = {
config: {
bench: 0, // debug: number of parse bench loops
debug: 0,
databaseDirs: "examples", // example base directories (comma separated)
database: "examples", // examples, apps, saved
example: "cpcbasic",
exampleIndex: "0index.js", // example index for every exampleDir
input: "", // keyboard input when starting the app
kbdLayout: "alphanum", // alphanum, alpha, num
showInput: true,
showInp2: false,
showCpc: true,
showKbd: false,
showKbdLayout: false,
showOutput: false,
showResult: false,
showText: false,
showVariable: false,
showConsole: false,
sound: true,
tron: false, // trace on
redirectExamples: {}
},
model: null,
view: null,
controller: null,
fnHereDoc: function (fn) {
return String(fn).
replace(/^[^/]+\/\*\S*/, "").
replace(/\*\/[^/]+$/, "");
},
addIndex: function (sDir, input) {
var tmp;
if (typeof input === "function") {
tmp = JSON.parse(this.fnHereDoc(input).trim());
input = {};
input[sDir] = tmp;
}
return cpcBasic.controller.addIndex(sDir, input);
},
addItem: function (sKey, input) {
if (typeof input !== "string") {
input = this.fnHereDoc(input);
}
return cpcBasic.controller.addItem(sKey, input);
},
// https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript
fnParseUri: function (oConfig) {
var aMatch,
rPlus = /\+/g, // Regex for replacing addition symbol with a space
rSearch = /([^&=]+)=?([^&]*)/g,
fnDecode = function (s) { return decodeURIComponent(s.replace(rPlus, " ")); },
sQuery = window.location.search.substring(1),
sName,
sValue;
while ((aMatch = rSearch.exec(sQuery)) !== null) {
sName = fnDecode(aMatch[1]);
sValue = fnDecode(aMatch[2]);
if (sValue !== null && oConfig.hasOwnProperty(sName)) {
switch (typeof oConfig[sName]) {
case "string":
break;
case "boolean":
sValue = (sValue === "true");
break;
case "number":
sValue = Number(sValue);
break;
case "object":
break;
default:
break;
}
}
oConfig[sName] = sValue;
}
},
setDebugUtilsConsole: function (sCpcBasicLog) {
var oCurrentConsole = Utils.console,
oConsole = {
consoleLog: {
value: sCpcBasicLog || "" // already something collected?
},
console: oCurrentConsole,
fnMapObjectProperties: function (arg) {
var aRes, sKey, value;
if (typeof arg === "object") {
aRes = [];
for (sKey in arg) { // eslint-disable-line guard-for-in
// if (arg.hasOwnProperty(sKey)) {
value = arg[sKey];
if (typeof value !== "object" && typeof value !== "function") {
aRes.push(sKey + ": " + value);
}
}
arg = String(arg) + "{" + aRes.join(", ") + "}";
}
return arg;
},
rawLog: function (fnMethod, sLevel, aArgs) {
if (sLevel) {
aArgs.unshift(sLevel);
}
if (fnMethod) {
if (fnMethod.apply) {
fnMethod.apply(console, aArgs);
}
}
if (this.consoleLog) {
this.consoleLog.value += aArgs.map(this.fnMapObjectProperties).join(" ") + ((sLevel !== null) ? "\n" : "");
}
},
log: function () {
this.rawLog(this.console && this.console.log, "", Array.prototype.slice.call(arguments));
},
debug: function () {
this.rawLog(this.console && this.console.debug, "DEBUG:", Array.prototype.slice.call(arguments));
},
info: function () {
this.rawLog(this.console && this.console.info, "INFO:", Array.prototype.slice.call(arguments));
},
warn: function () {
this.rawLog(this.console && this.console.warn, "WARN:", Array.prototype.slice.call(arguments));
},
error: function () {
this.rawLog(this.console && this.console.error, "ERROR:", Array.prototype.slice.call(arguments));
},
changeLog: function (oLog) {
var oldLog = this.consoleLog;
this.consoleLog = oLog;
if (oldLog && oldLog.value && oLog) { // some log entires collected?
oLog.value += oldLog.value; // take collected log entries
}
}
};
Utils.console = oConsole;
},
fnRedirectExamples: function () {
var redirectExamples = this.model.getProperty("redirectExamples"),
name = this.model.getProperty("database") + "/" + this.model.getProperty("example");
if (redirectExamples[name]) {
this.model.setProperty("database", redirectExamples[name].database);
this.model.setProperty("example", redirectExamples[name].example);
}
},
fnDoStart: function () {
var that = this,
oStartConfig = this.config,
sCpcBasicLog, oInitialConfig, iDebug;
Object.assign(oStartConfig, cpcconfig || {}); // merge external config from cpcconfig.js
oInitialConfig = Object.assign({}, oStartConfig); // save config
this.fnParseUri(oStartConfig); // modify config with URL parameters
this.model = new Model(oStartConfig, oInitialConfig);
this.view = new View({});
iDebug = Number(this.model.getProperty("debug"));
Utils.debug = iDebug;
if (Utils.console.cpcBasicLog) {
sCpcBasicLog = Utils.console.cpcBasicLog;
Utils.console.cpcBasicLog = null; // do not log any more to dummy console
}
if (Utils.debug > 1 && this.model.getProperty("showConsole")) { // console log window?
this.setDebugUtilsConsole(sCpcBasicLog);
Utils.console.log("CPCBasic log started at", Utils.dateFormat(new Date()));
Utils.console.changeLog(document.getElementById("consoleText"));
}
this.fnRedirectExamples();
that.controller = new Controller(this.model, this.view);
},
fnOnLoad: function () {
Utils.console.log("CPCBasic started at", Utils.dateFormat(new Date()));
this.fnDoStart();
}
};
cpcBasic.fnOnLoad(); // if cpcbasic.js is the last script, we do not need to wait for window.onload