Git Product home page Git Product logo

boson-lang's Introduction

boson

An interpreted, dynamically-typed, multi-threaded, general purpose hobby programming language written in Rust.

Features:

  1. Multiple Data Types: char, int, float, string, array, hashtable, bytes and buffer
  2. Airthmetic, Logical operations
  3. Variables and Constants
  4. Control and Looping structures
  5. Functions and Lambda expressions
  6. Threads and Multi-threading
  7. Shell operator to run shell commands within the language statements
  8. Some basic built-in functions
  9. Iterators (psuedo iterators)
  10. Byte code generation, serialization and loading

Note: The documentation of this project is still in early stages.

Installation:

Building the language from source requires a working rust toolchain installed on the host machine. Check out the tutorial here to set-up Rust and Cargo.

  1. Grab the source code:
git clone [email protected]:Narasimha1997/boson-lang.git
  1. Build boson:
./build.sh

The build script should install Boson suite of tools on your system. Explore different options supported by this script.

  1. Run sample source code
boson-eval examples/hello.np

This should print hello, world! on screen.

Using the tools

If compilation is successful, it should generate four binary tools, these are:

  1. boson: This is the REPL of boson lang, you can execute boson language statements in the CLI.
Welcome to Boson REPL
This is a REPL binary for boson - a general purpose programming language written in rust. (Ctrl + C to quit)
Boson v0.0.1
VM Check - Passed.
>> println(10 + 20)
30
  1. boson-dis: This tool generates stringified representation of the compiled version of source file.
boson-dis examples/hello.np

This should generate the output:

Instructions: 
00000000 IConstant 0
00000003 ILoadBuiltIn 2
00000006 ICall 1

Constants: 
00000000 hello, world!
  1. boson-compile: This tool generates the compiled bytecode of the source file, which can then be executed.
boson-eval ./examples/hello.np

This 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.

  1. boson-eval: Evaluates the source file or the bytecode file and stdouts the result.
boson-eval ./examples/hello.np

Language examples:

  1. Hello, world
println('hello,world')
  1. Keyboard input and display
const ip = input()
const ip2 = input();
const greeting = "Hello! " + ip2 + " " + ip;
println(greeting);
  1. Arithmetic operators
const d = a + b + c;
const e = a * b - c;
const f = ((a + b) * c * d) / (a + b);

const g = (a + b) % c;

println(a, b, c, d, e, f, g); # 1 2 3 6 -1 18 0
  1. Bitwise operators
const x = 10;
const y = 20;

var z = ((x & 0) | y);
println(~z) # -21
  1. Logical operators
const m = 10;
const n = 20;

println(m > n, n < m, n > m + 5) # false, false, true
println(m == n - 10, !0, !(m == n - 10)) # true true false
  1. Arrays
var array = [1, 2, 3, 4, "Hello", 6.455]
println(array[0] + 2) # 3
println(array[4] + ", world") # Hello, world
println(array) # Array([1, 2, 3, 4, Hello, 6.455])

array[4] = 9678967;
println(array) # Array([1, 2, 3, 4, 9678967, 6.455])
  1. Hash tables
var myHashMap = {
    "name": "Prasanna",
    "age": 24,
    "country": "India"
}

println(myHashMap) # HashTable({age: 24, country: India, name: Prasanna})
println(myHashMap["age"] + 2) # 26

const key = "name"
println("Hey! " + myHashMap[key]) # Hey! Prasanna

myHashMap["city"] = "Bengaluru"
println(myHashMap["city"]) # Bengaluru
  1. While loop
const N = 100;

var n0 = 0;
var n1 = 1;
var n2 = 0;
var idx = 2;

while (idx <= N ) {
    n2 = n0 + n1;
    n0 = n1;
    n1 = n2;
    idx = idx + 1;
}

println(n1);
  1. If else
const x = 10;

if (x > 20) {
    println("X > 20");
} else {
    println("X < 20"); # this will be executed
}
  1. Functions
func fib(N) {
    
    if (N == 0) {
        return 0;
    }

    if (N == 1) {
        return 1;
    }

    return fib(N - 1) + fib(N - 2);
}

const result = fib(10);
println('got result: ', result);
  1. Shell operator Shell operator can be used to execute shell commands within the program statements.
# count the number of files in the given directory
func count_files() {
    const res = $ "ls | wc -l";
    return int(res[1]);
}

# call the function and print it's output
println(count_files());

# count the number of occurences of a given pattern in the given file
func count_occurences(file, pattern) {
    const res = $ "cat "+file+" | grep -c "+pattern; 
    return int(res[1])
}

const res = count_occurences("LICENSE", "GPL")
println(res);

Running tests

You can use cargo test tools to run the test

cargo test

Credits

  1. Monkey lang
  2. Monkey lang rust version

TODO:

  1. Web assembly port
  2. Proper documentation
  3. Proper test cases
  4. Bug fixes

Contributing

Feel free to raise any issues, make Pull Requests, suggest changes, clone the project and make your own changes.

boson-lang's People

Contributors

narasimha1997 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.