{"id":18683424,"url":"https://github.com/narasimha1997/boson-lang","last_synced_at":"2025-04-12T19:43:04.102Z","repository":{"id":45600947,"uuid":"380684913","full_name":"Narasimha1997/boson-lang","owner":"Narasimha1997","description":"A hybrid programming language written in Rust. ","archived":false,"fork":false,"pushed_at":"2024-08-08T18:41:53.000Z","size":1523,"stargazers_count":122,"open_issues_count":2,"forks_count":7,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-26T14:01:39.859Z","etag":null,"topics":["compiler","cpp","dynamic-typing","interpreter","programming-language","rust","scripting-language"],"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/Narasimha1997.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-06-27T08:07:36.000Z","updated_at":"2025-02-01T13:38:20.000Z","dependencies_parsed_at":"2024-01-18T23:10:31.370Z","dependency_job_id":"48a5c4e1-fccb-4787-95eb-5625c91ab658","html_url":"https://github.com/Narasimha1997/boson-lang","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/Narasimha1997%2Fboson-lang","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Narasimha1997%2Fboson-lang/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Narasimha1997%2Fboson-lang/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Narasimha1997%2Fboson-lang/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Narasimha1997","download_url":"https://codeload.github.com/Narasimha1997/boson-lang/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248625467,"owners_count":21135512,"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","cpp","dynamic-typing","interpreter","programming-language","rust","scripting-language"],"created_at":"2024-11-07T10:14:37.703Z","updated_at":"2025-04-12T19:43:04.080Z","avatar_url":"https://github.com/Narasimha1997.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# boson\nAn interpreted, dynamically-typed, multi-threaded, general purpose hobby programming language written in Rust.\n\n\n## Check out the live web server written in boson-lang:\nTo experiment with the capabilities of boson, I wrote a simple TCP single threaded web server which serves a sample HTML document. Check out this URL (The static web server is packaged as a container image and is deployed on Heroku)\n\n* [Live demo](https://boson-demo.herokuapp.com/)\n* [Source code can be found here](https://github.com/Narasimha1997/boson-lang/blob/main/examples/webserver/server.np)\n* [Multithreaded web server](https://github.com/Narasimha1997/boson-lang/blob/main/examples/webserver/server-multithreaded.np)\n\n\n## Features:\n1. Multiple Data Types: char, int, float, string, array, hashtable, bytes and buffer\n2. Airthmetic, Logical operations\n3. Variables and Constants\n4. Control and Looping structures\n5. Functions and Lambda expressions\n6. Many built-in functions\n7. Threads and Multi-threading\n8. Shell operator to run shell commands within the language statements\n9. Some basic built-in functions\n10. Iterators (psuedo iterators)\n11. Byte code generation, serialization and loading\n12. Dynamic modules - can load rust modules at runtime as addons\n13. System calls\n\nNote: The documentation of this project is still in early stages.\n\n## Installation:\nBuilding the language from source requires a working rust toolchain installed on the host machine. Check out the tutorial [here](https://doc.rust-lang.org/cargo/getting-started/installation.html) to set-up Rust and Cargo.\n\n1. Grab the source code:\n```\ngit clone git@github.com:Narasimha1997/boson-lang.git\n```\n2. Build boson:\n```\n./build.sh\n```\nThe build script should install Boson suite of tools on your system. Explore different options supported by this script.\n\n3. Run sample source code\n```\nboson-eval examples/hello.np\n```\nThis should print `hello, world!` on screen.\n\n### Using the tools\nIf compilation is successful, it should generate four binary tools, these are:\n\n1. boson: This is the REPL of boson lang, you can execute boson language statements in the CLI.\n```\nWelcome to Boson REPL\nThis is a REPL binary for boson - a general purpose programming language written in rust. (Ctrl + C to quit)\nBoson v0.0.1\nVM Check - Passed.\n\u003e\u003e println(10 + 20)\n30\n```\n\n2. boson-dis: This tool generates stringified representation of the compiled version of source file.\n```\nboson-dis examples/hello.np\n```\n\nThis should generate the output:\n```\nInstructions: \n00000000 IConstant 0\n00000003 ILoadBuiltIn 2\n00000006 ICall 1\n\nConstants: \n00000000 hello, world!\n```\n\n3. boson-compile: This tool generates the compiled bytecode of the source file, which can then be executed.\n```\nboson-eval ./examples/hello.np\n```\nThis should generates  a file called `hello.np.b` in the same folder `hello.np` was present, i.e `examples/hello.np.b`. This file has the binary representation of the compiled bytecode.\n\n4. boson-eval: Evaluates the source file or the bytecode file and stdouts the result.\n```\nboson-eval ./examples/hello.np\n```\n\n## Language examples:\n1. Hello, world\n```python\nprintln('hello,world')\n```\n\n2. Keyboard input and display\n```python\nconst ip = input()\nconst ip2 = input();\nconst greeting = \"Hello! \" + ip2 + \" \" + ip;\nprintln(greeting);\n```\n\n3. Arithmetic operators\n```python\nconst d = a + b + c;\nconst e = a * b - c;\nconst f = ((a + b) * c * d) / (a + b);\n\nconst g = (a + b) % c;\n\nprintln(a, b, c, d, e, f, g); # 1 2 3 6 -1 18 0\n```\n\n4. Bitwise operators\n```python\nconst x = 10;\nconst y = 20;\n\nvar z = ((x \u0026 0) | y);\nprintln(~z) # -21\n```\n\n5. Logical operators\n```python\nconst m = 10;\nconst n = 20;\n\nprintln(m \u003e n, n \u003c m, n \u003e m + 5) # false, false, true\nprintln(m == n - 10, !0, !(m == n - 10)) # true true false\n```\n\n6. Arrays\n```python\nvar array = [1, 2, 3, 4, \"Hello\", 6.455]\nprintln(array[0] + 2) # 3\nprintln(array[4] + \", world\") # Hello, world\nprintln(array) # Array([1, 2, 3, 4, Hello, 6.455])\n\narray[4] = 9678967;\nprintln(array) # Array([1, 2, 3, 4, 9678967, 6.455])\n```\n\n7. Hash tables\n```python\nvar myHashMap = {\n    \"name\": \"Prasanna\",\n    \"age\": 24,\n    \"country\": \"India\"\n}\n\nprintln(myHashMap) # HashTable({age: 24, country: India, name: Prasanna})\nprintln(myHashMap[\"age\"] + 2) # 26\n\nconst key = \"name\"\nprintln(\"Hey! \" + myHashMap[key]) # Hey! Prasanna\n\nmyHashMap[\"city\"] = \"Bengaluru\"\nprintln(myHashMap[\"city\"]) # Bengaluru\n```\n\n8. While loop\n```python\nconst N = 100;\n\nvar n0 = 0;\nvar n1 = 1;\nvar n2 = 0;\nvar idx = 2;\n\nwhile (idx \u003c= N ) {\n    n2 = n0 + n1;\n    n0 = n1;\n    n1 = n2;\n    idx = idx + 1;\n}\n\nprintln(n1);\n```\n\n9. If else:\n\n```python\n\nconst x = 10;\n\nif (x \u003e 20) {\n    println(\"X \u003e 20\");\n} else {\n    println(\"X \u003c 20\"); # this will be executed\n}\n\n```\n\n10. Functions:\n\n```python\nfunc fib(N) {\n    \n    if (N == 0) {\n        return 0;\n    }\n\n    if (N == 1) {\n        return 1;\n    }\n\n    return fib(N - 1) + fib(N - 2);\n}\n\nconst result = fib(10);\nprintln('got result: ', result);\n```\n\n11. Shell operator:\n\nShell operator can be used to execute shell commands within the program statements.\n```python\n\n# count the number of files in the given directory\nfunc count_files() {\n    const res = $ \"ls | wc -l\";\n    return int(res[1]);\n}\n\n# call the function and print it's output\nprintln(count_files());\n\n# count the number of occurences of a given pattern in the given file\nfunc count_occurences(file, pattern) {\n    const res = $ \"cat \"+file+\" | grep -c \"+pattern; \n    return int(res[1])\n}\n\nconst res = count_occurences(\"LICENSE\", \"GPL\")\nprintln(res);\n```\n\n12. Lambda functions:\n\n```python\n# define a adder that takes two parameters\nconst lambda_adder = lambda x, y =\u003e x + y\nprintln(lambda_adder(10, 20)) # 30\n```\n\n13. Functions as objects:\n\n```python\n# here the adder function accepts a function as argument and executes it\nfunc adder_exec(fn, x, y) {\n    return fn(x, y)\n}\n\n# adder function is defined here\nfunc adder(x, y) {\n    return x + y\n}\n\n# adder function is passed as the parameter\nconst result = adder_exec(adder, 10, 20)\nprintln(result) # 30\n```\n\n14. Closures:\n\n```python\n\n# this is the wrapper function that returns a adder function with enclosed local variables\nfunc wrap_adder(x, y) {\n    const z = 30\n    func adder() {\n        return x + y + z\n    }\n\n    return adder\n}\n\n# call the wrapper and obtain the inner child\nconst adder = wrap_adder(10, 20)\n\n# call the inner child adder function\nconst result = adder()\nprintln(result) # result = 60\n```\n\n15. Iterators:\n\n**Note**: Iterators are yet to be tested completely\n```\nconst arr = [1, 2, 3, 4]\nconst iterator = iter(arr)\nwhile (has_next(iterator)) {\n    println(next(iterator))\n}\n```\n\n16. Multithreading:\n```python\n# this function prints Hello, world from \u003cthread\u003e every 3 seconds \nfunc print_periodic(name) {\n    while (true) {\n        println(\"Hello, world from \", name);\n        sleep_sec(3);\n    }\n}\n\n# spawn thread1 and spawn thread2\nconst th1 = thread print_periodic(\"thread1\");\nconst th2 = thread print_periodic(\"thread2\");\n\n# wait for thread 1 and thread 2 to complete\nwait(th1)\nwait(th2)\n```\n\n**Threads and global variables**: In boson, every thread gets it's own copy of global variables space, so when a thread mutates a global variable, it mutates it's local variable copy and not the one in global space.\n\n### Native modules\nBoson has a support for native modules written in Rust, every module has to implement a set of functions as per the standard function signatures shown below:\n```rust\npub type OpenFunctionSymbol = unsafe extern \"Rust\" fn(Rc\u003cObject\u003e) -\u003e DynamicModuleResult;\npub type CloseFunctionSymbol = unsafe extern \"Rust\" fn(Rc\u003cObject\u003e) -\u003e DynamicModuleResult;\npub type ReadFunctionSymbol = unsafe extern \"Rust\" fn(Rc\u003cObject\u003e) -\u003e DynamicModuleResult;\npub type WriteFunctionSymbol = unsafe extern \"Rust\" fn(Rc\u003cObject\u003e) -\u003e DynamicModuleResult;\npub type ExecFunctionSymbol = unsafe extern \"Rust\" fn(String, \u0026Vec\u003cRc\u003cObject\u003e\u003e) -\u003e DynamicModuleResult;\n```\n\nAny cargo crate that implements these functions can be loaded and used as a dynamic module. Look at the [hello-world module](https://github.com/Narasimha1997/boson-lang/blob/main/modules/hello_world/src/lib.rs) for reference. Make sure you add the following lines to `Cargo.toml` of the cargo crate you are trying to build as a module:\n```\n[lib]\ncrate-type = [\"rlib\", \"cdylib\"]\n```\n\nThe native modules that are under `modules/` directory will be installed at `/usr/local/lib/boson` and can be loaded using `mopen`:\n```\n# you can pass init parameters also while calling `mopen`\nconst regex = mopen(\"std::re\", none)[0];\n```\nThis will look for `libre.so` in `/usr/local/lib/boson`, in general it will look at `/usr/local/bin/boson/lib{{module-name}}.so`. However you can also load modules directly by their file-paths like:\n\n```\nconst regex = mopen(\"/usr/local/lib/boson/libre.so\", none)[0];\n```\n\n### System calls\nSystem call methods are provided as builtin functions in boson. `get_syscalls` method returns a list of all the system calls supported on the target architecture and `syscall` method executes the system call. Look at the example below:\n```python\n\nconst no = get_syscalls();\nconst fd = syscall(no.OPEN, \"hello.txt\", 0)\n\nif (fd \u003c 0) {\n    println(\"file hello.txt not found\")\n    exit(fd)\n}\n\nconst buffer = create_buffer(10)\n\nsyscall(\n    no.READ,\n    fd, \n    buffer,\n    10\n)\n\nprintln(string(buffer))\n```\n\n### Embedding Boson:\nBoson language compiler + VM can be easily integrated into other projects using the API. As of now, any Rust codebase can import statically the Boson crate or use foreign function interface (FFI) to load Boson shared library by manually defining the ABI. We are yet to test [CXXABI](https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/libsupc%2B%2B/cxxabi.h) compatibility of the boson crate, so it can be considered unsafe to import boson in Non-Rust codebases as of now. \n\nHere is how you can embed boson in other rust projects:\n```rust\nextern crate boson;\n\nuse boson::api::BosonLang;\n\npub fn main() {\n    let result = BosonLang::eval_buffer(\n        \"10 + 3 / 2\"\n            .as_bytes()\n            .to_vec()\n    );\n\n    println!(\"eval result: {}\", result);\n    // will output \"eval result: Some(Float(11.5))\"\n}\n```\n\n\n### Running tests\nYou can use cargo test tools to run the test\n```\ncargo test\n```\n\n### Benchmarks\nBenchmarks are still under implementation\n\n### Credits\n1. [Monkey lang](https://monkeylang.org/)\n2. [Monkey lang rust version](https://github.com/wadackel/rs-monkey-lang)\n\n### TODO:\n1. Web assembly port\n2. Proper documentation\n3. Proper test cases\n4. Bug fixes\n\n### Contributing\nFeel free to raise any issues, make Pull Requests, suggest changes, clone the project and make your own changes.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnarasimha1997%2Fboson-lang","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnarasimha1997%2Fboson-lang","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnarasimha1997%2Fboson-lang/lists"}