GithubHelp home page GithubHelp logo

pauloday / compiler-demo Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 1.0 17 KB

Super simple compiler demo. Intended to be the content for a 15 minute presentation

License: MIT License

JavaScript 100.00%

compiler-demo's Introduction

Let's build a (very simple) compiler!

A compiler is just a normal program that takes a string and outputs a different string. The input string is lines of code, and the output string is different lines of code. Typically the output string is code in a lower level language (i.e. closer to machine code/pure numbers). An example of a compiler you may be using is Babel, which translates Javascript from newer to older versions. More accurately babel is transpiler since both input and output are Javascript, but that's just a type of compiler.

A quick note on interpreters

Interpreters are like compilers, but they translate lines of code one at a time on the fly. Any langauge can be either interpreted or compiled, but most languages are designed with one or the other in mind. Typically an interpreted langauge has a running program that will run your code as as its received. A very common way for this to happen is through a REPL (read-eval-print loop). An example of a REPL is the Javascript console in your web browser. Javascript is designed to be interpreted by web browsers rather than compiled. We're building a compiler, but the basic steps are the same for interpreters - they just happen at a different speed.

Steps

Compilers can get very complex, but at their core they usually have 4 basic steps:

  1. Lexing - break the input code into meaningful chunks (or "words")
  2. Parsing - convert the words into a data structure
  3. Optimization - modify the data structure so it will generate efficient output
  4. Translation - use the data structure to construct output code

Demo Language

For this demo, we'll be implementing a compiler for a very simple langauge that can only express addition. It will take an input string that consists of numbers and '+', and output a line of Lisp code. Each the integers and pluses must be separated by a single space. I chose Lisp as the output because it's my favorite langauge, but also represents math a bit differently, so there'll actually be a difference between input and output.

compiler-demo's People

Contributors

pauloday avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

risingbirdsong

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.