porffor 0.2.0-c6c8c81 → 0.2.0-c7b7423

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/compiler/2c.js CHANGED
@@ -35,7 +35,10 @@ const todo = msg => {
35
35
  throw new TodoError(`todo: ${msg}`);
36
36
  };
37
37
 
38
- const removeBrackets = str => str.startsWith('(') && str.endsWith(')') ? str.slice(1, -1) : str;
38
+ const removeBrackets = str => {
39
+ if (str.startsWith('(long)(unsigned long)')) return '(long)(unsigned long)(' + removeBrackets(str.slice(22, -1)) + ')';
40
+ return str.startsWith('(') && str.endsWith(')') ? str.slice(1, -1) : str;
41
+ };
39
42
 
40
43
  export default ({ funcs, globals, tags, exceptions, pages }) => {
41
44
  const invOperatorOpcode = Object.values(operatorOpcode).reduce((acc, x) => {
@@ -116,13 +116,12 @@ export default (wasm, name = '', ind = 0, locals = {}, params = [], returns = []
116
116
  return highlightAsm(out);
117
117
  };
118
118
 
119
- export const highlightAsm = asm =>
120
- asm
121
- .replace(/(local|global|memory)\.[^\s]*/g, _ => `\x1B[31m${_}\x1B[0m`)
122
- .replace(/(i(8|16|32|64)x[0-9]+|v128)(\.[^\s]*)?/g, _ => `\x1B[34m${_}\x1B[0m`)
123
- .replace(/[^m](i32|i64|f32|f64|drop)(\.[^\s]*)?/g, _ => `${_[0]}\x1B[36m${_.slice(1)}\x1B[0m`)
124
- .replace(/(return_call|call|br_if|br|return|throw|rethrow)/g, _ => `\x1B[35m${_}\x1B[0m`)
125
- .replace(/(block|loop|if|end|else|try|catch_all|catch|delegate)/g, _ => `\x1B[95m${_}\x1B[0m`)
126
- .replace(/unreachable/g, _ => `\x1B[91m${_}\x1B[0m`)
127
- .replace(/ \-?[0-9\.]+/g, _ => ` \x1B[33m${_.slice(1)}\x1B[0m`)
128
- .replace(/ ;;.*$/gm, _ => `\x1B[90m${_.replaceAll(/\x1B\[[0-9]+m/g, '')}\x1B[0m`);
119
+ export const highlightAsm = asm => asm
120
+ .replace(/(local|global|memory)\.[^\s]*/g, _ => `\x1B[31m${_}\x1B[0m`)
121
+ .replace(/(i(8|16|32|64)x[0-9]+|v128)(\.[^\s]*)?/g, _ => `\x1B[34m${_}\x1B[0m`)
122
+ .replace(/[^m](i32|i64|f32|f64|drop)(\.[^\s]*)?/g, _ => `${_[0]}\x1B[36m${_.slice(1)}\x1B[0m`)
123
+ .replace(/(return_call|call|br_if|br|return|rethrow|throw)/g, _ => `\x1B[35m${_}\x1B[0m`)
124
+ .replace(/(block|loop|if|end|else|try|catch_all|catch|delegate)/g, _ => `\x1B[95m${_}\x1B[0m`)
125
+ .replace(/unreachable/g, _ => `\x1B[91m${_}\x1B[0m`)
126
+ .replace(/ \-?[0-9\.]+/g, _ => ` \x1B[33m${_.slice(1)}\x1B[0m`)
127
+ .replace(/ ;;.*$/gm, _ => `\x1B[90m${_.replaceAll(/\x1B\[[0-9]+m/g, '')}\x1B[0m`);
package/compiler/index.js CHANGED
@@ -28,8 +28,8 @@ const logFuncs = (funcs, globals, exceptions) => {
28
28
 
29
29
  const getArg = name => process.argv.find(x => x.startsWith(`-${name}=`))?.slice(name.length + 2);
30
30
 
31
- const writeFileSync = (typeof process !== 'undefined' ? (await import('node:fs')).writeFileSync : undefined);
32
- const execSync = (typeof process !== 'undefined' ? (await import('node:child_process')).execSync : undefined);
31
+ const writeFileSync = (typeof process?.version !== 'undefined' ? (await import('node:fs')).writeFileSync : undefined);
32
+ const execSync = (typeof process?.version !== 'undefined' ? (await import('node:child_process')).execSync : undefined);
33
33
 
34
34
  export default (code, flags) => {
35
35
  globalThis.optLog = process.argv.includes('-opt-log');
@@ -75,6 +75,7 @@ export default (code, flags) => {
75
75
 
76
76
  if (target === 'c') {
77
77
  const c = toc(out);
78
+ out.c = c;
78
79
 
79
80
  if (outFile) {
80
81
  writeFileSync(outFile, c);
@@ -82,7 +83,7 @@ export default (code, flags) => {
82
83
  console.log(c);
83
84
  }
84
85
 
85
- process.exit();
86
+ if (process.version) process.exit();
86
87
  }
87
88
 
88
89
  if (target === 'native') {
@@ -98,7 +99,7 @@ export default (code, flags) => {
98
99
  // obvious command escape is obvious
99
100
  execSync(args.join(' '), { stdio: 'inherit' });
100
101
 
101
- process.exit();
102
+ if (process.version) process.exit();
102
103
  }
103
104
 
104
105
  return out;
package/compiler/parse.js CHANGED
@@ -8,10 +8,16 @@ if (typeof process === 'undefined' && typeof Deno !== 'undefined') {
8
8
 
9
9
  // import { parse } from 'acorn';
10
10
 
11
- // todo: review which to use by default
12
- const parser = process.argv.find(x => x.startsWith('-parser='))?.split('=')?.[1] ?? 'acorn';
13
- const { parse } = (await import((globalThis.document ? 'https://esm.sh/' : '') + parser));
11
+ let parser, parse;
12
+
13
+ const loadParser = async () => {
14
+ parser = process.argv.find(x => x.startsWith('-parser='))?.split('=')?.[1] ?? 'acorn';
15
+ 0, { parse } = (await import((globalThis.document ? 'https://esm.sh/' : '') + parser));
16
+ };
17
+ globalThis._porf_loadParser = loadParser;
18
+ await loadParser();
14
19
 
20
+ // todo: review which to use by default
15
21
  // supported parsers:
16
22
  // - acorn
17
23
  // - meriyah
@@ -24,8 +30,26 @@ const types = process.argv.includes('-types');
24
30
  if (types && !['@babel/parser', 'hermes-parser'].includes(parser)) log.warning('parser', `passed -types with a parser (${parser}) which does not support`);
25
31
 
26
32
  export default (input, flags) => {
27
- return parse(input, {
33
+ const ast = parse(input, {
34
+ // acorn
28
35
  ecmaVersion: 'latest',
29
- sourceType: flags.includes('module') ? 'module' : 'script'
36
+
37
+ // meriyah
38
+ next: true,
39
+ module: flags.includes('module'),
40
+ webcompat: true,
41
+
42
+ // babel
43
+ plugins: types ? ['estree', 'typescript'] : ['estree'],
44
+
45
+ // multiple
46
+ sourceType: flags.includes('module') ? 'module' : 'script',
47
+ ranges: false,
48
+ tokens: false,
49
+ comments: false,
30
50
  });
51
+
52
+ if (ast.type === 'File') return ast.program;
53
+
54
+ return ast;
31
55
  };
package/compiler/wrap.js CHANGED
@@ -25,7 +25,7 @@ export default async (source, flags = [ 'module' ], customImports = {}, print =
25
25
  const times = [];
26
26
 
27
27
  const t1 = performance.now();
28
- const { wasm, funcs, globals, tags, exceptions, pages } = compile(source, flags);
28
+ const { wasm, funcs, globals, tags, exceptions, pages, c } = compile(source, flags);
29
29
 
30
30
  if (source.includes('export function')) flags.push('module');
31
31
 
@@ -126,8 +126,8 @@ export default async (source, flags = [ 'module' ], customImports = {}, print =
126
126
  }
127
127
 
128
128
  if (flags.includes('decomp')) {
129
- return { exports, wasm, times, decomps: funcs.map(x => decompile(x.wasm, x.name, x.index, x.locals, x.params, x.returns, funcs, globals, exceptions)) };
129
+ return { exports, wasm, times, decomps: funcs.map(x => decompile(x.wasm, x.name, x.index, x.locals, x.params, x.returns, funcs, globals, exceptions)), c };
130
130
  }
131
131
 
132
- return { exports, wasm, times, pages };
132
+ return { exports, wasm, times, pages, c };
133
133
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "porffor",
3
3
  "description": "a basic experimental wip aot optimizing js -> wasm engine/compiler/runtime in js",
4
- "version": "0.2.0-c6c8c81",
4
+ "version": "0.2.0-c7b7423",
5
5
  "author": "CanadaHonk",
6
6
  "license": "MIT",
7
7
  "dependencies": {
package/tmp.c ADDED
@@ -0,0 +1,69 @@
1
+
2
+ #include <stdio.h>
3
+
4
+ struct ReturnValue {
5
+ double value;
6
+ long type;
7
+ };
8
+
9
+ double sum = 0;
10
+ long sumdtype = 0;
11
+ double counter = 0;
12
+ long counterdtype = 0;
13
+
14
+ double inline f64_f(double x, double y) {
15
+ return (x - ((int)(x / y) * y));
16
+ }
17
+
18
+ struct ReturnValue isPrime(double number, long numberdtype) {
19
+ double i = 0;
20
+ long idtype = 0;
21
+ double __tmpop_left = 0;
22
+ double __tmpop_right = 0;
23
+ long compare_left_pointer = 0;
24
+ long compare_left_length = 0;
25
+ long compare_right_pointer = 0;
26
+ long compare_right_length = 0;
27
+ long compare_index = 0;
28
+ long compare_index_end = 0;
29
+
30
+ if (number < 2e+0) {
31
+ return (struct ReturnValue){ 1, 0e+0 };
32
+ }
33
+ i = 2e+0;
34
+ idtype = 0;
35
+ while (i < number) {
36
+ if (f64_f(number, i) == 0e+0) {
37
+ return (struct ReturnValue){ 1, 0e+0 };
38
+ }
39
+ i = i + 1e+0;
40
+ }
41
+ return (struct ReturnValue){ 1, 1e+0 };
42
+ }
43
+
44
+ double inline __console_log(double x) {
45
+ printf("%f\n", x);
46
+ printf("%c", (int)(1e+1));
47
+ }
48
+
49
+ int main() {
50
+ long dlast_type = 0;
51
+ double elogicinner_tmp = 0;
52
+ long dtypeswitch_tmp = 0;
53
+
54
+ sum = 0e+0;
55
+ sumdtype = 0;
56
+ counter = 0e+0;
57
+ counterdtype = 0;
58
+ while (counter <= 1e+5) {
59
+ const struct ReturnValue _ = isPrime(counter, counterdtype);
60
+ dlast_type = _.type;
61
+ if ((unsigned long)(elogicinner_tmp = _.value) == 1e+0) {
62
+ sum = sum + counter;
63
+ sumdtype = 0;
64
+ }
65
+ counter = counter + 1e+0;
66
+ }
67
+ __console_log(sum);
68
+ }
69
+