Skip to content

Commit 159cb1b

Browse files
committed
"round" in "float" plugin
1 parent e495ad1 commit 159cb1b

File tree

4 files changed

+71
-21
lines changed

4 files changed

+71
-21
lines changed

dist/easycoder-min.js

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

dist/easycoder.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3546,8 +3546,8 @@ const EasyCoder_Browser = {
35463546
}
35473547
} else {
35483548
const imports = compiler.imports;
3549-
if (imports && imports.length > 0
3550-
&& compiler.program[compiler.symbols[imports[0]].pc].keyword === `div`) {
3549+
if (imports && imports.length > 0 && compiler.parent === `Codex`) {
3550+
// && compiler.program[compiler.parent.symbols[imports[0]].pc].keyword === `div`) {
35513551
// This is used by Codex to force run in Run panel, which must be the first import
35523552
compiler.addCommand({
35533553
domain: `browser`,
@@ -8162,7 +8162,6 @@ const EasyCoder = {
81628162
console.log(errString);
81638163
return;
81648164
}
8165-
// const compiler = EasyCoder_Compiler;
81668165
const {
81678166
tokens,
81688167
scriptLines
@@ -8205,7 +8204,7 @@ const EasyCoder = {
82058204
},
82068205

82078206
verifySymbol: function (name) {
8208-
return this.symbols.hasOwnProperty(name);
8207+
return typeof this.symbols[name] !== `undefined`;
82098208
},
82108209

82118210
encode: function (value) {
@@ -8348,10 +8347,11 @@ const EasyCoder = {
83488347
this.compiler = compiler;
83498348
compiler.value = EasyCoder_Value;
83508349
compiler.condition = EasyCoder_Condition;
8350+
compiler.parent = parent;
83518351
compiler.domain = this.domain;
83528352
compiler.imports = imports;
83538353
compiler.continue = false;
8354-
const program = EasyCoder_Compiler.compile(tokens);
8354+
const program = compiler.compile(tokens);
83558355
// console.log('Program: ' + JSON.stringify(program, null, 2));
83568356
this.compiling = false;
83578357

dist/plugins/float.js

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -410,20 +410,45 @@ const EasyCoder_Float = {
410410

411411
value: {
412412

413-
compile: () => {
413+
compile: (compiler) => {
414+
const type = compiler.getToken();
415+
if (type === `round`) {
416+
try {
417+
const value = compiler.getNextValue();
418+
const decimals = compiler.getValue();
419+
return {
420+
domain: `float`,
421+
type,
422+
value,
423+
decimals
424+
};
425+
} catch (err) {
426+
return null;
427+
}
428+
}
414429
return null;
415430
},
416431

417-
get: () => {
432+
get: (program, value) => {
433+
switch (value.type) {
434+
case `round`:
435+
const v = parseFloat(program.getValue(value.value));
436+
const dp = program.getValue(value.decimals);
437+
return {
438+
type: `constant`,
439+
numeric: false,
440+
content: String(v.toFixed(dp))
441+
};
442+
}
418443
return null;
419444
}
420445
},
421446

422447
condition: {
423448

424-
compile: () => {},
449+
compile: () => { },
425450

426-
test: () => {}
451+
test: () => { }
427452
}
428453
};
429454

js/plugins/float.js

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -410,20 +410,45 @@ const EasyCoder_Float = {
410410

411411
value: {
412412

413-
compile: () => {
413+
compile: (compiler) => {
414+
const type = compiler.getToken();
415+
if (type === `round`) {
416+
try {
417+
const value = compiler.getNextValue();
418+
const decimals = compiler.getValue();
419+
return {
420+
domain: `float`,
421+
type,
422+
value,
423+
decimals
424+
};
425+
} catch (err) {
426+
return null;
427+
}
428+
}
414429
return null;
415430
},
416431

417-
get: () => {
432+
get: (program, value) => {
433+
switch (value.type) {
434+
case `round`:
435+
const v = parseFloat(program.getValue(value.value));
436+
const dp = program.getValue(value.decimals);
437+
return {
438+
type: `constant`,
439+
numeric: false,
440+
content: String(v.toFixed(dp))
441+
};
442+
}
418443
return null;
419444
}
420445
},
421446

422447
condition: {
423448

424-
compile: () => {},
449+
compile: () => { },
425450

426-
test: () => {}
451+
test: () => { }
427452
}
428453
};
429454

0 commit comments

Comments
 (0)