Code Monkey home page Code Monkey logo

monkeycompiler's Introduction

Monkey lang Compiler

follow the design from the book Writing an interpreter in Go

let a = 1;
let b = "text";
let c = 3 * (2 + 1);
let arr = [ 1, 2, 3 ];
let obj = { w: 1, x: 2 };
arr[0] // = 1
obj["w"] = 1;
let add = func(x, y) { x + y; };
add(1, 2);

let twice = func(f, y) {
    reutrn f(f(x));
}

KEYWORD, IDENTIFIER, NUMBER, STRING, OPERATOR

IDENTIFIER: must start with [A-Za-z_] or [0-9] STRINGLITERAL: start with " and end with "

let x = 5;
let y = 10;
let foo = add(1,2);
let bar = 1 + (2 + 3) * 4 + add(2, 3)
let foobar = func(x) { x + 3; }

Parsing let statement

let <identifier> = <expression>;

Parsing return statement

return <expression>;

Parsing expression

Example of expression type

-1
!true
-5 - 3
2 + 3 * 4 - 5
5 * (5 + 5)
add(2,3)
add(2, add(3, add(4, 5)))
foo * bar + foo
add(2, foo) + bar - 6
fn() {return 3}()
x = if (2 > 3) return 4
x

Prefix, infix & postfix operator

--3   // prefix
3++   // postfix
2 + 3 // infix

Prefix operator

-5
!foo
6 + -3
<prefix op><expression>

Infix operator

<expression><infix op><expression>

Expression produces value, Statement doesn't.

let x = 3;
        <statement>
        /         \
  <Identifier> = <Expression>

Simple recursive descent parsing

pseudo-code for number [0-9] and operator + - * / only

3 + 2 * 2 * 1 * 6 - 5
const parseNumber = (str) => number;
const parseProductAndDivide = function() {
    let num1 = parseNumber()
    while (currPtr == '*' || currPtr == '/') {
        const num2 = parseNumber()
        if (currPtr == '*') {
            num1 *= num2
        } else {
            num1 /= num2
        }
    }
    return num1
}
const parsePlusAndMinus = function() {
    let num1 = parseProductAndDivide()
    
    while (currPtr == '+') {
        const num2 = parseNumber()
        num1 += num2
    }   
    return num1 
}

const evaluate = () => {
    return parsePlusAndMinus()
}

monkeycompiler's People

Contributors

changhengliou 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.