{"id":16012759,"url":"https://github.com/siraben/scheme-to-c","last_synced_at":"2025-04-09T22:03:34.011Z","repository":{"id":111748588,"uuid":"125726604","full_name":"siraben/scheme-to-c","owner":"siraben","description":"A toy Scheme to C compiler","archived":false,"fork":false,"pushed_at":"2018-03-26T11:50:31.000Z","size":40,"stargazers_count":7,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-09T22:03:26.418Z","etag":null,"topics":["c","compiler","scheme"],"latest_commit_sha":null,"homepage":null,"language":"Scheme","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/siraben.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":"2018-03-18T13:14:38.000Z","updated_at":"2025-01-22T14:16:12.000Z","dependencies_parsed_at":null,"dependency_job_id":"cffdb4e3-9ff6-4c1e-81f1-4e052285c508","html_url":"https://github.com/siraben/scheme-to-c","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/siraben%2Fscheme-to-c","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/siraben%2Fscheme-to-c/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/siraben%2Fscheme-to-c/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/siraben%2Fscheme-to-c/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/siraben","download_url":"https://codeload.github.com/siraben/scheme-to-c/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248119297,"owners_count":21050755,"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","scheme"],"created_at":"2024-10-08T14:20:58.685Z","updated_at":"2025-04-09T22:03:33.989Z","avatar_url":"https://github.com/siraben.png","language":"Scheme","funding_links":[],"categories":[],"sub_categories":[],"readme":"scheme-to-c - The quest to write a Scheme-\u003eC compiler\n======================================================\n\nThis is an experiment in compiling Scheme to C.\n\n*Disclaimer*: I do not have any background in writing compilers. Pull\nrequests are welcome!\n\nAlso included in this repository:\n\n- A SICP register machine language to C compiler\n  - This is in the hopes that an existing Scheme to SICP register\n    machine compiler and be used as an IR on the way to C.\n- A meta-circular evaluator for Scheme\n  - Now includes tests!\n  - Includes lexical scoping and proper closures.\n  - [ ] Add mutation and `define`\n\n\nGrammar of input language\n-------------------------\n\n``` xml\n\u003cexpr\u003e := #t\n          #f\n          (-)\u003cnumber\u003e\n          \u003csym\u003e\n          (eq? \u003cexpr\u003e \u003cexpr\u003e)\n          (boolean? \u003cexpr\u003e)\n          (fixnum? \u003cexpr\u003e)\n          (add1 \u003cexpr\u003e)\n          (sub1 \u003cexpr\u003e)\n          (zero? \u003cexpr\u003e)\n          (define \u003csym\u003e \u003cexpr\u003e)\n          (let ((\u003csym\u003e \u003cexpr\u003e) ...)\n            \u003cbody\u003e)\n          (car \u003cexpr\u003e)\n          (cdr \u003cexpr\u003e)\n          (cons \u003cexpr\u003e \u003cexpr\u003e)\n          (quote \u003cexpr\u003e)\n          (cond (\u003cexpr\u003e \u003cexpr\u003e) ...)\n          (begin \u003cexpr\u003e ...)\n          (+ \u003cexpr\u003e \u003cexpr\u003e)\n          (- \u003cexpr\u003e \u003cexpr\u003e)\n          (* \u003cexpr\u003e \u003cexpr\u003e)\n          (/ \u003cexpr\u003e \u003cexpr\u003e)\n          (remainder \u003cexpr\u003e \u003cexpr\u003e)\n          (if \u003cexpr\u003e \u003cexpr\u003e \u003cexpr\u003e)\n          (label \u003csym\u003e)\n          (goto \u003csym\u003e)\n```\n\nExample use\n-----------\n\n``` scheme\n(begin\n  (define a (quote (a b c d e)))\n  (define b (quote ()))\n  (label start)\n  (if (null? a)\n      (goto end)\n      (begin (set! b (cons (car a) b))\n             (display a)\n             (set! a (cdr a))\n             (goto start)))\n  (label end)\n  (display b))\n```\n\nOutput\n------\n\n``` c\n// Skipping ~ 200 lines of helper code\nint main(void)\n{\nGC_INIT();\neax = *make_symbol(\"a\");\npush();\neax = *make_symbol(\"b\");\npush();\neax = *make_symbol(\"c\");\npush();\neax = *make_symbol(\"d\");\npush();\neax = *make_symbol(\"e\");\npush();\neax.t = NIL;\nebx = eax;\npop();\neax = *cons(\u0026eax, \u0026ebx);\nebx = eax;\npop();\neax = *cons(\u0026eax, \u0026ebx);\nebx = eax;\npop();\neax = *cons(\u0026eax, \u0026ebx);\nebx = eax;\npop();\neax = *cons(\u0026eax, \u0026ebx);\nebx = eax;\npop();\neax = *cons(\u0026eax, \u0026ebx);\nreg a = eax;\neax.t = NIL;\nreg b = eax;\nstart:\neax = a;\nal = (eax.t == NIL);\neax.t = BOOLEAN;\neax.b = al;\nif (!eax.b){goto label2;};\ngoto end;\ngoto label1;\nlabel2:;\neax = a;\neax = *car(\u0026eax);\npush();\neax = b;\nebx = eax;\npop();\neax = *cons(\u0026eax, \u0026ebx);\nb = eax;\nprint((a));\nputs(\"\");\neax = a;\neax = *cdr(\u0026eax);\na = eax;\ngoto start;\nlabel1:;\n;\nend:\nprint((b));\nputs(\"\");\n}\n```\n\nRunning the code results in:\n\n```\n(e d c b a)\n```\nProject Goals\n-------------\n\n- [x] Be Turing complete!\n- [ ] Implement IO\n- [ ] Implement closures (i.e. `lambda`)\n    -  [ ] Implement frames and environments\n- [ ] Implement define (in the sense of functions)\n- [ ] Implement strings, vectors and their respective operations\n- [ ] Be self-hosting\n\nFutamura Projections\n--------------------\n\nProgram *a* := Scheme -\u003e C, written in Scheme\n\nProgram *b* := (a a) yields a compiler, in this case a C program that\nconverts Scheme programs to C\n\n(b b) yields b, a C program that converts Scheme programs to C, here\nit ends up being a quine.\n\nThen, given an interpreter *x* written in Scheme for a language (say,\nBrainfuck), performing (a x) yields a C interpreter for language *x*.\n\nMany more interesting semantic games to be played...\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsiraben%2Fscheme-to-c","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsiraben%2Fscheme-to-c","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsiraben%2Fscheme-to-c/lists"}