{"id":13578748,"url":"https://github.com/dibyendumajumdar/ravi-compiler","last_synced_at":"2025-04-14T17:07:54.321Z","repository":{"id":44388059,"uuid":"240679583","full_name":"dibyendumajumdar/ravi-compiler","owner":"dibyendumajumdar","description":"Parser and compiler for Ravi and Lua","archived":false,"fork":false,"pushed_at":"2022-11-12T12:17:50.000Z","size":1950,"stargazers_count":68,"open_issues_count":19,"forks_count":5,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-14T17:07:44.836Z","etag":null,"topics":["abstract-syntax-tree","ast","c","compiler","compiler-construction","intermediate-representation","lexer","lua","parser"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dibyendumajumdar.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}},"created_at":"2020-02-15T09:47:45.000Z","updated_at":"2024-12-20T03:15:39.000Z","dependencies_parsed_at":"2023-01-22T20:00:16.437Z","dependency_job_id":null,"html_url":"https://github.com/dibyendumajumdar/ravi-compiler","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/dibyendumajumdar%2Fravi-compiler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dibyendumajumdar%2Fravi-compiler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dibyendumajumdar%2Fravi-compiler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dibyendumajumdar%2Fravi-compiler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dibyendumajumdar","download_url":"https://codeload.github.com/dibyendumajumdar/ravi-compiler/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248923765,"owners_count":21183953,"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":["abstract-syntax-tree","ast","c","compiler","compiler-construction","intermediate-representation","lexer","lua","parser"],"created_at":"2024-08-01T15:01:33.415Z","updated_at":"2025-04-14T17:07:54.291Z","avatar_url":"https://github.com/dibyendumajumdar.png","language":"C","funding_links":[],"categories":["C"],"sub_categories":[],"readme":"![build](https://github.com/dibyendumajumdar/ravi-compiler/workflows/build/badge.svg)\n\n# ravi-compiler\nA compiler for Ravi and Lua that processes Lua/Ravi source code and generates C code.\n\n## Goals\n\n* Create a re-usable Lua/Ravi lexer/parser.\n* Define conventional linear intermediate representation (IR).\n* Generate C code from Lua/Ravi source.\n* Support Ahead of time (AOT) compilation.\n* The generated code can be executed by Ravi. Since the generated code depends on various VM structures it is not binary compatible with Lua, but in theory one can modify the code relatively easily to work with Lua 5.3. Lua 5.4 has modifications to the call stack used by Lua which makes it harder to port to.\n\n## Modules\n\nThe compiler library consists of distinct modules:\n\n* lexer (alpha) - responsible for tokenizing an input buffer.\n* parser (alpha) - responsible for generating abstract syntax tree (AST).\n* AST lowerer (alpha) - currently transforms generic for loops to while loops.\n* typechecker (alpha) - responsible for assigning types to variables when possible.\n* AST simplifier (alpha) - responsible for performing some initial simplifications such as constant folding, and string concatenation re-writing.\n* linearizer (alpha) - responsible for constructing a linear IR representation of the AST.\n* optimizer (WIP) - responsible for improving the code, this doesn't do much right now.\n* codegenerator (alpha) - responsible for generating C code. Each input is translated to a standalone C file that can be compiled using any C compiler. Ravi can compile this at runtime using MIR C JIT compiler. For AOT compilation, dynamic library needs to be created and a special loader needs to be used that treats the shared library as a compiled version of Lua chunk. The generated C code doesn't use the Lua C call api, as it is designed to look like Lua code.\n\n## Status\n\n### Limitations\n\n* No support for var args\n\n### Change Log\n\n* 12-nov-2022 More bug fixes, e.g. repeat statement was incorrectly compiled\n* 10-July-2022 Many bug fixes to do with how the virtual registers are allocated\n* 12-Oct-2021 Initial proof of concept for [new embedded C syntax](https://github.com/dibyendumajumdar/ravi-compiler/wiki/Embedding-C)\n* 22-Jun-2021 Increased coverage of Lua syntax to cover string concatenations and generic for loops.\n* 17-Jan-2021 The code is now C++ compliant so we can compile everything in C++ or C.\n* 28-Nov-2020 We can generate code for a large subset of Ravi language and run the compiled code from Ravi.\n* 01-Dec-2020 The generated code is now also suitable for AOT compilation but requires special loading facility in Ravi.\n\n## LICENSE\n\nThe project is available under MIT license.\nIt includes code from [chibicc](https://github.com/rui314/chibicc) copyrighted by Rui Ueyama.\n\n## Documentation\n\nDocumentation is coming soon.\n\nFor now you can look at following:\n* [Linear IR](https://github.com/dibyendumajumdar/ravi-compiler/blob/master/docs/linear-ir.md)\n* [WIP public api](https://github.com/dibyendumajumdar/ravi-compiler/blob/master/include/ravi_compiler.h) - only the lexer and parser API are public for now\n* [Test inputs and outputs](https://github.com/dibyendumajumdar/ravi-compiler/blob/master/tests)\n* [Example Ravi Tests](https://github.com/dibyendumajumdar/ravi/tree/master/tests/comptests)\n* [AOT Examples](https://github.com/dibyendumajumdar/ravi-compiler/tree/master/examples)\n* [CFG Examples](https://github.com/dibyendumajumdar/ravi-compiler/tree/master/docs/cfg)\n* See the [Wiki](https://github.com/dibyendumajumdar/ravi-compiler/wiki) for various notes\n\n## Why\n\nLua's inbuilt parser and code generator is a work of art, very compact and low overhead but extremely fast. It uses minimal memory and produces bytecodes as it parses the source code (single pass compiler). This is great for Lua and Ravi given the use cases of these languages, but makes the parser and code generator hard to understand, play with, or reuse in tools such as IDEs. It also makes it harder to perform any advanced type checking or performance optimizations. \n\nThis project will create a new parser and code generator that is not a replacement for the default one in Lua/Ravi but can be used for more specialised code generation, as well as as a means of understanding how the parser and code generator works.\n\n## Technology\n\nThis project is written in C for maximum portability like Lua. \n\n## Building \n\nYou will need CMake 3.12 or greater. The build steps are fairly simple on Linux:\n\n```\nmkdir build\ncd build\ncmake ..\nmake \n```\n\n## Try it out!\n\nThe compiler can be run using the `trun` command line utility.\nExample:\n\n```\ntrun \"return 'hello'\"\n```\n\nTo see the C output, try:\n\n```\ntrun --gen-C \"print 'hello world'\"\n```\n\n## Testing\n\nAt the moment we have a simple test driver programs: `trun`. The driver takes a string or file input which must be a valid Lua/Ravi chunk of code, and outputs the AST, the result of type checking, linear IR output if supported, and the CFG as a `dot` file. Example of the output can be found in the `tests/expected` folder.\n\nSuppose `trun` was built in `build` folder then you can run the tests as follows:\n\n```\ncd tests \u0026\u0026 sh truntests.sh ../build/trun\n```\n\nThe test script compares the output to the expected output. Any difference will cause the test script to fail.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdibyendumajumdar%2Fravi-compiler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdibyendumajumdar%2Fravi-compiler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdibyendumajumdar%2Fravi-compiler/lists"}