{"id":21158718,"url":"https://github.com/wqweto/kscope","last_synced_at":"2026-05-22T07:02:21.821Z","repository":{"id":145870309,"uuid":"52974244","full_name":"wqweto/kscope","owner":"wqweto","description":"LLVM's Kaleidoscope Toy Language to Lua Transpiler","archived":false,"fork":false,"pushed_at":"2016-03-02T18:23:37.000Z","size":645,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-08T20:43:40.246Z","etag":null,"topics":["compiler","dsl","llvm","lua"],"latest_commit_sha":null,"homepage":"","language":"Lua","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/wqweto.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":"2016-03-02T15:35:26.000Z","updated_at":"2023-04-12T07:13:35.000Z","dependencies_parsed_at":null,"dependency_job_id":"dedde457-8882-469a-ba1b-e9948eaad7b0","html_url":"https://github.com/wqweto/kscope","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wqweto%2Fkscope","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wqweto%2Fkscope/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wqweto%2Fkscope/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wqweto%2Fkscope/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wqweto","download_url":"https://codeload.github.com/wqweto/kscope/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243602097,"owners_count":20317568,"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","dsl","llvm","lua"],"created_at":"2024-11-20T12:32:51.236Z","updated_at":"2026-05-22T07:02:16.768Z","avatar_url":"https://github.com/wqweto.png","language":"Lua","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Kaleidoscope Toy Language to Lua Transpiler\n\n`kscope` is a LuaJIT implementation of [LLVM's Kaleidoscope toy language](http://llvm.org/docs/tutorial/LangImpl1.html) providing both an interpreter and a compiler which can build statically linked (portable) executables.\n\nCurrently the implementation is Windows based/tested only and heavily relies on [MinGW-w64 project](http://mingw-w64.org/) for bootstrapping the compiler and linking compiler output executables.\n\n    C:\u003e bin\\kscope-win32.exe mandel.ks\n    ******************************************************************************\n    ******************************************************************************\n    ****************************************++++++********************************\n    ************************************+++++...++++++****************************\n    *********************************++++++++.. ...+++++**************************\n    *******************************++++++++++..   ..+++++*************************\n    ******************************++++++++++.     ..++++++************************\n    ****************************+++++++++....      ..++++++***********************\n    **************************++++++++.......      .....++++**********************\n    *************************++++++++.   .            ... .++*********************\n    ***********************++++++++...                     ++*********************\n    *********************+++++++++....                    .+++********************\n    ******************+++..+++++....                      ..+++*******************\n    **************++++++. ..........                        +++*******************\n    ***********++++++++..        ..                         .++*******************\n    *********++++++++++...                                 .++++******************\n    ********++++++++++..                                   .++++******************\n    *******++++++.....                                    ..++++******************\n    *******+........                                     ...++++******************\n    *******+... ....                                     ...++++******************\n    *******+++++......                                    ..++++******************\n    *******++++++++++...                                   .++++******************\n    *********++++++++++...                                  ++++******************\n    **********+++++++++..        ..                        ..++*******************\n    *************++++++.. ..........                        +++*******************\n    ******************+++...+++.....                      ..+++*******************\n    *********************+++++++++....                    ..++********************\n    ***********************++++++++...                     +++********************\n    *************************+++++++..   .            ... .++*********************\n    **************************++++++++.......      ......+++**********************\n    ****************************+++++++++....      ..++++++***********************\n    *****************************++++++++++..     ..++++++************************\n    *******************************++++++++++..  ...+++++*************************\n    *********************************++++++++.. ...+++++**************************\n    ***********************************++++++....+++++****************************\n    ***************************************++++++++*******************************\n    ******************************************************************************\n    ******************************************************************************\n    ******************************************************************************\n    ******************************************************************************\n    Evaluated to nil\n    \n    C:\u003e\n\n## Building the compiler\n\nThere is a `build.bat` batch file in `bin` folder that automates compilation of both `kscope-win32.exe` and `kscope-win64.exe` compiler executables. These are both cross-compilers for _Kaleidoscope toy language_, meaning that `kscope-win32.exe` can compile both 32-bit and 64-bit executables (defaulting to 32-bit) and vice-versa for `kscope-win64.exe` compiler.\n\nCurrently `build.bat` expects [`i686-w64-mingw32`](https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win32/Personal%20Builds/mingw-builds/5.3.0/threads-posix/dwarf/) in `C:\\mingw32` and [`x86_64-w64-mingw32`](https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/Personal%20Builds/mingw-builds/5.3.0/threads-posix/seh/) in `C:\\mingw64` (links to version 5.3). Include both folders in global `PATH` like this `set PATH=C:\\mingw64;C:\\mingw32;%PATH%` to prefer 64-bit toolchain and still be able to access 32-bit tools for cross compilation using compiler triplets (with something like `i686-w64-mingw32-gcc.exe`).\n\n`kscope` depends on MinGW toolchain to bootstrap the compiler and the compiled executables. More specifically the C bootstrap (`startup.c`) includes a `main` entry-point function which initializes the Lua environment and additionally LuaJIT and LPeg are statically precompiled with MinGW to be compatible with `kscope`'s linking phase.\n\n## Building support files (optional)\n\nThere is a `build.bat` batch file in `lib` folder that prepares compiler bootstrap and libraries. These come precompiled so using this batch file is optional.\n\nEach platform has subfolder under `lib`, that is 32-bit object/lib files go to `lib\\win32` while 64-bit precompiled files go to `lib\\win64`. The compiler expects to find this `lib` folder under the folder of the executable (e.g. `bin`) or in its parent folder.\n\n## Implementation\n\n##### Using Visual Studio\nThe Lua project in `src` is using [babelua](https://babelua.codeplex.com/) plugin for VS2015 for coding and debugging. This plugin project obviously hoisted [decoda](http://unknownworlds.com/decoda/)'s hooking debugger and the resulting execution under debug is dog-slow but then the plugin brings some of the familiar VS goodness -- breakpoints, inspections, **editing** experience.\n\n##### Frontend\n`parser.lua` implements the language parser using a somewhat extended version of [LPeg.re](http://www.inf.puc-rio.br/~roberto/lpeg/re.html) that understands meta-patterns (functions that return patterns based on params) with `%def'param'` syntax. The parser builds the AST with instances from `tree.lua` which are a bunch of glorified tables. Properties of AST nodes are accessed by name, not by index as in metalua AST, i.e. `IfExpr` instance has `test`, `cons` and `altn` members, not `node[1]`, `node[2]` and `node[3]`. Base \"class\" `Node` implements the double-dispatch for [Visitor pattern](https://en.wikipedia.org/wiki/Visitor_pattern) in a strictly dynamic language way, so there is no need for `accept` method on each and every subclass.\n\n`visitor.lua` implements a \"virtual\" `Visitor` class and a single `Dumper` implementation that prints the AST. Again the output is similar to metalua's AST but with \"strongly\" named members.\n\n##### Backend\n`emitter.lua` implements a visitor that does the actual codegen. Kaleidoscope language is pretty functional so the resulting code contains somewhat similar abominations as CoffeeScript lowering to JS, e.g. `return (function(a) return x+a end)(42)`. Nevertheless we are not pursuing performance in MVP release and the LuaJIT does miracles optimizing this bloat.\n\n##### Linking\nThe compiler expects `link.bat` in `lib` folder for final linking. The batch file shells `gcc` and bundles the bootstrap module and LuaJIT runtime in the output portable executable.\n\n##### Inspiration and 10x goes to\n - [@richardhundt](https://github.com/richardhundt) for [Shine](https://github.com/richardhundt/shine) implementation\n - [@carp](https://github.com/capr) of luapower for [bundle](https://github.com/luapower/bundle) scripts\n\n## Known issues\n\n- LuaJIT built-in object file emitter seems to be incompatible with MinGW linker which spits `corrupt .drectve at end of def file` warnings when parsing these. You can safely ignore the warnings.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwqweto%2Fkscope","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwqweto%2Fkscope","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwqweto%2Fkscope/lists"}