{"id":18195818,"url":"https://github.com/yichengdwu/yoho","last_synced_at":"2025-04-07T17:15:17.698Z","repository":{"id":243425801,"uuid":"812074480","full_name":"YichengDWu/yoho","owner":"YichengDWu","description":" A compiler written in Mojo 🔥 and generates RISC-V assembly ","archived":false,"fork":false,"pushed_at":"2024-06-25T15:43:00.000Z","size":57,"stargazers_count":14,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-05T05:02:01.616Z","etag":null,"topics":["compiler","mojo","parser","risc-v"],"latest_commit_sha":null,"homepage":"","language":"Mojo","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/YichengDWu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","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},"funding":{"github":["YichengDWu"]}},"created_at":"2024-06-07T22:46:26.000Z","updated_at":"2024-08-11T10:38:16.000Z","dependencies_parsed_at":"2024-09-24T16:22:54.460Z","dependency_job_id":"f90b50ed-ab48-4f9d-9024-3a6d3a077072","html_url":"https://github.com/YichengDWu/yoho","commit_stats":null,"previous_names":["yichengdwu/yoho"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/YichengDWu%2Fyoho","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/YichengDWu%2Fyoho/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/YichengDWu%2Fyoho/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/YichengDWu%2Fyoho/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/YichengDWu","download_url":"https://codeload.github.com/YichengDWu/yoho/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247694876,"owners_count":20980733,"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":["compiler","mojo","parser","risc-v"],"created_at":"2024-11-03T08:05:02.070Z","updated_at":"2025-04-07T17:15:17.671Z","avatar_url":"https://github.com/YichengDWu.png","language":"Mojo","funding_links":["https://github.com/sponsors/YichengDWu"],"categories":[],"sub_categories":[],"readme":"# Yoho 🔥\n\nYoho 🔥 is a toy compiler crafted in Mojo 🔥 and designed to generate RISC-V assembly language.\n\n# Status\n\nThis project is currently under active development. \nPlease refer to `test.sh` for example programs that the current version of the compiler supports.\n\n# Overview\n\n## Tokenizer\n\nThe tokenizer for this compiler is implemented quite manually. It calls the [`re2`](https://github.com/google/re2) Python library\nfor regular expression operations at the low level.\n\n## Parser\n\nThe parser is based on Parsing Expression Grammar (PEG). It is capable of automatically generating parsers from .gram files. Below is the meta-grammar used for defining the grammar:\n\n```c\nstart: grammar ENDMARKER { grammar }\n\ngrammar: rules=rule+ { Grammar(rules) }\n\nrule: \n    | NAME ':' NEWLINE INDENT rhs NL DEDENT { Rule(name.text, rhs)}\n    | NAME ':' rhs NL { Rule(name.text, rhs)}\n\nrhs: \n    | '|' alt NEWLINE rhs { Rhs(List(alt) + rhs.args)}\n    | '|' alt NEWLINE { Rhs(List(alt)) }\n    | alt NEWLINE { Rhs(List(alt)) }\n\nalt: \n    | items action { Alt(items, action) } \n    | items { Alt(items) }\n\nitems: items=named_item+ { Items(items) }\n\nnamed_item: \n    | NAME '=' item { NamedItem(item, name.text)}\n    | item { NamedItem(item) }\n    \nitem: \n    | atom '*' { Repeat0(atom) }\n    | atom '+' { Repeat1(atom) }\n    | sep=atom '.' node=atom '+' { Gather(sep.text, node) }\n    | atom \n    \natom: \n    | '(' items ')' { Group(items.args) }\n    | NAME { Atom(name.text) } \n    | STRING { Atom(string.text) }\n    \naction: '{' target '}' { target }\n\ntarget: \n    | NAME target_atoms { Action(target_atoms, name)}\n    | target_atom { Action(target_atom) }\n\ntarget_atoms: \n    | target_atom target_atoms { String(target_atom + ' ' + target_atoms) }\n    | target_atom \n\ntarget_atom:\n    | NAME { String(name.text) }\n    | NUMBER { String(number.text) }\n    | ',' { String(', ') }\n    | '+' { String(' + ') }\n    | '(' { String('(') }\n    | ')' { String(')') }\n    | '.' { String('.')}\n\n```\nThis meta-grammar provides a flexible and powerful way to define and generate the parser.\n\nThe parse tree can be nicely printed out:\n```python\n      0:12      ┃            ┃  BinOp                             \n       0:1      ┃     '1'    ┃    NUMBER                          ✔\n       1:2      ┃     '-'    ┃    MINUS                           ✔\n      3:12      ┃            ┃    BinOp                           \n       3:8      ┃            ┃      BinOp                         \n       3:4      ┃     '2'    ┃        NUMBER                      ✔\n       4:5      ┃     '+'    ┃        PLUS                        ✔\n       5:8      ┃            ┃        BinOp                       \n       5:6      ┃     '3'    ┃          NUMBER                    ✔\n       6:7      ┃     '*'    ┃          STAR                      ✔\n       7:8      ┃     '2'    ┃          NUMBER                    ✔\n       8:9      ┃     '-'    ┃      MINUS                         ✔\n      9:12      ┃            ┃      BinOp                         \n      9:10      ┃     '3'    ┃        NUMBER                      ✔\n     10:11      ┃     '/'    ┃        SLASH                       ✔\n     11:12      ┃     '2'    ┃        NUMBER                      ✔\n```\n\n# CodeGen\n\nThe code generator simply reads in an ast and emit RISC-V assembly. \nYes some of the source code might look silly to you and it was somewhat intentional. Simplicity and readability for first-time readers is my top priority. There is no IR or LLVM/MLIR in yoho.\n\n\n# References\n\nThis compiler was developed with inspiration and reference from the following projects:\n\n- [chibicc](https://github.com/rui314/chibicc): A small C compiler.\n- [pegen](https://github.com/we-like-parsers/pegen): A PEG-based parser generator.\n    \n# Installation\n\n## Prerequisites\n\nEnsure you have the following installed:\n\n- Mojo Nightly Version. Visit the [Mojo Lang website](https://www.modular.com/max/mojo) and follow the instructions to download and install the nightly version of Mojo.\n- RISC-V Toolchain. Visit [this guide](https://github.com/johnwinans/riscv-toolchain-install-guide) to install RISC-V toolchain.\n\n## Steps to Install the Compiler\n\n1. Clone the Repository.\n2. Build the Compiler:\n```shell\nmake yoho\n```\n\n3. Run test:\n```shell\nmake test\n```\n4. Clean Up:\n```shell\nmake clean\n```\n\n## Example Output\n\nHere is an example of the output RISC-V assembly code of '1*2+ 4*5-(4-3\u003e2)':\n\n```assembly\n.global  main\nmain:\n    li t0, 1\n    li t1, 2\n    mul t0, t0, t1\n    li t1, 4\n    li t2, 5\n    mul t1, t1, t2\n    add t0, t0, t1\n    li t1, 4\n    li t2, 3\n    sub t1, t1, t2\n    li t2, 2\n    slt t1, t2, t1\n    sub t0, t0, t1\n    mv a0, t0\n    ret\n```\n\nPlease refer to `test.sh` for more example programs.\n\n# Contributing\n\nInspired by [chibicc](https://github.com/rui314/chibicc)'s approach to maintaining a clean commit history, we adopt a similar style for handling contributions.\n\n\u003e When a bug is found in this compiler, I trace back to the original commit that introduced the bug and rewrite the commit history as if the bug never existed. This method, while unconventional, ensures that each commit remains bug-free, which is crucial for the integrity of the project.\n\nThe repository is committed to \"every commit is bug free\".  If you discover a bug and submit an issue, I will apply the necessary changes to the relevant previous commits by rewriting the history. \n\n# License\n\nThis project is licensed under the MIT License. See the [LICENSE](https://github.com/YichengDWu/yoho/blob/main/LICENSE) file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyichengdwu%2Fyoho","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyichengdwu%2Fyoho","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyichengdwu%2Fyoho/lists"}