Skip to content

Claude/refactor sol2ts transpiler yp1ez#55

Merged
sudo-owen merged 13 commits intomainfrom
claude/refactor-sol2ts-transpiler-Yp1ez
Feb 2, 2026
Merged

Claude/refactor sol2ts transpiler yp1ez#55
sudo-owen merged 13 commits intomainfrom
claude/refactor-sol2ts-transpiler-Yp1ez

Conversation

@sudo-owen
Copy link
Collaborator

No description provided.

claude and others added 13 commits February 2, 2026 00:55
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
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
…ctoring

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
Generated TypeScript output should not be committed.

https://claude.ai/code/session_01GjrjyxhRAhckrhyJGogaQw
- 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
…istry

Both modules now import successfully with the runtime replacements
for EIP712 and Ownable. All 40 tests pass.

https://claude.ai/code/session_01GjrjyxhRAhckrhyJGogaQw
- 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
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
- 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
- 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
- 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
- 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
@sudo-owen sudo-owen merged commit d09559d into main Feb 2, 2026
1 check passed
@sudo-owen sudo-owen deleted the claude/refactor-sol2ts-transpiler-Yp1ez branch February 2, 2026 17:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants