Skip to content

Latest commit

 

History

History
311 lines (245 loc) · 4.44 KB

README.md

File metadata and controls

311 lines (245 loc) · 4.44 KB

HyLang

A turing complete tiny esoteric programming language.



Table of Contents
  1. About HyLang
  2. Usage
  3. Examples
  4. License

About HyLang

HyLang is a Python compiler & executer.

Built With

Before HyLang v5, it is written in Python, and v6, v7 is written in C++.

Versions

v1

v1 is the first version of HyLang, there is only executer in this version.

It can run the assembly code like this:

VAL a 100
VAL b 10

ADD a b
SUB b a
VAL c 8
ADD b 1000
MSG a
MSG b

LAB TEST

LDA 10
MSG 'aasd10'
JMP TEST
END

v2

At v2, I first try to build a compiler, and update the executer to execute more complex function.

In this version, it can run if-else statment and for-loop:

var a = 21

if(a > 10)
    print('a is better than 10!')

    if(a > 20)
        print('a is better than 20!!')
    endif

    var b = 1
    for(b < 5; b += 1)
        print('YEAH')
    endfor 
endif

The output of the program:

a is better than 10!
a is better than 20!!
YEAH
YEAH
YEAH
YEAH

v3

I update the syntax of HyLang in v3. Now it's more like a real programming language.

for(var a = 0, a <= 10, cal a = a + 1){
    if(a > 5){
        print(a);
    }
    else{
        print('a is less than 5');
    }
    var b = 1;
}

The output of the program:

a is less than 5
a is less than 5
a is less than 5
a is less than 5
a is less than 5
6
7
8
9

v5

v5 is the most complete verion of HyLang. In this version, it can even run recursive function and import modules!

For example, this is a module math.hm:

/*
    Standard HyLang Math Module
*/

function abs(int a){
    if(a < 0){
        return -1 * a;
    }
    return a;
}

function pow(int a, int b){
    return b ** a;
}

function round(float a){
    return (a - a % 1);
}

We can use #import statement to import math.hm:

#import <math.hm>

int a = -100;
int b = 2;
int c = 10;

print(abs(a)); // 100

int p = pow(b, c);
print(p); // 1024

Usage

  1. First write your HyLang program (For example: my_program.hy).
  2. Using the following command to compile the file, and this will generate a HYA file output.hya.
python ./compiler.py my_program.hy output.hya
  1. Using the following command to run output.hya.
python ./executer.py output.hya

Examples

A program of Fibonacci in HyLang. There are recusive and DP versions:

/*
    Fibonacci Recursion
*/

function fib(int n){
    if(n <= 1){
        return n;
    }
    return fib(n - 1) + fib(n - 2);
}

print(fib(10)); // 55

/*
    Fibonacci DP
*/

function fib(int n){
    int temp[n];
    int a;

    temp[0] = 1;
    temp[1] = 1;

    for(int i = 2; i < n; i = i + 1){
        a = temp[i - 1] + temp[i - 2];
        temp[i] = a;
    }

    return temp[-1];
}

print(fib(10)); // 55

And here is the HYA (HyLang Assembly) code:

; This is comment.

SSKP        ; This is the start of Fibonacci Recursion.
:fib
INT $n _
LDA $n
LDA 1
LTE _ _
LNT _ 
JMP _ ITEDUWVUIKQGXVYC  ; The label generated by compiler.
LDA $n
RET
LABEL MZVVVYANEVBAFWKB
LABEL ITEDUWVUIKQGXVYC
LDA $n
LDA 1
SUB _ _
CALL fib    ; Call the function itself.
LDA $n
LDA 2
SUB _ _
CALL fib
ADD _ _
RET         ; Return.
ESKP        ; This is the end of Fibonacci Recursion.

LDA 10      ; Load number 10 in stack.
CALL fib    ; Call the function fib.
MSG _       ; Print the result.

SSKP        ; This is the start of Fibonacci DP.
:fib
INT $n _
LDA $n
INTA $temp _
INT $a 0
LDA 1
LDA 0
STAA $temp _
LDA 1
LDA 1
STAA $temp _
INT $i 0
LDA 2
STA $i
LABEL DXJBKZWHNCSLWOVU
LDA $i
LDA $n
LST _ _
LNT _ 
JMP _ LZIGNCEUBLLIDSCR
LDA $i
LDA 1
SUB _ _
LDAA $temp _
LDA $i
LDA 2
SUB _ _
LDAA $temp _
ADD _ _
STA $a
LDA $a
LDA $i
STAA $temp _
LDA $i
LDA 1
ADD _ _
STA $i
JMP 1 DXJBKZWHNCSLWOVU
LABEL LZIGNCEUBLLIDSCR
LDA -1
LDAA $temp _
RET
ESKP        ; This is the end of Fibonacci DP.

LDA 10      ; Load number 10 in stack.
CALL fib    ; Call the function fib.
MSG _       ; Print the result.

License

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