Typescript runtime in C# (WIP and PoC trial) Note that this is a Proof Of Concept! By all means this is NOT to be used for production, and is intended as a SandBox and test ground.
This is the attempt to prove that we use too much processing power because we are lazy: we build with simple tools and languages - no harm there, and we should all aim to do that - but we care not to improve the foundations of our technology stack. Today, we surely burn through energy and batteries with our CPUs at a rate that could be improved, if only we had less browser driven development even more so for native applications.
It is intended to defy the very assumptions most people hold when they think 'javascript' or 'typescript' need to be executed in a browser. No, that is not a necessity and one could even implement assembly code from either to compile to native architectures. Be aware that you are entering an opinionated realm, for which barriers need to be taken down. The first barrier is always our dearest: what we assume we know - which is usually our biggest fault and impediment to progress.
If we as developers aim at elegant and simple code, shouldn't we also aim to clean up and build a more pristine foundation? I believe we should. If you believe so as well, reach out and try your best. You might learn a lot with that mindset.
The simplicity of TS is a great asset and if one could have it as a scripting language as lua or AngelScript, we could gain enourmous power. Obviously, if one can runtime a subset of TS, we might as well compile it :) And... why not?
-
dotnet publish -r osx-x64
(just publishes) -
See more at https://avaloniaui.net/docs/packing/macOS#manual
-
dotnet msbuild -t:BundleApp -p:RuntimeIdentifier=osx-x64
(bundles into TypeScriptNative.app)
dotnet publish -r linux-x64 -p:PublishSingleFile=true --self-contained true
If you have only build the debug, use the following:
- ./bin/Debug/net5.0/osx-x64/TypeScriptNative
- Scan source code and generate token list
- Parse the token list
- Resolve all structure (Type checking to be done still - it's the next pass)
- Interpret
Note: ANTLR was dropped in favour of a manual approach as it is not that much work :)
- const
- let
- import
- types
- you can type vars and functions but it will do nothing as of now
- as Type
println("a" + 3);
according to JS implementation, we should allow for it to be concatenated:a3
. Warns the user, but allows it.
-
llc code.ll
-> generatescode.s
-
gcc code.s -o code
-> generatescode
executable: run it by./code
-
Roman language (and how to interpret a simple ANTLR) and a SO to explain further.
-
Tutorial on how to generate the Lexer and Parser from TS grammar
-
http://franckgaspoz.fr/en/first-steps-with-antlr4-in-csharp/
-
https://stackoverflow.com/questions/29971097/how-to-create-ast-with-antlr4
Check out the amazing online book Crafting Interpreters by Bob Nystrom.
Here are a abit of juicy results for you :)
For now, we just do a time ./executable
with some example of the "hello world". Of course the inner guts of the language are not sampled, therefore I antecipate the analysis of environements would prove that my PoC is the worst performing of all current examples. Nevertheless, it's just implementation detail if you bear with me :) .
Go to benchmarks/cpp
and execute
time ./benchmarks/c/hello ./examples/time-benchmark.ts
Expected output:
Hello, world!
./benchmarks/c/hello ./examples/time-benchmark.ts 0.00s user 0.00s system 69% cpu 0.005 total
Install deno and:
time deno run ./examples/time-benchmark.ts
Output:
Check file:///Users/nmpribeiro/work/prog_lang/csts_gh/examples/time-benchmark.ts
Hello world
deno run ./examples/time-benchmark.ts 0.49s user 0.03s system 140% cpu 0.371 total
time ./bin/Debug/net5.0/osx-x64/TypeScriptNative ./examples/time-benchmark.ts
Output:
============================================
|| ::Welcome:: ||
|| TypeScript Native PoC v0.1 ||
============================================
Running from the following path: ./examples
Get: Undefined variable 'console'.
[line 10]
Exiting...
./bin/Debug/net5.0/osx-x64/TypeScriptNative ./examples/time-benchmark.ts 0.07s user 0.02s system 108% cpu 0.083 total