Skip to content

nao23/monkey

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

monkey

An interpreted language written in Go, as described in Write an interpreter in Go (Go言語でつくるインタプリタ)

Run REPL

$ git clone https://github.com/nao23/monkey
$ cd monkey
$ go run main.go

Hello nao23! This is the Monkey programming language!
Feel free to type in command
>> "HELLO"
HELLO

The Monkey language

https://monkeylang.org

Data Types

Integer

>> 1
1
>> -1
-1

Boolean

>> true
true
>> false
false

String

>> "HELLO"
HELLO
>> "HELLO" + " WORLD"
HELLO WORLD

Array

>> [1, 2, 3]
[1, 2, 3]
>> [1, 2, 3][2]
3
>> [1, "a", true][2]
true

Hash

>> {"a": 1, "b": 2, "c": 3}["b"]
2
>> {1: "x", "b": 2, true: 3}[true]
3

Operator

arithmetic operators

>> 1 + 2
3
>> -1 + 5
4
>> 3 - 1
2
>> 2 * (3 + 4)
14
>> 2 * (3 + 4) + 8 / 4
16

comparison operators

>> 1 < 2
true
>> 1 > 2
false
>> 1 == 1
true
>> 1 != 1
false

boolean operators

>> !(true)
false

Assignment

>> let a = 1
>> a
1
>> let b = 2
>> a + b
3

If-else Statement

>> if (1 < 5) { 1 }
1
>> if (1 > 5) { 1 } else { 2 }
2

Function

>> let add = fn(a, b) { a + b };
>> add(1, 2)
3
>> let sub = fn(a, b) { a - b };
>> sub(3, 1)
2

high order function:

>> let applyFunc = fn(a, b, func) { func(a, b) }
>> applyFunc(2, 2, add)
4
>> applyFunc(10, 2, sub)
8

closures:

>> let newAdder = fn(x) { fn(y) { x + y } }
>> let addTwo = newAdder(2)
>> addTwo(3)
5

Builtin functions

>> len("ABC")
3
>> len([1, 2, 3])
3
>> first([1, 2, 3])
1
>> last([1, 2, 3])
3
>> rest([1, 2, 3])
[2, 3]
>> push([1, 2], 3)
[1, 2, 3]
>> puts("HELLO")
HELLO
null

Releases

No releases published

Packages

No packages published

Languages