Skip to content

Commit

Permalink
Enhancement for evaluating expressions (cncjs#444)
Browse files Browse the repository at this point in the history
* Enhancement for evaluating expressions

* Centralize global objects in one place

* Rename context to vars

* eslint fixes

* Update variables

* Update package.json
  • Loading branch information
cheton committed Apr 3, 2019
1 parent 4f6fddc commit 262e7b2
Show file tree
Hide file tree
Showing 14 changed files with 695 additions and 233 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@
"ensure-array": "~1.0.0",
"errorhandler": "~1.5.0",
"es5-shim": "~4.5.10",
"escodegen": "~1.11.1",
"esprima": "~4.0.1",
"expand-tilde": "~2.0.2",
"expr-eval": "~1.2.2",
Expand Down Expand Up @@ -264,7 +265,6 @@
"socketio-jwt": "~4.5.0",
"sortablejs": "~1.7.0",
"spawn-default-shell": "~2.0.0",
"static-eval": "~2.0.0",
"styled-components": "~3.3.3",
"superagent": "~3.8.3",
"superagent-use": "~0.1.0",
Expand Down
21 changes: 12 additions & 9 deletions src/app/controllers/Grbl/GrblController.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ import Workflow, {
WORKFLOW_STATE_RUNNING
} from '../../lib/Workflow';
import ensurePositiveNumber from '../../lib/ensure-positive-number';
import evaluateExpression from '../../lib/evaluate-expression';
import evaluateAssignmentExpression from '../../lib/evaluate-assignment-expression';
import logger from '../../lib/logger';
import translateExpression from '../../lib/translate-expression';
import config from '../../services/configstore';
import monitor from '../../services/monitor';
import taskRunner from '../../services/taskrunner';
import store from '../../store';
import {
GLOBAL_OBJECTS as globalObjects,
WRITE_SOURCE_CLIENT,
WRITE_SOURCE_FEEDER
} from '../constants';
Expand Down Expand Up @@ -191,7 +192,7 @@ class GrblController {

// Expression
// %_x=posx,_y=posy,_z=posz
evaluateExpression(line.slice(1), context);
evaluateAssignmentExpression(line.slice(1), context);
return '';
}

Expand Down Expand Up @@ -277,7 +278,7 @@ class GrblController {

// Expression
// %_x=posx,_y=posy,_z=posz
evaluateExpression(line.slice(1), context);
evaluateAssignmentExpression(line.slice(1), context);
return '';
}

Expand Down Expand Up @@ -730,35 +731,33 @@ class GrblController {
const tool = this.runner.getTool();

return Object.assign(context || {}, {
// Primitive types and global variables
Boolean,
Number,
Object,
String,
JSON,
// User-defined global variables
global: this.sharedContext,

// Bounding box
xmin: Number(context.xmin) || 0,
xmax: Number(context.xmax) || 0,
ymin: Number(context.ymin) || 0,
ymax: Number(context.ymax) || 0,
zmin: Number(context.zmin) || 0,
zmax: Number(context.zmax) || 0,

// Machine position
mposx: Number(mposx) || 0,
mposy: Number(mposy) || 0,
mposz: Number(mposz) || 0,
mposa: Number(mposa) || 0,
mposb: Number(mposb) || 0,
mposc: Number(mposc) || 0,

// Work position
posx: Number(posx) || 0,
posy: Number(posy) || 0,
posz: Number(posz) || 0,
posa: Number(posa) || 0,
posb: Number(posb) || 0,
posc: Number(posc) || 0,

// Modal group
modal: {
motion: modal.motion,
Expand All @@ -772,8 +771,12 @@ class GrblController {
// M7 and M8 may be active at the same time, but a modal group violation might occur when issuing M7 and M8 together on the same line. Using the new line character (\n) to separate lines can avoid this issue.
coolant: ensureArray(modal.coolant).join('\n'),
},

// Tool
tool: Number(tool) || 0,

// Global objects
...globalObjects,
});
}
clearActionValues() {
Expand Down
20 changes: 11 additions & 9 deletions src/app/controllers/Marlin/MarlinController.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ import Workflow, {
} from '../../lib/Workflow';
import delay from '../../lib/delay';
import ensurePositiveNumber from '../../lib/ensure-positive-number';
import evaluateExpression from '../../lib/evaluate-expression';
import evaluateAssignmentExpression from '../../lib/evaluate-assignment-expression';
import logger from '../../lib/logger';
import translateExpression from '../../lib/translate-expression';
import config from '../../services/configstore';
import monitor from '../../services/monitor';
import taskRunner from '../../services/taskrunner';
import store from '../../store';
import {
GLOBAL_OBJECTS as globalObjects,
WRITE_SOURCE_CLIENT,
WRITE_SOURCE_SERVER,
WRITE_SOURCE_FEEDER,
Expand Down Expand Up @@ -352,7 +353,7 @@ class MarlinController {

// Expression
// %_x=posx,_y=posy,_z=posz
evaluateExpression(line.slice(1), context);
evaluateAssignmentExpression(line.slice(1), context);
return '';
}

Expand Down Expand Up @@ -446,7 +447,7 @@ class MarlinController {

// Expression
// %_x=posx,_y=posy,_z=posz
evaluateExpression(line.slice(1), context);
evaluateAssignmentExpression(line.slice(1), context);
return '';
}

Expand Down Expand Up @@ -769,26 +770,23 @@ class MarlinController {
const tool = this.runner.getTool();

return Object.assign(context || {}, {
// Primitive types and global variables
Boolean,
Number,
Object,
String,
JSON,
// User-defined global variables
global: this.sharedContext,

// Bounding box
xmin: Number(context.xmin) || 0,
xmax: Number(context.xmax) || 0,
ymin: Number(context.ymin) || 0,
ymax: Number(context.ymax) || 0,
zmin: Number(context.zmin) || 0,
zmax: Number(context.zmax) || 0,

// Work position
posx: Number(posx) || 0,
posy: Number(posy) || 0,
posz: Number(posz) || 0,
pose: Number(pose) || 0,

// Modal group
modal: {
motion: modal.motion,
Expand All @@ -802,8 +800,12 @@ class MarlinController {
// M7 and M8 may be active at the same time, but a modal group violation might occur when issuing M7 and M8 together on the same line. Using the new line character (\n) to separate lines can avoid this issue.
coolant: ensureArray(modal.coolant).join('\n'),
},

// Tool
tool: Number(tool) || 0,

// Global objects
...globalObjects,
});
}
destroy() {
Expand Down
21 changes: 12 additions & 9 deletions src/app/controllers/Smoothie/SmoothieController.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ import Workflow, {
} from '../../lib/Workflow';
import delay from '../../lib/delay';
import ensurePositiveNumber from '../../lib/ensure-positive-number';
import evaluateExpression from '../../lib/evaluate-expression';
import evaluateAssignmentExpression from '../../lib/evaluate-assignment-expression';
import logger from '../../lib/logger';
import translateExpression from '../../lib/translate-expression';
import config from '../../services/configstore';
import monitor from '../../services/monitor';
import taskRunner from '../../services/taskrunner';
import store from '../../store';
import {
GLOBAL_OBJECTS as globalObjects,
WRITE_SOURCE_CLIENT,
WRITE_SOURCE_FEEDER
} from '../constants';
Expand Down Expand Up @@ -165,7 +166,7 @@ class SmoothieController {

// Expression
// %_x=posx,_y=posy,_z=posz
evaluateExpression(line.slice(1), context);
evaluateAssignmentExpression(line.slice(1), context);
return '';
}

Expand Down Expand Up @@ -244,7 +245,7 @@ class SmoothieController {

// Expression
// %_x=posx,_y=posy,_z=posz
evaluateExpression(line.slice(1), context);
evaluateAssignmentExpression(line.slice(1), context);
return '';
}

Expand Down Expand Up @@ -635,35 +636,33 @@ class SmoothieController {
const tool = this.runner.getTool();

return Object.assign(context || {}, {
// Primitive types and global variables
Boolean,
Number,
Object,
String,
JSON,
// User-defined global variables
global: this.sharedContext,

// Bounding box
xmin: Number(context.xmin) || 0,
xmax: Number(context.xmax) || 0,
ymin: Number(context.ymin) || 0,
ymax: Number(context.ymax) || 0,
zmin: Number(context.zmin) || 0,
zmax: Number(context.zmax) || 0,

// Machine position
mposx: Number(mposx) || 0,
mposy: Number(mposy) || 0,
mposz: Number(mposz) || 0,
mposa: Number(mposa) || 0,
mposb: Number(mposb) || 0,
mposc: Number(mposc) || 0,

// Work position
posx: Number(posx) || 0,
posy: Number(posy) || 0,
posz: Number(posz) || 0,
posa: Number(posa) || 0,
posb: Number(posb) || 0,
posc: Number(posc) || 0,

// Modal group
modal: {
motion: modal.motion,
Expand All @@ -677,8 +676,12 @@ class SmoothieController {
// M7 and M8 may be active at the same time, but a modal group violation might occur when issuing M7 and M8 together on the same line. Using the new line character (\n) to separate lines can avoid this issue.
coolant: ensureArray(modal.coolant).join('\n'),
},

// Tool
tool: Number(tool) || 0,

// Global objects
...globalObjects,
});
}
clearActionValues() {
Expand Down
21 changes: 12 additions & 9 deletions src/app/controllers/TinyG/TinyGController.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ import Workflow, {
} from '../../lib/Workflow';
import delay from '../../lib/delay';
import ensurePositiveNumber from '../../lib/ensure-positive-number';
import evaluateExpression from '../../lib/evaluate-expression';
import evaluateAssignmentExpression from '../../lib/evaluate-assignment-expression';
import logger from '../../lib/logger';
import translateExpression from '../../lib/translate-expression';
import config from '../../services/configstore';
import monitor from '../../services/monitor';
import taskRunner from '../../services/taskrunner';
import store from '../../store';
import {
GLOBAL_OBJECTS as globalObjects,
WRITE_SOURCE_CLIENT,
WRITE_SOURCE_FEEDER
} from '../constants';
Expand Down Expand Up @@ -198,7 +199,7 @@ class TinyGController {

// Expression
// %_x=posx,_y=posy,_z=posz
evaluateExpression(line.slice(1), context);
evaluateAssignmentExpression(line.slice(1), context);
return '';
}

Expand Down Expand Up @@ -276,7 +277,7 @@ class TinyGController {

// Expression
// %_x=posx,_y=posy,_z=posz
evaluateExpression(line.slice(1), context);
evaluateAssignmentExpression(line.slice(1), context);
return '';
}

Expand Down Expand Up @@ -761,35 +762,33 @@ class TinyGController {
const tool = this.runner.getTool();

return Object.assign(context || {}, {
// Primitive types and global variables
Boolean,
Number,
Object,
String,
JSON,
// User-defined global variables
global: this.sharedContext,

// Bounding box
xmin: Number(context.xmin) || 0,
xmax: Number(context.xmax) || 0,
ymin: Number(context.ymin) || 0,
ymax: Number(context.ymax) || 0,
zmin: Number(context.zmin) || 0,
zmax: Number(context.zmax) || 0,

// Machine position
mposx: Number(mposx) || 0,
mposy: Number(mposy) || 0,
mposz: Number(mposz) || 0,
mposa: Number(mposa) || 0,
mposb: Number(mposb) || 0,
mposc: Number(mposc) || 0,

// Work position
posx: Number(posx) || 0,
posy: Number(posy) || 0,
posz: Number(posz) || 0,
posa: Number(posa) || 0,
posb: Number(posb) || 0,
posc: Number(posc) || 0,

// Modal group
modal: {
motion: modal.motion,
Expand All @@ -803,8 +802,12 @@ class TinyGController {
// M7 and M8 may be active at the same time, but a modal group violation might occur when issuing M7 and M8 together on the same line. Using the new line character (\n) to separate lines can avoid this issue.
coolant: ensureArray(modal.coolant).join('\n'),
},

// Tool
tool: Number(tool) || 0,

// Global objects
...globalObjects,
});
}
clearActionValues() {
Expand Down
24 changes: 24 additions & 0 deletions src/app/controllers/constants.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects
export const GLOBAL_OBJECTS = {
// Function properties
parseFloat,
parseInt,

// Fundamental objects
Object,
Function,
Boolean,

// Numbers and dates
Number,
Math,
Date,

// Text processing
String,
RegExp,

// Structured data
JSON,
};

// Write Source
export const WRITE_SOURCE_CLIENT = 'client';
export const WRITE_SOURCE_SERVER = 'server';
Expand Down
Loading

0 comments on commit 262e7b2

Please sign in to comment.