{"id":18773906,"url":"https://github.com/ejunjsh/kaleidoscope","last_synced_at":"2026-03-05T20:49:17.459Z","repository":{"id":61147725,"uuid":"523937870","full_name":"ejunjsh/kaleidoscope","owner":"ejunjsh","description":"My First Language Frontend with LLVM Tutorial","archived":false,"fork":false,"pushed_at":"2022-10-05T06:23:21.000Z","size":91,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-11-07T19:47:42.557Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/ejunjsh.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}},"created_at":"2022-08-12T03:01:32.000Z","updated_at":"2024-05-07T07:36:29.000Z","dependencies_parsed_at":"2022-10-11T18:21:26.779Z","dependency_job_id":null,"html_url":"https://github.com/ejunjsh/kaleidoscope","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/ejunjsh%2Fkaleidoscope","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ejunjsh%2Fkaleidoscope/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ejunjsh%2Fkaleidoscope/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ejunjsh%2Fkaleidoscope/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ejunjsh","download_url":"https://codeload.github.com/ejunjsh/kaleidoscope/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":231717832,"owners_count":18415884,"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":[],"created_at":"2024-11-07T19:36:17.014Z","updated_at":"2026-03-05T20:49:17.387Z","avatar_url":"https://github.com/ejunjsh.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# kaleidoscope\n\nMy First Language Frontend with LLVM Tutorial\n\n## 前提\n\n根据下面链接安装llvm\n\nhttps://llvm.org/docs/CMake.html#quick-start \n\n## 编译\n\n````\n$ mkdir build\n$ cd build\n$ cmake -DCMAKE_BUILD_TYPE=Release -DLLVM_DIR={path to llvm config cmake} .. \u0026\u0026 cmake --build .\n# {path to llvm config cmake} 是llvm的安装路径下LLVMConfig.cmake所在的目录，\n# 一般你llvm是安装到标准的目录，cmake在查找包能找到话，可以不加-DLLVM_DIR 这个选项\n````\n这时候就会生成一个可执行文件kaleidocscope,运行它\n````\n$ ./kaleidocscope\n````\n\n## 语法\n\n只有一个类型，浮点类型\n\n### 支持函数定义：\n````\nready\u003e def add(x y) x+y;\nready\u003e add(1,3);\nready\u003e Evaluated to 4.000000\n````\n\n### 支持外部函数：\n````\nready\u003e extern sin(x);\nready\u003e sin(1.0);\nready\u003e Evaluated to 0.841471\n````\n\n### 支持条件判断\n````\nready\u003e def fib(x)                          \n        if x \u003c 3 then\n            1\n        else\n            fib(x-1)+fib(x-2);\nready\u003e fib(4);  \nready\u003e Evaluated to 3.000000\n````\n\n### 支持循环\n````\nextern putchard(char);\ndef printstar(n)\n  for i = 1, i \u003c n, 1.0 in\n    putchard(42);  # ascii 42 = '*'\n\n# print 100 '*' characters\nprintstar(100);\n````\n\n### 支持操作符重载\n\n#### 二元操作符重载\n\n下面代码实现了对`:`的重载，实现了类似多表达式的（多条语句）语法\n````\nready\u003e extern printd(x);\nready\u003e def binary : 1 (x y) 0;  # Low-precedence operator that ignores operands.\nready\u003e printd(123) : printd(456) : printd(789);\n123.000000\n456.000000\n789.000000\nEvaluated to 0.000000\n````\n\n#### 一元操作符重载\n\n````\nready\u003e def unary!(v)\n  if v then\n    0\n  else\n    1;\nready\u003e !1;\nready\u003e Evaluated to 0.000000\nready\u003e !0;\nready\u003e Evaluated to 1.000000\n````\n\n### 支持定义变量\n````\n# Define ':' for sequencing: as a low-precedence operator that ignores operands\n# and just returns the RHS.\ndef binary : 1 (x y) y;\n\ndef fibi(x)\n  var a = 1, b = 1, c in\n  (for i = 3, i \u003c x in\n     c = a + b :\n     a = b :\n     b = c) :\n  b;\n\n# Call it.\nfibi(10);\n# Evaluated to 55.000000\n````\n\n## 参考\n\nhttps://llvm.org/docs/tutorial/MyFirstLanguageFrontend/index.html\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fejunjsh%2Fkaleidoscope","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fejunjsh%2Fkaleidoscope","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fejunjsh%2Fkaleidoscope/lists"}