GithubHelp home page GithubHelp logo

91d906h4 / hic Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 0.0 486 KB

⌨ HyLang compiler and executer build with Python.

License: MIT License

Python 59.07% Clojure 0.92% Hy 13.92% C++ 25.64% C 0.08% Assembly 0.37%

hic's Introduction

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.

hic's People

Contributors

91d906h4 avatar

Stargazers

 avatar Eason Wang avatar dillen-hub 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.