force-lang 0.0.12 → 0.1.0
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/.eslintrc.cjs +54 -0
- package/README.md +40 -15
- package/doc/force-lang.1 +55 -36
- package/doc/force.1 +55 -36
- package/examples/cli-args.j +3 -3
- package/examples/nodejs-js-interaction.j +2 -1
- package/examples/testjs.js +2 -2
- package/examples/web_scrap.j +17 -0
- package/force +88 -88
- package/force-lang-0.1.0.tgz +0 -0
- package/package.json +17 -14
- package/src/BKlib.j +51 -0
- package/src/env.js +221 -162
- package/src/error.js +71 -59
- package/src/eval.js +454 -299
- package/src/force-lang.js +38 -34
- package/src/lib.j +17 -0
- package/src/load-file.js +62 -35
- package/src/native_lib.js +1702 -1230
- package/src/obj_utils.js +20 -18
- package/src/read.js +431 -309
- package/src/stack.js +113 -68
- package/src/token-stream.js +34 -28
- package/tests/pippo.j +1 -1
- package/tests/test-js-require.js +13 -9
- package/tests/test.j +1 -1
- package/tests/unit_tests.j +1 -1
- package/.eslintrc.js +0 -60
package/src/stack.js
CHANGED
|
@@ -1,71 +1,116 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import log from 'bunny-logger';
|
|
2
|
+
import obj_utils from './obj_utils.js';
|
|
3
3
|
|
|
4
|
-
class Stack{
|
|
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
|
-
|
|
4
|
+
class Stack {
|
|
5
|
+
constructor() {
|
|
6
|
+
this._stack = [];
|
|
7
|
+
this._rstack = [];
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
cloneJS(e) {
|
|
11
|
+
return typeof e === 'object' ? JSON.parse(JSON.stringify(e)) : e;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
push(e) {
|
|
15
|
+
this._stack.unshift(e);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
pop() {
|
|
19
|
+
return this._stack.shift();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
rpush(e) {
|
|
23
|
+
this._rstack.unshift(e);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
rpop() {
|
|
27
|
+
return this._rstack.shift();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
peek() {
|
|
31
|
+
// if(this._stack[0]?._type === 'TC_FUNC_JS') {
|
|
32
|
+
// return this._stack[0];
|
|
33
|
+
// }
|
|
34
|
+
// if(this._stack[0]?._type==='TC_JSON') {
|
|
35
|
+
// return this._stack[0];
|
|
36
|
+
// }
|
|
37
|
+
// //return this.cloneJS(this._stack[0]);
|
|
38
|
+
return this._stack[0];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
rpeek() {
|
|
42
|
+
return this._rstack[0];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
look_at(i) {
|
|
46
|
+
// return this.cloneJS(this._stack[i]);
|
|
47
|
+
return this._stack[i];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
get_list() {
|
|
51
|
+
return this._stack;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
item_to_str(item) {
|
|
55
|
+
let str = '{';
|
|
56
|
+
str += item._type;
|
|
57
|
+
if (item._name) {
|
|
58
|
+
str += ` ${item._name}`;
|
|
59
|
+
}
|
|
60
|
+
try {
|
|
61
|
+
switch (item._type) {
|
|
62
|
+
case 'TC_NUM':
|
|
63
|
+
case 'TC_STR':
|
|
64
|
+
case 'TC_BOOL':
|
|
65
|
+
str += ` ${item._datum}`;
|
|
66
|
+
break;
|
|
67
|
+
case 'TC_VAR':
|
|
68
|
+
if (item._datum._type == 'TC_JSON') {
|
|
69
|
+
str += ` ${obj_utils.stringify(item._datum._datum)}`;
|
|
70
|
+
} else {
|
|
71
|
+
str += ` ${item._datum._datum}`;
|
|
72
|
+
}
|
|
73
|
+
break;
|
|
74
|
+
case 'TC_JSON':
|
|
75
|
+
str += ` ${obj_utils.stringify(item._datum)}`;
|
|
76
|
+
break;
|
|
77
|
+
case 'TC_FUNC_JS':
|
|
78
|
+
str += ` ${obj_utils.stringify(item._datum)}`;
|
|
79
|
+
break;
|
|
80
|
+
case 'TC_LAMBDA_FUNC':
|
|
81
|
+
str += ' '; // + JSON.stringify(item._datum._datum);
|
|
82
|
+
break;
|
|
83
|
+
case 'TC_ERR':
|
|
84
|
+
str += ` ${JSON.stringify(item._datum)}`;
|
|
85
|
+
break;
|
|
86
|
+
case 'TC_PROMISE':
|
|
87
|
+
str += ` ${item._datum}`;
|
|
88
|
+
break;
|
|
89
|
+
default:
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
92
|
+
} catch (e) {
|
|
93
|
+
str += ' ... ';
|
|
94
|
+
}
|
|
95
|
+
str += '}';
|
|
96
|
+
return str;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
print() {
|
|
100
|
+
let pos = 1;
|
|
101
|
+
if (this._stack.length == 0) {
|
|
102
|
+
log.info('(empty)');
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
for (const item of this._stack) {
|
|
106
|
+
log.info(`${pos}: ${this.item_to_str(item)}`);
|
|
107
|
+
pos++;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
print_debug() {
|
|
112
|
+
log.info(this._stack);
|
|
113
|
+
}
|
|
69
114
|
}
|
|
70
115
|
|
|
71
|
-
|
|
116
|
+
export default Stack;
|
package/src/token-stream.js
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
import log from 'bunny-logger';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
if(!Array.isArray(tokens)){
|
|
8
|
-
//return false;
|
|
3
|
+
class TokenStream {
|
|
4
|
+
constructor(tokens, filename) {
|
|
5
|
+
if (!Array.isArray(tokens)) {
|
|
6
|
+
// return false;
|
|
9
7
|
throw new TypeError('tokens must be passed to TokenStream as an array.');
|
|
10
8
|
}
|
|
11
9
|
this._tokens = tokens;
|
|
@@ -13,61 +11,69 @@ class TokenStream{
|
|
|
13
11
|
this._col = 1;
|
|
14
12
|
this._filename = filename;
|
|
15
13
|
}
|
|
16
|
-
|
|
14
|
+
|
|
15
|
+
set_filename(name) {
|
|
17
16
|
this._filename = name;
|
|
18
17
|
}
|
|
18
|
+
|
|
19
19
|
lookahead(index) {
|
|
20
20
|
if (this._tokens.length <= index) {
|
|
21
21
|
return false;
|
|
22
|
-
//throw new Error('Cannot read past the end of a stream');
|
|
22
|
+
// throw new Error('Cannot read past the end of a stream');
|
|
23
23
|
}
|
|
24
24
|
return this._tokens[index];
|
|
25
25
|
}
|
|
26
|
+
|
|
26
27
|
peek() {
|
|
27
28
|
if (this._tokens.length === 0) {
|
|
28
29
|
return false;
|
|
29
|
-
//throw new Error('Cannot read past the end of a stream');
|
|
30
|
+
// throw new Error('Cannot read past the end of a stream');
|
|
30
31
|
}
|
|
31
32
|
return this._tokens[0];
|
|
32
33
|
}
|
|
34
|
+
|
|
33
35
|
advance() {
|
|
34
36
|
if (this._tokens.length === 0) {
|
|
35
37
|
return false;
|
|
36
|
-
//throw new Error('Cannot read past the end of a stream');
|
|
38
|
+
// throw new Error('Cannot read past the end of a stream');
|
|
37
39
|
}
|
|
38
40
|
this._col++;
|
|
39
|
-
try{
|
|
40
|
-
if(this.peek()=='\n'){
|
|
41
|
-
this._col=1;
|
|
41
|
+
try {
|
|
42
|
+
if (this.peek() == '\n') {
|
|
43
|
+
this._col = 1;
|
|
42
44
|
this._line++;
|
|
43
45
|
}
|
|
44
|
-
}catch(e){
|
|
45
|
-
//eof
|
|
46
|
+
} catch (e) {
|
|
47
|
+
// eof
|
|
46
48
|
this._col--;
|
|
47
49
|
}
|
|
48
50
|
return this._tokens.shift();
|
|
49
51
|
}
|
|
52
|
+
|
|
50
53
|
defer(token) {
|
|
51
54
|
this._col--;
|
|
52
|
-
try{
|
|
53
|
-
if(this.peek()=='\n'){
|
|
55
|
+
try {
|
|
56
|
+
if (this.peek() == '\n') {
|
|
54
57
|
this._col = 1;
|
|
55
58
|
this._line--;
|
|
56
59
|
}
|
|
57
|
-
}catch(e){
|
|
58
|
-
|
|
59
|
-
}
|
|
60
|
+
} catch (e) {}
|
|
60
61
|
this._tokens.unshift(token);
|
|
61
62
|
}
|
|
62
|
-
|
|
63
|
-
|
|
63
|
+
|
|
64
|
+
print() {
|
|
65
|
+
if (this._filename) {
|
|
66
|
+
log.info(`in file ${this._filename}`);
|
|
67
|
+
}
|
|
64
68
|
log.info(`at line ${this._line} , col ${this._col}`);
|
|
65
69
|
}
|
|
66
|
-
|
|
67
|
-
|
|
70
|
+
|
|
71
|
+
print_err() {
|
|
72
|
+
if (this._filename) {
|
|
73
|
+
log.error(`in file ${this._filename}`);
|
|
74
|
+
}
|
|
68
75
|
log.error(`at line ${this._line} , col ${this._col}`);
|
|
69
76
|
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
module.exports = TokenStream;
|
|
77
|
+
}
|
|
73
78
|
|
|
79
|
+
export default TokenStream;
|
package/tests/pippo.j
CHANGED
package/tests/test-js-require.js
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}
|
|
1
|
+
export default {
|
|
2
|
+
pippo: 2,
|
|
3
|
+
pluto: 'pluto',
|
|
4
|
+
kk: () => 2,
|
|
5
|
+
succ: x => x + 1,
|
|
6
|
+
add: (x, y) => x + y,
|
|
7
|
+
kk2() {
|
|
8
|
+
return this.pluto;
|
|
9
|
+
},
|
|
10
|
+
alvaro() {
|
|
11
|
+
return 'ww';
|
|
12
|
+
},
|
|
13
|
+
};
|
package/tests/test.j
CHANGED
package/tests/unit_tests.j
CHANGED
package/.eslintrc.js
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
module.exports = {
|
|
2
|
-
env: {
|
|
3
|
-
es6: true,
|
|
4
|
-
node: true,
|
|
5
|
-
},
|
|
6
|
-
extends: ['airbnb-base', 'prettier'],
|
|
7
|
-
plugins: ['prettier'],
|
|
8
|
-
rules: {
|
|
9
|
-
'prettier/prettier': [
|
|
10
|
-
'error',
|
|
11
|
-
{
|
|
12
|
-
printWidth: 120,
|
|
13
|
-
tabWidth: 2, // reformat to 2 eventually
|
|
14
|
-
useTabs: false, // default
|
|
15
|
-
semi: true, // default
|
|
16
|
-
singleQuote: true,
|
|
17
|
-
trailingComma: 'all',
|
|
18
|
-
bracketSpacing: true, // default
|
|
19
|
-
jsxBracketSameLine: true,
|
|
20
|
-
arrowParens: 'avoid', // default
|
|
21
|
-
},
|
|
22
|
-
],
|
|
23
|
-
indent: ['warn', 2],
|
|
24
|
-
camelcase: [0, { properties: 'never' }],
|
|
25
|
-
eqeqeq: ['error', 'always'],
|
|
26
|
-
curly: ['error', 'all'],
|
|
27
|
-
'no-prototype-builtins': 0, // refactor and enable
|
|
28
|
-
'consistent-return': 0, // refactor and enable
|
|
29
|
-
'no-param-reassign': 0, // refactor and enable
|
|
30
|
-
'no-unsafe-finally': 0, // refactor and enable
|
|
31
|
-
'no-use-before-define': 0, // refactor and enable
|
|
32
|
-
'import/no-unresolved': 0, // some linter import errors
|
|
33
|
-
'no-undef': 0,
|
|
34
|
-
'class-methods-use-this': 0, // refactor and enable
|
|
35
|
-
'vue/max-attributes-per-line': [
|
|
36
|
-
'error',
|
|
37
|
-
{
|
|
38
|
-
singleline: 5,
|
|
39
|
-
multiline: {
|
|
40
|
-
max: 5,
|
|
41
|
-
allowFirstLine: true,
|
|
42
|
-
},
|
|
43
|
-
},
|
|
44
|
-
],
|
|
45
|
-
'max-len': ['warn', 1000], // usage of SVG
|
|
46
|
-
'no-restricted-syntax': [
|
|
47
|
-
'error',
|
|
48
|
-
{
|
|
49
|
-
selector: 'LabeledStatement',
|
|
50
|
-
message:
|
|
51
|
-
'Labels are a form of GOTO; using them makes code confusing and hard to maintain and understand.',
|
|
52
|
-
},
|
|
53
|
-
{
|
|
54
|
-
selector: 'WithStatement',
|
|
55
|
-
message:
|
|
56
|
-
'`with` is disallowed in strict mode because it makes code impossible to predict and optimize.',
|
|
57
|
-
},
|
|
58
|
-
],
|
|
59
|
-
},
|
|
60
|
-
};
|