{"id":21495927,"url":"https://github.com/king-11/c_compiler","last_synced_at":"2025-04-23T15:21:00.569Z","repository":{"id":53825837,"uuid":"520746237","full_name":"king-11/c_compiler","owner":"king-11","description":"Writing a compiler for c in rust language","archived":false,"fork":false,"pushed_at":"2024-08-30T14:48:42.000Z","size":75,"stargazers_count":7,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-30T00:51:15.563Z","etag":null,"topics":["c","code-generator","compiler","lexer","parser","rust"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/king-11.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2022-08-03T05:14:04.000Z","updated_at":"2024-08-31T06:52:15.000Z","dependencies_parsed_at":"2025-01-23T22:01:42.422Z","dependency_job_id":null,"html_url":"https://github.com/king-11/c_compiler","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/king-11%2Fc_compiler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/king-11%2Fc_compiler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/king-11%2Fc_compiler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/king-11%2Fc_compiler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/king-11","download_url":"https://codeload.github.com/king-11/c_compiler/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250457778,"owners_count":21433734,"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":["c","code-generator","compiler","lexer","parser","rust"],"created_at":"2024-11-23T16:14:06.947Z","updated_at":"2025-04-23T15:21:00.548Z","avatar_url":"https://github.com/king-11.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Writing a C Compiler\n\n## Context\n\nI am currently studying Compilers as a part of my Computer Science and Engineering curriculum. Given the slow pace of class I have decided that I want a fruitful outcome from this course and hence am building an actual compiler.\n\n## Resource\n\nI have been following the [Writing a C Compiler](https://norasandler.com/2017/11/29/Write-a-Compiler.html) Series by [Nora Sandler](https://norasandler.com/about/). Given the series was made for x32 bit architecture and that isn't the industry standard now. I have tried myself to change many of the code generation calls into x86_x64 bit alternative.\n\n### Testing\n\nThe series is provided with a [github repository](https://github.com/nlsandler/write_a_c_compiler) consisting of several test cases for each stage of series.\n\n## Learnings\n\n- You’ll learn about abstract syntax trees (ASTs) and how programs can represent and manipulate other programs. Handy for working with linters, static analyzers, and metaprogramming of all sorts.\n- You’ll learn about assembly, calling conventions, and all the gritty, low-level details of how computers, like, do stuff.\n- It seems like an impossibly hard project (but isn’t!), so writing one will make you feel like a badass.\n\n## Preliminaries\n\n- I have been excited about Rust since I got to know about it. Writing a compiler requires us to have capabilities for **sum types** and **pattern matching**. Hence Rust shines with memory safety as well so am going ahead with it.\n- I also won't be using automatic parser and scanner generators instead as in series will be implementing the lexer and a recursive decent parser.\n\n## Modules\n\n- [main.rs](./src/main.rs) is the main file which will call other module functions.\n- [lexer.rs](./src/lex/mod.rs) is responsible for tokenizing and setting up the model for tokens.\n- [ast.rs](./src/ast/mod.rs) is our parser which generated the Abstract Syntax Tree based on the provided grammar.\n- [codegen.rs](./src/codegen/mod.rs) generates the assembly code for x86_x64 architecture provided the AST.\n\n## Status\n\n- [x] Compile a program that returns a single integer\n- [x] Adding three unary operators (~,-,!)\n- [x] Binary operations to support basic arithmetic while handling operator precedence and associativity\n- [x] boolean operators (\u0026\u0026, ||) and a whole bunch of relational operators (\u003c, ==, etc.)\n\n## Grammar\n\nThe following grammar is supported as of now in [Backus Naur Form](https://en.wikipedia.org/wiki/Backus%E2%80%93Naur_form):\n\n```\n\u003cprogram\u003e ::= \u003cfunction\u003e\n\u003cfunction\u003e ::= \"int\" \u003cid\u003e \"(\" \")\" \"{\" { \u003cstatement\u003e } \"}\"\n\u003cstatement\u003e ::= \"return\" \u003cexp\u003e \";\"\n              | \u003cexp\u003e \";\"\n              | \"int\" \u003cid\u003e [ = \u003cexp\u003e] \";\"\n\u003cexp\u003e ::= \u003cid\u003e \"=\" \u003cexp\u003e | \u003clogical-or-exp\u003e\n\u003clogical-or-exp\u003e ::= \u003clogical-and-exp\u003e { \"||\" \u003clogical-and-exp\u003e }\n\u003clogical-and-exp\u003e ::= \u003cequality-exp\u003e { \"\u0026\u0026\" \u003cequality-exp\u003e }\n\u003cequality-exp\u003e ::= \u003crelational-exp\u003e { (\"!=\" | \"==\") \u003crelational-exp\u003e }\n\u003crelational-exp\u003e ::= \u003cadditive-exp\u003e { (\"\u003c\" | \"\u003e\" | \"\u003c=\" | \"\u003e=\") \u003cadditive-exp\u003e }\n\u003cadditive-exp\u003e ::= \u003cterm\u003e { (\"+\" | \"-\") \u003cterm\u003e }\n\u003cterm\u003e ::= \u003cfactor\u003e { (\"*\" | \"/\") \u003cfactor\u003e }\n\u003cfactor\u003e ::= \"(\" \u003cexp\u003e \")\" | \u003cunary_op\u003e \u003cfactor\u003e | \u003cint\u003e | \u003cid\u003e\n\u003cunary_op\u003e ::= \"!\" | \"~\" | \"-\"\n```\n\n## Tokens\n\nFollwing are the supported tokens given the current state of compiler:\n\n- Open brace {\n- Close brace }\n- Open parenthesis (\n- Close parenthesis )\n- Semicolon ;\n- Int keyword int\n- Return keyword return\n- Identifier [a-zA-Z_]\\w*\n- Integer literal [0-9]+\n- Minus -\n- Bitwise complement ~\n- Logical negation !\n- Addition +\n- Multiplication *\n- Division /\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fking-11%2Fc_compiler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fking-11%2Fc_compiler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fking-11%2Fc_compiler/lists"}