typenative 0.0.7 → 0.0.9

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 CHANGED
@@ -4,9 +4,9 @@ Build native applications using Typescript.
4
4
 
5
5
  ## Get Started
6
6
 
7
- - Write a file `test.ts` with content `console.log('Hello World!'); getchar()` or any other message.
7
+ - Write a file `test.ts` with content `console.log('Hello World!');` or any other message
8
8
  - Run `npx typenative --source test.ts`
9
- - Run the executable in `dist/native.exe` and see your message
9
+ - Run `dist/native.exe` in terminal and see your message
10
10
 
11
11
  ## Todo
12
12
 
package/bin/transpiler.js CHANGED
@@ -1,16 +1,13 @@
1
1
  import ts from 'typescript';
2
+ let TypeCheker;
2
3
  export function transpileToC(code) {
3
4
  const sourceFile = ts.createSourceFile('main.ts', code, ts.ScriptTarget.ES2020, true, ts.ScriptKind.TS);
4
- return `
5
- #define console.log(message) printf(message)
6
-
7
- #include <stdio.h>
5
+ TypeCheker = ts.createProgram(['main.ts'], {}).getTypeChecker();
6
+ return `#include <stdio.h>
8
7
  #include <string.h>
9
8
 
10
-
11
9
  int main() {
12
- ${visit(sourceFile)}
13
- return 0;
10
+ ${visit(sourceFile)}return 0;
14
11
  }
15
12
  `;
16
13
  }
@@ -22,18 +19,58 @@ export function visit(node) {
22
19
  else if (ts.isStringLiteral(node)) {
23
20
  return `"${node.text}"`;
24
21
  }
22
+ else if (ts.isNumericLiteral(node)) {
23
+ return `${node.text}`;
24
+ }
25
+ else if (ts.isToken(node) && node.kind === ts.SyntaxKind.TrueKeyword) {
26
+ return `1`;
27
+ }
28
+ else if (ts.isToken(node) && node.kind === ts.SyntaxKind.FalseKeyword) {
29
+ return `0`;
30
+ }
25
31
  else if (ts.isPropertyAccessExpression(node)) {
26
32
  return `${visit(node.expression)}.${visit(node.name)}`;
27
33
  }
34
+ else if (ts.isVariableDeclaration(node)) {
35
+ const type = getCType(node.type);
36
+ const initializer = node.initializer ? ` = ${visit(node.initializer)}` : '';
37
+ return `${type.typeStr} ${visit(node.name)}${type.arrayStr}${initializer}`;
38
+ }
28
39
  else if (ts.isCallExpression(node)) {
29
40
  const expr = visit(node.expression);
30
41
  const leftSide = expr === 'console.log' ? 'printf' : expr;
31
42
  return `${leftSide}(${node.arguments.map((a) => visit(a)).join(',')})`;
32
43
  }
44
+ else if (ts.isBinaryExpression(node)) {
45
+ return `${visit(node.left)} ${node.operatorToken.getText()} ${visit(node.right)}`;
46
+ }
47
+ else if (ts.isParenthesizedExpression(node)) {
48
+ return `(${visit(node.expression)})`;
49
+ }
50
+ else if (ts.isVariableDeclarationList(node)) {
51
+ return node.declarations.map((n) => visit(n)).join(';\n\t') + ';\n\t';
52
+ }
33
53
  else if (ts.isExpressionStatement(node)) {
34
- return visit(node.expression) + ';\n';
54
+ return visit(node.expression) + ';\n\t';
35
55
  }
36
- // console.log(ts.SyntaxKind[node.kind], node.getText());
37
- ts.forEachChild(node, (subNode) => (code = code + visit(subNode)));
56
+ console.log(ts.SyntaxKind[node.kind], node.getText());
57
+ ts.forEachChild(node, (subNode) => {
58
+ code += visit(subNode);
59
+ return null;
60
+ });
38
61
  return code;
39
62
  }
63
+ function getCType(typeNode) {
64
+ const type = TypeCheker.getTypeFromTypeNode(typeNode);
65
+ const typeName = TypeCheker.typeToString(type);
66
+ switch (typeName) {
67
+ case 'string':
68
+ return { typeStr: 'char', arrayStr: '[]' };
69
+ case 'number':
70
+ return { typeStr: 'double', arrayStr: '' };
71
+ case 'boolean':
72
+ return { typeStr: 'int', arrayStr: '' };
73
+ default:
74
+ return { typeStr: typeName, arrayStr: '' };
75
+ }
76
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typenative",
3
- "version": "0.0.7",
3
+ "version": "0.0.9",
4
4
  "description": "Build native applications using Typescript.",
5
5
  "type": "module",
6
6
  "bin": {