Giter Club home page Giter Club logo

chonk's Introduction

The Chonk programming language

This is the main source code repository for Chonk. It is a simple interpreted programming language based on the book Crafting Interpreters.

Table of Contents

About Chonk

Chonk's syntax is based on a mixture of Python and Golang. It is dynamically typed and uses a tree-walk interpreter, which is pretty slow but simple to implement. It doesn't support OOP and the goal is to keep it that way for simplicity's sake.

Getting Started

To install the Chonk interpreter, follow these steps.

Installation

  1. Clone the repo

    git clone https://github.com/rakin406/chonk.git
  2. Install the package

    cargo install --path .

Usage

  • You can either run the REPL (with zero arguments)

    chonk
  • Or run a script file

    chonk <path-to-file>

    Example:

    chonk dummy.ck

    You can use any file extension you want, I chose ".ck" as "chonk" starts with 'c' and ends with 'k'.

Syntax and Semantics

Comments start with a hashtag. They can be placed at the end of a line, and the rest of the line will be ignored. Only single-line comments are supported in Chonk.

# This is a comment
a = 5;  # This is also allowed

Expressions

Chonk supports 5 arithmetic operations: addition, subtraction, multiplication, division and modulo.

3 + 2;   # 5
10 - 3;  # 7
7 * 10;  # 70
70 / 7;  # 10
500 % 3; # 2

Expressions can be grouped inside parentheses.

(5 * (2 + 4)); # 30

In Chonk, variables cannot be declared like in other programming languages. Instead, to create a variable, you just assign a value to it.

Assignment is done with an equal sign (=).

a = 5;

Augmented assignment is used to replace a variable's value. It can be done by appending an equal sign to any of the arithmetic operators, like "+=", "-=" and so on.

a = 10;
a += 5; # 15

Chonk provides these unary operators: !, -, +

!false; # true
a = 10;
-a; # -10
+a; # 10

Prefix increment(++) and decrement(--) operators are allowed on variables. They can be suffixes too but are desugared(converted) into prefix expression.

a = 5;
++a;    # 6
a++;    # 7
--a;    # 6

Comparison operators are used to compare two values.

1 == 1; # true
1 != 1; # false

Statements

The echo statement displays the specified message to the screen. The message can be a string, or any other object.

echo "Hello World"; # Output: Hello World
echo 123;           # Output: 123
echo true;          # Output: true
echo null;          # Output: null

An "if statement" is written by using the if keyword. It is used to specify a block of code to be executed if a condition is true.

else if is used to specify a new condition if the first condition is false.

Finally, else is used to execute a block if all of the previous conditions are false.

a = 5;
b = 10;

if a < b {
    echo "b is greater than a";
} else if a == b {
    echo "a and b are equal";
} else {
    echo "a is greater than b";
}

Unfortunately, Chonk only has one type of loop: while loops.

With the while loop, we can execute a set of statements as long as a condition is true.

i = 1;
while i < 6 {
    echo i;
    ++i;
}

A function is defined using the func keyword. You can pass parameters into a function.

The return keyword is used to exit a function and return a value.

A function can be called just like in other programming languages. The number of arguments has to match the number of parameters of the function.

func add(a, b) {
    return a + b;
}

echo add(3, 2);

Currently, there is only one native function named "clock", which displays the current time in seconds.

The del keyword is used to delete variables.

a = 5;
del a;
b = c = 10;
del b, c;

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

Acknowledgments

Contact

Rakin Rahman - [email protected]

License

Distributed under the Apache License. See LICENSE for more information.

chonk's People

Contributors

rakin406 avatar

Watchers

 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.