Skip to content

Commit 60fccfa

Browse files
committed
Minor updates & bug fixes
1 parent 813e58e commit 60fccfa

File tree

13 files changed

+486
-138
lines changed

13 files changed

+486
-138
lines changed

easycoder/easycoder-min.js

Lines changed: 82 additions & 80 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

easycoder/easycoder.js

Lines changed: 114 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,8 @@ const EasyCoder_Core = {
959959
},
960960

961961
run: program => {
962+
program.parent.run(program.parent.nextPc);
963+
program.parent.nextPc = 0;
962964
program.exit();
963965
return 0;
964966
}
@@ -2146,6 +2148,55 @@ const EasyCoder_Core = {
21462148
}
21472149
},
21482150

2151+
Split: {
2152+
2153+
compile: compiler => {
2154+
const lino = compiler.getLino();
2155+
item = compiler.getNextValue();
2156+
let on = `\n`;
2157+
if (compiler.tokenIs(`on`)) {
2158+
on = compiler.getNextValue();
2159+
}
2160+
if ([`giving`, `into`].includes(compiler.getToken())) {
2161+
if (compiler.nextIsSymbol()) {
2162+
const targetRecord = compiler.getSymbolRecord();
2163+
if (targetRecord.keyword === `variable`) {
2164+
compiler.next();
2165+
compiler.addCommand({
2166+
domain: `core`,
2167+
keyword: `split`,
2168+
lino,
2169+
item,
2170+
on,
2171+
target: targetRecord.name
2172+
});
2173+
return true;
2174+
}
2175+
}
2176+
}
2177+
return false;
2178+
},
2179+
2180+
run: program => {
2181+
let command = program[program.pc];
2182+
let content = program.getValue(command.item);
2183+
let on = program.getValue(command.on);
2184+
content = content.split(on);
2185+
let elements = content.length;
2186+
targetRecord = program.getSymbolRecord(command.target);
2187+
targetRecord.elements = elements;
2188+
for (let n = 0; n < elements; n++) {
2189+
targetRecord.value[n] = {
2190+
type: `constant`,
2191+
numeric: false,
2192+
content: content[n]
2193+
};
2194+
}
2195+
targetRecord.index = 0;
2196+
return command.pc + 1;
2197+
}
2198+
},
2199+
21492200
Stop: {
21502201

21512202
compile: compiler => {
@@ -2506,6 +2557,8 @@ const EasyCoder_Core = {
25062557
return EasyCoder_Core.Set;
25072558
case `sort`:
25082559
return EasyCoder_Core.Sort;
2560+
case `split`:
2561+
return EasyCoder_Core.Split;
25092562
case `stop`:
25102563
return EasyCoder_Core.Stop;
25112564
case `take`:
@@ -2715,6 +2768,18 @@ const EasyCoder_Core = {
27152768
}
27162769
}
27172770
}
2771+
if ([`character`, `char`].includes(token)) {
2772+
let index = compiler.getNextValue();
2773+
if (compiler.tokenIs(`of`)) {
2774+
let value = compiler.getNextValue();
2775+
return {
2776+
domain: `core`,
2777+
type: `char`,
2778+
index,
2779+
value
2780+
};
2781+
}
2782+
}
27182783
if (compiler.tokenIs(`the`)) {
27192784
compiler.next();
27202785
}
@@ -2822,7 +2887,12 @@ const EasyCoder_Core = {
28222887
}
28232888
break;
28242889
case `position`:
2825-
if (compiler.nextTokenIs(`of`)) {
2890+
let nocase = false;
2891+
if (compiler.nextTokenIs(`nocase`)) {
2892+
nocase = true;
2893+
compiler.next();
2894+
}
2895+
if (compiler.tokenIs(`of`)) {
28262896
var last = false;
28272897
if (compiler.nextTokenIs(`the`)) {
28282898
if (compiler.nextTokenIs(`last`)) {
@@ -2838,7 +2908,8 @@ const EasyCoder_Core = {
28382908
type: `position`,
28392909
needle,
28402910
haystack,
2841-
last
2911+
last,
2912+
nocase
28422913
};
28432914
}
28442915
}
@@ -2956,8 +3027,12 @@ const EasyCoder_Core = {
29563027
content: to ? fstr.substr(from, to) : fstr.substr(from)
29573028
};
29583029
case `position`:
2959-
const needle = program.getValue(value.needle);
2960-
const haystack = program.getValue(value.haystack);
3030+
let needle = program.getValue(value.needle);
3031+
let haystack = program.getValue(value.haystack);
3032+
if (value.nocase) {
3033+
needle = needle.toLowerCase();
3034+
haystack = haystack.toLowerCase();
3035+
}
29613036
return {
29623037
type: `constant`,
29633038
numeric: true,
@@ -2984,17 +3059,22 @@ const EasyCoder_Core = {
29843059
const spec = JSON.parse(program.getValue(value.value));
29853060
switch (spec.mode) {
29863061
case `time`:
3062+
29873063
return {
29883064
type: `constant`,
29893065
numeric: true,
29903066
content: new Date(fmtValue).toLocaleTimeString(spec.locale, spec.options)
29913067
};
29923068
case `date`:
29933069
default:
3070+
const date = new Date(fmtValue);
3071+
const content = (spec.format === `iso`)
3072+
? `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()}`
3073+
: date.toLocaleDateString(spec.locale, spec.options);
29943074
return {
29953075
type: `constant`,
29963076
numeric: true,
2997-
content: new Date(fmtValue).toLocaleDateString(spec.locale, spec.options)
3077+
content
29983078
};
29993079
}
30003080
} catch (err) {
@@ -3022,10 +3102,15 @@ const EasyCoder_Core = {
30223102
content: Math.floor(date.getTime() / 1000)
30233103
};
30243104
case `date`:
3105+
content = Date.parse(program.getValue(value.value)) / 1000;
3106+
if (isNaN(content)) {
3107+
program.runtimeError(program[program.pc].lino, `Invalid date format; expecting 'yyyy-mm-dd'`);
3108+
return null;
3109+
}
30253110
return {
30263111
type: `constant`,
30273112
numeric: true,
3028-
content: Date.parse(program.getValue(value.value)) / 1000
3113+
content
30293114
};
30303115
case `newline`:
30313116
return {
@@ -3152,6 +3237,14 @@ const EasyCoder_Core = {
31523237
numeric: !isNaN(content),
31533238
content
31543239
};
3240+
case `char`:
3241+
let index = program.getValue(value.index);
3242+
let string = program.getValue(value.value);
3243+
return {
3244+
type: `constant`,
3245+
numeric: false,
3246+
content: string[index]
3247+
};
31553248
}
31563249
return null;
31573250
},
@@ -3216,7 +3309,8 @@ const EasyCoder_Core = {
32163309
return {
32173310
domain: `core`,
32183311
type: `numeric`,
3219-
value1
3312+
value1,
3313+
negate
32203314
};
32213315
case `even`:
32223316
compiler.next();
@@ -3291,7 +3385,9 @@ const EasyCoder_Core = {
32913385
case `boolean`:
32923386
return program.getValue(condition.value);
32933387
case `numeric`:
3294-
return !isNaN(program.getValue(condition.value1));
3388+
let v = program.getValue(condition.value1);
3389+
let test = v === ` ` || isNaN(v);
3390+
return condition.negate ? test : !test;
32953391
case `even`:
32963392
return (program.getValue(condition.value1) % 2) === 0;
32973393
case `odd`:
@@ -3419,7 +3515,8 @@ const EasyCoder = {
34193515
if (v.type === `boolean`) {
34203516
return v.content ? `true` : `false`;
34213517
}
3422-
if (v.content.substr(0, 2) === `{"` || v.content[0] === `[`) {
3518+
if (typeof v.content !==`undefined` && v.content.length >= 2
3519+
&& (v.content.substr(0, 2) === `{"` || v.content[0] === `[`)) {
34233520
try {
34243521
const parsed = JSON.parse(v.content);
34253522
return JSON.stringify(parsed, null, 2);
@@ -3497,8 +3594,11 @@ const EasyCoder = {
34973594
EasyCoder.reportError(err, program, program.source);
34983595
if (program.onError) {
34993596
program.run(program.onError);
3500-
} else if (program.parent && program.parent.onError) {
3501-
program.parent.run(program.parent.onError);
3597+
} else {
3598+
let parent = program.parent;
3599+
if (parent && parent.onError) {
3600+
parent.run(parent.onError);
3601+
}
35023602
}
35033603
return;
35043604
}
@@ -3637,9 +3737,9 @@ const EasyCoder = {
36373737
`${finishCompile - startCompile} ms`);
36383738
} catch (err) {
36393739
if (err.message !== `stop`) {
3640-
this.reportError(err, program, source);
3641-
if (program && program.onError) {
3642-
program.run(program.onError);
3740+
this.reportError(err, parent, source);
3741+
if (parent && parent.onError) {
3742+
parent.run(parent.onError);
36433743
}
36443744
}
36453745
}

easycoder/plugins/json.js

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,11 @@ const EasyCoder_Json = {
166166
break;
167167
case `split`:
168168
item = compiler.getNextValue();
169-
if (compiler.tokenIs(`into`)) {
169+
let on = `\n`;
170+
if (compiler.tokenIs(`on`)) {
171+
on = compiler.getNextValue();
172+
}
173+
if ([`giving`, `into`].includes(compiler.getToken())) {
170174
if (compiler.nextIsSymbol()) {
171175
const targetRecord = compiler.getSymbolRecord();
172176
if (targetRecord.keyword === `variable`) {
@@ -177,13 +181,39 @@ const EasyCoder_Json = {
177181
lino,
178182
request,
179183
item,
184+
on,
180185
target: targetRecord.name
181186
});
182187
return true;
183188
}
184189
}
185190
}
186191
break;
192+
case `replace`:
193+
if (compiler.nextTokenIs(`element`)) {
194+
const index = compiler.getNextValue();
195+
if (compiler.tokenIs(`of`)) {
196+
if (compiler.nextIsSymbol()) {
197+
const targetRecord = compiler.getSymbolRecord();
198+
if (targetRecord.keyword === `variable`) {
199+
if ([`by`, `with`].includes(compiler.nextToken())) {
200+
const value = compiler.getNextValue();
201+
compiler.addCommand({
202+
domain: `json`,
203+
keyword: `json`,
204+
lino,
205+
request,
206+
target: targetRecord.name,
207+
index,
208+
value
209+
});
210+
return true;
211+
}
212+
}
213+
}
214+
}
215+
}
216+
break;
187217
}
188218
compiler.addWarning(`Unrecognised json command syntax`);
189219
return false;
@@ -238,7 +268,7 @@ const EasyCoder_Json = {
238268
case `sort`:
239269
targetRecord = program.getSymbolRecord(command.target);
240270
const list = program.getValue(targetRecord.value[targetRecord.index]);
241-
content = list ? JSON.stringify(JSON.parse(list).sort()) : list;
271+
content = list ? JSON.stringify(JSON.parse(list).sort()) : null;
242272
targetRecord.value[targetRecord.index] = {
243273
type: `constant`,
244274
numeric: false,
@@ -328,13 +358,34 @@ const EasyCoder_Json = {
328358
targetRecord = program.getSymbolRecord(command.target);
329359
const existing = targetRecord.value[targetRecord.index].content;
330360
record = existing ? JSON.parse(existing) : [];
331-
record.push(content);
332-
targetRecord.value[targetRecord.index].content = JSON.stringify(record);
361+
record.push((`[`, `{`).includes(content[0]) ? JSON.parse(content) :content);
362+
targetRecord.value[targetRecord.index] = {
363+
type: `constant`,
364+
numeric: false,
365+
content: JSON.stringify(record)
366+
};
333367
break;
334368
case `split`:
335369
content = program.getValue(command.item);
370+
const on = program.getValue(command.on);
336371
targetRecord = program.getSymbolRecord(command.target);
337-
targetRecord.value[targetRecord.index].content = content.split(`\n`);
372+
targetRecord.value[targetRecord.index] = {
373+
type: `constant`,
374+
numeric: false,
375+
content: JSON.stringify(content.split(on))
376+
};
377+
break;
378+
case `replace`:
379+
targetRecord = program.getSymbolRecord(command.target);
380+
const index = program.getValue(command.index);
381+
const value = program.getValue(command.value);
382+
const current = targetRecord.value[targetRecord.index].content;
383+
record = current ? JSON.parse(current) : [];
384+
if (index > record.length - 1) {
385+
program.runtimeError(command.lino, `Index out of range`);
386+
}
387+
record[index] = value;
388+
targetRecord.value[targetRecord.index].content = JSON.stringify(record);
338389
break;
339390
}
340391
return command.pc + 1;

easycoder/plugins/rest.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ const EasyCoder_Rest = {
123123
// request.command = command;
124124

125125
request.onload = function () {
126-
if (200 <= request.status && request.status < 300) {
126+
if (200 <= request.status && request.status < 400) {
127127
var content = request.responseText.trim();
128128
if (command.target) {
129129
const targetRecord = program.getSymbolRecord(command.target);
@@ -135,12 +135,12 @@ const EasyCoder_Rest = {
135135
targetRecord.used = true;
136136
}
137137
} else {
138-
const error = `Error ${request.status}: ${request.statusText}`;
138+
const error = `${request.status} ${request.statusText}`;
139139
if (command.onError) {
140-
program.errorMessage = error;
140+
program.errorMessage = `Exception trapped: ${error}`;
141141
program.run(command.onError);
142142
} else {
143-
program.runtimeError(command.lino, error);
143+
program.runtimeError(command.lino, `Error: ${error}`);
144144
}
145145
}
146146
program.run(command.pc + 1);
@@ -167,7 +167,7 @@ const EasyCoder_Rest = {
167167
const value = program.getValue(command.value);
168168
console.log(`POST to ${path}`);
169169
// request.open(`POST`, path);
170-
if (value.charAt(0) === `{` || !isNaN(value)) {
170+
if (value.length >0 && value.charAt(0) === `{`) {
171171
request.setRequestHeader(`Content-Type`, `application/json; charset=UTF-8`);
172172
// console.log(`value=${value}`);
173173
request.send(value.charAt(0) === `{` ? value : value.toString());

0 commit comments

Comments
 (0)