banner

Kang2

Kang2 is a high-level, easy to learn programming language. It combines many loved features of other popular languages, while adding new and exciting ideas for future languages to come. It uses NodeJS and is best used for small terminal applications and to see, how a interpreter works under the hood. It's relatively small size makes it easy to tinker with and make new additions.

Documentation

In- and OutputOutput is gotten, by using the log keyword
      log "Hello World!"
            
    // CONSOLE
    Hello World!
    
To get Input, the input keyword needs to be used
    input "Name?"

    // CONSOLE
    >> Name?
    
Both log and input are not functions though, they cannot be invoked in a calculation and do not return anything. They are statements
input can be saved with the arrow
    var name
    input "Name? " -> name

    input "Age? " -> var age
      
      
    log name
    log age
      
    // CONSOLE

    >> Name? acho
    >> Age?  18
      
    "acho"
    18
    

Variables Variables in Kang2 are declared with the var keyword, with the name, an equals sign and the value following
There are no types, rather the variables are declared dynamically
    var name = "acho"
    var number = 10
    var condition = true
    
    var array = [10, 20, 30, 40]
    var arrayValue = array[1]
Everywhere, where a value must be put in, calculations are possible
    var num1 = 10
    var num2 = 20

    var num3 = (num1 + num2) * 2

    log num3 + 1

    // CONSOLE
    61

Comparisons You can compare two values with these keywords:
  • ==
            log 10 == 10
            log "Hello" == "World"
            log 1 == 2
            
            // CONSOLE
            true
            false
            false
  • > <
            log 10 < 15
            log 20 > 30
            
            log 10 >= 10
            log 10 <= 30 
            
            // CONSOLE
            true
            false
            true
            true
  • &&
            log 10 == 10 && 20 == 20
            log 1 > 2 && 10 == 10
            
            // CONSOLE
            true
            false

Conditions

Conditions

Conditions can be used with the if keyword
Note that Kang2 doesn't use parenthesis, unlike other languages
    if 10 == 10 {
        log "hello"
    }

    if input "what is your name?" == "acho" {
        log "hello acho!"
    }
Get an else if with the elseif- and a final else statement with the else keyword
    if 10 == 20 {
        log "5 + 5 = 20"
    } elseif 10 == 30{
        log "5 + 5 = 30"
    } else {
        log "omg"
    }

Loops

Loops

The loop keyword is needed to use loops
To loop for a specific amount of times, just a number is necessary
    loop 3 {
        log "Hello"
    }

    // CONSOLE
    "Hello"
    "Hello"
    "Hello"
Unlike other programming languages, there is no for- and while, rather the loop combines both of those COMING SOON

How it works

Basically one block of code is called a Statement. This can be everything from just a small "Hello World!" Output, to a whole giant function. These Statements are saved into a Statement Sequence, which is just a list of Statements. Now what's important is that every Statement Sequence has it's own Symbol Table, which means it saves new Variables locally. This Table has a reference to it's parent, but not backwards. So it's just going upwards.

This is easily represented by a Function, within the global scope. We'll just call it getSmt(). If a Variable is now declared outside getSmt, then itself and all other Functions gave access to it, due to them having a reference, to their parents Table, which now translates to the global scope. But if a Variable is declared within getSmt, then just it has access, because the parent, doesn't have a reference, therefore it doesn't know it exists.

Everytime a Function is called, a new Table is created. This also counts for Loops and Conditions. This also means, that a Variable inside a function can only be taken out by either a return or a mutation of another Variable, which lies higher in the Table-Tree.

Now going back, every Function has it's own Statement Sequence, which holds it's own Symbol Table. The global scope itself can be seen as a big Function, which is just called instantely.

With that said, the language has a hierarchy. At the top is the Statment Sequence, with the Statement just below that. Up until now, there is only a total of eight possible Statements. These include:

  • Assigning a new Variable
  • Creating a new Function
  • Mutating an existing Variable
  • Logging Something
  • Returning anything
  • Getting Input
  • Invoking a Loop
  • Checking a Condition

If there is not one of those written, it will lead to an error.

Below that is the Expression. One of those is used, when there is any type of value, like an argument or the value for a new Variable. This part of the hierarchy is there to provide calculations, that are done right. Here we have:

  • Addition
  • Subtraction
  • Any type of comparison

After that comes the second to last one, which is the Term. This includes:

  • Multiplication
  • Division
  • Modulo

And the last one is the Factor. This one has:

  • Positive and negative numbers
  • Partenthesis
  • Strings
  • Variable references
  • Function calls
  • Booleans

All these layers are there to provide a foundation, on which to build an easy to understand parser, which takes all the tokens, generated by the lexer and turning them into a huge Syntax Tree

To calculate all multiplications before any additions for example, every Node in the Tree has one purpose, like adding two Numbers. Now the Interpreter, which traverses this tree through every branch, sees that two Nodes have to be added together and opens them up. Inside it finds a multiplication with two other Nodes. And he opens them up again. Now he finds just a Number. This Number is then returned and multiplied with the other one. The Result is now added to the other one and we have our addition, with the multiplication being done first.

While the Interpreter goes inside every Node, like creating a new Variable or calling a Function in the end, it went through every branch and is done.

All this may be a bit complicated, but once the basics get into someones head, it's actually way easier than the general assumption is.

Why I made it

I was initially motivated to create a programming language after I saw the SPWN-language, made by Spu7Nix. I didn't even look up, how programming languages work at all, instead I just went head-first into my first an... attempt. Since I didn't have any Idea what I was doing and I was using Java for some misterious reason, It failed miserably.

A couple months later, I saw this Video by Sebastian Lague, where he created a small language too. This got my motivation up again, which lead me to look up how a programming language is structured. I followed a simple tutorial in the beginning, to get the basics.

What I needed to know was, how the abstract syntax tree works and how traverse one with an interpreter. Compilation was one step too far for me, since I didn't aim for a quote unquote "professional" or very serious language. As soon as I understood the most important, foundational parts of an interpreter, I diverged from that tutorial, to go on my own. After a lot of brutal months, hard work and dedication, I had somewhat of a finished product. It's still very buggy, but I'll definitely come back one day and fix those and add certain features. It took my half a year in the end to get that and I am very proud. Maybe one day it'll be of use to anybody.

  • Fix weird bugs
  • Add advanced loops
  • Add .kg extension reader
  • Line and Character on error
  • Classes
  • Make browser playground on this site

Conclusion

Kang2 is best used either as a toolbox for someone interested in the inner workings of programming languages or someone who is an absolute beginner, due to there being some features catered to those people.

In the end it's just a student project and shouldn't be taken to seriously. The language doesn't go to deep and isn't really meant to be adapted into the mainstream, but the ideas themselves could be.