Giter Club home page Giter Club logo

tinycompiler's Introduction

TinyCompiler

A simple Compiler in python Using Antlr4 to parse, and using llvmlite to generate bytecode.

You can write rule like this (test.gl):

def func(x){
    x = x+1;
    return x;
}
out = 0;
for(i=0;i<5;i=i+1){
    out = out + 10;
}
out = out +1;
c = func(out);
d = 0;
if(c<0){
    d = 1;
}

Following is llvm ir code:

; ModuleID = ""
target triple = "unknown-unknown-unknown"
target datalayout = ""

define double @"main"() 
{
entry:
  %"out" = alloca double
  store double              0x0, double* %"out"
  %"i" = alloca double
  store double              0x0, double* %"i"
  br label %"loop.header"
loop.header:
  %".5" = load double, double* %"i"
  %"smalltmp" = fcmp ult double %".5", 0x4014000000000000
  br i1 %"smalltmp", label %"loop.body", label %"loop.end"
loop.body:
  %".7" = load double, double* %"out"
  %"addtmp" = fadd double %".7", 0x4024000000000000
  store double %"addtmp", double* %"out"
  %".9" = load double, double* %"i"
  %"addtmp.1" = fadd double %".9", 0x3ff0000000000000
  store double %"addtmp.1", double* %"i"
  br label %"loop.header"
loop.end:
  %".12" = load double, double* %"out"
  %"addtmp.2" = fadd double %".12", 0x3ff0000000000000
  store double %"addtmp.2", double* %"out"
  %".14" = load double, double* %"out"
  %"calltmp" = call double @"func"(double %".14")
  %"c" = alloca double
  store double %"calltmp", double* %"c"
  %"d" = alloca double
  store double              0x0, double* %"d"
  %".17" = load double, double* %"c"
  %"smalltmp.1" = fcmp ult double %".17",              0x0
  br i1 %"smalltmp.1", label %"loop.end.if", label %"loop.end.else"
loop.end.if:
  store double 0x3ff0000000000000, double* %"d"
  br label %"loop.end.endif"
loop.end.else:
  br label %"loop.end.endif"
loop.end.endif:
  %"d.1" = load double, double* %"d"
  ret double %"d.1"
}

define double @"func"(double %".1") 
{
entry:
  %"addtmp" = fadd double %".1", 0x3ff0000000000000
  %"x" = alloca double
  store double %"addtmp", double* %"x"
  %".4" = load double, double* %"x"
  ret double %".4"
}

Run python3 main.py to use.

Implement

Todo List

  • Basic OP: +, -, *, /, Parentheses
  • ASSIGN OP: =
  • Control Flow
    • function
    • if
  • [] Array

tinycompiler's People

Contributors

jiefisher avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

yuping322

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.