@@ -8,217 +8,125 @@ SharpTS is a TypeScript interpreter and compiler implemented in C# using .NET 10
88
99## Build and Run Commands
1010
11- ### Build the project
1211``` bash
13- dotnet build
12+ dotnet build # Build the project
13+ dotnet test # Run xUnit tests
14+ dotnet test --filter " FullyQualifiedName~SomeTest" # Run specific test
15+ dotnet run # Start REPL mode
16+ dotnet run -- script.ts # Interpret a TypeScript file
17+ dotnet run -- --compile script.ts # Compile to .NET assembly
18+ dotnet run -- --compile script.ts -o out.dll # Custom output path
19+ dotnet run -- --compile script.ts --verify # Verify emitted IL
1420```
1521
16- ### Run REPL mode
17- ``` bash
18- dotnet run
19- ```
22+ ## Architecture
2023
21- ### Execute a TypeScript file (interpreted)
22- ``` bash
23- dotnet run -- < filename> .ts
24- ```
24+ ### Pipeline Flow
2525
26- ### Compile to .NET IL (ahead-of-time)
27- ``` bash
28- dotnet run -- --compile < filename> .ts # Outputs <filename>.dll
29- dotnet run -- --compile < filename> .ts -o out.dll # Custom output path
30- dotnet run -- --compile < filename> .ts --pack # Generate NuGet package
3126```
32-
33- ### Run compiled output
34- ``` bash
35- dotnet < filename> .dll
27+ Source → Lexer → Parser → TypeChecker → Interpreter (tree-walk)
28+ ↘ ILCompiler (AOT to .NET IL)
3629```
3730
38- ### Run tests
39- The project uses xUnit tests in the ` SharpTS.Tests/ ` directory:
40- ``` bash
41- dotnet test
42- ```
31+ ### Directory Structure
4332
44- ## Architecture
33+ | Directory | Purpose |
34+ | -----------| ---------|
35+ | ` Parsing/ ` | Lexer, Parser (14 partial files), AST node definitions |
36+ | ` TypeSystem/ ` | Static type checking (47 files), type compatibility, generics |
37+ | ` Execution/ ` | Tree-walking interpreter (16 files) |
38+ | ` Compilation/ ` | IL compilation (316 files), async state machines, bundling |
39+ | ` Runtime/ ` | Runtime values, environment, built-ins |
40+ | ` Runtime/Types/ ` | TypeScript value types (87 files): arrays, classes, promises, etc. |
41+ | ` Runtime/BuiltIns/ ` | Built-in method implementations (41 files) |
42+ | ` Runtime/Exceptions/ ` | Control flow exceptions (return, break, continue, yield) |
43+ | ` Modules/ ` | Module resolution, script detection |
44+ | ` Diagnostics/ ` | Error reporting, source locations |
45+ | ` Packaging/ ` | NuGet package generation |
46+ | ` Cli/ ` | Command-line argument parsing |
47+ | ` Declaration/ ` | TypeScript declaration generation from .NET types |
48+ | ` LspBridge/ ` | Language Server Protocol bridge for IDE integration |
49+ | ` SharpTS.Tests/ ` | xUnit test project |
50+
51+ ### Critical Architecture Patterns
4552
46- SharpTS follows a traditional compiler/interpreter pipeline with a critical separation between compile-time and runtime phases:
53+ ** Two-Environment System:**
54+ - ` TypeEnvironment ` - Tracks types during static analysis (compile-time)
55+ - ` RuntimeEnvironment ` - Tracks values during execution (runtime)
56+ - Never mix these - they serve completely different phases
4757
48- ### Directory Structure
58+ ** Control Flow via Exceptions:**
59+ - ` ReturnException ` , ` BreakException ` , ` ContinueException ` , ` YieldException `
60+ - This is intentional - exceptions as control flow mechanism for unwinding
4961
50- ```
51- SharpTS/
52- ├── Parsing/ # Frontend pipeline (namespace: SharpTS.Parsing)
53- │ ├── Token.cs # Token types and definitions
54- │ ├── Lexer.cs # Lexical analyzer
55- │ ├── Parser.cs # Recursive descent parser
56- │ └── AST.cs # Expr and Stmt record types
57- │
58- ├── TypeSystem/ # Static type analysis (namespace: SharpTS.TypeSystem)
59- │ ├── TypeInfo.cs # Type representations
60- │ ├── TypeChecker.cs # Static type validator
61- │ └── TypeEnvironment.cs # Type scope management
62- │
63- ├── Runtime/ # Runtime values and infrastructure
64- │ ├── RuntimeEnvironment.cs # Variable scope management (namespace: SharpTS.Runtime)
65- │ ├── Types/ # Runtime value types (namespace: SharpTS.Runtime.Types)
66- │ │ ├── SharpTSArray.cs
67- │ │ ├── SharpTSClass.cs
68- │ │ ├── SharpTSEnum.cs
69- │ │ ├── SharpTSFunction.cs
70- │ │ ├── SharpTSInstance.cs
71- │ │ ├── SharpTSMath.cs
72- │ │ └── SharpTSObject.cs
73- │ ├── BuiltIns/ # Built-in methods (namespace: SharpTS.Runtime.BuiltIns)
74- │ │ ├── ArrayBuiltIns.cs
75- │ │ ├── StringBuiltIns.cs
76- │ │ ├── ObjectBuiltIns.cs
77- │ │ ├── MathBuiltIns.cs
78- │ │ ├── BuiltInTypes.cs
79- │ │ └── BuiltInMethod.cs
80- │ └── Exceptions/ # Control flow exceptions (namespace: SharpTS.Runtime.Exceptions)
81- │ ├── ReturnException.cs
82- │ ├── BreakException.cs
83- │ ├── ContinueException.cs
84- │ └── ThrowException.cs
85- │
86- ├── Execution/ # Tree-walking interpreter (namespace: SharpTS.Execution)
87- │ ├── Interpreter.cs # Core infrastructure
88- │ ├── Interpreter.Statements.cs
89- │ ├── Interpreter.Expressions.cs
90- │ ├── Interpreter.Properties.cs
91- │ ├── Interpreter.Calls.cs
92- │ └── Interpreter.Operators.cs
93- │
94- ├── Compilation/ # IL compilation (namespace: SharpTS.Compilation)
95- │ ├── ILCompiler.cs # Main orchestrator
96- │ ├── ILEmitter.cs # IL instruction emission (+ partial files)
97- │ ├── ClosureAnalyzer.cs # Closure detection
98- │ ├── CompilationContext.cs # Compilation state
99- │ ├── RuntimeTypes.cs # Runtime type emission
100- │ ├── RuntimeEmitter.cs # Runtime code emission
101- │ ├── LocalsManager.cs # Local variable tracking
102- │ ├── TypeMapper.cs # TypeScript-to-.NET type mapping
103- │ └── EmittedRuntime.cs # Emitted runtime references
104- │
105- ├── Packaging/ # NuGet package generation (namespace: SharpTS.Packaging)
106- │ ├── AssemblyMetadata.cs # Assembly version and attributes
107- │ ├── AssemblyAttributeBuilder.cs # Build assembly-level attributes
108- │ ├── PackageJson.cs # package.json model
109- │ ├── PackageJsonLoader.cs # package.json parser
110- │ ├── NuGetPackager.cs # .nupkg generation
111- │ ├── SymbolPackager.cs # .snupkg generation
112- │ ├── PackageValidator.cs # Pre-packaging validation
113- │ └── NuGetPublisher.cs # Push to NuGet feeds
114- │
115- ├── SharpTS.Tests/ # xUnit test project
116- ├── Program.cs # Entry point
117- └── SharpTS.csproj
118- ```
62+ ** Type System:**
63+ - Structural typing for interfaces (duck typing)
64+ - Nominal typing for classes (inheritance-based)
65+ - ` TypeInfo ` records represent types statically; runtime objects are independent
11966
120- ### Pipeline Phases
67+ ### RuntimeValue Boxing Elimination (Active Optimization)
12168
122- 1 . ** Lexical Analysis** (` Parsing/Lexer.cs ` )
123- - Tokenizes source code into ` Token ` objects
124- - Produces a flat stream for parsing
69+ The codebase is migrating from ` object? ` to ` RuntimeValue ` struct to eliminate boxing:
12570
126- 2 . ** Syntax Analysis** (` Parsing/Parser.cs ` )
127- - Recursive descent parser
128- - Builds Abstract Syntax Tree (AST) from tokens
129- - Performs "desugaring" (e.g., ` for ` loops become ` while ` loops)
130- - AST nodes defined in ` Parsing/AST.cs ` with expression (` Expr ` ) and statement (` Stmt ` ) records
71+ ** RuntimeValue** (` Runtime/RuntimeValue.cs ` ):
72+ - 24-byte discriminated union storing primitives inline
73+ - ` ValueKind ` enum: Undefined, Null, Boolean, Number, String, Object, Symbol, BigInt
74+ - Factory methods: ` FromNumber() ` , ` FromString() ` , ` FromBoolean() ` , ` FromObject() ` , ` FromBoxed() `
75+ - Accessors: ` AsNumber() ` , ` AsString() ` , ` AsBoolean() ` , ` AsObject<T>() `
76+ - JavaScript semantics: ` IsTruthy() ` , ` TypeofString() `
13177
132- 3 . ** Static Type Checking** (` TypeSystem/TypeChecker.cs ` ) - ** Separate compile-time phase**
133- - Runs BEFORE interpretation or compilation
134- - Validates type compatibility (nominal and structural)
135- - Checks function signatures, inheritance safety
136- - Uses ` TypeSystem/TypeEnvironment.cs ` for type scopes
137- - Type representations in ` TypeSystem/TypeInfo.cs ` (Primitive, Function, Class, Interface, Instance, Array, Record, Void, Any)
78+ ** ISharpTSCallableV2** (` Runtime/Types/ISharpTSCallableV2.cs ` ):
79+ - New callable interface using ` RuntimeValue ` instead of ` object? `
80+ - ` CallableV2Adapter ` wraps legacy → V2, ` CallableLegacyAdapter ` wraps V2 → legacy
81+ - Extension methods: ` .AsV2() ` , ` .AsLegacy() ` , ` .CallWithRuntimeValues() `
13882
139- 4 . ** Runtime Interpretation** (` Execution/Interpreter*.cs ` ) - One execution path
140- - Tree-walking interpreter split across partial class files
141- - Executes validated AST
142- - Uses ` Runtime/RuntimeEnvironment.cs ` for variable scopes
143- - Runtime types in ` Runtime/Types/ ` : SharpTSClass, SharpTSInstance, SharpTSFunction, SharpTSArray, SharpTSObject
83+ ** BuiltInMethod** (` Runtime/BuiltIns/BuiltInMethod.cs ` ):
84+ - Implements both ` ISharpTSCallable ` and ` ISharpTSCallableV2 `
85+ - ` CreateV2() ` factory for RuntimeValue-based methods
86+ - Thread-local pooling in array built-ins to avoid allocations
14487
145- 5 . ** IL Compilation** (` Compilation/ ` directory) - Alternative execution path
146- - ` ILCompiler.cs ` : Main orchestrator, multi-phase compilation
147- - ` ILEmitter.cs ` : Emits IL instructions for statements and expressions
148- - ` ClosureAnalyzer.cs ` : Detects captured variables for closure support
149- - ` CompilationContext.cs ` : Tracks locals, parameters, and compilation state
150- - Uses ` System.Reflection.Emit ` with ` PersistedAssemblyBuilder ` to generate .NET assemblies
88+ ### Visitor-Style Traversal Pattern
15189
152- ### Critical Architecture Notes
90+ All major phases use switch pattern matching on AST node types:
91+ - ` TypeChecker.Check() ` / ` CheckExpr() ` - static analysis
92+ - ` Interpreter.Execute() ` / ` Evaluate() ` - runtime
93+ - ` ILEmitter.EmitStatement() ` / ` EmitExpression() ` - IL compilation
15394
154- ** Two-Environment System:**
155- - ` TypeEnvironment ` : Tracks types during static analysis
156- - ` RuntimeEnvironment ` : Tracks values during execution
157- - These are completely separate - never mix them
158-
159- ** Type System Design:**
160- - Supports ** structural typing** for interfaces (duck typing)
161- - Supports ** nominal typing** for classes (inheritance-based)
162- - Type checking happens at compile-time; runtime uses dynamic object model
163- - ` TypeInfo ` records represent types statically
164- - Runtime objects (` SharpTSInstance ` , etc.) are independent of ` TypeInfo `
165-
166- ** Control Flow via Exceptions:** (` Runtime/Exceptions/ ` )
167- - ` ReturnException.cs ` : Unwinding the call stack on ` return ` statements
168- - ` BreakException.cs ` : Breaking out of loops and switch statements
169- - ` ContinueException.cs ` : Continuing to next loop iteration
170- - ` ThrowException.cs ` : User-thrown exceptions in try/catch
171- - This is intentional - exceptions as control flow mechanism
172-
173- ** Entry Point:**
174- - ` Program.cs ` orchestrates the pipeline: Lex → Parse → TypeCheck → (Interpret OR Compile)
175- - Errors in type checking prevent execution or compilation from running
176- - The ` --compile ` flag switches from interpretation to IL compilation
177-
178- ## Language Features
179-
180- - ** Primitives:** ` string ` , ` number ` , ` boolean ` , ` null `
181- - ** Arrays:** Homogeneous typed arrays (` number[] ` ) with built-in methods (push, pop, map, filter, etc.)
182- - ** Objects:** JSON-style object literals with structural typing
183- - ** Classes:** Constructors, methods, fields, inheritance (` extends ` ), ` super ` calls
184- - ** Interfaces:** Structural type checking (shape-based compatibility)
185- - ** Functions:** First-class functions, arrow functions with closures, default parameters
186- - ** Control Flow:** ` if/else ` , ` while ` , ` for ` , ` for...of ` , ` switch ` , ` break ` , ` continue `
187- - ** Error Handling:** ` try/catch/finally ` , ` throw `
188- - ** Operators:** Nullish coalescing (` ?? ` ), optional chaining (` ?. ` ), ternary (` ?: ` ), template literals
189- - ** Built-ins:** ` console.log ` , ` Math ` object (constants and methods), string methods
190-
191- ## Development Patterns
192-
193- ### AST Node Pattern
194- - All AST nodes are discriminated unions using C# records
95+ ### AST Nodes
96+
97+ All AST nodes are C# records in ` Parsing/AST.cs ` :
19598- Expression nodes inherit from ` Expr `
19699- Statement nodes inherit from ` Stmt `
197- - Use pattern matching (` switch ` expressions) to traverse
100+ - Use pattern matching to traverse
101+
102+ ### IL Compilation Phases
198103
199- ### Visitor-Style Traversal
200- - ` TypeChecker.Check() ` and ` TypeChecker.CheckExpr() ` for static analysis
201- - ` Interpreter.Execute() ` and ` Interpreter.Evaluate() ` for runtime
202- - ` ILEmitter.EmitStatement() ` and ` ILEmitter.EmitExpression() ` for IL compilation
203- - All use switch pattern matching on AST node types
104+ ILCompiler runs in multiple phases:
105+ 1 . Emit runtime types
106+ 2 . Analyze closures
107+ 3 . Define classes/functions
108+ 4 . Collect arrow functions
109+ 5 . Emit arrow bodies
110+ 6 . Emit class methods
111+ 7 . Emit entry point
112+ 8 . Finalize types
204113
205- ### Error Handling
206- - Type errors throw exceptions with "Type Error:" prefix
207- - Runtime errors throw exceptions with "Runtime Error:" prefix
208- - Main loop in ` Program.cs ` catches and displays errors
114+ Arrow functions use display classes for captured variables; non-capturing arrows compile to static methods.
209115
210- ## Code Conventions
116+ ### Error Handling Conventions
211117
212- - ** C# Version:** C# 12/13 with .NET 10 preview features
213- - ** Nullable Reference Types:** Enabled (` <Nullable>enable</Nullable> ` )
214- - ** Records:** Heavily used for immutable AST nodes and type representations
215- - ** Primary Constructors:** Used in ` Parser ` , ` RuntimeEnvironment ` , ` TypeEnvironment `
118+ - Type errors: "Type Error:" prefix
119+ - Runtime errors: "Runtime Error:" prefix
120+ - Compile errors: "Compile Error:" prefix
216121
217122## Important Implementation Details
218123
219- - ** For Loop Desugaring:** Parser converts ` for ` loops into ` while ` loops during parsing
220- - ** console.log Special Case:** Handled as a hardcoded special case in type checker, interpreter, and compiler
221- - ** Constructor Validation:** Type checker validates constructor signatures during class instantiation
222- - ** Method Lookup:** Searches up the inheritance chain for methods (see ` TypeSystem/TypeChecker.cs ` CheckGet and ` Execution/Interpreter.Properties.cs ` EvaluateGet)
223- - ** Closure Compilation:** Arrow functions use display classes for captured variables; non-capturing arrows compile to static methods
224- - ** IL Compilation Phases:** ILCompiler runs in 9 phases - emit runtime types, analyze closures, define classes/functions, collect arrow functions, emit arrow bodies, emit class methods, emit entry point, finalize types
124+ - ** For Loop Desugaring:** Parser converts ` for ` loops into ` while ` loops
125+ - ** console.log:** Hardcoded special case in type checker, interpreter, and compiler
126+ - ** Inner function declarations:** Not supported in IL compiler - use arrow functions instead
127+ - ** Method Lookup:** Searches up inheritance chain (see ` TypeChecker.cs ` CheckGet, ` Interpreter.Properties.cs ` EvaluateGet)
128+
129+ ## See Also
130+
131+ - ` STATUS.md ` - Feature implementation status and known bugs
132+ - ` README.md ` - User documentation and examples
0 commit comments