GithubHelp home page GithubHelp logo

ziman / ever-to-excel Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 455 KB

Compile a Scheme-like language into a spreadsheet

License: BSD 3-Clause "New" or "Revised" License

Haskell 99.77% Shell 0.23%

ever-to-excel's Introduction

ever-to-excel

Compile programs to spreadsheets.

Example

Input

The input language is called Ever. It's a restricted variant of Scheme.

(define (fac acc n)
  (if-zero
    n
    acc
    (fac (* n acc) (- n 1))))

(define (main)
  (display
    (fac 1 6)))

Bytecode

Ever compiles to instructions for a stack VM.

; -------[ main ] -------------
OP 0 (XStr "ret")
OP 0 (XInt 1)
OP 0 (XInt 6)
LOAD (Addr 3)
PUSHL (PC 8)
LOAD (Addr 2)
STORE (Addr 3)
JMP (PC 13)
LABEL (PC 8)
STORE (Addr 3)
POP 2
PRINT
HALT
; -------[ fac ] --------------
LABEL (PC 13)
OP 0 (XLoc 2)
JZ (PC 22)
OP 0 (XOp "*" (XLoc 2) (XLoc 3))
OP 0 (XOp "-" (XLoc 2) (XInt 1))
LSTORE 2
LSTORE 3
JMP (PC 13)
JMP (PC 24)
LABEL (PC 22)
OP 0 (XLoc 3)
LABEL (PC 24)
LSTORE 4
RET

Output

Some details

The input language, Ever, looks like Scheme but it's restricted even more than C.

  • You cannot have local variables (there's no let).
  • You cannot access the parent scope.
  • There is no heap, no dynamic allocation, no closures.

All of the above can be addressed; it just needs more work. :)

However:

  • You can have (mutual) recursion.
  • Tail recursion is supported (at the moment only for tail calls with the same number of arguments).
  • There are some spreadsheet-specific optimisations, e.g. compiling expressions directly to spreadsheet expressions rather than into multiple instructions, whenever possible.

More details:

  • Every row in the spreadsheet is the same. (Except for the initial row, which does not contain any formulas.)
  • We could make every cell the same but we use the static knowledge of the column number to make some optimisations.
  • The compiler runs the program in the interpreter first to determine the minimal width and height of the spreadsheet. This is not cheating; it's fine to generate more cells than necessary.

ever-to-excel's People

Contributors

ziman avatar

Stargazers

 avatar  avatar

Watchers

 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.