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/force CHANGED
@@ -1,90 +1,90 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- //const repl = require('repl');
4
- var program = require('commander');
5
- const log = require('bunny-logger');
6
- const readline = require('readline');
7
- const pkg = require('./package');
8
- const force = require('./src/force-lang');
9
-
10
- (async function main(){
11
-
12
- program
13
- .version(pkg.version)
14
- .usage('[options] <file ...>')
15
- //.option('-f, --file [file]', 'input from file')
16
- .option('-x, --exec [text]', 'input from string')
17
- .option('-i, --shell', 'run repl')
18
- .option('-s, --stdin', 'input from stdin (default action)')
19
- .parse(process.argv);
20
-
21
- await force.load_lib();
22
-
23
- if(! (program.file || program.shell || program.exec || program.args[0])) program.stdin = true;
24
-
25
- if(program.file){
26
- if(typeof(program.file)!=='string'){
27
- log.error('no file given.');
28
- process.exit(1);
29
- }
30
- await force.eval_file(program.file);
31
- }
32
-
33
- if(program.exec){
34
- if(typeof(program.exec)!=='string'){
35
- log.error('no input given.');
36
- process.exit(1);
37
- }
38
- force.exec(program.exec);
39
- }
40
- if(program.shell){
41
- //repl.start({prompt: '> ',eval: function(e){eval.eval(e);}});
42
-
43
- force.populate_repl();
44
-
45
- var buffered_line = '';
46
-
47
- const rl = readline.createInterface({
48
- input: process.stdin,
49
- output: process.stdout,
50
- prompt: '> '
51
- });
52
-
53
- rl.prompt();
54
-
55
- rl.on('line', (line) => {
56
- if(line[line.length-1]==='\\'){
57
- buffered_line += line.substr(0,line.length-1) +' ';
58
- rl.setPrompt('... ');
59
- } else {
60
- if(buffered_line!=='') {
61
- buffered_line += line;
62
- force.exec(buffered_line);
63
- buffered_line='';
64
- }else {
65
- force.exec(line);
66
- }
67
- rl.setPrompt('> ');
68
- }
69
- rl.prompt();
70
- });
71
- }
72
-
73
- if(program.args[0]){
74
- await force.eval_file(program.args[0]);
75
- }
76
-
77
- if(program.stdin){
78
- //log.info(process.stdin);
79
- var rl = readline.createInterface({
80
- input: process.stdin,
81
- output: process.stdout,
82
- terminal: false
83
- });
84
-
85
- rl.on('line', function(line){
86
- force.exec(line);
87
- })
88
- }
89
-
90
- })();
3
+ // const repl = require('repl');
4
+ import { Command } from 'commander';
5
+ import log from 'bunny-logger';
6
+ // import readline from 'readline';
7
+ import readline from 'node:readline';
8
+ import { stdin as input, stdout as output } from 'node:process';
9
+ // const pkg = require('./package');
10
+ // import pkg from './package.json' assert { type: 'json' };
11
+ import { createRequire } from 'module';
12
+ import force from './src/force-lang.js';
13
+
14
+ (async function main() {
15
+ const program = new Command();
16
+ const require = createRequire(import.meta.url);
17
+ const pkg = require('./package.json');
18
+
19
+ program
20
+ .version(pkg.version)
21
+ .usage('[options] [file [args...]]')
22
+ .arguments('[files...]')
23
+ // .option('-f, --file [file]', 'input from file')
24
+ .option('-x, --exec [text]', 'input from string')
25
+ .option('-i, --shell', 'run repl')
26
+ .option('-s, --stdin', 'input from stdin (default action)')
27
+ .allowUnknownOption();
28
+ // .helpOption('-H, --help', 'display help for force');
29
+ program.parse(process.argv);
30
+ const options = program.opts();
31
+
32
+ await force.load_lib();
33
+
34
+ if (program.args.length > 0) {
35
+ const scriptFile = program.args[0];
36
+ const scriptArgs = program.args.slice(1);
37
+ process.argv = [process.argv[0], scriptFile, ...scriptArgs];
38
+ await force.eval_file(scriptFile);
39
+ } else if (options.exec) {
40
+ if (typeof options.exec !== 'string') {
41
+ log.error('no input given.');
42
+ process.exit(1);
43
+ }
44
+ await force.exec(options.exec);
45
+ } else if (options.shell) {
46
+ // repl.start({prompt: '> ',eval: function(e){eval.eval(e);}});
47
+
48
+ force.populate_repl();
49
+
50
+ let buffered_line = '';
51
+ input.setRawMode(true);
52
+ input.resume();
53
+
54
+ const rl = readline.createInterface({
55
+ input,
56
+ output,
57
+ });
58
+
59
+ rl.setPrompt('> ');
60
+ rl.prompt();
61
+
62
+ rl.on('line', async line => {
63
+ if (line[line.length - 1] === '\\') {
64
+ buffered_line += `${line.substr(0, line.length - 1)} `;
65
+ rl.setPrompt('... ');
66
+ } else {
67
+ if (buffered_line !== '') {
68
+ buffered_line += line;
69
+ await force.exec(buffered_line);
70
+ buffered_line = '';
71
+ } else {
72
+ await force.exec(line);
73
+ }
74
+ rl.setPrompt('> ');
75
+ }
76
+ rl.prompt();
77
+ });
78
+ } else {
79
+ // log.info(process.stdin);
80
+ const rl = readline.createInterface({
81
+ input: process.stdin,
82
+ output: process.stdout,
83
+ terminal: false,
84
+ });
85
+
86
+ rl.on('line', async line => {
87
+ await force.exec(line);
88
+ });
89
+ }
90
+ })();
Binary file
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "force-lang",
3
- "version": "0.0.13",
3
+ "version": "0.1.0",
4
4
  "description": "a modern forth lang compatible with NodeJS",
5
+ "type": "module",
5
6
  "main": "./src/force-lang.js",
6
7
  "bin": {
7
8
  "force": "./force"
@@ -23,14 +24,12 @@
23
24
  },
24
25
  "dependencies": {
25
26
  "bunny-logger": "0.0.2",
26
- "cheerio": "^1.0.0-rc.3",
27
- "commander": "^2.19.0",
28
- "format": "^0.2.2",
29
- "json5": "^2.1.0",
30
- "marked-man": "^0.2.1",
31
- "request": "^2.88.0",
32
- "request-promise-native": "^1.0.5",
33
- "sync-request": "^6.0.0"
27
+ "cheerio": "^1.2.0",
28
+ "commander": "^14.0.2",
29
+ "got": "^14.6.6",
30
+ "json5": "^2.2.3",
31
+ "marked-man": "^2.1.0",
32
+ "sprintf-js": "^1.1.3"
34
33
  },
35
34
  "keywords": [
36
35
  "forth",
@@ -41,11 +40,14 @@
41
40
  ],
42
41
  "homepage": "https://github.com/elboza/force-lang.git",
43
42
  "devDependencies": {
44
- "eslint": "^6.8.0",
45
- "eslint-config-airbnb-base": "^14.1.0",
46
- "eslint-config-prettier": "^6.10.1",
47
- "eslint-plugin-import": "^2.20.2",
48
- "eslint-plugin-prettier": "^3.1.2",
49
- "prettier": "^2.0.4"
43
+ "eslint": "^8.57.1",
44
+ "eslint-config-airbnb-base": "^15.0.0",
45
+ "eslint-config-prettier": "^10.1.8",
46
+ "eslint-plugin-import": "^2.32.0",
47
+ "eslint-plugin-prettier": "^5.5.5",
48
+ "prettier": "^3.8.1"
49
+ },
50
+ "resolutions": {
51
+ "moment": "2.29.4"
50
52
  }
51
53
  }
package/src/BKlib.j ADDED
@@ -0,0 +1,51 @@
1
+ : cr
2
+ 10 emit ;
3
+
4
+ : true
5
+ T ;
6
+
7
+ : false
8
+ F ;
9
+
10
+ : is_truthy
11
+ is_falsy not ;
12
+
13
+ : nip
14
+ swap drop ;
15
+
16
+ : ddup
17
+ over over ;
18
+
19
+ : ndrop2
20
+ dup is_num if
21
+ 'no number on TOS' throw else
22
+ begin dup 0 > while 1 - swap drop repeat drop then
23
+ ;
24
+
25
+ : ddrop
26
+ drop drop ;
27
+
28
+ : f+
29
+ case
30
+ dup is_string of s:+ endof
31
+ over is_string of s:+ endof
32
+ dup is_list of a:+ endof
33
+ dup is_num of n:+ endof
34
+ endcase ;
35
+
36
+ : a:join
37
+ s:join ;
38
+
39
+ : j:encode
40
+ j:stringify ;
41
+
42
+ : j:decode
43
+ j:parse ;
44
+
45
+ : a:nth \ (a n -- o)
46
+ 0 begin ddup > while >r >r a:tail r> r> 1 + repeat ddrop a:shift ;
47
+
48
+ : s:chomp \ ( s -- s )
49
+ "" "\n$" rx:replace ;
50
+
51
+ \ a:find notes \ [11,22,33] ( dup a:len begin dup 0 > while 1 - "q " . ddup a:nth .s repeat "f" . ) !!
package/src/env.js CHANGED
@@ -1,163 +1,222 @@
1
- const log = require('bunny-logger');
2
- const Stack = require('./stack');
3
-
4
- class dict_obj{
5
- constructor(){
6
- this._type = null;
7
- this._name = null;
8
- this._datum = null;
9
- this._where = null;
10
- }
1
+ import log from 'bunny-logger';
2
+ import Stack from './stack.js';
3
+
4
+ class dict_obj {
5
+ constructor() {
6
+ this._type = null;
7
+ this._name = null;
8
+ this._datum = null;
9
+ this._where = null;
10
+ }
11
+ }
12
+ class Env {
13
+ constructor() {
14
+ this._use_ns = false;
15
+ this._dict = [];
16
+ this._dict_ns = {};
17
+
18
+ this.s = new Stack();
19
+ }
20
+
21
+ print_stack() {
22
+ this.s.print();
23
+ }
24
+
25
+ TOS() {
26
+ return this.s.peek();
27
+ }
28
+
29
+ RTOS() {
30
+ return this.s.rpeek();
31
+ }
32
+
33
+ TOS2() {
34
+ return this.s.look_at(1);
35
+ }
36
+
37
+ is_bool(e) {
38
+ if (e && e._type == 'TC_BOOL') {
39
+ return true;
40
+ }
41
+ return false;
42
+ }
43
+
44
+ is_true(e) {
45
+ if (e && e._type == 'TC_BOOL' && e._datum == 'T') {
46
+ return true;
47
+ }
48
+ return false;
49
+ }
50
+
51
+ is_false(e) {
52
+ if (e && e._type == 'TC_BOOL' && e._datum == 'F') {
53
+ return true;
54
+ }
55
+ return false;
56
+ }
57
+
58
+ is_num(e) {
59
+ if (e && e._type == 'TC_NUM') {
60
+ return true;
61
+ }
62
+ return false;
63
+ }
64
+
65
+ is_string(e) {
66
+ if (e && e._type == 'TC_STR') {
67
+ return true;
68
+ }
69
+ return false;
70
+ }
71
+
72
+ is_json(e) {
73
+ if (e && e._type == 'TC_JSON') {
74
+ return true;
75
+ }
76
+ return false;
77
+ }
78
+
79
+ is_obj(e) {
80
+ if (e && e._type == 'TC_JSON' && !this.is_list(e)) {
81
+ return true;
82
+ }
83
+ return false;
84
+ }
85
+
86
+ is_list(e) {
87
+ if (e && e._type == 'TC_JSON' && e._datum instanceof Array) {
88
+ return true;
89
+ }
90
+ return false;
91
+ }
92
+
93
+ true_obj() {
94
+ return { _type: 'TC_BOOL', _datum: 'T' };
95
+ }
96
+
97
+ false_obj() {
98
+ return { _type: 'TC_BOOL', _datum: 'F' };
99
+ }
100
+
101
+ adj_bool_val(e) {
102
+ if (e === true) {
103
+ return 'T';
104
+ }
105
+ if (e === false) {
106
+ return 'F';
107
+ }
108
+ return e;
109
+ }
110
+
111
+ guess_type(e) {
112
+ switch (typeof e) {
113
+ case 'number':
114
+ return 'TC_NUM';
115
+ break;
116
+ case 'string':
117
+ return 'TC_STR';
118
+ break;
119
+ case 'object':
120
+ if (e instanceof Promise) {
121
+ return 'TC_PROMISE';
122
+ }
123
+ return 'TC_JSON';
124
+ break;
125
+ case 'boolean':
126
+ return 'TC_BOOL';
127
+ break;
128
+ case 'function':
129
+ return 'TC_FUNC_JS';
130
+ break;
131
+ default:
132
+ return 'TC_UNDEF';
133
+ break;
134
+ }
135
+ }
136
+
137
+ get_bool_val(e) {
138
+ if (this.is_bool(e)) {
139
+ if (this.is_true(e)) {
140
+ return true;
141
+ }
142
+ if (this.is_false(e)) {
143
+ return false;
144
+ }
145
+ }
146
+ }
147
+
148
+ set_bool_val(e) {
149
+ if (e === true) {
150
+ return this.true_obj();
151
+ }
152
+ if (e === false) {
153
+ return this.false_obj();
154
+ }
155
+ }
156
+
157
+ lookup_ns(name) {}
158
+
159
+ lookup_norm(name, type) {
160
+ for (const item of this._dict) {
161
+ // if(type && item._type == type)
162
+ if (item._name == name) {
163
+ return item;
164
+ }
165
+ }
166
+ return false;
167
+ }
168
+
169
+ lookup(name, type) {
170
+ return this.lookup_norm(name, type);
171
+ }
172
+
173
+ delete_norm(name) {
174
+ const index = this._dict.map(e => e._name).indexOf(name);
175
+ // log.info(index);
176
+ // log.info(this._dict[index]);
177
+ if (index >= 0) {
178
+ this._dict.splice(index, 1);
179
+ } else {
180
+ throw 'word not found.';
181
+ }
182
+ }
183
+
184
+ delete(name) {
185
+ return this.delete_norm(name);
186
+ }
187
+
188
+ set_ns(name, val) {}
189
+
190
+ set_norm(name, val, type, where) {
191
+ let x;
192
+ if ((x = this.lookup(name))) {
193
+ x._datum = val;
194
+ } else {
195
+ this.add_norm(name, val, type, where);
196
+ }
197
+ }
198
+
199
+ add_norm(name, val, type, where) {
200
+ let x;
201
+ x = new dict_obj();
202
+ x._type = type;
203
+ x._name = name;
204
+ x._datum = val;
205
+ x._where = where;
206
+ this._dict.unshift(x);
207
+ }
208
+
209
+ set(name, val, type, where) {
210
+ this.add_norm(name, val, type, where);
211
+ // this.set_norm(name, val, type, where);
212
+ }
213
+
214
+ print_debug() {
215
+ // log.info(this._dict);
216
+ this._dict.forEach(item => {
217
+ log.info(`${item._type} ${item._name} ${item._datum._type}`);
218
+ });
219
+ }
11
220
  }
12
- class Env{
13
- constructor(){
14
- this._use_ns = false;
15
- this._dict = [];
16
- this._dict_ns = {};
17
-
18
- this.s = new Stack();
19
- }
20
- print_stack(){
21
- this.s.print();
22
- }
23
- TOS(){
24
- return this.s.peek();
25
- }
26
- TOS2(){
27
- return this.s.look_at(1);
28
- }
29
- is_bool(e){
30
- if(e && e._type=='TC_BOOL') return true;
31
- return false;
32
- }
33
- is_true(e){
34
- if(e && e._type=='TC_BOOL' && e._datum=='T') return true;
35
- return false;
36
- }
37
- is_false(e){
38
- if(e && e._type=='TC_BOOL' && e._datum=='F') return true;
39
- return false;
40
- }
41
- is_num(e){
42
- if(e && e._type=='TC_NUM') return true;
43
- return false;
44
- }
45
- is_string(e){
46
- if(e && e._type=='TC_STR') return true;
47
- return false;
48
- }
49
- is_json(e){
50
- if(e && e._type=='TC_JSON') return true;
51
- return false;
52
- }
53
- is_obj(e){
54
- if(e && e._type=='TC_JSON' && !this.is_list(e)) return true;
55
- return false;
56
- }
57
- is_list(e){
58
- if(e && e._type=='TC_JSON' && e._datum instanceof Array) return true;
59
- return false;
60
- }
61
- true_obj(){
62
- return {"_type":"TC_BOOL","_datum": 'T'};
63
- }
64
- false_obj(){
65
- return {"_type":"TC_BOOL","_datum": 'F'};
66
- }
67
- adj_bool_val(e){
68
- if(e===true) return 'T';
69
- if(e===false) return 'F';
70
- return e;
71
- }
72
- guess_type(e){
73
- switch(typeof(e)){
74
- case 'number':
75
- return 'TC_NUM';
76
- break;
77
- case 'string':
78
- return 'TC_STR';
79
- break;
80
- case 'object':
81
- if(e instanceof Promise) return 'TC_PROMISE';
82
- return 'TC_JSON';
83
- break;
84
- case 'boolean':
85
- return 'TC_BOOL';
86
- break;
87
- case 'function':
88
- return 'TC_FUNC_JS';
89
- break;
90
- default:
91
- return 'TC_UNDEF';
92
- break;
93
- }
94
- }
95
- get_bool_val(e){
96
- if(this.is_bool(e)){
97
- if(this.is_true(e)) return true;
98
- if(this.is_false(e)) return false;
99
- }
100
- }
101
- set_bool_val(e){
102
- if(e===true) return this.true_obj();
103
- if(e===false) return this.false_obj();
104
- }
105
- lookup_ns(name){
106
-
107
- }
108
- lookup_norm(name, type){
109
- for(var item of this._dict){
110
- //if(type && item._type == type)
111
- if(item._name == name) return item;
112
- }
113
- return false;
114
- }
115
- lookup(name, type){
116
- return this.lookup_norm(name, type);
117
- }
118
- delete_norm(name){
119
- let index = this._dict.map(function(e) { return e._name; }).indexOf(name);
120
- //log.info(index);
121
- //log.info(this._dict[index]);
122
- if(index >= 0) {
123
- this._dict.splice(index,1);
124
- } else {
125
- throw("word not found.");
126
- }
127
- }
128
- delete(name){
129
- return this.delete_norm(name);
130
- }
131
- set_ns(name, val){
132
-
133
- }
134
- set_norm(name, val, type, where){
135
- var x;
136
- if(x=this.lookup(name)){
137
- x._datum = val;
138
- }else{
139
- this.add_norm(name, val, type, where);
140
- }
141
- }
142
- add_norm(name, val, type, where){
143
- var x;
144
- x=new dict_obj();
145
- x._type = type;
146
- x._name = name;
147
- x._datum = val;
148
- x._where = where;
149
- this._dict.unshift(x);
150
- }
151
- set(name, val, type, where){
152
- this.add_norm(name, val, type, where);
153
- //this.set_norm(name, val, type, where);
154
- }
155
- print_debug(){
156
- //log.info(this._dict);
157
- this._dict.forEach(item => {
158
- log.info(`${item._type} ${item._name} ${item._datum._type}`);
159
- });
160
- }
161
- };
162
-
163
- module.exports = new Env();
221
+
222
+ export default new Env();