Commit d09559d
Claude/refactor sol2ts transpiler yp1ez (#55)
* Refactor sol2ts transpiler into modular architecture
Split the monolithic 6,065-line sol2ts.py into a modular structure:
- transpiler/lexer/ - Tokenization (tokens.py, lexer.py)
- TokenType enum, Token dataclass
- Keyword/operator mappings
- Lexer class
- transpiler/parser/ - AST and parsing (ast_nodes.py, parser.py)
- All AST node dataclasses (SourceUnit, ContractDefinition, etc.)
- Recursive descent parser
- transpiler/types/ - Type system (registry.py, mappings.py)
- TypeRegistry for cross-file type discovery
- Solidity-to-TypeScript type mappings
- Default value helpers
- transpiler/codegen/ - Code generation helpers (yul.py, abi.py, context.py)
- YulTranspiler for inline assembly
- AbiTypeInferer for ABI encoding
- CodeGenerationContext for state management
The original sol2ts.py is preserved for backward compatibility.
New code can import from the modules directly:
from transpiler.lexer import Lexer
from transpiler.parser import Parser
from transpiler.types import TypeRegistry
https://claude.ai/code/session_01GjrjyxhRAhckrhyJGogaQw
* Complete modular refactoring of sol2ts transpiler
Decompose the monolithic TypeScriptCodeGenerator into focused generator classes:
- BaseGenerator: Shared utilities for all generators
- TypeConverter: Solidity to TypeScript type conversions
- ExpressionGenerator: Expression AST code generation
- StatementGenerator: Statement AST code generation
- FunctionGenerator: Function/constructor generation
- DefinitionGenerator: Struct/enum/constant generation
- ImportGenerator: Import statement generation
- ContractGenerator: Contract class generation
- generator.py: Main orchestrator coordinating all generators
Benefits:
- Single responsibility for each generator class
- Easier testing and maintenance of individual components
- Cleaner separation of concerns
- Total 116 Solidity files transpiled successfully in testing
https://claude.ai/code/session_01GjrjyxhRAhckrhyJGogaQw
* Fix ABI type inference for method return types, finalize modular refactoring
Changes:
- Add current_method_return_types tracking in CodeGenerationContext
- Populate method return types in ContractGenerator._setup_contract_context
- Fix ExpressionGenerator._infer_single_abi_type to lookup method return types
- Update test_transpiler.py to use modular imports
- Replace old 6,065-line sol2ts.py with new 264-line modular version
- Update transpiler/__init__.py exports for new module structure
All 8 unit tests pass and 111 Solidity files transpile successfully.
https://claude.ai/code/session_01GjrjyxhRAhckrhyJGogaQw
* Add ts-output/ to gitignore
Generated TypeScript output should not be committed.
https://claude.ai/code/session_01GjrjyxhRAhckrhyJGogaQw
* Add metadata generation for factories.ts dependency injection
- Create MetadataExtractor to collect contract info from ASTs
- Create FactoryGenerator to produce factories.ts with container registrations
- Integrate metadata generation with --emit-metadata flag
- Auto-generate interface aliases (IEngine -> Engine, etc.)
- Auto-generate lazy singletons with constructor dependencies
All 38 TypeScript tests now pass (previously 31 passed, 1 suite failed).
https://claude.ai/code/session_01GjrjyxhRAhckrhyJGogaQw
* Enable previously skipped tests for SignedMatchmaker and GachaTeamRegistry
Both modules now import successfully with the runtime replacements
for EIP712 and Ownable. All 40 tests pass.
https://claude.ai/code/session_01GjrjyxhRAhckrhyJGogaQw
* Replace CHANGELOG.md with README.md documenting modular architecture
- Document new module structure (lexer/, parser/, types/, codegen/)
- Document code generator architecture with specialized generators
- Update test counts (40 TypeScript tests, 8 Python tests)
- Document factories.ts generation with --emit-metadata
- Preserve clear, no-frills documentation style
https://claude.ai/code/session_01GjrjyxhRAhckrhyJGogaQw
* Refactor parser.py to reduce repetition with DRY helpers
Add utility methods:
- _parse_binary_op(): Generic left-associative binary operator parsing
- parse_comma_separated(): Parse comma-separated lists with end token
- parse_storage_location(): Parse storage/memory/calldata
- skip_balanced(): Skip balanced bracket pairs (parens, braces)
Apply to simplify:
- 10 binary operator methods now use _parse_binary_op helper
- Event/error/function parameters use parse_comma_separated
- State variable/function attributes use token->value dictionaries
- Function skip, try/catch, base contracts use skip_balanced
All 40 TypeScript tests and Python unit tests pass.
https://claude.ai/code/session_01GjrjyxhRAhckrhyJGogaQw
* Apply DRY refactoring to codegen modules and fix types/ naming conflict
- expression.py: Extract _resolve_abi_base_type() and _infer_expression_type()
to share logic between ABI type inference methods (~80 lines reduced)
- statement.py: Extract _generate_body_statements() for loop body generation
- function.py: Extract _get_visibility_modifier() and _get_static_modifier()
- Rename types/ to type_system/ to avoid shadowing Python's standard library
All 40 TypeScript tests and 8 Python unit tests pass.
https://claude.ai/code/session_01GjrjyxhRAhckrhyJGogaQw
* Fix bigint index type error and single-file import path calculation
- contract.py: Convert non-string mapping keys to String() for Record indexing
Fixes TS2538: Type 'bigint' cannot be used as an index type
- sol2ts.py: Use discovery_dir as source_dir in single-file mode
Fixes import paths being '../' instead of './' when processing single files
https://claude.ai/code/session_01GjrjyxhRAhckrhyJGogaQw
* Fix bytes32 cast and Constants reference in Yul assembly
- type_converter.py: Handle bytes32/bytesN casts of non-literal expressions
Convert bigint to padded hex string at runtime: `0x${expr.toString(16).padStart(64, "0")}`
Fixes TS2322: Type 'bigint' is not assignable to type 'string'
- yul.py: Prefix ALL_CAPS identifiers with Constants.
Fixes TS2304: Cannot find name 'PACKED_CLEARED_MON_STATE'
https://claude.ai/code/session_01GjrjyxhRAhckrhyJGogaQw
* Use type registry for Yul constant resolution instead of heuristic
- YulTranspiler now accepts known_constants set from type registry
- StatementGenerator passes ctx.known_constants to YulTranspiler
- Replaces fragile ALL_CAPS heuristic with proper type tracking
https://claude.ai/code/session_01GjrjyxhRAhckrhyJGogaQw
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent b09b79d commit d09559d
5 files changed
Lines changed: 33 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
402 | 402 | | |
403 | 403 | | |
404 | 404 | | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
405 | 408 | | |
406 | 409 | | |
407 | | - | |
| 410 | + | |
408 | 411 | | |
409 | 412 | | |
410 | | - | |
| 413 | + | |
411 | 414 | | |
412 | 415 | | |
413 | 416 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
72 | 72 | | |
73 | 73 | | |
74 | 74 | | |
75 | | - | |
| 75 | + | |
76 | 76 | | |
77 | 77 | | |
78 | 78 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
190 | 190 | | |
191 | 191 | | |
192 | 192 | | |
193 | | - | |
| 193 | + | |
194 | 194 | | |
195 | 195 | | |
196 | 196 | | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
197 | 200 | | |
198 | 201 | | |
199 | 202 | | |
200 | 203 | | |
201 | 204 | | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
202 | 209 | | |
203 | 210 | | |
204 | 211 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
51 | 59 | | |
52 | 60 | | |
53 | 61 | | |
| |||
285 | 293 | | |
286 | 294 | | |
287 | 295 | | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
288 | 300 | | |
289 | 301 | | |
290 | 302 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
| 16 | + | |
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| |||
244 | 244 | | |
245 | 245 | | |
246 | 246 | | |
| 247 | + | |
| 248 | + | |
247 | 249 | | |
248 | 250 | | |
249 | 251 | | |
| |||
254 | 256 | | |
255 | 257 | | |
256 | 258 | | |
257 | | - | |
| 259 | + | |
258 | 260 | | |
259 | 261 | | |
260 | 262 | | |
261 | 263 | | |
| 264 | + | |
| 265 | + | |
262 | 266 | | |
| 267 | + | |
263 | 268 | | |
264 | 269 | | |
265 | 270 | | |
| |||
0 commit comments