Giter VIP home page Giter VIP logo

funlang's Introduction

FUN is a simple, structured, dynamic, functional and high-level programming language.

FUN's syntax is simple and clear. It contains expressions and declerations.

abstract class Exp 
abstract class BExp 
abstract class Decl

case class Var(s: String) extends Exp
case class Num(i: Int) extends Exp
case class Aop(o: String, a1: Exp, a2: Exp) extends Exp 
case class If(a: BExp, e1: Exp, e2: Exp) extends Exp
case class Write(e: Exp) extends Exp
case class Sequ(e1: Exp, e2: Exp) extends Exp
case class Call(name: String, args: List[Exp]) extends Exp

case class Bop(o: String, a1: Exp, a2: Exp) extends BExp

case class Def(name: String,
                args: List[String],
                body: Exp) extends Decl 

case class Main(e: Exp) extends Decl

The fun_llvm.sc compiler targets the LLVM Intermediate Language, or LLVM Intermediate Representation (short LLVM‐IR). LLVM-IR will also allow us to benefit from the modular structure of the LLVM compiler and let for example the compiler generate code for different CPUs, like X86 or ARM. LLVM‐IR allows compilation of multiple source languages to multiple targets. It is also the place where most of the target independent optimisations are performed.

However, what we have to do for LLVM is to generate code in Static Single-Assignment format (short SSA), because that is what the LLVM‐IR expects from us. The main idea behind the SSA format is to use very simple variable assignments where every variable is assigned only once.

Compilers have to solve the problem of bridging the gap between “high‐level” programs and “low‐level” hardware. If the gap is too wide for one step, then a good strategy is to lay a stepping stone somewhere in between. We will use our own stepping stone which I call the K‐language. K‐values are the atomic operations that can be on the right‐hand side of equal‐signs. The K‐language is restricted such that it is easy to generate the SSA format for the LLVM‐IR.

// K-language (K-expressions , K-values)
abstract class KExp
abstract class KVal

case class KVar(s: String) extends KVal
case class KNum(i: Int) extends KVal
case class Kop(o: String, v1: KVal, v2: KVal) extends KVal
case class KCall(o: String, vrs: List[KVal]) extends KVal
case class KWrite(v: KVal) extends KVal

case class KIf(x1: String, e1: KExp, e2: KExp) extends KExp
case class KLet(x: String, v: KVal, e: KExp) extends KExp
case class KReturn(v: KVal) extends KExp

The main difficulty of generating instructions in SSA format is that large compound expressions need to be broken up into smaller pieces and intermediate results need to be chained into later instructions. To do this conveniently, CPS‐translations have been developed. They use functions (“continuations”) to represent what is coming next in a sequence of instructions. Continuations are functions of type KVal to KExp. They can be seen as a sequence of KLets where there is a “hole” that needs to be filled.

Build

The 6 simple FUN programs include:

  • defs.fun
  • fact.fun
  • hanoi.fun
  • mand.fun
  • mand2.fun
  • sqr.fun

To install ammonite repl:

// Linux
$ curl -L https://github.com/lihaoyi/ammonite/releases/download/3.0.0-M2/2.13-3.0.
0-M2-bootstrap > amm && chmod +x amm

// Mac
$ brew install ammonite-repl

To run the compiler:

$ amm fun_llvm.sc run fact.fun
$ amm fun_llvm.sc run mand.fun

Sample ascii output from the Mandelbrot programs: mand.fun

mand2.fun

funlang's People

Contributors

muffpy avatar

Watchers

Lucian avatar  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.