{"id":30116378,"url":"https://github.com/banghuazhao/swift-super-tiny-compiler","last_synced_at":"2025-09-03T12:43:15.068Z","repository":{"id":308882248,"uuid":"1034434263","full_name":"banghuazhao/swift-super-tiny-compiler","owner":"banghuazhao","description":"Swift implementation of The Super Tiny Compiler - transforms LISP syntax to Swift function calls","archived":false,"fork":false,"pushed_at":"2025-08-08T11:54:42.000Z","size":13,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-08-08T13:33:49.878Z","etag":null,"topics":["abstract-syntax-tree","ast-transformation","code-generation","code-transformation","compiler","compiler-design","compiler-pipeline","educational","functional-programming","learning","learning-project","lexical-analysis","lisp","parsing","programming-languages","swift","swift-compiler","swift-language","tokenization","visitor-pattern"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/banghuazhao.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-08-08T11:37:51.000Z","updated_at":"2025-08-08T11:54:45.000Z","dependencies_parsed_at":"2025-08-08T13:33:53.879Z","dependency_job_id":"f7a03cff-b0bf-4eef-bc87-d911a7d6e4d1","html_url":"https://github.com/banghuazhao/swift-super-tiny-compiler","commit_stats":null,"previous_names":["banghuazhao/swift-super-tiny-compiler"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/banghuazhao/swift-super-tiny-compiler","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/banghuazhao%2Fswift-super-tiny-compiler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/banghuazhao%2Fswift-super-tiny-compiler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/banghuazhao%2Fswift-super-tiny-compiler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/banghuazhao%2Fswift-super-tiny-compiler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/banghuazhao","download_url":"https://codeload.github.com/banghuazhao/swift-super-tiny-compiler/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/banghuazhao%2Fswift-super-tiny-compiler/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273445639,"owners_count":25107150,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-03T02:00:09.631Z","response_time":76,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["abstract-syntax-tree","ast-transformation","code-generation","code-transformation","compiler","compiler-design","compiler-pipeline","educational","functional-programming","learning","learning-project","lexical-analysis","lisp","parsing","programming-languages","swift","swift-compiler","swift-language","tokenization","visitor-pattern"],"created_at":"2025-08-10T09:36:14.666Z","updated_at":"2025-09-03T12:43:15.019Z","avatar_url":"https://github.com/banghuazhao.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Swift Super Tiny Compiler\n\nA Swift implementation of a super tiny compiler that demonstrates the fundamental concepts of compiler design. This project transforms LISP-like function calls into JavaScript-style function calls.\n\n## Overview\n\nThis compiler takes a simple LISP-like syntax and converts it into Swift-style function calls. It demonstrates the four main phases of compilation:\n\n1. **Tokenization** - Breaking input into tokens\n2. **Parsing** - Converting tokens into an Abstract Syntax Tree (AST)\n3. **Transformation** - Converting the AST into a different format\n4. **Code Generation** - Converting the transformed AST into output code\n\n## Example\n\n**Input (LISP-like):**\n```\n(add 2 (subtract 4 2))\n```\n\n**Output (Swift-style):**\n```\nadd(2, subtract(4, 2))\n```\n\n## Architecture\n\nThe compiler is organized into several key components that work together to transform LISP-like syntax into Swift-style function calls:\n\n### 1. Tokenizer (`tokenizer.swift`)\n- Breaks the input string into tokens\n- Recognizes parentheses, numbers, strings, and identifiers\n- Handles whitespace and different character types\n\n### 2. Parser (`parser.swift`)\n- Converts tokens into an Abstract Syntax Tree (AST)\n- Defines AST node types: Program, CallExpression, NumberLiteral, StringLiteral, Identifier\n- Implements recursive descent parsing\n\n### 3. Transformer (`transformer.swift`)\n- Transforms the AST from LISP-style to Swift-style\n- Uses the Visitor pattern to traverse and transform nodes\n- Converts CallExpression nodes to Swift function call format using SwiftASTNode structures\n\n### 4. Code Generator (`codeGenerator.swift`)\n- Converts the transformed AST back into code\n- Generates Swift-style function calls without semicolons\n\n### 5. Traverser (`traverser.swift`)\n- Implements the Visitor pattern for AST traversal\n- Allows for different operations during tree traversal\n\n## Project Structure\n\n```\nSwiftSuperTinyCompiler/\n├── main.swift           # Entry point and test runner\n├── compiler.swift       # Main compiler orchestration\n├── tokenizer.swift      # Tokenization logic\n├── parser.swift         # Parsing and AST creation\n├── transformer.swift    # AST transformation\n├── traverser.swift      # AST traversal utilities\n└── codeGenerator.swift  # Code generation\n```\n\n## How to Run\n\n1. Open the project in Xcode\n2. Build and run the project\n3. The main function will execute a test case and display the results\n\n## Test Case\n\nThe compiler includes a built-in test that demonstrates the transformation:\n\n```swift\nlet input = \"(add 2 (subtract 4 2))\"\nlet output = compiler(input: input)\nlet expectedOutput = \"add(2, subtract(4, 2))\"\n```\n\n## Supported Syntax\n\nThe compiler supports a simple LISP-like syntax:\n\n- **Function calls**: `(functionName arg1 arg2 ...)`\n- **Numbers**: `123`, `456`\n- **Strings**: `\"hello world\"`\n- **Nested expressions**: `(add 2 (subtract 4 2))`\n\n## Compiler Pipeline\n\n1. **Input**: `\"(add 2 (subtract 4 2))\"`\n2. **Tokens**: `[paren, name, number, paren, name, number, number, paren, paren]`\n3. **AST**: Nested CallExpression nodes with parameters\n4. **Transformed AST**: Swift-style AST with callee and arguments\n5. **Output**: `\"add(2, subtract(4, 2))\"`\n\n## Learning Objectives\n\nThis project demonstrates:\n\n- **Lexical Analysis**: How to break source code into tokens\n- **Syntax Analysis**: How to build an Abstract Syntax Tree\n- **AST Transformation**: How to convert between different AST formats (LISP-style to Swift-style)\n- **Code Generation**: How to generate Swift code from an AST\n- **Visitor Pattern**: How to traverse and operate on tree structures\n- **Swift-Specific Features**: How to implement Swift-style AST structures and code generation\n\n## Dependencies\n\n- Swift 5.0+\n- Foundation framework\n- Xcode (for building and running)\n\n## Author\n\nCreated by [Banghua Zhao](https://github.com/banghuazhao)\n\n---\n\nThis project is inspired by [The Super Tiny Compiler](https://github.com/jamiebuilds/the-super-tiny-compiler) by Jamie Kyle, which demonstrates compiler concepts in JavaScript. This Swift implementation extends the original concept by generating Swift-style function calls instead of JavaScript, serving as an educational tool for understanding compiler design principles in Swift.\n\n## Related Projects\n\n- **Original JavaScript Implementation**: [jamiebuilds/the-super-tiny-compiler](https://github.com/jamiebuilds/the-super-tiny-compiler) - The original JavaScript version that inspired this project\n- **Swift Implementation**: [banghuazhao/swift-super-tiny-compiler](https://github.com/banghuazhao/swift-super-tiny-compiler) - This Swift port that generates Swift-style function calls\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbanghuazhao%2Fswift-super-tiny-compiler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbanghuazhao%2Fswift-super-tiny-compiler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbanghuazhao%2Fswift-super-tiny-compiler/lists"}