Git Product home page Git Product logo

monkey's Introduction

Monkey

A interpreter language implementation in Go

1 Introduction

Write an Interpreter in Go source code.

Chinese Version

Monkey interpreter language which is implemented Go language. After typing monkey in terminal, you get into the monkey programming language.

$ ./monkey
Hello $Username! This is Monkey programming language!
Feel free to type in commnd
Enter "exit()" or CTRL+C to quit command interface
>>>

2 Syntax

2.1 Definition

using let as keyword, each line ends with ;.

>>>let a = 3;
>>>let b = 1.2;
>>>a+b
4.2

2.2 Arithmetic operations

monkey supports all basic arithmetic operation of int and float types. int type is represented by int64 and float type is represented by float64.

>>> let a = 3;
>>> let b = 1.2;
>>> a + b
4.2
>>> a - b
1.8
>>> a * b 
3.6
>>> a / b 
2.5

2.3 Builtin containers

monkey contains two builtin containers: array and map.

  • array

array is a list which organizes items by linear sequence. But types of items can be different from each other.

>>> let a = [1, 2.3, "array"];
>>> a 
[1, 2.3, array]
>>> let b = push(a, "another");
>>> b 
[1, 2.3, array, another]
  • map

map is treated as key-value container. please attention to that only boolean, int and string types can be used as key.

>>> let a = {"name":"moneky", true:1, 7:"sevent"};
>>> a
{name: monkey, true: 1, 7: seven}
>>> a["name"]
monkey
>>> a[true]
1
>> a[7]
seven
>>>let b = set(a, 8, "eight");
>>> b 
{name: moneky, true: 1, 7: sevent, 8: eight}

2.4 Builtin functions

  • len

yield the length of builtin containers.

  • first

yield the first element of array.

  • last

yield the last element of array.

  • rest

yield an array which excludes the first element.

  • push

push an elements into the array.

  • set

insert key value pair into the map

  • puts

print literal value of objects.

2.5 Function

monkey use fn as the definition of function. Apart from regular function using, monkey also includes high order function.

>>>let add = fn(a, b) { return a + b;};
>>> add(1,2)
3
>>>let addTwo = fn(a,b, f) { return 2 + f(a, b);};
>>>addTwo(1,2, add)
5

2.5 If-else statements

monkey supports if-else statements.

>>> let max = fn(a, b) { if (a > b) { return a;} else { return b; } };
>>> max(1, 2)
2

2.6 For-loop statements

monkey support for-loop statement.

>>> let sum = fn(x) { let i = 1; let sum = 0; for (i < x) { let sum = sum + i; let i = i+1; } return sum; };
>>> sum(100)
4950

3 Extensions

To make monkey interpreter language to be more powerfull, it is deserved improving it.

  • float type
  • unicode literal
  • loop branch
  • translate into Chinese
  • ...

monkey's People

Contributors

gaufung avatar

Stargazers

 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.