force-lang 0.0.13 → 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/src/stack.js CHANGED
@@ -1,75 +1,116 @@
1
- const log = require('bunny-logger');
2
- const obj_utils = require('./obj_utils');
1
+ import log from 'bunny-logger';
2
+ import obj_utils from './obj_utils.js';
3
3
 
4
- class Stack{
5
- constructor(){
6
- this._stack=[];
7
- }
8
- push(e){
9
- this._stack.unshift(e);
10
- }
11
- pop(){
12
- return this._stack.shift();
13
- }
14
- peek(){
15
- return this._stack[0];
16
- }
17
- look_at(i){
18
- return this._stack[i];
19
- }
20
- item_to_str(item){
21
- var str = '{';
22
- str += item._type;
23
- if(item._name) str += ' ' + item._name;
24
- try{
25
- switch(item._type){
26
- case 'TC_NUM':
27
- case 'TC_STR':
28
- case 'TC_BOOL':
29
- str += ' ' + item._datum;
30
- break;
31
- case 'TC_VAR':
32
- if(item._datum._type=='TC_JSON') str += ' ' + obj_utils.stringify(item._datum._datum);
33
- else str += ' ' + item._datum._datum;
34
- break;
35
- case 'TC_JSON':
36
- str += ' ' + obj_utils.stringify(item._datum);
37
- break;
38
- case 'TC_FUNC_JS':
39
- str += ' ' + obj_utils.stringify(item._datum);
40
- break;
41
- case 'TC_LAMBDA_FUNC':
42
- str += ' ' //+ JSON.stringify(item._datum._datum);
43
- break;
44
- case 'TC_ERR':
45
- str += ' ' + JSON.stringify(item._datum);
46
- break;
47
- case 'TC_PROMISE':
48
- str += ' ' + item._datum;
49
- break;
50
- default:
51
- break;
52
- }
53
- }catch(e) {
54
- str += ' ... ';
55
- }
56
- str += '}';
57
- return str;
58
- }
59
- print(){
60
- var pos=1;
61
- if(this._stack.length == 0){
62
- log.info('(empty)');
63
- return;
64
- }
65
- for(var item of this._stack){
66
- log.info(`${pos}: ${this.item_to_str(item)}`);
67
- pos++;
68
- }
69
- }
70
- print_debug(){
71
- log.info(this._stack);
72
- }
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
+ }
73
114
  }
74
115
 
75
- module.exports = Stack;
116
+ export default Stack;
@@ -1,11 +1,9 @@
1
- 'use strict';
1
+ import log from 'bunny-logger';
2
2
 
3
- const log = require('bunny-logger');
4
-
5
- class TokenStream{
6
- constructor(tokens, filename){
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
- set_filename(name){
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
- print(){
63
- if(this._filename) log.info(`in file ${this._filename}`);
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
- print_err(){
67
- if(this._filename) log.error(`in file ${this._filename}`);
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
@@ -1,4 +1,4 @@
1
- #!/usr/local/bin/force
1
+ #!/usr/bin/env force
2
2
 
3
3
  "Hi !!!"
4
4
  .
@@ -1,9 +1,13 @@
1
- module.exports= {
2
- pippo:2,
3
- pluto:"pluto",
4
- kk:()=>{return 2;},
5
- succ:(x)=>{return x+1;},
6
- add:(x,y)=>{return x+y;},
7
- kk2:function(){ return this.pluto;},
8
- alvaro: function(){return "ww";}
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
@@ -1,4 +1,4 @@
1
- #!/usr/local/bin/force
1
+ #!/usr/bin/env force
2
2
 
3
3
  : xx2 33 44 ;
4
4
  xx2
@@ -1,4 +1,4 @@
1
- #!../src/force -f
1
+ #!/usr/bin/env force
2
2
 
3
3
  : eq = ;
4
4
 
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
- };