{"id":16311913,"url":"https://github.com/shenniger/toycompiler","last_synced_at":"2025-10-06T11:52:04.363Z","repository":{"id":156025530,"uuid":"187055642","full_name":"shenniger/toycompiler","owner":"shenniger","description":"Toy compiler","archived":false,"fork":false,"pushed_at":"2019-05-16T15:38:51.000Z","size":60,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-14T22:46:48.940Z","etag":null,"topics":["c","compiler","language","lisp","programming-language"],"latest_commit_sha":null,"homepage":"","language":"C","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/shenniger.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":"2019-05-16T15:34:38.000Z","updated_at":"2023-01-19T22:01:22.000Z","dependencies_parsed_at":null,"dependency_job_id":"faa60f71-2eb7-444a-bef5-7ff932416f6d","html_url":"https://github.com/shenniger/toycompiler","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shenniger%2Ftoycompiler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shenniger%2Ftoycompiler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shenniger%2Ftoycompiler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shenniger%2Ftoycompiler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shenniger","download_url":"https://codeload.github.com/shenniger/toycompiler/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248975299,"owners_count":21192199,"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","compiler","language","lisp","programming-language"],"created_at":"2024-10-10T21:45:56.180Z","updated_at":"2025-10-06T11:52:04.286Z","avatar_url":"https://github.com/shenniger.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"This is a toy project I created mostly for learning about\ncompiler/transpiler design, computer architecture, and LLVM.\n\n## Overview\nThis is an attempt at bridging the two worlds of C/C++-like systems programming\nlanguages and Common Lisp/Scheme-like languages. The idea is that it should be\npossible to mix both concepts seamlessly within one program by defining a flexible,\nmacro-friendly S-expression-based grammar and add additional syntactic sugar to make\nit look like C while similarly building a core language that is very much like C\n(except with a few more modern features like lambda/closure support and\nadvanced introspection capabilities) and make it extensible using a macro system\nwhich can be used to create Lisp-like functions and a list type.\n\nAt the moment, I am at the point where the Lisp-like syntax and the C-like\ncore language are almost finished (although still poorly tested), but I have not\nstarted implementing the C syntax in terms of S-expressions (see below for ideas\non how to do that) or implementing Lisp's type system in terms of C's.\n\nSee the TODO file for a detailed list of implemented and planned language features.\n\nThe language compiles to either C code or LLVM IR.\n\n## Source files\n* **main.c**: Error formatting, memory allocator, driver.\n* **reader.c**: List functions such as an S-expression reader.\n* **parser.c**: Grammar, but combined with a type system, control structures etc.\n* **middle.c**: Macro system.\n* **back\\_c.c**: C transpiler.\n* **back\\_llvm.cpp**: LLVM backend.\n\n## Compiling\nJust type `make`. The Makefile is non-standard, but should be self-explanatory. If\nyou do not have LLVM and/or a C++ compiler, you can choose to build only the\nC transpiler by using `make test_c`.\n\nTip: You can use `make report` to see which compiler and LLVM version is currently\nbeing used.\n\n## Usage example\nWith LLVM: `./test_llvm test2.crp | clang -xir - -o print_file_test`\n\nWith C: `./test_c test2.crp | gcc -xc - -o print_file_test`\n\nThen `./print_file_test` should print the first characters of its own sourcecode.\n\n## Code example\n```\n; This example should compile with the compiler in its current state.\n\n; Declare a prototype for libc's `puts` function.\n(funproto puts (((ptr const char) str)) i32)\n\n; Define the `main` function with a call to `puts`. The string literal\n; has, at the moment, the type of (ptr const char) (like in C), although\n; I think this will change in the future.\n(defun main () i32 (\n  (puts \"Hello, world.\")\n 0))\n```\n\n## A crazy idea about syntax\nWhat if S-expressions could be written not just with parentheses but with\nother characters as well? Can a syntax allow for the same flexibility as\nS-expressions while looking less intimidating to those used to modern programming\nlanguages?\n\nI gave it a try:\n```\n// Declare a prototype for libc's `puts` function.\nfunproto puts (ptr const char: str;) i32;\n\n// Define the `main` function with a call to `puts`. The string literal\n// has, at the moment, the type of (ptr const char) (like in C), although\n// I think this will change in the future.\ndefun main () i32 {\n  puts \"Hello, world.\";\n  0\n}\n```\nTranslate this to S-exprs using the following rules:\n* Replace `a b c { d e f }` with `(a b c (d e f))`.\n* Replace `a b c : d e f` with `(a b c) d e f`.\n* Replace `a b c;` with `(a b c)`\n(You will also need to change the comment syntax.)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshenniger%2Ftoycompiler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshenniger%2Ftoycompiler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshenniger%2Ftoycompiler/lists"}