bare-script 2.1.0 → 2.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -5
- package/lib/model.js +3 -0
- package/lib/parser.js +11 -5
- package/lib/runtime.js +5 -2
- package/lib/runtimeAsync.js +10 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -33,7 +33,7 @@ import {parseScript} from 'bare-script/lib/parser.js';
|
|
|
33
33
|
// Parse the script
|
|
34
34
|
const script = parseScript(`\
|
|
35
35
|
# Double a number
|
|
36
|
-
function double(n)
|
|
36
|
+
function double(n):
|
|
37
37
|
return n * 2
|
|
38
38
|
endfunction
|
|
39
39
|
|
|
@@ -58,9 +58,9 @@ This outputs:
|
|
|
58
58
|
includes a set of built-in functions for mathematical operations, object manipulation, array
|
|
59
59
|
manipulation, regular expressions, HTTP fetch and more. The following example demonstrates the use
|
|
60
60
|
of the
|
|
61
|
-
[systemFetch](https://craigahobbs.github.io/bare-script/library/#var.
|
|
62
|
-
[objectGet](https://craigahobbs.github.io/bare-script/library/#var.
|
|
63
|
-
[arrayLength](https://craigahobbs.github.io/bare-script/library/#var.
|
|
61
|
+
[systemFetch](https://craigahobbs.github.io/bare-script/library/#var.vGroup='System'&systemfetch),
|
|
62
|
+
[objectGet](https://craigahobbs.github.io/bare-script/library/#var.vGroup='Object'&objectget), and
|
|
63
|
+
[arrayLength](https://craigahobbs.github.io/bare-script/library/#var.vGroup='Array'&arraylength)
|
|
64
64
|
functions.
|
|
65
65
|
|
|
66
66
|
~~~ javascript
|
|
@@ -134,7 +134,7 @@ bare script.bare
|
|
|
134
134
|
~~~
|
|
135
135
|
|
|
136
136
|
**Note:** In the BareScript CLI, import statements and the
|
|
137
|
-
[systemFetch](https://craigahobbs.github.io/bare-script/library/#var.
|
|
137
|
+
[systemFetch](https://craigahobbs.github.io/bare-script/library/#var.vGroup='System'&systemfetch)
|
|
138
138
|
function read non-URL paths from the local file system.
|
|
139
139
|
|
|
140
140
|
|
package/lib/model.js
CHANGED
|
@@ -79,6 +79,9 @@ struct FunctionStatement
|
|
|
79
79
|
# The function's argument names
|
|
80
80
|
optional string[len > 0] args
|
|
81
81
|
|
|
82
|
+
# If true, the function's last argument is the array of all remaining arguments
|
|
83
|
+
optional bool lastArgArray
|
|
84
|
+
|
|
82
85
|
# The function's statements
|
|
83
86
|
ScriptStatement[] statements
|
|
84
87
|
|
package/lib/parser.js
CHANGED
|
@@ -9,8 +9,10 @@ const rScriptLineSplit = /\r?\n/;
|
|
|
9
9
|
const rScriptContinuation = /\\\s*$/;
|
|
10
10
|
const rScriptComment = /^\s*(?:#.*)?$/;
|
|
11
11
|
const rScriptAssignment = /^\s*(?<name>[A-Za-z_]\w*)\s*=\s*(?<expr>.+)$/;
|
|
12
|
-
const rScriptFunctionBegin =
|
|
13
|
-
|
|
12
|
+
const rScriptFunctionBegin = new RegExp(
|
|
13
|
+
'^(?<async>\\s*async)?\\s*function\\s+(?<name>[A-Za-z_]\\w*)\\s*\\(' +
|
|
14
|
+
'\\s*(?<args>[A-Za-z_]\\w*(?:\\s*,\\s*[A-Za-z_]\\w*)*)?(?<lastArgArray>\\s*\\.\\.\\.)?\\s*\\)(?:\\s*:)?\\s*$'
|
|
15
|
+
);
|
|
14
16
|
const rScriptFunctionArgSplit = /\s*,\s*/;
|
|
15
17
|
const rScriptFunctionEnd = /^\s*endfunction\s*$/;
|
|
16
18
|
const rScriptLabel = /^\s*(?<name>[A-Za-z_]\w*)\s*:\s*$/;
|
|
@@ -119,14 +121,18 @@ export function parseScript(scriptText, startLineNumber = 1) {
|
|
|
119
121
|
functionDef = {
|
|
120
122
|
'function': {
|
|
121
123
|
'name': matchFunctionBegin.groups.name,
|
|
122
|
-
'args': typeof matchFunctionBegin.groups.args !== 'undefined'
|
|
123
|
-
? matchFunctionBegin.groups.args.split(rScriptFunctionArgSplit) : [],
|
|
124
124
|
'statements': []
|
|
125
125
|
}
|
|
126
126
|
};
|
|
127
|
-
if (matchFunctionBegin.groups.
|
|
127
|
+
if (typeof matchFunctionBegin.groups.args !== 'undefined') {
|
|
128
|
+
functionDef.function.args = matchFunctionBegin.groups.args.split(rScriptFunctionArgSplit);
|
|
129
|
+
}
|
|
130
|
+
if (typeof matchFunctionBegin.groups.async !== 'undefined') {
|
|
128
131
|
functionDef.function.async = true;
|
|
129
132
|
}
|
|
133
|
+
if (typeof matchFunctionBegin.groups.lastArgArray !== 'undefined') {
|
|
134
|
+
functionDef.function.lastArgArray = true;
|
|
135
|
+
}
|
|
130
136
|
statements.push(functionDef);
|
|
131
137
|
continue;
|
|
132
138
|
}
|
package/lib/runtime.js
CHANGED
|
@@ -134,8 +134,11 @@ export function executeScriptHelper(statements, options, locals) {
|
|
|
134
134
|
const funcLocals = {};
|
|
135
135
|
if ('args' in statement.function) {
|
|
136
136
|
const argsLength = args.length;
|
|
137
|
-
|
|
138
|
-
|
|
137
|
+
const funcArgsLength = statement.function.args.length;
|
|
138
|
+
const ixArgLast = (statement.function.lastArgArray ?? null) && (funcArgsLength - 1);
|
|
139
|
+
for (let ixArg = 0; ixArg < funcArgsLength; ixArg++) {
|
|
140
|
+
const argName = statement.function.args[ixArg];
|
|
141
|
+
funcLocals[argName] = (ixArg < argsLength ? (ixArg === ixArgLast ? args.slice(ixArg) : args[ixArg]) : null);
|
|
139
142
|
}
|
|
140
143
|
}
|
|
141
144
|
return executeScriptHelper(statement.function.statements, fnOptions, funcLocals);
|
package/lib/runtimeAsync.js
CHANGED
|
@@ -104,8 +104,11 @@ async function executeScriptHelperAsync(statements, options, locals) {
|
|
|
104
104
|
const funcLocals = {};
|
|
105
105
|
if ('args' in statement.function) {
|
|
106
106
|
const argsLength = args.length;
|
|
107
|
-
|
|
108
|
-
|
|
107
|
+
const funcArgsLength = statement.function.args.length;
|
|
108
|
+
const ixArgLast = (statement.function.lastArgArray ?? null) && (funcArgsLength - 1);
|
|
109
|
+
for (let ixArg = 0; ixArg < funcArgsLength; ixArg++) {
|
|
110
|
+
const argName = statement.function.args[ixArg];
|
|
111
|
+
funcLocals[argName] = (ixArg < argsLength ? (ixArg === ixArgLast ? args.slice(ixArg) : args[ixArg]) : null);
|
|
109
112
|
}
|
|
110
113
|
}
|
|
111
114
|
return executeScriptHelperAsync(statement.function.statements, fnOptions, funcLocals);
|
|
@@ -115,8 +118,11 @@ async function executeScriptHelperAsync(statements, options, locals) {
|
|
|
115
118
|
const funcLocals = {};
|
|
116
119
|
if ('args' in statement.function) {
|
|
117
120
|
const argsLength = args.length;
|
|
118
|
-
|
|
119
|
-
|
|
121
|
+
const funcArgsLength = statement.function.args.length;
|
|
122
|
+
const ixArgLast = (statement.function.lastArgArray ?? null) && (funcArgsLength - 1);
|
|
123
|
+
for (let ixArg = 0; ixArg < funcArgsLength; ixArg++) {
|
|
124
|
+
const argName = statement.function.args[ixArg];
|
|
125
|
+
funcLocals[argName] = (ixArg < argsLength ? (ixArg === ixArgLast ? args.slice(ixArg) : args[ixArg]) : null);
|
|
120
126
|
}
|
|
121
127
|
}
|
|
122
128
|
return executeScriptHelper(statement.function.statements, fnOptions, funcLocals);
|