UltraScript (Go TypeScript) Compiler
(Work In Progress!)
A high-performance programming language compiler that combines the best of Go's concurrency model with TypeScript's syntax and static typing, generating direct machine code for maximum performance.
Features
Core Language Features
- JavaScript/TypeScript Syntax Compatibility: Familiar syntax for web developers
- Static Typing: Optional but powerful type system with automatic type inference
- Goroutines: Lightweight concurrency using thread pools
- Promises & Async/Await: JavaScript-style asynchronous programming
- Tensor Operations: Built-in PyTorch-compatible tensor support
Compiler Features
- Direct Machine Code Generation: No intermediate representation for maximum speed
- Dual Backend Support: Both x86-64 and WebAssembly targets
- JIT Compilation: Runtime code generation and optimization
- Type-driven Optimization: Uses static type information for performance
Performance Features
- Zero-cost Abstractions: High-level features compile to efficient machine code
- Memory-optimized Dynamic Code: When types are unknown, trades memory for speed
- Atomic Operations: Thread-safe operations without locks where possible
- SharedArrayBuffer Support: WebAssembly backend uses shared memory
Syntax Examples
Basic Function Declaration
function doSomething(x: int64): int64 {
return x + 42;
}
Goroutines and Concurrency
// Spawn a goroutine
go doSomething(100);
// Await a goroutine result
let result = await go doSomething(200);
// Parallel execution with goMap
let numbers = [1, 2, 3, 4, 5];
let results = await Promise.all(numbers.goMap(doSomething));
Type System
// Explicit typing
let x: int64 = 42;
let y: float64 = 3.14;
// Any Type
let z = 100; // standard flexible js variable
Tensor Operations (NOT YET WORKING)
var x: [int64] = Array.full([2,4], 5) // 2x4 array of 5
x.shape; // returns [2]
var y = Array.zeros([10, 4, 5]);
var z = y.transpose().matmul(x);
Control Flow (No Parentheses Required)
if x == 5
console.log("x is 5")
for let i: int64 = 0; i < 100; ++i
console.log("i is", i)
for let item of list
print item
Compilation Targets
x86-64 Backend
- Direct assembly generation
- Register allocation optimization
- Function calling conventions
- Memory management
WebAssembly Backend [NOT IMPLEMENTED YET]
- WASM bytecode generation
- SharedArrayBuffer for goroutines
- Browser compatibility
- Node.js support
Type System
Primitive Types
int8,int16,int32,int64uint8,uint16,uint32,uint64float32,float64boolean,stringtensor
Type Casting Rules
- Types "cast up" to prevent precision loss
float32 * int32→float64int64 * float64→float64- Automatic casting when safe
- Explicit casting when needed
Concurrency Model
Goroutines
- Lightweight threads using thread pool
- Automatic scheduling
- Promise-based results
- Exception handling
Promise System
Promise.all()for parallel executiongoMap()for parallel array processingPromise.race()for competitive execution- Async/await syntax support
Build and Usage
Building the Compiler
Running Examples
Makefile Targets
make all- Build compilermake debug- Debug buildmake test- Run testsmake clean- Clean build files
Compiler Pipeline
- Lexer: Tokenizes source code
- Parser: Builds Abstract Syntax Tree (AST)
- Type Inference: Analyzes and infers types
- Code Generation: Emits machine code directly
- Runtime: Provides goroutine scheduling and memory management
Key Components
compiler.h/cpp- Main compiler interfacelexer.cpp- Tokenizationparser.cpp- AST constructiontype_inference.cpp- Type analysisx86_codegen.cpp- x86-64 code generationwasm_codegen.cpp- WebAssembly code generationruntime.h/cpp- Goroutine scheduler and memory managementtensor.h- Tensor operations
Performance Characteristics
Compilation Speed
- Direct code generation (no IR)
- Parallel compilation phases
- Incremental compilation support
Runtime Performance
- Near-C performance for statically typed code
- Optimized dynamic dispatch for untyped code
- Efficient goroutine scheduling
- Lock-free data structures where possible
Memory Usage
- Stack-allocated by default
- Shared memory for goroutines
- Garbage collection for dynamic objects
- Memory pooling for frequent allocations
Planned Features
- Libtorch integration
- Add ability to import parse and run Python syntax.