All Projects → KennedyTedesco → monkey

KennedyTedesco / monkey

Licence: MIT license
The Monkey Programming Language & Interpreter written in PHP.

Programming Languages

PHP
23972 projects - #3 most used programming language
Monkey
6 projects

Projects that are alternatives of or similar to monkey

MonkeyLang.jl
"Writing an Interpreter in GO" and "Writing a Compiler in GO" in Julia.
Stars: ✭ 30 (+42.86%)
Mutual labels:  interpreter, lexer
pascal-interpreter
A simple interpreter for a large subset of Pascal language written for educational purposes
Stars: ✭ 21 (+0%)
Mutual labels:  interpreter, lexer
aria
Expressive, noiseless, interpreted, toy programming language
Stars: ✭ 40 (+90.48%)
Mutual labels:  interpreter, lexer
monkey-rs
An interpreter for the Monkey programming language written in Rust
Stars: ✭ 27 (+28.57%)
Mutual labels:  interpreter, monkey-programming-language
Lioness
The Lioness Programming Language
Stars: ✭ 155 (+638.1%)
Mutual labels:  interpreter, lexer
malluscript
A simple,gentle,humble scripting language for mallus, based on malayalam memes.
Stars: ✭ 112 (+433.33%)
Mutual labels:  interpreter, lexer
monkers
Bytecode compiler and VM for the Monkeylang language, written in Rust
Stars: ✭ 34 (+61.9%)
Mutual labels:  interpreter, monkey-language
fayrant-lang
Simple, interpreted, dynamically-typed programming language
Stars: ✭ 30 (+42.86%)
Mutual labels:  interpreter, lexer
Rs Monkey Lang
Monkey Programming Language written in Rust.
Stars: ✭ 80 (+280.95%)
Mutual labels:  interpreter, lexer
Mico
Mico ("Monkey" in catalan). Monkey language implementation done with C++. https://interpreterbook.com/
Stars: ✭ 19 (-9.52%)
Mutual labels:  interpreter, lexer
Own-Programming-Language-Tutorial
Репозиторий курса "Как создать свой язык программирования"
Stars: ✭ 95 (+352.38%)
Mutual labels:  interpreter, lexer
Cub
The Cub Programming Language
Stars: ✭ 198 (+842.86%)
Mutual labels:  interpreter, lexer
boba-js
Toy programming language. Now being reimplemented in Rust: https://github.com/poteto/monkers
Stars: ✭ 22 (+4.76%)
Mutual labels:  interpreter, monkey-language
Swiftpascalinterpreter
Simple Swift interpreter for the Pascal language inspired by the Let’s Build A Simple Interpreter article series.
Stars: ✭ 270 (+1185.71%)
Mutual labels:  interpreter, lexer
Monkey Rust
An interpreter for the Monkey programming language written in Rust
Stars: ✭ 174 (+728.57%)
Mutual labels:  interpreter, lexer
types-and-programming-languages
C++ Implementations of programming languages and type systems studied in "Types and Programming Languages" by Benjamin C. Pierce..
Stars: ✭ 32 (+52.38%)
Mutual labels:  interpreter, lexer
ciao
Ciao is a modern Prolog implementation that builds up from a logic-based simple kernel designed to be portable, extensible, and modular.
Stars: ✭ 190 (+804.76%)
Mutual labels:  interpreter
monkey-interpreter
Monkey programming language interpreter designed in "Writing An Interpreter In Go".
Stars: ✭ 26 (+23.81%)
Mutual labels:  interpreter
ME
A DSL for macro expansion ,in order to help framework develop
Stars: ✭ 24 (+14.29%)
Mutual labels:  interpreter
picol
(Fossil repository mirror) A tiny interpreter
Stars: ✭ 19 (-9.52%)
Mutual labels:  interpreter

Monkey Programming Language

Monkey Programming Language

tests

The Monkey Programming Language & Interpreter written in PHP.

Features:

  • A REPL;
  • Integers (10), floats (10.5), booleans (false, true), strings ("foo"), arrays ([2016, "foo"]), hash maps (TODO);
  • Arithmetic expressions (+, -, *, %, /, **);
  • Postfix operators (i++, i--)
  • Comparison operators (>, <, >=, <=, ==, !=)
  • Conditional expressions (if else);
  • Logical operators (&&, ||)
  • Loop (while);
  • Let statements (let a = 1);
  • First-class and higher-order functions (let foo = fn(x) { x + 1 });
  • Built-in functions (puts(), etc);
  • Recursion;
  • Closures;

Future scope:

  • Enums;
  • Classes and Objects;
  • Match Expression;
  • Regex support;

Basic syntax:

A Fibonacci sequence using recursion:

let fibonacci = fn(x) {
    if (x == 0 || x == 1) {
        return x;
    }

    return fibonacci(x - 1) + fibonacci(x - 2);
};

puts(fibonacci(10));

A Fibonacci sequence using loop (much faster):

let fibonacci = fn(num) {
    let a = 0;
    let b = 1;
    let temp = 0;

    while (num > 0) {
        temp = b;
        b = b + a;
        a = temp;
        num--;
    }

    return a;
};

puts(fibonacci(32));

A raw implementation of mapping an array:

let rawMap = fn(arr, callback) {
    let iter = fn(arr, accumulated) {
        if (len(arr) == 0) {
            return accumulated;
        }

        return iter(slice(arr, 1), push(accumulated, callback(first(arr))));
    };

    return iter(arr, []);
};

let foo = rawMap([1, 2, 3, 4], fn(x) { x * 2 });

puts(foo); // [2, 4, 6, 8]

Or, you can just use the builtin function map():

let foo = map([1, 2, 3, 4], fn(x) { x * 2 });

puts(foo); // [2, 4, 6, 8]

See more examples here.

(A working in progress. More features and docs soon.)

Why?

This is just a C-like language and its interpreter that I built to learn and understand how lexers and parsers work. I hope that could be useful to you, at least, inspire you to create your interpreter to learn those things.

Is it fast?

It's not so fast because this is a pure tree-walk interpreter (which is notable slow) written in a PHP (a high-level programming language), and PHP compiles to an intermediate code that runs on top of a Virtual Machine written in C. Also, speed isn't the goal. The goal here is to learn, play, and have fun with the foundation of interpreters.

How it works?

This interpreter uses an approach called Tree-Walking, it parses the source code, builds an abstract syntax tree (AST), and then evaluates this tree.

The steps are:

  1. Lexical analysis: from source code (free text) to Tokens/Lexemes;
  2. Parsing: uses the generated tokens to create an Abstract Syntax Tree;
  3. Abstract Syntax Tree (AST): a structural representation of the source code with its precedence level, associativity, etc;
  4. Evaluator: runs through the AST evaluating all expressions.

How it works

Running with Docker (PHP ^8.1)

Pull the docker image:

docker pull php:8.1-cli

Running the tests:

docker run --rm -v $(pwd):/monkey -w /monkey php:8.1-cli ./vendor/bin/pest

If you're using a fish-like shell, omit the $:

docker run --rm -v (pwd):/monkey -w /monkey php:8.1-cli ./vendor/bin/pest

Running from a file contents of the examples folder:

docker run --rm -v (pwd):/monkey -w /monkey php:8.1-cli ./monkey run examples/fibo_while.monkey

Using the REPL

Clone this repository, execute composer install, then:

docker run --rm -v (pwd):/monkey -w /monkey php:8.1-cli ./monkey repl

Example:

            __,__
   .--.  .-"     "-.  .--.
  / .. \/  .-. .-.  \/ .. \
 | |  '|  /   Y   \  |'  | |
 | \   \  \ 0 | 0 /  /   / |
  \ '- ,\.-"`` ``"-./, -' /
   `'-' /_   ^ ^   _\ '-'`
       |  \._   _./  |
       \   \ `~` /   /
        '._ '-=-' _.'
           '~---~'
-------------------------------
| Monkey Programming Language |
-------------------------------

 > let a = 20 + fn(x){ return x + 10; }(2);
32

Or, if you want to execute a file:

docker run --rm -v (pwd):/monkey -w /monkey php:8.1-cli ./monkey run examples/closure.monkey

Contributing

I'll be pleased to have you contributing to any aspect of this project. You can fix a bug, implement new functionality, or add more tests.

Credits

This language is a version of the incredible Monkey Lang with some extra batteries included.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].