Git Product home page Git Product logo

monke-go's Introduction

Monke-go ๐Ÿ’

Learning how interpreters work going through Thorsten Ball - Writing an Interpreter in Go.

Features

List of language features:

  • C-like syntax
  • variable bindings
  • integers and booleans
  • arithmetic expressions
  • built-in functions
  • first-class and higher-order functions
  • closures
  • recursion
  • a string data structure
  • an array data structure
  • a hash data structure

Types:

  • integer
  • boolean
  • string
  • array
  • hash
  • function

Binding example:

let age = 1;
let name = "Monke";
let result = 10 * (20/2);
let arr = [1, 2, 3];
let dev = {"name": "Camilo", "age": 30}
let adder = fn(a, b) {a + b};

Accessing elements:

let v = arr[0];
dev["name"]
adder(3, 5);

Recursion:

let fibonacci = fn(x) {
  if (x == 0) {
    0
  } else {
    if (x == 1) {
      1
    } else {
      fibonacci(x - 1) + fibonacci(x - 2);
    }
  }
};

HOF:

let twice = fn(f, x) {
  return f(f(x));
};

let addTwo = fn(x) {
  return x + 2;
};

twice(addTwo, 2);

Monke Elements

  • Lexer: called "lexical analysis" or "lexing", source code => tokens. It may also attach filename, column and line numbers.
  • Parser: tokens => Abstract Syntax Tree
  • Abstract Syntax Tree (AST)
  • Internal object system
  • Evaluator

Source code:

lex x = 5 + 5;

Tokens:

[
  LET,
  IDENTIFIER("x"),
  EQUAL_SIGN,
  INTEGER(5),
  PLUS_SIGN,
  INTEGER(5),
  SEMICOLON
]

1st Iteration - Lexer

Starting with a small language subset, enough to parse the following program:

let five = 5;
let ten = 10;

let add = fn(x, y) {
  x + y;
};

let result = add(five, ten);

The tokens:

[
  LET,
  FUNCTION,
  EQUAL,
  COMMA,
  SEMICOLON,
  PLUS,
  LBRACE,
  RBRACE,
  LPAREN,
  RPAREN,
  INT(5),
  INT(10),
  IDENT("add"),
  IDENT("five"),
  IDENT("result"),
  IDENT("ten"),
  IDENT("x"),
  IDENT("y"),
]

Then, the lexer should be able to identify single (+, {, etc.), double (== or !=) or multiple (let, fn, return) characters and return a Token.

Result:

lexer working

Improvements

  • TokenType could be replaced by int or byte to increase performance and decrease memory usage
  • Add filename and line number to Token to increase error handling and debuggability
  • Use io.Reader to lexer
  • Support for UNICODE characters and emojis
  • Support for floats, hexa, octal and binary numbers

monke-go's People

Watchers

 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.