{"id":23392404,"url":"https://github.com/tamaroning/haribote-lang","last_synced_at":"2025-04-08T15:19:57.069Z","repository":{"id":111995861,"uuid":"419543848","full_name":"tamaroning/haribote-lang","owner":"tamaroning","description":"A simple interpreter w/ static analysis and constant propagation","archived":false,"fork":false,"pushed_at":"2021-11-29T06:40:37.000Z","size":199,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-14T11:35:07.844Z","etag":null,"topics":["compiler-optimization","interpreter"],"latest_commit_sha":null,"homepage":"","language":"Rust","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/tamaroning.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":"2021-10-21T01:35:08.000Z","updated_at":"2022-05-25T12:12:54.000Z","dependencies_parsed_at":null,"dependency_job_id":"e5376675-6172-4bc9-b21e-c3f393c585e5","html_url":"https://github.com/tamaroning/haribote-lang","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tamaroning%2Fharibote-lang","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tamaroning%2Fharibote-lang/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tamaroning%2Fharibote-lang/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tamaroning%2Fharibote-lang/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tamaroning","download_url":"https://codeload.github.com/tamaroning/haribote-lang/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247867362,"owners_count":21009240,"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-optimization","interpreter"],"created_at":"2024-12-22T04:30:15.924Z","updated_at":"2025-04-08T15:19:57.063Z","avatar_url":"https://github.com/tamaroning.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# haribote-lang\n[![CircleCI](https://circleci.com/gh/tamaroning/haribote-lang/tree/main.svg?style=shield)](https://circleci.com/gh/tamaroning/haribote-lang/tree/main)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\nharibote-lang is a simple and fast programming language for education originally made by Mr.Kawai.  \nThis repository is a remodelled implementation in Rust of the original version, [Creating programming languages in 10 days](http://essen.osask.jp/?a21_txt01).  \n\n## Features\n- This repository contains hrb, haribote-lang interpreter\n- hrb runs in two modes, normal mode and interactive mode (a.k.a. REPL)\n- Input source code is converted into internal code\n- and is optimized it in several ways\n\nFor further information, See [Optimization Strategy](#Optimization-Strategy) or [My Blog(ja)](https://tamaron.hatenablog.com/entry/2021/11/20/165929).\n\n# Build\nharibote-lang run on Windows, OSX, Linux, UNIX.  \n\n```sh\ngit clone https://github.com/tamaroning/haribote-lang.git\ncd haribote-lang\ncargo build --release\n```\n\n# Run\nRun a .hrb file:\n``` sh\n./target/release/hrb \u003cfilepath\u003e\n```\n\nRun in an interactive mode:\n``` sh\n./target/release/hrb\n```\n\n# Usage\n- `hrb [OPTIONS] FILEPATH`: Run the program\n- `hrb [OPTIONS]`: Run in interactive mode\n- `hrb help`: show the usage\n\n### Options\n- `-emit-ir`: Display the intermidiate representation\n- `-no-optimize`: Doesn't optimize the program\n- `-no-exec`: Doesn't execute the program\n\n# Demo\n\nIn interactive mode, you can type an expression to check the result\n```\n?\u003e hrb\nharibote-lang version 1.1.1\nRunning in Interactive mode\nType \"run \u003cfilepath\u003e\" to load and run the file.\n\u003e\u003e\u003e print \"Hello World\\n\";\nHello World\n\u003e\u003e\u003e a = 15; b = 20;\n\u003e\u003e\u003e a * b\n300\n\u003e\u003e\u003e exit\n?\u003e\n```\n\n\n```\n?\u003e hrb ./example/fibo.hrb`\nFibo_0 = 1\nFibo_1 = 1\nFibo_2 = 2\nFibo_3 = 3\nFibo_4 = 5\nFibo_5 = 8\nFibo_6 = 13\nFibo_7 = 21\nFibo_8 = 34\nFibo_9 = 55\nFibo_10 = 89\nFibo_11 = 144\n?\u003e\n```\n\n```\n?\u003e hrb -emit-ir example/calc.hrb\nOptimizing...\n--------------- Dump of internal code ---------------\n        copy a, i32 1\n        copy b, i32 3\n        copy _tmp0, i32 3\n        copy _tmp1, i32 4\n        copy c, i32 4\n        copy _tmp0, i32 8\n        copy _tmp1, i32 10\n        copy c, i32 10\n-----------------------------------------------------\n?\u003e\n```\n\n# Grammar\n\n## Lexical elements\n\n- Num : Decimal numbers (0, 100, 53, 1024)\n- Ident : Identifiers which starts with alphabet (abc, ABc123)\n- Str : Strings encloses in double quotes (\"Hello Hari-bote \", \"World\\n\")\n\n## Definition by EBNF\n```\nprogram     ::= top*\n\ntop         ::= label | stmt\n\nlabel       ::= \u003cIdent\u003e \":\"\n\nstmt        :: = array-decl\n               | if-else\n               | for\n               | goto-stmt\n               | expr? \";\"\n               | \"print\" (expr | \u003cStr\u003e) \";\"\n               | \"println\" (expr | \u003cStr\u003e) \";\"\n\narray-decl  ::= \"let\" \u003cIdent\u003e \"[\" expr \"]\" \";\"\n\nif-else     ::= if-goto | if-else-sub\nif-goto     ::= \"if\" \"(\" expr \")\" goto-stmt\nif-else-sub ::= \"if\" \"(\" expr \")\" \"{\" top* \"}\" ( \"else\" \"{\" top* \"}\" )?\n\nfor         ::= \"for\" \"(\" expr? \";\" expr? \";\" expr? \")\" \"{\" top* \"}\"\n\ngoto-stmt   ::= \"goto\" \u003cIdent\u003e \";\"\n\nexpr        ::= assign\nassign      ::= equality ( (\"=\" | \"+=\" | \"-=\" | \"*=\" | \"/=\") expr )?\nequality    ::= relational ( ( \"==\" | \"!=\" ) relational )*\nrelational  ::= add        ( (  \"\u003c\" | \"\u003c=\" ) add        )*\nadd         ::= mul        ( (  \"+\" | \"-\"  ) mul        )*\nmul         ::= unary      ( (  \"*\" | \"/\"  ) unary      )*\nunary       ::= (\"+\" | \"-\")? primary\nprimary     ::= \u003cNum\u003e | \u003cIdent\u003e ( \"[\" expr \"]\" )?\n\n```\n\n# Intermidiate Representation\nIntermidiate Representation (IR) is a low-level code of haribote language.  \nOptimizations are taken place on IR.  \nIn order to learn how IR is implemented, see a struct `Operation` in /src/parser.rs. \n\n# Optimization Strategy\n\nhrb supports the following optimization methods:\n- [Constant Folding \u0026 Constant Propagation](#Constant-Folding-Constant-Propagation)\u003c!--remove \u0026 from link--\u003e\n- [Removing Unreachable Operations](#Removing-Unreachable-Operations)\n- [Peekhole Optimization](#Peekhole-Optimization)\n    - [Jump Chain Optimization](#Jump-Chain-Optimization)\n\nBasic strategy is as follows:\n\n1. Build a control-flow graph\n2. Data-flow analysis\n3. Change code\n\n## Constant Folding \u0026Constant Propagation\n1. Let each CFG node have a constant variable table.\n2. Information of constant variables moves to other nodes along the control flow. (One of the good ways is using a queue.)\n3. Replace arithmetic operations with copy operations by using information of the final stete.\n\n## Removing Unreachable Operations\n1. Let each CFG node n have a boolean value b[n].\n2. Set all b[n]s falses.\n3. Set b[entry point] true.\n4. Do BFS and set every b[reachable node] true.\n5. Remove n such that b[n] = false.\n\n## Peekhole Optimization\nIt's unnecessary to build a CFG.\n\n### Jump Chain Optimization\n1. Search a `goto(jump) L` operation. L is a label.\n2. If the distination label of L starts with a goto(jump) operation, get the destination of L.\n3. Repeat step 2. Finally get the final destination of label D. \n4. If detects a cyclic control-flow in step 3, do nothing and finish.\n5. Replace the original `goto(jump) L` with `goto(jump) D`. \n\n\u003c!--\n# Commit Logs\nYou can see the commit log to follow the steps of implementation.  \nThe steps from 1 to 8 are the same as those of [the original version](http://essen.osask.jp/?a21_txt01).  \n\n| Step | features |\n| ---- | ---- |\n| 1 | A very simple language. |\n| 2 | Multi-character variable name. Skips spaces. |\n| 3 | Conditional branch. Run loops. |\n| 4 | REPL (interactive mode). |\n| 5 | Speed up the program. |\n| 6 | Speed up the program. |\n| 7 | Expression. |\n| 8 | if-else \u0026 for statement. Optimize goto. |\n| 9 | Array (declaration, assignment, random access). |\n| 10 | Constant Folding \u0026 Constant Propagation. |\n--\u003e\n\n# References\n- http://essen.osask.jp/?a21_txt01\n- http://pages.cs.wisc.edu/~horwitz/CS704-NOTES/2.DATAFLOW.html\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftamaroning%2Fharibote-lang","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftamaroning%2Fharibote-lang","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftamaroning%2Fharibote-lang/lists"}