{"id":22320919,"url":"https://github.com/tsych0/vm_translator","last_synced_at":"2025-03-26T04:40:55.381Z","repository":{"id":243569139,"uuid":"812644659","full_name":"tsych0/vm_translator","owner":"tsych0","description":"VM translator made in rust","archived":false,"fork":false,"pushed_at":"2024-06-12T11:42:49.000Z","size":37,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-22T10:20:37.526Z","etag":null,"topics":["nand2tetris","rust","stackmachine","vm-translator"],"latest_commit_sha":null,"homepage":"","language":"Rust","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/tsych0.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}},"created_at":"2024-06-09T13:33:58.000Z","updated_at":"2024-06-12T11:42:53.000Z","dependencies_parsed_at":"2025-01-31T06:42:27.699Z","dependency_job_id":null,"html_url":"https://github.com/tsych0/vm_translator","commit_stats":null,"previous_names":["veryshyjelly/vm_translator","tsych0/vm_translator"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tsych0%2Fvm_translator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tsych0%2Fvm_translator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tsych0%2Fvm_translator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tsych0%2Fvm_translator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tsych0","download_url":"https://codeload.github.com/tsych0/vm_translator/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245591622,"owners_count":20640692,"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","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":["nand2tetris","rust","stackmachine","vm-translator"],"created_at":"2024-12-04T00:15:26.850Z","updated_at":"2025-03-26T04:40:55.369Z","avatar_url":"https://github.com/tsych0.png","language":"Rust","readme":"# VM Translator\n\n## Introduction\n\nThis project is an implementation of the VM Translator for the Hack computer, written in Rust. The VM Translator\ntranslates high-level virtual machine (VM) code into Hack assembly language, which can then be assembled into machine\ncode for the Hack computer as described in the \"From NAND to Tetris\" course.\n\n## VM Language Contract\n\nThe Hack VM language consists of stack-based instructions that can be categorized into three types:\n\n1. **Arithmetic and Logical Commands**: Perform arithmetic and logical operations.\n    - `add`, `sub`, `neg`, `eq`, `gt`, `lt`, `and`, `or`, `not`\n2. **Memory Access Commands**: Access different segments of the VM memory.\n    - `push segment index`, `pop segment index`\n    - Segments: `constant`, `local`, `argument`, `this`, `that`, `pointer`, `temp`, `static`\n3. **Program Flow Commands**: Handle branching and function calls.\n    - `label label`, `goto label`, `if-goto label`\n    - `function functionName nVars`, `call functionName nArgs`, `return`\n\n### VM Commands Details\n\n- **Arithmetic Commands**:\n    - `add`: Adds the top two stack values.\n    - `sub`: Subtracts the top stack value from the second top stack value.\n    - `neg`: Negates the top stack value.\n    - `eq`: Pushes `true` if the top two stack values are equal, otherwise `false`.\n    - `gt`: Pushes `true` if the second top stack value is greater than the top stack value.\n    - `lt`: Pushes `true` if the second top stack value is less than the top stack value.\n    - `and`: Performs bitwise AND on the top two stack values.\n    - `or`: Performs bitwise OR on the top two stack values.\n    - `not`: Performs bitwise NOT on the top stack value.\n\n- **Memory Access Commands**:\n    - `push segment index`: Pushes the value from the specified segment and index onto the stack.\n    - `pop segment index`: Pops the top stack value into the specified segment and index.\n\n- **Program Flow Commands**:\n    - `label label`: Declares a label in the code.\n    - `goto label`: Jumps to the specified label.\n    - `if-goto label`: Jumps to the specified label if the top stack value is not zero.\n    - `function functionName nVars`: Declares a function and initializes local variables.\n    - `call functionName nArgs`: Calls a function, passing nArgs arguments.\n    - `return`: Returns from the current function, restoring the caller's state.\n\n## Features of the VM Translator\n\n- **Complete Implementation**: Supports all VM commands including arithmetic, memory access, and program flow commands.\n- **Optimized Code Generation**: Produces efficient Hack assembly code.\n- **Error Handling**: Provides informative error messages for syntax errors and undefined symbols.\n- **User-Friendly CLI**: Easy-to-use command-line interface for translating `.vm` files.\n- **Modular Design**: Well-structured codebase, making it easy to extend and maintain.\n\n## Installation\n\nTo use this translator, you'll need to have Rust installed on your machine. If you don't have Rust installed, you can\nget it [here](https://www.rust-lang.org/tools/install).\n\nClone the repository and navigate to the project directory:\n\n```sh\ngit clone https://github.com/veryshyjelly/vm-translator.git\ncd vm-translator\n```\n\nBuild the project using Cargo:\n\n```sh\ncargo build --release\n```\n\nThe executable will be located in the `target/release` directory.\n\n## Usage\n\nTo translate a VM file or directory containing VM files, run the following command:\n\n```sh\n./vm-translator path/to/yourfile.vm\n```\n\nor\n\n```sh\n./vm-translator path/to/yourdirectory\n```\n\nThis will generate a corresponding Hack assembly file with a `.asm` extension in the same directory as the input file(\ns).\n\n## Example\n\nGiven the following VM code in `example.vm`:\n\n```vm\n// Simple function call\nfunction SimpleFunction 2\npush constant 10\npush constant 20\nadd\npop local 0\npush constant 30\npush constant 40\nsub\npop local 1\nreturn\n```\n\nRunning the translator:\n\n```sh\n./vm-translator example.vm\n```\n\nWill produce the following `example.asm` file:\n\n```asm\n// Function(\"SimpleFunction\", 2)\n(SimpleFunction)\n@2\nD=A\n(SimpleFunction$arg.2)\n@SimpleFunction$arg.2.end\nD;JEQ\nD=D-1\n@SP\nA=M\nM=0\n@SP\nM=M+1\n@SimpleFunction$arg.2\n0;JMP\n(SimpleFunction$arg.2.end)\n\n// Push(Constant, 10)\n@10\nD=A\n@SP\nA=M\nM=D\n@SP\nM=M+1\n\n// Push(Constant, 20)\n@20\nD=A\n@SP\nA=M\nM=D\n@SP\nM=M+1\n\n// Arithmetic(ADD)\n@SP\nAM=M-1\nD=M\nA=A-1\nM=M+D\n\n// Pop(Local, 0)\n@LCL\nD=M\n@0\nD=A+D\n@R13\nM=D\n@SP\nM=M-1\nA=M\nD=M\n@R13\nA=M\nM=D\n\n// Push(Constant, 30)\n@30\nD=A\n@SP\nA=M\nM=D\n@SP\nM=M+1\n\n// Push(Constant, 40)\n@40\nD=A\n@SP\nA=M\nM=D\n@SP\nM=M+1\n\n// Arithmetic(SUB)\n@SP\nAM=M-1\nD=M\nA=A-1\nM=M-D\n\n// Pop(Local, 1)\n@LCL\nD=M\n@1\nD=A+D\n@R13\nM=D\n@SP\nM=M-1\nA=M\nD=M\n@R13\nA=M\nM=D\n\n// Return\n@LCL\nD=M\n@R13\nM=D\n@5\nA=D-A\nD=M\n@R14\nM=D\n@SP\nM=M-1\nA=M\nD=M\n@ARG\nA=M\nM=D\nD=A\n@SP\nM=D+1\n@R13\nAM=M-1\nD=M\n@THAT\nM=D\n@R13\nAM=M-1\nD=M\n@THIS\nM=D\n@R13\nAM=M-1\nD=M\n@ARG\nM=D\n@R13\nAM=M-1\nD=M\n@LCL\nM=D\n@R14\nA=M\n0;JMP\n```\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a pull request or open an issue if you have any improvements or\nbug fixes.\n\n## License\n\nThis project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.\n\n## Acknowledgments\n\n- The creators of the \"From NAND to Tetris\" course, Noam Nisan and Shimon Schocken, for providing the framework and\n  inspiration for this project.\n- The Rust community for their excellent documentation and support.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftsych0%2Fvm_translator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftsych0%2Fvm_translator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftsych0%2Fvm_translator/lists"}