Code Monkey home page Code Monkey logo

monkey-in-java's Introduction

Monkey language in Java

Build Status

Monkey is very simple dynamic language. It supports only three different statement types, but you can do a lot with it!

Just take a look at example below.

Example program

We are going to evaluate n-th Fibonacci number. To do this, we will use recursive approach. It's not efficient at all, but it show that we can call function's from themself.

let fib = fn(n) {
  if (n == 0) { 1 }
  else {
    if (n == 1) { 2 }
    else { fib(n - 1) + fib(n - 2) }
  }
};

fib(6);

You can find this and more examples in examples directory. To run this code type monkey examples/fib.mon.

Functions = first class citizens

As you understand from the title functions are first class citizens in Monkey language. It's super powerful feature, that can be used to implement many different design patterns, closures, and more.

let add = fn(a, b) { a + b; };
let sub = fn() { return fn(a, b) { a - b }; }();
let applyFunc = fn(a, b, func) { func(a, b); };

applyFunc(2, 3, add)
  + applyFunc(3, 10, sub)
  + applyFunc(5, 10, fn(a, b) { a * b });

To run this code type monkey examples/funcs.mon. We are creating function applyFunc that takes function as a third parameter, and function that returns function. You can also see function-literal that is called at the time of definition.

Having function as first class citizens makes creating closures super easy. E.g.

let superSecretCode = 1234;
let encrypt = fn(x) { x + superSecretCode; };

let superSecretCode = 4321;

encrypt(17); // having superSecretCode equals 1234

Language specification

Here will be formal language specification in EBNF.

<program>               ::= <statement-list>
<statement-list>        :: { <statement> }
<statement>             ::= <let-statement>
                          | <return-statement>
                          | <expression-statement>
<let-statement>         ::= "let" <identifier> "=" <expression> ";"
<return-statement>      ::= "return" <expression> ";"
<expression-statement>  ::= <expression> [ ";" ]


<expression>            ::= <relational-expression>
<relational-expression> ::= <relational-expression> "==" <relation>
                          | <relational-expression> "!=" <relation>
                          | <relation>
<relation>              ::= <relation> ">" <arithmetic-expression>
                          | <relation> "<" <arithmetic-expression>
                          | <arithmetic-expression>
<arithmetic-expression> ::= <arithmetic-expression> "+" <term>
                          | <arithmetic-expression> "-" <term>
                          | <term>
<term>                  ::= <term> "*" <factor>
                          | <term> "/" <factor>
                          | <factor>
<factor>                ::= "-" <primary>
                          | "!" <primary>
                          | <primary>
<primary>               ::= "(" <expression> ")"
                          | <function-call>
                          | <identifier>
                          | <int>
                          | <bool>


<function-call>         ::= <function> "(" <argument-list> ")"
<function>              ::= <function-literal>
                          | <identifier>
<function-literal>      ::= "fn (" <parameter-list> ")" <block-statement>
<block-statement>       ::= "{" <statement-list> "}"
<argument-list>         ::= { <expression> "," }
<parameter-list>        ::= { <identifier> "," }

<int>                   ::= <digit> { <digit> }
<digit>                 ::= "0..9"
<alpha>                 ::= "a..zA..Z"
<bool>                  ::= "true"
                          | "false"

How to use

Project developed using Bazel build system. All examples below are going to use it.

To start REPL just type bazel run //java/monkey

To run tests type bazel test //javatests/monkey/...

Dependencies

These are libraries used to build Monkey language. If you are using prebuilt JAR archieves, you don't need them to run Monkey program via interpreter or REPL.

License

MIT

monkey-in-java's People

Contributors

lionell avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

irwin1985

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.