GithubHelp home page GithubHelp logo

wadackel / rs-monkey-lang Goto Github PK

View Code? Open in Web Editor NEW
274.0 4.0 19.0 554 KB

Monkey Programming Language written in Rust.

Home Page: https://wadackel.github.io/rs-monkey-lang/

License: MIT License

Rust 82.08% Makefile 0.38% JavaScript 9.86% CSS 6.08% HTML 1.59%
rust monkey interpreter lexer parser

rs-monkey-lang's Introduction

rs-monkey-lang

CircleCI

Monkey Programming Language written in Rust.

What's Monkey?

The Monkey Programming Language

The official Monkey logo

Monkey is a programming language designed to learn interpreters.
It's came from Writing An Interpreter In Go.

Try Monkey!

with REPL

REPL Screenshot

$ git clone https://github.com/wadackel/rs-monkey-lang.git
$ make repl

with Online Playground

Playground Screenshot

Working with Wasm!! Awesome ๐Ÿถ
https://wadackel.github.io/rs-monkey-lang/

Documentation

I created the document with reference to Writing An Interpreter In Go.

โš ๏ธ Please note that there may be some mistakes.

Table of Contents

Summary

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

Syntax overview

An example of Fibonacci function.

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

fibonacci(10);

If

It supports the general if. else exists, but else if does not exist.

if (true) {
  10;
} else {
  5;
}

Operators

It supports the general operations.

1 + 2 + (3 * 4) - (10 / 5);
!true;
!false;
+10;
-5;
"Hello" + " " + "World";

Return

It returns the value immediately. No further processing will be executed.

if (true) {
  return;
}
let identity = fn(x) {
  return x;
};

identity("Monkey");

Variable bindings

Variable bindings, such as those supported by many programming languages, are implemented. Variables can be defined using the let keyword.

Format:

let <identifier> = <expression>;

Example:

let x = 0;
let y = 10;
let foobar = add(5, 5);
let alias = foobar;
let identity = fn(x) { x };

Literals

Five types of literals are implemented.

Integer

Integer represents an integer value. Floating point numbers can not be handled.

Format:

[-+]?[1-9][0-9]*;

Example:

10;
1234;

Boolean

Boolean represents a general boolean types.

Format:

true | false;

Example:

true;
false;

let truthy = !false;
let falsy = !true;

String

String represents a string. Only double quotes can be used.

Format:

"<value>";

Example:

"Monkey Programming Language";
"Hello" + " " + "World";

Array

Array represents an ordered contiguous element. Each element can contain different data types.

Format:

[<expression>, <expression>, ...];

Example:

[1, 2, 3 + 3, fn(x) { x }, add(2, 2), true];
let arr = [1, true, fn(x) { x }];

arr[0];
arr[1];
arr[2](10);
arr[1 + 1](10);

Hashes

Hash expresses data associating keys with values.

Format:

{ <expression>: <expression>, <expression>: <expression>, ... };

Example:

let hash = {
  "name": "Jimmy",
  "age": 72,
  true: "a boolean",
  99: "an integer"
};

hash["name"];
hash["a" + "ge"];
hash[true];
hash[99];
hash[100 - 1];

Function

Function supports functions like those supported by other programming languages.

Format:

fn (<parameter one>, <parameter two>, ...) { <block statement> };

Example:

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

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

add(10, 20);

If return does not exist, it returns the result of the last evaluated expression.

let addThree = fn(x) { x + 3 };
let callTwoTimes = fn(x, f) { f(f(x)) };

callTwoTimes(3, addThree);

Passing around functions, higher-order functions and closures will also work.

Built-in Functions

You can use 6 built-in functions ๐Ÿš€

puts(<arg1>, <arg2>, ...): void

It outputs the specified value to stdout. In the case of Playground, it is output to console.

puts("Hello");
puts("World!");

len(<arg>): Intger

For String, it returns the number of characters. If it's Array, it returns the number of elements.

len("Monkey");
len([0, 1, 2]);

first(<arg>): any

Returns the element at the beginning of Array.

first([0, 1, 2]);

last(<arg>): any

Returns the element at the last of Array.

last([0, 1, 2]);

rest(<arg>): Array

Returns a new Array with the first element removed.

rest([0, 1, 2]);

push(<arg1>, <arg2>): Array

Returns a new Array with the element specified at the end added.

push([0, 1], 2);

Enjoy Monkey ๐Ÿต !


License

MIT ยฉ wadackel

rs-monkey-lang's People

Contributors

wadackel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

rs-monkey-lang's Issues

Cascading returns are not collapsed

Add the following test cases

("return if (true) { return 10; };", Some(Object::Int(10))),
("return if (true) { return if (true) { return 10; }; };", Some(Object::Int(10))),
("return if (true) { return if (false) { return 10; } else { return 5; }; };", Some(Object::Int(5))),

to the test_return_stmt function in src/evaluator/mod.rs to see what I mean.

For e.g. the tests fail as follows:

left: `Some(Int(10))`,
right: `Some(ReturnValue(Int(10)))`

left: `Some(Int(10))`,
right: `Some(ReturnValue(ReturnValue(Int(10))))`

left: `Some(Int(5))`,
right: `Some(ReturnValue(ReturnValue(Int(5))))`

Three possible solutions:

  1. Implement Eq for Object such that ReturnValue(ReturnValue(Int(10)) would be equal to Int(10) etc. For e.g. that approach taken here.
  2. Add a smart constructor specifically for ReturnValue, say makeReturnValue v, such that if v is already a ReturnValue we simply return v otherwise we return ReturnValue v.
  3. Collapse the ReturnValue when the final output from the evaluator is a ReturnValue.

Note: If these examples are tried in the online interpreter they will display the correct result because of how Display is implemented for Object. Hence, it hides the issue.

Using multiple operators causes failure

I noticed this in the Online Playground.

When you enter let a = 1 ++ 1 it spits out the expected error... "Unexpected Token: no prefix parse function for "Plus" found".

But if you enter more operators... let a = 1 ++++ 1, it outputs nothing. It's the same for other operators like slash and multiply.

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.