Claude/refactor sol2ts transpiler yp1ez#55
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.