Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

16 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

davbjor-compiler

This program defines a brand new programming language: DBLang .

This program is able to interpret DBLang code, and also transpile to C++ .

Under the hood it first does a tokenization using a lexer, and then parses the tokens into an AST-tree. Then a semantic analysys checks for variable handling. The AST is then optimized with methods including constant folding and dead code removal . Finally the AST is either transpiled to C++, or real-time interpreted.

BNF definition av DBLang

<program> ::= <block> | <block> <nl> <program>
<block> ::= <ws> <block> 
    | <while> 
    | <if> 
    | <declaration> 
    | <assignment> 
    | <output>

<while> ::= "while" <ws> "(" <ws> <condition> <ws> ")" <ws> "{" <ws> <nl> <program> <nl> <ws> "}" <ws> ";"
<if> ::= "if" <ws> "(" <ws> <condition> <ws> ")" <ws> "{" <nl> <program> <nl> <ws> "}" <ws> ";"

<declaration> ::= "let " <ws> <identifier> <ws> "=" <ws> <statement> <ws> ";"
<assignment> ::= <identifier> <ws> <assignor> <ws> <statement> <ws> ";"

<output> ::= "print(" <ws> <statement> <ws> ")" <ws> ";" 

<statement> ::= <condition> | <expression>
<condition> ::= <ws> <expression> <ws> <comparator> <ws> <expression>
<expression> ::= <value> 
    | <expression> <ws> <operator> <ws> <expression> 
    | "(" <ws> <expression> <ws> ")"

<value> ::= <number> | <identifier>

<identifier> ::= <letter> | <identifier> <letter>
<number> ::= <digit> | <digit> <number> | <unary> <digit> | <unary> <digit> <number>
<ws> ::= " " <ws> | ""
<nl> ::= "\n" <nl> | ""
<comparator> ::= "<" | ">" | "!=" | "==" | "<=" | ">="
<assignor> ::= "=" | "+=" | "-=" | "*=" | "/="
<operator> ::= "+" | "-" | "*" | "/"
<unary> ::= "-"
<digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
<letter> ::= "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z" | "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z" 

https://bnfplayground.pauliankline.com...

Examples

DBLang Code To Print The First 28 Fibbonacci Numbers

This messy code gets optimized into clean C++. Let's see the optimizations:

// FIBONACCI MESSY
let a = (3 + 2) * (4 + 1) * (6 - 5 - 1);
let b = (3 + 1) / 4;
let i = 31 + 3 - 3 * 11 - 1;

while(i < (13 + 4) + (4 * 4 - 5) * (2 - 1)){
    if(13 != 41){
        b += 1 - 1;
    };
    while(2 == 0){
        print(b);
    };
    print(1 - 1 + a);

    b = a + (((13 + 4) - (4 * 4 + 1)) + (1 - 1 + b) * (2 - 1));
    a = b * (3 * 4 - 11) - a + (6 * 5) * (1 - 1);

    i -= 300 - 30 * 10;
    i += 1;
};

Optimized And Transpiled To C++ Code

#include <iostream>

int main(){
    int a = 0;
    int b = 1;
    int i = 0;
    while(i < 28){
        std::cout << (a) << "\n";
        b = a + b;
        a = b - a;
        i = i + 1;
    }
}

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages