typenative 0.0.13 → 0.0.14
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/.github/dependabot.yml +11 -0
- package/.github/workflows/npm-publish-github-packages.yml +36 -0
- package/CHANGELOG.md +44 -0
- package/LICENSE +21 -21
- package/README.md +64 -64
- package/bin/index.js +129 -126
- package/bin/transpiler.js +253 -253
- package/package.json +48 -49
- package/types/typenative.d.ts +54 -54
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# To get started with Dependabot version updates, you'll need to specify which
|
|
2
|
+
# package ecosystems to update and where the package manifests are located.
|
|
3
|
+
# Please see the documentation for all configuration options:
|
|
4
|
+
# https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file
|
|
5
|
+
|
|
6
|
+
version: 2
|
|
7
|
+
updates:
|
|
8
|
+
- package-ecosystem: "npm" # See documentation for possible values
|
|
9
|
+
directory: "/" # Location of package manifests
|
|
10
|
+
schedule:
|
|
11
|
+
interval: "weekly"
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
|
|
2
|
+
# For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages
|
|
3
|
+
|
|
4
|
+
name: Node.js Package
|
|
5
|
+
|
|
6
|
+
on:
|
|
7
|
+
release:
|
|
8
|
+
types: [created]
|
|
9
|
+
|
|
10
|
+
jobs:
|
|
11
|
+
build:
|
|
12
|
+
runs-on: ubuntu-latest
|
|
13
|
+
steps:
|
|
14
|
+
- uses: actions/checkout@v4
|
|
15
|
+
- uses: actions/setup-node@v4
|
|
16
|
+
with:
|
|
17
|
+
node-version: 20
|
|
18
|
+
- run: npm ci
|
|
19
|
+
- run: npm test
|
|
20
|
+
|
|
21
|
+
publish-gpr:
|
|
22
|
+
needs: build
|
|
23
|
+
runs-on: ubuntu-latest
|
|
24
|
+
permissions:
|
|
25
|
+
contents: read
|
|
26
|
+
packages: write
|
|
27
|
+
steps:
|
|
28
|
+
- uses: actions/checkout@v4
|
|
29
|
+
- uses: actions/setup-node@v4
|
|
30
|
+
with:
|
|
31
|
+
node-version: 20
|
|
32
|
+
registry-url: https://npm.pkg.github.com/
|
|
33
|
+
- run: npm ci
|
|
34
|
+
- run: npm publish
|
|
35
|
+
env:
|
|
36
|
+
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
|
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to TypeNative will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [0.0.14] - 2025-05-25
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
- Project creation with new `--new` command
|
|
12
|
+
- Support for functions and arrow functions
|
|
13
|
+
- Support for switch statements
|
|
14
|
+
- If statements, while loops, and for...of loops for arrays
|
|
15
|
+
|
|
16
|
+
### Changed
|
|
17
|
+
- Replaced shelljs dependency with execa for improved process execution
|
|
18
|
+
|
|
19
|
+
### Fixed
|
|
20
|
+
- Project creation issues
|
|
21
|
+
|
|
22
|
+
## [0.0.12] - 2025-05-23
|
|
23
|
+
|
|
24
|
+
### Changed
|
|
25
|
+
- Switched transpilation target to Go language
|
|
26
|
+
|
|
27
|
+
## [0.0.9] - 2024-03-01
|
|
28
|
+
|
|
29
|
+
### Added
|
|
30
|
+
- Basic types support (number, boolean, string, null, any)
|
|
31
|
+
- Support for variable declarations
|
|
32
|
+
- Support for binary expressions
|
|
33
|
+
- Arithmetic, comparison, logical, and increment/decrement operators
|
|
34
|
+
|
|
35
|
+
## [0.0.1] - 2024-02-24
|
|
36
|
+
|
|
37
|
+
### Added
|
|
38
|
+
- Initial project setup
|
|
39
|
+
- Basic project structure
|
|
40
|
+
|
|
41
|
+
## [0.0.0] - 2022-12-20
|
|
42
|
+
|
|
43
|
+
### Added
|
|
44
|
+
- Initial commit
|
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2025 danisss9
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 danisss9
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,65 +1,65 @@
|
|
|
1
|
-
# TypeNative
|
|
2
|
-
|
|
3
|
-
Build native applications using Typescript.
|
|
4
|
-
|
|
5
|
-
## PreRequisites
|
|
6
|
-
|
|
7
|
-
- [Nodejs v20](https://nodejs.org/en) or newer.
|
|
8
|
-
- [Go 1.21](https://go.dev/doc/install) or newer.
|
|
9
|
-
|
|
10
|
-
## Get Started
|
|
11
|
-
|
|
12
|
-
- Write a file `test.ts` with content `console.log('Hello World!');` or any other message
|
|
13
|
-
- Run `npx typenative --source test.ts --script`
|
|
14
|
-
- See your message in the terminal
|
|
15
|
-
|
|
16
|
-
## Create a TypeNative Project
|
|
17
|
-
|
|
18
|
-
- Run `npx typenative --new`
|
|
19
|
-
- Give your project a name
|
|
20
|
-
- Start writing code
|
|
21
|
-
|
|
22
|
-
## Typescript Syntax Support
|
|
23
|
-
|
|
24
|
-
TypeNative currently supports a subset of TypeScript syntax elements that are transpiled to Go code:
|
|
25
|
-
|
|
26
|
-
| Feature | Supported | Notes |
|
|
27
|
-
|---------|:---------:|-------|
|
|
28
|
-
| **Basic Types** | | |
|
|
29
|
-
| number | ✅ | Transpiled to `float64` |
|
|
30
|
-
| boolean | ✅ | Transpiled to `bool` |
|
|
31
|
-
| string | ✅ | |
|
|
32
|
-
| null | ✅ | |
|
|
33
|
-
| any | ✅ | Used for type inference |
|
|
34
|
-
| **Variables** | | |
|
|
35
|
-
| Variable declarations | ✅ | `let` and `const` |
|
|
36
|
-
| **Operators** | | |
|
|
37
|
-
| Arithmetic operators | ✅ | `+`, `-`, etc. |
|
|
38
|
-
| Comparison operators | ✅ | `==`, `!=`, etc. |
|
|
39
|
-
| Logical operators | ✅ | `&&`, `\|\|` |
|
|
40
|
-
| Increment/Decrement | ✅ | `++`, `--` |
|
|
41
|
-
| **Control Flow** | | |
|
|
42
|
-
| For loops | ✅ | Standard `for` loops |
|
|
43
|
-
| For...of loops | ✅ | Iteration over arrays |
|
|
44
|
-
| While loops | ✅ | Transpiled to Go's `for` loops |
|
|
45
|
-
| Do...while loops | ✅ | Implemented with conditional break |
|
|
46
|
-
| If/Else statements | ✅ | Fully supported |
|
|
47
|
-
| Switch statements | ✅ | Case and default statements |
|
|
48
|
-
| **Data Structures** | | |
|
|
49
|
-
| Arrays | ✅ | Basic array operations |
|
|
50
|
-
| Array methods | ✅ | `push` supported |
|
|
51
|
-
| **Functions** | | |
|
|
52
|
-
| Function declarations | ✅ | Transpiled to Go functions |
|
|
53
|
-
| Arrow Functions | ✅ | Transpiled to anonymous functions |
|
|
54
|
-
| console.log | ✅ | Mapped to `fmt.Println` |
|
|
55
|
-
| console.time/timeEnd | ✅ | Performance measurement |
|
|
56
|
-
| Math.random | ✅ | Mapped to Go's `rand.Float64()` |
|
|
57
|
-
| Math.floor | ✅ | Mapped to Go's `math.Floor()` |
|
|
58
|
-
| **Unsupported Features** | | |
|
|
59
|
-
| Classes | ❌ | Not implemented |
|
|
60
|
-
| Interfaces | ❌ | Not implemented |
|
|
61
|
-
| Async/Await | ❌ | Not implemented |
|
|
62
|
-
| Modules/Imports | ❌ | Not implemented |
|
|
63
|
-
| Generics | ❌ | Not implemented |
|
|
64
|
-
|
|
1
|
+
# TypeNative
|
|
2
|
+
|
|
3
|
+
Build native applications using Typescript.
|
|
4
|
+
|
|
5
|
+
## PreRequisites
|
|
6
|
+
|
|
7
|
+
- [Nodejs v20](https://nodejs.org/en) or newer.
|
|
8
|
+
- [Go 1.21](https://go.dev/doc/install) or newer.
|
|
9
|
+
|
|
10
|
+
## Get Started
|
|
11
|
+
|
|
12
|
+
- Write a file `test.ts` with content `console.log('Hello World!');` or any other message
|
|
13
|
+
- Run `npx typenative --source test.ts --script`
|
|
14
|
+
- See your message in the terminal
|
|
15
|
+
|
|
16
|
+
## Create a TypeNative Project
|
|
17
|
+
|
|
18
|
+
- Run `npx typenative --new`
|
|
19
|
+
- Give your project a name
|
|
20
|
+
- Start writing code
|
|
21
|
+
|
|
22
|
+
## Typescript Syntax Support
|
|
23
|
+
|
|
24
|
+
TypeNative currently supports a subset of TypeScript syntax elements that are transpiled to Go code:
|
|
25
|
+
|
|
26
|
+
| Feature | Supported | Notes |
|
|
27
|
+
|---------|:---------:|-------|
|
|
28
|
+
| **Basic Types** | | |
|
|
29
|
+
| number | ✅ | Transpiled to `float64` |
|
|
30
|
+
| boolean | ✅ | Transpiled to `bool` |
|
|
31
|
+
| string | ✅ | |
|
|
32
|
+
| null | ✅ | |
|
|
33
|
+
| any | ✅ | Used for type inference |
|
|
34
|
+
| **Variables** | | |
|
|
35
|
+
| Variable declarations | ✅ | `let` and `const` |
|
|
36
|
+
| **Operators** | | |
|
|
37
|
+
| Arithmetic operators | ✅ | `+`, `-`, etc. |
|
|
38
|
+
| Comparison operators | ✅ | `==`, `!=`, etc. |
|
|
39
|
+
| Logical operators | ✅ | `&&`, `\|\|` |
|
|
40
|
+
| Increment/Decrement | ✅ | `++`, `--` |
|
|
41
|
+
| **Control Flow** | | |
|
|
42
|
+
| For loops | ✅ | Standard `for` loops |
|
|
43
|
+
| For...of loops | ✅ | Iteration over arrays |
|
|
44
|
+
| While loops | ✅ | Transpiled to Go's `for` loops |
|
|
45
|
+
| Do...while loops | ✅ | Implemented with conditional break |
|
|
46
|
+
| If/Else statements | ✅ | Fully supported |
|
|
47
|
+
| Switch statements | ✅ | Case and default statements |
|
|
48
|
+
| **Data Structures** | | |
|
|
49
|
+
| Arrays | ✅ | Basic array operations |
|
|
50
|
+
| Array methods | ✅ | `push` supported |
|
|
51
|
+
| **Functions** | | |
|
|
52
|
+
| Function declarations | ✅ | Transpiled to Go functions |
|
|
53
|
+
| Arrow Functions | ✅ | Transpiled to anonymous functions |
|
|
54
|
+
| console.log | ✅ | Mapped to `fmt.Println` |
|
|
55
|
+
| console.time/timeEnd | ✅ | Performance measurement |
|
|
56
|
+
| Math.random | ✅ | Mapped to Go's `rand.Float64()` |
|
|
57
|
+
| Math.floor | ✅ | Mapped to Go's `math.Floor()` |
|
|
58
|
+
| **Unsupported Features** | | |
|
|
59
|
+
| Classes | ❌ | Not implemented |
|
|
60
|
+
| Interfaces | ❌ | Not implemented |
|
|
61
|
+
| Async/Await | ❌ | Not implemented |
|
|
62
|
+
| Modules/Imports | ❌ | Not implemented |
|
|
63
|
+
| Generics | ❌ | Not implemented |
|
|
64
|
+
|
|
65
65
|
TypeNative is currently in early development and new features are being added regularly.
|
package/bin/index.js
CHANGED
|
@@ -8,143 +8,146 @@ import { fileURLToPath } from 'url';
|
|
|
8
8
|
const __filename = fileURLToPath(import.meta.url);
|
|
9
9
|
const __dirname = path.dirname(__filename);
|
|
10
10
|
(async function main() {
|
|
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
|
-
if (newCommand) {
|
|
54
|
-
const projectName = answers.projectName.trim();
|
|
55
|
-
await fs.ensureDir(projectName);
|
|
56
|
-
await fs.writeFile(path.join(projectName, 'main.ts'), `// Write your TypeScript code here\nconsole.log('Hello, World!');\n`, { encoding: 'utf-8' });
|
|
57
|
-
await fs.writeFile(path.join(projectName, 'tsconfig.json'), getTsConfig(), {
|
|
58
|
-
encoding: 'utf-8'
|
|
59
|
-
});
|
|
60
|
-
await fs.writeFile(path.join(projectName, 'package.json'), getPackageJson(projectName), {
|
|
61
|
-
encoding: 'utf-8'
|
|
62
|
-
});
|
|
63
|
-
await fs.writeFile(path.join(projectName, '.gitignore'), getGitIgnore(), {
|
|
64
|
-
encoding: 'utf-8'
|
|
65
|
-
});
|
|
66
|
-
await fs.writeFile(path.join(projectName, 'README.md'), getReadMe(projectName), {
|
|
67
|
-
encoding: 'utf-8'
|
|
68
|
-
});
|
|
69
|
-
console.log(`Project "${projectName}" created successfully!`);
|
|
70
|
-
if (answers.installDependencies) {
|
|
71
|
-
console.log('Installing dependencies...');
|
|
72
|
-
await execa('npm install', { cwd: projectName, stdio: 'inherit' });
|
|
73
|
-
console.log('Dependencies installed successfully!');
|
|
74
|
-
}
|
|
75
|
-
return;
|
|
11
|
+
const scriptMode = process.argv.findIndex((a) => a === '--script') > -1;
|
|
12
|
+
const newCommand = process.argv.findIndex((a) => a === '--new') > -1;
|
|
13
|
+
const sourceIndex = process.argv.findIndex((a) => a === '--source');
|
|
14
|
+
const source = sourceIndex > -1 ? process.argv[sourceIndex + 1] : null;
|
|
15
|
+
const outputIndex = process.argv.findIndex((a) => a === '--output');
|
|
16
|
+
const output = outputIndex > -1 ? process.argv[outputIndex + 1] : null;
|
|
17
|
+
const answers = await inquirer.prompt([
|
|
18
|
+
{
|
|
19
|
+
type: 'input',
|
|
20
|
+
name: 'projectName',
|
|
21
|
+
message: 'Enter Project Name:',
|
|
22
|
+
when: newCommand,
|
|
23
|
+
validate: (input) => input.trim() !== ''
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
type: 'confirm',
|
|
27
|
+
name: 'installDependencies',
|
|
28
|
+
message: 'Do you want to install dependencies?',
|
|
29
|
+
when: newCommand
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
type: 'input',
|
|
33
|
+
name: 'path',
|
|
34
|
+
message: 'Enter Path to typescript main file:',
|
|
35
|
+
when: !newCommand && !scriptMode && !source,
|
|
36
|
+
validate: (input) => input.trim() !== ''
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
type: 'input',
|
|
40
|
+
name: 'output',
|
|
41
|
+
message: 'Enter Output Path:',
|
|
42
|
+
when: !newCommand && !scriptMode && !output,
|
|
43
|
+
validate: (input) => input.trim() !== ''
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
type: 'editor',
|
|
47
|
+
name: 'tsCode',
|
|
48
|
+
message: 'Write your typescript code here:',
|
|
49
|
+
when: !newCommand && scriptMode && !source,
|
|
50
|
+
default: `console.log('Hello, World!');`
|
|
76
51
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
await fs.
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
52
|
+
]);
|
|
53
|
+
if (newCommand) {
|
|
54
|
+
const projectName = answers.projectName.trim();
|
|
55
|
+
await fs.ensureDir(projectName);
|
|
56
|
+
await fs.writeFile(
|
|
57
|
+
path.join(projectName, 'main.ts'),
|
|
58
|
+
`// Write your TypeScript code here\nconsole.log('Hello, World!');\n`,
|
|
59
|
+
{ encoding: 'utf-8' }
|
|
60
|
+
);
|
|
61
|
+
await fs.writeFile(path.join(projectName, 'tsconfig.json'), getTsConfig(), {
|
|
62
|
+
encoding: 'utf-8'
|
|
85
63
|
});
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
64
|
+
await fs.writeFile(path.join(projectName, 'package.json'), getPackageJson(projectName), {
|
|
65
|
+
encoding: 'utf-8'
|
|
66
|
+
});
|
|
67
|
+
await fs.writeFile(path.join(projectName, '.gitignore'), getGitIgnore(), {
|
|
68
|
+
encoding: 'utf-8'
|
|
69
|
+
});
|
|
70
|
+
await fs.writeFile(path.join(projectName, 'README.md'), getReadMe(projectName), {
|
|
71
|
+
encoding: 'utf-8'
|
|
72
|
+
});
|
|
73
|
+
console.log(`Project "${projectName}" created successfully!`);
|
|
74
|
+
if (answers.installDependencies) {
|
|
75
|
+
console.log('Installing dependencies...');
|
|
76
|
+
await execa('npm install', { cwd: projectName, stdio: 'inherit' });
|
|
77
|
+
console.log('Dependencies installed successfully!');
|
|
97
78
|
}
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
const tsCode = answers.tsCode
|
|
82
|
+
? answers.tsCode
|
|
83
|
+
: await fs.readFile(source ?? answers.path, { encoding: 'utf-8' });
|
|
84
|
+
const nativeCode = transpileToNative(tsCode);
|
|
85
|
+
await fs.ensureDir('dist');
|
|
86
|
+
await fs.writeFile('dist/code.go', nativeCode, { encoding: 'utf-8' });
|
|
87
|
+
await execa('go build -o dist/native.exe dist/code.go', {
|
|
88
|
+
stdio: 'inherit'
|
|
89
|
+
});
|
|
90
|
+
// await fs.remove('dist/code.go');
|
|
91
|
+
if (scriptMode) {
|
|
92
|
+
await execa('dist/native.exe', {
|
|
93
|
+
stdio: 'inherit'
|
|
94
|
+
});
|
|
95
|
+
//await fs.remove('dist/native.exe');
|
|
96
|
+
} else if (output || answers.output) {
|
|
97
|
+
await fs.copy('dist/native.exe', output ?? answers.output, { overwrite: true });
|
|
98
|
+
//await fs.remove('dist/native.exe');
|
|
99
|
+
console.log(`Created native executable at: ${output ?? answers.output}`);
|
|
100
|
+
}
|
|
98
101
|
})();
|
|
99
102
|
function getPackageJson(projectName) {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
103
|
+
const pckg = {
|
|
104
|
+
name: projectName,
|
|
105
|
+
version: '1.0.0',
|
|
106
|
+
scripts: {
|
|
107
|
+
execute: 'npx typenative --source main.ts --script',
|
|
108
|
+
build: `npx typenative --source main.ts --output bin/${projectName}.exe`
|
|
109
|
+
},
|
|
110
|
+
devDependencies: {
|
|
111
|
+
typenative: '^0.0.14'
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
return JSON.stringify(pckg, null, 2);
|
|
112
115
|
}
|
|
113
116
|
function getTsConfig() {
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
117
|
+
const tsConfig = {
|
|
118
|
+
include: ['**/*.ts'],
|
|
119
|
+
compilerOptions: {
|
|
120
|
+
target: 'es2020',
|
|
121
|
+
lib: [],
|
|
122
|
+
types: ['./node_modules/typenative/types/typenative.d.ts'],
|
|
123
|
+
rootDir: '.',
|
|
124
|
+
strict: true,
|
|
125
|
+
noImplicitAny: true
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
return JSON.stringify(tsConfig, null, 2);
|
|
126
129
|
}
|
|
127
130
|
function getGitIgnore() {
|
|
128
|
-
|
|
129
|
-
node_modules/
|
|
130
|
-
dist/
|
|
131
|
-
bin/
|
|
131
|
+
return `# TypeNative generated files
|
|
132
|
+
node_modules/
|
|
133
|
+
dist/
|
|
134
|
+
bin/
|
|
132
135
|
`;
|
|
133
136
|
}
|
|
134
137
|
function getReadMe(projectName) {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
This project was created using TypeNative, a tool to transpile TypeScript code to native Go code.
|
|
138
|
-
|
|
139
|
-
## How to Run
|
|
140
|
-
|
|
141
|
-
You can write your TypeScript code in the \`main.ts\` file. The code will be transpiled to Go and compiled into a native executable.
|
|
142
|
-
You can also run the code directly in script mode using \`npm run execute\`.
|
|
143
|
-
|
|
144
|
-
## How to Build
|
|
145
|
-
|
|
146
|
-
1. Install dependencies: \`npm install\` (if not done already)
|
|
147
|
-
2. Build the project: \`npm run build\`
|
|
148
|
-
3. Run the executable: \`./bin/${projectName}.exe\`
|
|
138
|
+
return `# ${projectName}
|
|
139
|
+
|
|
140
|
+
This project was created using TypeNative, a tool to transpile TypeScript code to native Go code.
|
|
141
|
+
|
|
142
|
+
## How to Run
|
|
143
|
+
|
|
144
|
+
You can write your TypeScript code in the \`main.ts\` file. The code will be transpiled to Go and compiled into a native executable.
|
|
145
|
+
You can also run the code directly in script mode using \`npm run execute\`.
|
|
146
|
+
|
|
147
|
+
## How to Build
|
|
148
|
+
|
|
149
|
+
1. Install dependencies: \`npm install\` (if not done already)
|
|
150
|
+
2. Build the project: \`npm run build\`
|
|
151
|
+
3. Run the executable: \`./bin/${projectName}.exe\`
|
|
149
152
|
`;
|
|
150
153
|
}
|
package/bin/transpiler.js
CHANGED
|
@@ -1,253 +1,253 @@
|
|
|
1
|
-
import ts from 'typescript';
|
|
2
|
-
let TypeCheker;
|
|
3
|
-
const importedPackages = new Set();
|
|
4
|
-
let outsideNodes = [];
|
|
5
|
-
export function transpileToNative(code) {
|
|
6
|
-
const sourceFile = ts.createSourceFile('main.ts', code, ts.ScriptTarget.ES2020, true, ts.ScriptKind.TS);
|
|
7
|
-
TypeCheker = ts.createProgram(['main.ts'], {}).getTypeChecker();
|
|
8
|
-
importedPackages.clear();
|
|
9
|
-
outsideNodes = [];
|
|
10
|
-
const transpiledCode = visit(sourceFile, { addFunctionOutside: true });
|
|
11
|
-
const transpiledCodeOutside = outsideNodes.map((n) => visit(n, { isOutside: true })).join('\n');
|
|
12
|
-
return `package main
|
|
13
|
-
|
|
14
|
-
${[...importedPackages].map((pkg) => `import "${pkg}"`).join('\n')}
|
|
15
|
-
|
|
16
|
-
func main() {
|
|
17
|
-
${transpiledCode.trim()}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
${transpiledCodeOutside.trim()}`;
|
|
21
|
-
}
|
|
22
|
-
export function visit(node, options = {}) {
|
|
23
|
-
let code = '';
|
|
24
|
-
if (ts.isSourceFile(node)) {
|
|
25
|
-
return node.statements
|
|
26
|
-
.map((n) => visit(n, { addFunctionOutside: true }))
|
|
27
|
-
.filter((n) => !!n)
|
|
28
|
-
.join(options.inline ? '' : '\n\t');
|
|
29
|
-
}
|
|
30
|
-
else if (ts.isIdentifier(node)) {
|
|
31
|
-
return node.text;
|
|
32
|
-
}
|
|
33
|
-
else if (ts.isStringLiteral(node)) {
|
|
34
|
-
return `"${node.text}"`;
|
|
35
|
-
}
|
|
36
|
-
else if (ts.isNumericLiteral(node)) {
|
|
37
|
-
return `float64(${node.text})`;
|
|
38
|
-
}
|
|
39
|
-
else if (ts.isToken(node) && node.kind === ts.SyntaxKind.TrueKeyword) {
|
|
40
|
-
return `true`;
|
|
41
|
-
}
|
|
42
|
-
else if (ts.isToken(node) && node.kind === ts.SyntaxKind.FalseKeyword) {
|
|
43
|
-
return `false`;
|
|
44
|
-
}
|
|
45
|
-
else if (ts.isArrayLiteralExpression(node)) {
|
|
46
|
-
const type = ts.isVariableDeclaration(node.parent) ? getType(node.parent.type, true) : '';
|
|
47
|
-
return `[]${type} {${node.elements.map((e) => visit(e)).join(', ')}}`;
|
|
48
|
-
}
|
|
49
|
-
else if (ts.isBlock(node)) {
|
|
50
|
-
return `{\n\t\t${node.statements.map((n) => visit(n)).join('\t')}${options.extraBlockContent ?? ''}}${options.inline ? '' : '\n\t'}`;
|
|
51
|
-
}
|
|
52
|
-
else if (ts.isElementAccessExpression(node)) {
|
|
53
|
-
return `${visit(node.expression)}[int(${visit(node.argumentExpression)})]`;
|
|
54
|
-
}
|
|
55
|
-
else if (ts.isPropertyAccessExpression(node)) {
|
|
56
|
-
const leftSide = visit(node.expression);
|
|
57
|
-
const rightSide = visit(node.name);
|
|
58
|
-
return getAcessString(leftSide, rightSide);
|
|
59
|
-
}
|
|
60
|
-
else if (ts.isVariableDeclaration(node)) {
|
|
61
|
-
const type = getType(node.type);
|
|
62
|
-
const initializer = node.initializer ? `= ${visit(node.initializer)}` : '';
|
|
63
|
-
return `${type === ':' ? '' : 'var '}${visit(node.name)} ${type}${type === ':' ? '' : ' '}${initializer}`;
|
|
64
|
-
}
|
|
65
|
-
else if (ts.isCallExpression(node)) {
|
|
66
|
-
const caller = visit(node.expression);
|
|
67
|
-
const args = node.arguments.map((a) => visit(a));
|
|
68
|
-
return getCallString(caller, args);
|
|
69
|
-
}
|
|
70
|
-
else if (ts.isPrefixUnaryExpression(node)) {
|
|
71
|
-
return `${getOperatorText(node.operator)}${visit(node.operand)}`;
|
|
72
|
-
}
|
|
73
|
-
else if (ts.isPostfixUnaryExpression(node)) {
|
|
74
|
-
return `${visit(node.operand, { inline: true })}${getOperatorText(node.operator)}`;
|
|
75
|
-
}
|
|
76
|
-
else if (ts.isBinaryExpression(node)) {
|
|
77
|
-
return `${visit(node.left)} ${node.operatorToken.getText()} ${visit(node.right)}`;
|
|
78
|
-
}
|
|
79
|
-
else if (ts.isParenthesizedExpression(node)) {
|
|
80
|
-
return `(${visit(node.expression)})`;
|
|
81
|
-
}
|
|
82
|
-
else if (ts.isVariableDeclarationList(node)) {
|
|
83
|
-
return (node.declarations.map((n) => visit(n)).join(options.inline ? ';' : ';\n\t') +
|
|
84
|
-
(options.inline ? '' : ';\n\t'));
|
|
85
|
-
}
|
|
86
|
-
else if (ts.isExpressionStatement(node)) {
|
|
87
|
-
return visit(node.expression) + (options.inline ? '' : ';\n\t');
|
|
88
|
-
}
|
|
89
|
-
else if (ts.isForStatement(node)) {
|
|
90
|
-
return `for ${visit(node.initializer, { inline: true })}; ${visit(node.condition, {
|
|
91
|
-
inline: true
|
|
92
|
-
})}; ${visit(node.incrementor, { inline: true })}${visit(node.statement)}`;
|
|
93
|
-
}
|
|
94
|
-
else if (ts.isForOfStatement(node)) {
|
|
95
|
-
return `for _,${visit(node.initializer, { inline: true })}= range ${visit(node.expression, {
|
|
96
|
-
inline: true
|
|
97
|
-
})}${visit(node.statement)}`;
|
|
98
|
-
}
|
|
99
|
-
else if (ts.isWhileStatement(node)) {
|
|
100
|
-
return `for ${visit(node.expression, { inline: true })}${visit(node.statement)}`;
|
|
101
|
-
}
|
|
102
|
-
else if (ts.isDoStatement(node)) {
|
|
103
|
-
const condition = `\tif !(${visit(node.expression, {
|
|
104
|
-
inline: true
|
|
105
|
-
})}) {\n\t\t\tbreak \n\t\t}\n\t`;
|
|
106
|
-
return `for ${visit(node.statement, { inline: true, extraBlockContent: condition })}`;
|
|
107
|
-
}
|
|
108
|
-
else if (ts.isIfStatement(node)) {
|
|
109
|
-
const condition = `if ${visit(node.expression, { inline: true })} ${visit(node.thenStatement, {
|
|
110
|
-
inline: !!node.elseStatement
|
|
111
|
-
})}`;
|
|
112
|
-
if (node.elseStatement) {
|
|
113
|
-
return `${condition} else ${visit(node.elseStatement)}`;
|
|
114
|
-
}
|
|
115
|
-
return condition;
|
|
116
|
-
}
|
|
117
|
-
else if (ts.isSwitchStatement(node)) {
|
|
118
|
-
return `switch ${visit(node.expression)} ${visit(node.caseBlock)}`;
|
|
119
|
-
}
|
|
120
|
-
else if (ts.isCaseBlock(node)) {
|
|
121
|
-
return `{\n\t\t${node.clauses.map((c) => visit(c)).join('\n\t\t')}\n\t}`;
|
|
122
|
-
}
|
|
123
|
-
else if (ts.isCaseClause(node)) {
|
|
124
|
-
const isFallThrough = !node.statements.some((c) => ts.isBreakStatement(c));
|
|
125
|
-
return `case ${visit(node.expression, { inline: true })}: \n\t\t\t${node.statements
|
|
126
|
-
.filter((n) => !ts.isBreakStatement(n))
|
|
127
|
-
.map((s) => visit(s))
|
|
128
|
-
.join('')}${isFallThrough ? 'fallthrough\n\t' : ''}`;
|
|
129
|
-
}
|
|
130
|
-
else if (ts.isDefaultClause(node)) {
|
|
131
|
-
return `default: \n\t\t\t${node.statements
|
|
132
|
-
.filter((n) => !ts.isBreakStatement(n))
|
|
133
|
-
.map((s) => visit(s))
|
|
134
|
-
.join('')}`;
|
|
135
|
-
}
|
|
136
|
-
else if (ts.isBreakStatement(node)) {
|
|
137
|
-
return 'break';
|
|
138
|
-
}
|
|
139
|
-
else if (ts.isReturnStatement(node)) {
|
|
140
|
-
return (`return ${node.expression ? visit(node.expression) : ''}` + (options.inline ? '' : ';\n\t'));
|
|
141
|
-
}
|
|
142
|
-
else if (ts.isFunctionDeclaration(node) || ts.isFunctionExpression(node)) {
|
|
143
|
-
if (options.addFunctionOutside) {
|
|
144
|
-
outsideNodes.push(node);
|
|
145
|
-
return '';
|
|
146
|
-
}
|
|
147
|
-
const name = visit(node.name, { inline: true });
|
|
148
|
-
const parameters = node.parameters
|
|
149
|
-
.map((p) => `${visit(p.name)} ${getType(p.type)}`)
|
|
150
|
-
.join(', ');
|
|
151
|
-
const returnType = node.type ? ` ${getType(node.type)}` : '';
|
|
152
|
-
if (options.isOutside) {
|
|
153
|
-
return `func ${name}(${parameters})${returnType} ${visit(node.body)}`;
|
|
154
|
-
}
|
|
155
|
-
return `${name} := func(${parameters})${returnType} ${visit(node.body)}`;
|
|
156
|
-
}
|
|
157
|
-
else if (ts.isArrowFunction(node)) {
|
|
158
|
-
const parameters = node.parameters
|
|
159
|
-
.map((p) => `${visit(p.name)} ${getType(p.type)}`)
|
|
160
|
-
.join(', ');
|
|
161
|
-
const returnType = node.type ? ` ${getType(node.type)}` : '';
|
|
162
|
-
return `func(${parameters})${returnType} ${visit(node.body)}`;
|
|
163
|
-
}
|
|
164
|
-
const syntaxKind = ts.SyntaxKind[node.kind];
|
|
165
|
-
if (!['FirstStatement', 'EndOfFileToken'].includes(syntaxKind)) {
|
|
166
|
-
console.log(ts.SyntaxKind[node.kind], node.getText());
|
|
167
|
-
}
|
|
168
|
-
ts.forEachChild(node, (subNode) => {
|
|
169
|
-
code += visit(subNode);
|
|
170
|
-
});
|
|
171
|
-
return code;
|
|
172
|
-
}
|
|
173
|
-
function getType(typeNode, getArrayType = false) {
|
|
174
|
-
const type = TypeCheker.getTypeFromTypeNode(typeNode);
|
|
175
|
-
let typeName = TypeCheker.typeToString(type);
|
|
176
|
-
const isArray = typeName.includes('[]');
|
|
177
|
-
if (isArray) {
|
|
178
|
-
if (getArrayType) {
|
|
179
|
-
typeName = typeName.replace('[]', '');
|
|
180
|
-
}
|
|
181
|
-
else {
|
|
182
|
-
return ':';
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
switch (typeName) {
|
|
186
|
-
case 'number':
|
|
187
|
-
return 'float64';
|
|
188
|
-
case 'boolean':
|
|
189
|
-
return 'bool';
|
|
190
|
-
case 'any':
|
|
191
|
-
return ':';
|
|
192
|
-
case 'void':
|
|
193
|
-
return '';
|
|
194
|
-
default:
|
|
195
|
-
return typeName;
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
function getAcessString(leftSide, rightSide) {
|
|
199
|
-
if (rightSide === 'length') {
|
|
200
|
-
return 'float64(len(arr))';
|
|
201
|
-
}
|
|
202
|
-
return `${leftSide}.${rightSide}`;
|
|
203
|
-
}
|
|
204
|
-
function getCallString(caller, args) {
|
|
205
|
-
if (caller === 'console.log') {
|
|
206
|
-
importedPackages.add('fmt');
|
|
207
|
-
return `fmt.Println(${args.join(', ')})`;
|
|
208
|
-
}
|
|
209
|
-
else if (caller === 'console.time') {
|
|
210
|
-
importedPackages.add('time');
|
|
211
|
-
return `${getTimerName(args[0])} := time.Now()`;
|
|
212
|
-
}
|
|
213
|
-
else if (caller === 'console.timeEnd') {
|
|
214
|
-
importedPackages.add('time');
|
|
215
|
-
importedPackages.add('fmt');
|
|
216
|
-
return `fmt.Println("Elapsed time:", time.Since(${getTimerName(args[0])}))`;
|
|
217
|
-
}
|
|
218
|
-
else if (caller === 'Math.random') {
|
|
219
|
-
importedPackages.add('math/rand');
|
|
220
|
-
return 'rand.Float64()';
|
|
221
|
-
}
|
|
222
|
-
else if (caller === 'Math.floor') {
|
|
223
|
-
importedPackages.add('math');
|
|
224
|
-
return `math.Floor(${args.join(', ')})`;
|
|
225
|
-
}
|
|
226
|
-
else if (caller.endsWith('.push')) {
|
|
227
|
-
const arrayName = caller.substring(0, caller.length - '.push'.length);
|
|
228
|
-
return `${arrayName} = append(${arrayName},${args.join(', ')})`;
|
|
229
|
-
}
|
|
230
|
-
return `${caller}(${args.join(', ')})`;
|
|
231
|
-
}
|
|
232
|
-
function getOperatorText(operator) {
|
|
233
|
-
switch (operator) {
|
|
234
|
-
case ts.SyntaxKind.PlusToken:
|
|
235
|
-
return '+';
|
|
236
|
-
case ts.SyntaxKind.MinusToken:
|
|
237
|
-
return '-';
|
|
238
|
-
case ts.SyntaxKind.TildeToken:
|
|
239
|
-
return '~';
|
|
240
|
-
case ts.SyntaxKind.ExclamationToken:
|
|
241
|
-
return '!';
|
|
242
|
-
case ts.SyntaxKind.PlusPlusToken:
|
|
243
|
-
return '++';
|
|
244
|
-
case ts.SyntaxKind.MinusMinusToken:
|
|
245
|
-
return '--';
|
|
246
|
-
default:
|
|
247
|
-
console.error('Did not find operator', operator);
|
|
248
|
-
return '';
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
function getTimerName(name) {
|
|
252
|
-
return `__timer_${name.replaceAll(' ', '_').replaceAll('"', '')}__`;
|
|
253
|
-
}
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
let TypeCheker;
|
|
3
|
+
const importedPackages = new Set();
|
|
4
|
+
let outsideNodes = [];
|
|
5
|
+
export function transpileToNative(code) {
|
|
6
|
+
const sourceFile = ts.createSourceFile('main.ts', code, ts.ScriptTarget.ES2020, true, ts.ScriptKind.TS);
|
|
7
|
+
TypeCheker = ts.createProgram(['main.ts'], {}).getTypeChecker();
|
|
8
|
+
importedPackages.clear();
|
|
9
|
+
outsideNodes = [];
|
|
10
|
+
const transpiledCode = visit(sourceFile, { addFunctionOutside: true });
|
|
11
|
+
const transpiledCodeOutside = outsideNodes.map((n) => visit(n, { isOutside: true })).join('\n');
|
|
12
|
+
return `package main
|
|
13
|
+
|
|
14
|
+
${[...importedPackages].map((pkg) => `import "${pkg}"`).join('\n')}
|
|
15
|
+
|
|
16
|
+
func main() {
|
|
17
|
+
${transpiledCode.trim()}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
${transpiledCodeOutside.trim()}`;
|
|
21
|
+
}
|
|
22
|
+
export function visit(node, options = {}) {
|
|
23
|
+
let code = '';
|
|
24
|
+
if (ts.isSourceFile(node)) {
|
|
25
|
+
return node.statements
|
|
26
|
+
.map((n) => visit(n, { addFunctionOutside: true }))
|
|
27
|
+
.filter((n) => !!n)
|
|
28
|
+
.join(options.inline ? '' : '\n\t');
|
|
29
|
+
}
|
|
30
|
+
else if (ts.isIdentifier(node)) {
|
|
31
|
+
return node.text;
|
|
32
|
+
}
|
|
33
|
+
else if (ts.isStringLiteral(node)) {
|
|
34
|
+
return `"${node.text}"`;
|
|
35
|
+
}
|
|
36
|
+
else if (ts.isNumericLiteral(node)) {
|
|
37
|
+
return `float64(${node.text})`;
|
|
38
|
+
}
|
|
39
|
+
else if (ts.isToken(node) && node.kind === ts.SyntaxKind.TrueKeyword) {
|
|
40
|
+
return `true`;
|
|
41
|
+
}
|
|
42
|
+
else if (ts.isToken(node) && node.kind === ts.SyntaxKind.FalseKeyword) {
|
|
43
|
+
return `false`;
|
|
44
|
+
}
|
|
45
|
+
else if (ts.isArrayLiteralExpression(node)) {
|
|
46
|
+
const type = ts.isVariableDeclaration(node.parent) ? getType(node.parent.type, true) : '';
|
|
47
|
+
return `[]${type} {${node.elements.map((e) => visit(e)).join(', ')}}`;
|
|
48
|
+
}
|
|
49
|
+
else if (ts.isBlock(node)) {
|
|
50
|
+
return `{\n\t\t${node.statements.map((n) => visit(n)).join('\t')}${options.extraBlockContent ?? ''}}${options.inline ? '' : '\n\t'}`;
|
|
51
|
+
}
|
|
52
|
+
else if (ts.isElementAccessExpression(node)) {
|
|
53
|
+
return `${visit(node.expression)}[int(${visit(node.argumentExpression)})]`;
|
|
54
|
+
}
|
|
55
|
+
else if (ts.isPropertyAccessExpression(node)) {
|
|
56
|
+
const leftSide = visit(node.expression);
|
|
57
|
+
const rightSide = visit(node.name);
|
|
58
|
+
return getAcessString(leftSide, rightSide);
|
|
59
|
+
}
|
|
60
|
+
else if (ts.isVariableDeclaration(node)) {
|
|
61
|
+
const type = getType(node.type);
|
|
62
|
+
const initializer = node.initializer ? `= ${visit(node.initializer)}` : '';
|
|
63
|
+
return `${type === ':' ? '' : 'var '}${visit(node.name)} ${type}${type === ':' ? '' : ' '}${initializer}`;
|
|
64
|
+
}
|
|
65
|
+
else if (ts.isCallExpression(node)) {
|
|
66
|
+
const caller = visit(node.expression);
|
|
67
|
+
const args = node.arguments.map((a) => visit(a));
|
|
68
|
+
return getCallString(caller, args);
|
|
69
|
+
}
|
|
70
|
+
else if (ts.isPrefixUnaryExpression(node)) {
|
|
71
|
+
return `${getOperatorText(node.operator)}${visit(node.operand)}`;
|
|
72
|
+
}
|
|
73
|
+
else if (ts.isPostfixUnaryExpression(node)) {
|
|
74
|
+
return `${visit(node.operand, { inline: true })}${getOperatorText(node.operator)}`;
|
|
75
|
+
}
|
|
76
|
+
else if (ts.isBinaryExpression(node)) {
|
|
77
|
+
return `${visit(node.left)} ${node.operatorToken.getText()} ${visit(node.right)}`;
|
|
78
|
+
}
|
|
79
|
+
else if (ts.isParenthesizedExpression(node)) {
|
|
80
|
+
return `(${visit(node.expression)})`;
|
|
81
|
+
}
|
|
82
|
+
else if (ts.isVariableDeclarationList(node)) {
|
|
83
|
+
return (node.declarations.map((n) => visit(n)).join(options.inline ? ';' : ';\n\t') +
|
|
84
|
+
(options.inline ? '' : ';\n\t'));
|
|
85
|
+
}
|
|
86
|
+
else if (ts.isExpressionStatement(node)) {
|
|
87
|
+
return visit(node.expression) + (options.inline ? '' : ';\n\t');
|
|
88
|
+
}
|
|
89
|
+
else if (ts.isForStatement(node)) {
|
|
90
|
+
return `for ${visit(node.initializer, { inline: true })}; ${visit(node.condition, {
|
|
91
|
+
inline: true
|
|
92
|
+
})}; ${visit(node.incrementor, { inline: true })}${visit(node.statement)}`;
|
|
93
|
+
}
|
|
94
|
+
else if (ts.isForOfStatement(node)) {
|
|
95
|
+
return `for _,${visit(node.initializer, { inline: true })}= range ${visit(node.expression, {
|
|
96
|
+
inline: true
|
|
97
|
+
})}${visit(node.statement)}`;
|
|
98
|
+
}
|
|
99
|
+
else if (ts.isWhileStatement(node)) {
|
|
100
|
+
return `for ${visit(node.expression, { inline: true })}${visit(node.statement)}`;
|
|
101
|
+
}
|
|
102
|
+
else if (ts.isDoStatement(node)) {
|
|
103
|
+
const condition = `\tif !(${visit(node.expression, {
|
|
104
|
+
inline: true
|
|
105
|
+
})}) {\n\t\t\tbreak \n\t\t}\n\t`;
|
|
106
|
+
return `for ${visit(node.statement, { inline: true, extraBlockContent: condition })}`;
|
|
107
|
+
}
|
|
108
|
+
else if (ts.isIfStatement(node)) {
|
|
109
|
+
const condition = `if ${visit(node.expression, { inline: true })} ${visit(node.thenStatement, {
|
|
110
|
+
inline: !!node.elseStatement
|
|
111
|
+
})}`;
|
|
112
|
+
if (node.elseStatement) {
|
|
113
|
+
return `${condition} else ${visit(node.elseStatement)}`;
|
|
114
|
+
}
|
|
115
|
+
return condition;
|
|
116
|
+
}
|
|
117
|
+
else if (ts.isSwitchStatement(node)) {
|
|
118
|
+
return `switch ${visit(node.expression)} ${visit(node.caseBlock)}`;
|
|
119
|
+
}
|
|
120
|
+
else if (ts.isCaseBlock(node)) {
|
|
121
|
+
return `{\n\t\t${node.clauses.map((c) => visit(c)).join('\n\t\t')}\n\t}`;
|
|
122
|
+
}
|
|
123
|
+
else if (ts.isCaseClause(node)) {
|
|
124
|
+
const isFallThrough = !node.statements.some((c) => ts.isBreakStatement(c));
|
|
125
|
+
return `case ${visit(node.expression, { inline: true })}: \n\t\t\t${node.statements
|
|
126
|
+
.filter((n) => !ts.isBreakStatement(n))
|
|
127
|
+
.map((s) => visit(s))
|
|
128
|
+
.join('')}${isFallThrough ? 'fallthrough\n\t' : ''}`;
|
|
129
|
+
}
|
|
130
|
+
else if (ts.isDefaultClause(node)) {
|
|
131
|
+
return `default: \n\t\t\t${node.statements
|
|
132
|
+
.filter((n) => !ts.isBreakStatement(n))
|
|
133
|
+
.map((s) => visit(s))
|
|
134
|
+
.join('')}`;
|
|
135
|
+
}
|
|
136
|
+
else if (ts.isBreakStatement(node)) {
|
|
137
|
+
return 'break';
|
|
138
|
+
}
|
|
139
|
+
else if (ts.isReturnStatement(node)) {
|
|
140
|
+
return (`return ${node.expression ? visit(node.expression) : ''}` + (options.inline ? '' : ';\n\t'));
|
|
141
|
+
}
|
|
142
|
+
else if (ts.isFunctionDeclaration(node) || ts.isFunctionExpression(node)) {
|
|
143
|
+
if (options.addFunctionOutside) {
|
|
144
|
+
outsideNodes.push(node);
|
|
145
|
+
return '';
|
|
146
|
+
}
|
|
147
|
+
const name = visit(node.name, { inline: true });
|
|
148
|
+
const parameters = node.parameters
|
|
149
|
+
.map((p) => `${visit(p.name)} ${getType(p.type)}`)
|
|
150
|
+
.join(', ');
|
|
151
|
+
const returnType = node.type ? ` ${getType(node.type)}` : '';
|
|
152
|
+
if (options.isOutside) {
|
|
153
|
+
return `func ${name}(${parameters})${returnType} ${visit(node.body)}`;
|
|
154
|
+
}
|
|
155
|
+
return `${name} := func(${parameters})${returnType} ${visit(node.body)}`;
|
|
156
|
+
}
|
|
157
|
+
else if (ts.isArrowFunction(node)) {
|
|
158
|
+
const parameters = node.parameters
|
|
159
|
+
.map((p) => `${visit(p.name)} ${getType(p.type)}`)
|
|
160
|
+
.join(', ');
|
|
161
|
+
const returnType = node.type ? ` ${getType(node.type)}` : '';
|
|
162
|
+
return `func(${parameters})${returnType} ${visit(node.body)}`;
|
|
163
|
+
}
|
|
164
|
+
const syntaxKind = ts.SyntaxKind[node.kind];
|
|
165
|
+
if (!['FirstStatement', 'EndOfFileToken'].includes(syntaxKind)) {
|
|
166
|
+
console.log(ts.SyntaxKind[node.kind], node.getText());
|
|
167
|
+
}
|
|
168
|
+
ts.forEachChild(node, (subNode) => {
|
|
169
|
+
code += visit(subNode);
|
|
170
|
+
});
|
|
171
|
+
return code;
|
|
172
|
+
}
|
|
173
|
+
function getType(typeNode, getArrayType = false) {
|
|
174
|
+
const type = TypeCheker.getTypeFromTypeNode(typeNode);
|
|
175
|
+
let typeName = TypeCheker.typeToString(type);
|
|
176
|
+
const isArray = typeName.includes('[]');
|
|
177
|
+
if (isArray) {
|
|
178
|
+
if (getArrayType) {
|
|
179
|
+
typeName = typeName.replace('[]', '');
|
|
180
|
+
}
|
|
181
|
+
else {
|
|
182
|
+
return ':';
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
switch (typeName) {
|
|
186
|
+
case 'number':
|
|
187
|
+
return 'float64';
|
|
188
|
+
case 'boolean':
|
|
189
|
+
return 'bool';
|
|
190
|
+
case 'any':
|
|
191
|
+
return ':';
|
|
192
|
+
case 'void':
|
|
193
|
+
return '';
|
|
194
|
+
default:
|
|
195
|
+
return typeName;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
function getAcessString(leftSide, rightSide) {
|
|
199
|
+
if (rightSide === 'length') {
|
|
200
|
+
return 'float64(len(arr))';
|
|
201
|
+
}
|
|
202
|
+
return `${leftSide}.${rightSide}`;
|
|
203
|
+
}
|
|
204
|
+
function getCallString(caller, args) {
|
|
205
|
+
if (caller === 'console.log') {
|
|
206
|
+
importedPackages.add('fmt');
|
|
207
|
+
return `fmt.Println(${args.join(', ')})`;
|
|
208
|
+
}
|
|
209
|
+
else if (caller === 'console.time') {
|
|
210
|
+
importedPackages.add('time');
|
|
211
|
+
return `${getTimerName(args[0])} := time.Now()`;
|
|
212
|
+
}
|
|
213
|
+
else if (caller === 'console.timeEnd') {
|
|
214
|
+
importedPackages.add('time');
|
|
215
|
+
importedPackages.add('fmt');
|
|
216
|
+
return `fmt.Println("Elapsed time:", time.Since(${getTimerName(args[0])}))`;
|
|
217
|
+
}
|
|
218
|
+
else if (caller === 'Math.random') {
|
|
219
|
+
importedPackages.add('math/rand');
|
|
220
|
+
return 'rand.Float64()';
|
|
221
|
+
}
|
|
222
|
+
else if (caller === 'Math.floor') {
|
|
223
|
+
importedPackages.add('math');
|
|
224
|
+
return `math.Floor(${args.join(', ')})`;
|
|
225
|
+
}
|
|
226
|
+
else if (caller.endsWith('.push')) {
|
|
227
|
+
const arrayName = caller.substring(0, caller.length - '.push'.length);
|
|
228
|
+
return `${arrayName} = append(${arrayName},${args.join(', ')})`;
|
|
229
|
+
}
|
|
230
|
+
return `${caller}(${args.join(', ')})`;
|
|
231
|
+
}
|
|
232
|
+
function getOperatorText(operator) {
|
|
233
|
+
switch (operator) {
|
|
234
|
+
case ts.SyntaxKind.PlusToken:
|
|
235
|
+
return '+';
|
|
236
|
+
case ts.SyntaxKind.MinusToken:
|
|
237
|
+
return '-';
|
|
238
|
+
case ts.SyntaxKind.TildeToken:
|
|
239
|
+
return '~';
|
|
240
|
+
case ts.SyntaxKind.ExclamationToken:
|
|
241
|
+
return '!';
|
|
242
|
+
case ts.SyntaxKind.PlusPlusToken:
|
|
243
|
+
return '++';
|
|
244
|
+
case ts.SyntaxKind.MinusMinusToken:
|
|
245
|
+
return '--';
|
|
246
|
+
default:
|
|
247
|
+
console.error('Did not find operator', operator);
|
|
248
|
+
return '';
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
function getTimerName(name) {
|
|
252
|
+
return `__timer_${name.replaceAll(' ', '_').replaceAll('"', '')}__`;
|
|
253
|
+
}
|
package/package.json
CHANGED
|
@@ -1,49 +1,48 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "typenative",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "Build native applications using Typescript.",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"bin": {
|
|
7
|
-
"typenative": "bin/index.js"
|
|
8
|
-
},
|
|
9
|
-
"scripts": {
|
|
10
|
-
"build": "tsc",
|
|
11
|
-
"watch": "tsc --watch",
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
"test2": "node ./bin/index --source test/test2.ts --script",
|
|
16
|
-
"test3": "node ./bin/index --source test/test3.ts --script",
|
|
17
|
-
"test3_node": "tsc test/test3.ts && node test/test3.js && rimraf test/test3.js",
|
|
18
|
-
"test4": "node ./bin/index --source test/test4.ts --script",
|
|
19
|
-
"test5": "node ./bin/index --source test/test5.ts --script",
|
|
20
|
-
"test6": "node ./bin/index --source test/test6.ts --script",
|
|
21
|
-
"test7": "node ./bin/index --source test/test7.ts --script",
|
|
22
|
-
"test8": "node ./bin/index --source test/test8.ts --script",
|
|
23
|
-
"test9": "node ./bin/index --source test/test9.ts --script",
|
|
24
|
-
"test10": "node ./bin/index --source test/test10.ts --script"
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
"
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
"
|
|
37
|
-
|
|
38
|
-
"@types/
|
|
39
|
-
"@types/
|
|
40
|
-
"
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "typenative",
|
|
3
|
+
"version": "0.0.14",
|
|
4
|
+
"description": "Build native applications using Typescript.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"typenative": "bin/index.js"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"build": "tsc",
|
|
11
|
+
"watch": "tsc --watch",
|
|
12
|
+
"test": "echo \"No tests specified\" && exit 0",
|
|
13
|
+
"release": "npm publish",
|
|
14
|
+
"test1": "node ./bin/index --source test/test1.ts --script",
|
|
15
|
+
"test2": "node ./bin/index --source test/test2.ts --script",
|
|
16
|
+
"test3": "node ./bin/index --source test/test3.ts --script",
|
|
17
|
+
"test3_node": "tsc test/test3.ts && node test/test3.js && rimraf test/test3.js",
|
|
18
|
+
"test4": "node ./bin/index --source test/test4.ts --script",
|
|
19
|
+
"test5": "node ./bin/index --source test/test5.ts --script",
|
|
20
|
+
"test6": "node ./bin/index --source test/test6.ts --script",
|
|
21
|
+
"test7": "node ./bin/index --source test/test7.ts --script",
|
|
22
|
+
"test8": "node ./bin/index --source test/test8.ts --script",
|
|
23
|
+
"test9": "node ./bin/index --source test/test9.ts --script",
|
|
24
|
+
"test10": "node ./bin/index --source test/test10.ts --script"
|
|
25
|
+
},
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "git+https://github.com/danisss9/typenative.git"
|
|
29
|
+
},
|
|
30
|
+
"author": "Dani Santos",
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"bugs": {
|
|
33
|
+
"url": "https://github.com/danisss9/typenative/issues"
|
|
34
|
+
},
|
|
35
|
+
"homepage": "https://github.com/danisss9/typenative#readme",
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@types/fs-extra": "^11.0.4",
|
|
38
|
+
"@types/inquirer": "^9.0.8",
|
|
39
|
+
"@types/node": "^22.15.21",
|
|
40
|
+
"rimraf": "^6.0.1"
|
|
41
|
+
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"execa": "^9.5.3",
|
|
44
|
+
"fs-extra": "^11.3.0",
|
|
45
|
+
"inquirer": "^12.6.1",
|
|
46
|
+
"typescript": "^5.8.3"
|
|
47
|
+
}
|
|
48
|
+
}
|
package/types/typenative.d.ts
CHANGED
|
@@ -1,54 +1,54 @@
|
|
|
1
|
-
interface SymbolConstructor {
|
|
2
|
-
readonly iterator: unique symbol;
|
|
3
|
-
}
|
|
4
|
-
|
|
5
|
-
declare var Symbol: SymbolConstructor;
|
|
6
|
-
|
|
7
|
-
interface IteratorYieldResult<TYield> {
|
|
8
|
-
done?: false;
|
|
9
|
-
value: TYield;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
interface IteratorReturnResult<TReturn> {
|
|
13
|
-
done: true;
|
|
14
|
-
value: TReturn;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
type IteratorResult<T, TReturn = any> = IteratorYieldResult<T> | IteratorReturnResult<TReturn>;
|
|
18
|
-
|
|
19
|
-
interface Iterator<T, TReturn = any, TNext = any> {
|
|
20
|
-
// NOTE: 'next' is defined using a tuple to ensure we report the correct assignability errors in all places.
|
|
21
|
-
next(...[value]: [] | [TNext]): IteratorResult<T, TReturn>;
|
|
22
|
-
return?(value?: TReturn): IteratorResult<T, TReturn>;
|
|
23
|
-
throw?(e?: any): IteratorResult<T, TReturn>;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
interface IterableIterator<T, TReturn = any, TNext = any> extends Iterator<T, TReturn, TNext> {
|
|
27
|
-
[Symbol.iterator](): IterableIterator<T, TReturn, TNext>;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
interface Array<T> extends IterableIterator<T> {
|
|
31
|
-
/**
|
|
32
|
-
* Gets or sets the length of the array. This is a number one higher than the highest index in the array.
|
|
33
|
-
*/
|
|
34
|
-
length: number;
|
|
35
|
-
/**
|
|
36
|
-
* Returns a string representation of an array.
|
|
37
|
-
*/
|
|
38
|
-
push(element: T): void;
|
|
39
|
-
|
|
40
|
-
[n: number]: T;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
interface Console {
|
|
44
|
-
log(...data: any[]): void;
|
|
45
|
-
time(label?: string): void;
|
|
46
|
-
timeEnd(label?: string): void;
|
|
47
|
-
}
|
|
48
|
-
declare var console: Console;
|
|
49
|
-
|
|
50
|
-
interface Math {
|
|
51
|
-
random(): number;
|
|
52
|
-
floor(x: number): number;
|
|
53
|
-
}
|
|
54
|
-
declare var Math: Math;
|
|
1
|
+
interface SymbolConstructor {
|
|
2
|
+
readonly iterator: unique symbol;
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
declare var Symbol: SymbolConstructor;
|
|
6
|
+
|
|
7
|
+
interface IteratorYieldResult<TYield> {
|
|
8
|
+
done?: false;
|
|
9
|
+
value: TYield;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
interface IteratorReturnResult<TReturn> {
|
|
13
|
+
done: true;
|
|
14
|
+
value: TReturn;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
type IteratorResult<T, TReturn = any> = IteratorYieldResult<T> | IteratorReturnResult<TReturn>;
|
|
18
|
+
|
|
19
|
+
interface Iterator<T, TReturn = any, TNext = any> {
|
|
20
|
+
// NOTE: 'next' is defined using a tuple to ensure we report the correct assignability errors in all places.
|
|
21
|
+
next(...[value]: [] | [TNext]): IteratorResult<T, TReturn>;
|
|
22
|
+
return?(value?: TReturn): IteratorResult<T, TReturn>;
|
|
23
|
+
throw?(e?: any): IteratorResult<T, TReturn>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface IterableIterator<T, TReturn = any, TNext = any> extends Iterator<T, TReturn, TNext> {
|
|
27
|
+
[Symbol.iterator](): IterableIterator<T, TReturn, TNext>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
interface Array<T> extends IterableIterator<T> {
|
|
31
|
+
/**
|
|
32
|
+
* Gets or sets the length of the array. This is a number one higher than the highest index in the array.
|
|
33
|
+
*/
|
|
34
|
+
length: number;
|
|
35
|
+
/**
|
|
36
|
+
* Returns a string representation of an array.
|
|
37
|
+
*/
|
|
38
|
+
push(element: T): void;
|
|
39
|
+
|
|
40
|
+
[n: number]: T;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
interface Console {
|
|
44
|
+
log(...data: any[]): void;
|
|
45
|
+
time(label?: string): void;
|
|
46
|
+
timeEnd(label?: string): void;
|
|
47
|
+
}
|
|
48
|
+
declare var console: Console;
|
|
49
|
+
|
|
50
|
+
interface Math {
|
|
51
|
+
random(): number;
|
|
52
|
+
floor(x: number): number;
|
|
53
|
+
}
|
|
54
|
+
declare var Math: Math;
|