百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
M

monkey

> 编程语言
开源

支持类、LINQ、SQL、NET、HTTP、FMT、JSON 的解释器,以及实时语法高亮显示的 REPL。

386 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

支持类、LINQ、SQL、NET、HTTP、FMT、JSON 的解释器,以及实时语法高亮显示的 REPL。

Deprecation Notice

This project is no longer being maintained. All the contents have been moved to magpie.

Monkey Programming Language

Chinese version: 中文

Table of Contents

  • Monkey Programming Language
    • Summary
    • Overview
    • Installation
    • Basic use
    • Language Tour
      • Comments
      • Data Types
      • Constants(Literal)
      • Variables
      • Reserved keywords
      • Type conversion
      • qw(Quote word) keyword
      • enum keyword
      • Meta-Operators
      • Control flow
      • using statement
      • User Defined Operator
      • Integer
      • Float
      • Decimal
      • Array
      • String
      • Hash
      • Tuple
      • class
        • inheritance and polymorphism
        • operator overloading
        • property(like c#)
        • indexer
        • static members/methods/properties
        • Class Category
        • Annotations
      • Standard input/output/error
      • Error Handling of standard library
      • About defer keyword
      • Concatenation of different types
      • Comprehensions
      • grep and map
      • Function
      • Pipe Operator
      • Spawn and channel
    • Use go language modules
    • Standard module introduction
      • fmt module
      • time module
      • logger module
      • flag module(for handling of command line options)
      • json module(for json marshal & unmarshal)
      • net module
      • linq module
      • Linq for file
      • csv module
      • template module
      • sql module
    • About regular expression
    • Useful Utilities
    • Document generator
    • Syntax Highlight
    • Future Plans
    • License

Summary

Monkey is a toy language interpreter, written in Go. It has C-style syntax, and is largely inspired by Ruby, Python, Perl and c# It support the normal control flow, functional programming and object oriented programming. It also has a REPL with realtime syntax highlighter.

This is a sample program using monkey language:

…

or, to run a program:

bash
monkey path/to/file

Language Tour

Comments

Monkey support two kinds of single line comment and also block comment.

swift
// this is a single line comment
# this is another single line comment

/* This is a 
   block comment.
*/

Data Types

Monkey supports 9 basic data types: String, Int, UInt, Float, Bool, Array, Hash, Tuple and Nil

swift
s1 = "hello, 黄"       # strings are UTF-8 encoded
s2 = `hello, "world"`  # raw string
i = 10                 # int
u = 10u                # uint
f = 10.0               # float
b = true               # bool
a = [1, "2"]           # array
h = {"a": 1, "b": 2}   # hash
t = (1,2,3)            # tuple
n = nil

Constants(Literal)

In monkey, there are mainly eleven types of constants(Literals).

  • Integer
  • UInteger
  • Float
  • String
  • Regular expression
  • Array
  • Hash
  • Tuple
  • Nil
  • Boolean
  • Function
…

Variables

Variables in Monkey could start with the keyword let, or nothing with the form variable=value.

swift
let a, b, c = 1, "hello world", [1,2,3]
d = 4
e = 5
姓 = "黄"

You can also use Destructuring assignment. Note, the left-hand side must be included using the '()'.

swift
//righ-hand side is an array
let (d,e,f) = [1,5,8]
//d=1, e=5, f=8

//right-hand side is a tuple
let (g, h, i) = (10, 20, "hhf")
//g=10, h=20, i=hhf

//righ-hand side is a hash
let (j, k, l) = {"j": 50, "l": "good"}
//j=50, k=nil, l=good

Note however, if you do not use the keyword let, you could not do multiple variable assignments. Below code is not correct:

swift
//Error, multiple variable assignments must be use `let` keyword
a, b, c = 1, "hello world", [1,2,3]

Note:Starting from Monkey 5.0,when the decalared variable already exists, it's value will be overwritten:

swift
let x, y = 10, 20;
let x, y = y, x //Swap the value of x and y
printf("x=%v, y=%v\n", x, y)  //result: x=20, y=10

let also support the placeholder(_), when assigned a value, it will just ignore it.

swift
let x, _, y = 10, 20, 30
printf("x=%d, y=%d\n", x, y) //result: x=10, y=30

Reserved keywords

Keywords are predefined, reserved identifiers that have special meanings to the compiler. They cannot be used as identifiers. Below is a list of reserved keywords

  • fn
  • let
  • true false nil
  • if elsif elseif elif else
  • unless
  • return
  • include
  • and or
  • enum
  • struct # reserved, not used
  • do while for break continue where
  • grep map
  • case is in
  • try catch finally throw
  • defer
  • spawn
  • qw
  • using
  • class new property set get static default
  • interface public private protected # reserved, not used

Type conversion

You can use the builtin int(), uint(), float(), str(), array(), tuple(), hash, decimal functions for type conversion.

swift
let i = 0xa
let u = uint(i)                 // result: 10
let s = str(i)                  // result: "10"
let f = float(i)                // result: 10
let a = array(i)                // result: [10]
let t = tuple(i)                // result:(10,)
let h = hash(("key", "value"))  // result: {"key": "value}
let d = decimal("123.45634567") // result: 123.45634567

You could create a tuple from an array:

swift
let t = tuple([10, 20])   //result:(10,20)

Similarly, you could also create an array from a tuple:

swift
let arr = array((10,20))  //result:[10,20]

You could only create a hash from an array or a tuple:

swift
//create an empty hash
let h1 = hash()  //same as h1 = {}

//create a hash from an array
let h1 = hash([10, 20])     //result: {10 : 20}
let h2 = hash([10,20,30])   //result: {10 : 20, 30 : nil}

//create a hash from a tuple
let h3 = hash((10, 20))     //result: {10 : 20}
let h4 = hash((10,20,30))   //result: {10 : 20, 30 : nil}

qw(Quote word) keyword

The qw keyword is like perl's qw keyword. When you want to use a lot of quoted strings, the qw keyword can make it a lot easier for those strings.

swift
for str in qw { //allowed 'qw' pair is '{}', '<>', '()'
  println('str={str}')
}

newArr = qw(1,2,3.5) //array with string values, not number values.
fmt.printf("newArr=%v\n", newArr)

enum keyword

In mokey, you can use enum to define constants.

…

using statement

In monkey, if you have some resources you want to release/free/close, e.g. close opended file, close network connection etc, you can use the using statement just like c#.

swift
// Here we use 'using' statement, so we do not need to call infile.close().
// When finished running 'using' statement, it will automatically call infile.close().
using (infile = newFile("./file.demo", "r")) {
    if (infile == nil) {
        println("opening 'file.demo' for reading failed, error:", infile.message())
        os.exit(1)
    }

    let line;
    let num = 0
    //Read file by using extraction operator(">>")
    while (infile>>line != nil) {
        num++
        printf("%d	%s\n", num, line)
    }
}

User Defined Operator

In monkey, you are free to define some operators, but you cannot overwrite predefined operators.

Note: Not all operators could be user defined.

Below is an example for showing how to write User Defined Operators:

swift
//infix operator '=@' which accept two parameters.
fn =@(x, y) {
    return x + y * y
}

//prefix operator '=^' which accept only one parameter.
fn =^(x) {
    return -x
}

let pp = 10 =@ 5 // Use the '=@' user defined infix operator
printf("pp=%d\n", pp) // result: pp=35

let hh = =^10 // Use the '=^' prefix operator
printf("hh=%d\n", hh) // result: hh=-10
swift
fn .^(x, y) {
    arr = []
    while x 
    =X
  

  

    ++
+=
    +X
  

  

    --
-=
->
    -X
  

  

    &gt;=
&lt;&gt;
    &gt;X
  

  

    &lt;=
&lt;&lt;
    &lt;X
  

  

    !=
!~
    !X
  

  

    *=
**
    *X
  

  

    ..
..
    .X
  

  

    &amp;=
&amp;&amp;
    &amp;X
  

  

    |=
||
    |X
  

  

    ^=
    ^X
  

> In the table above, `X` could be `.=+-*/%&,|^~},!?@#$`

### Integer

In monkey, integer is treated as an object, so you could call it's methods.
Please see below examples:

```swift
x = (-1).next()
println(x) //0

x = -1.next() //equals 'x = -(1.next())
println(x) //-2

x = (-1).prev()
println(x) //-2

x = -1.prev() //equals 'x = -(1.prev())
println(x) //0

x = [i for i in 10.upto(15)]
println(x) //[10, 11, 12, 13, 14, 15]

for i in 10.downto(5) {
    print(i, "") //10 9 8 7 6 5

}
println()

if 10.isEven() {
    println("10 is even")
}

if 9.isOdd() {
    println("9 is odd")
}

Float

In monkey, float is also treated as an object, so you could call it's methods. Please see below examples:

swift
f0 = 15.20
println(f0)

f1 = 15.20.ceil()
println(f1)

f2 = 15.20.floor()
println(f2)

Decimal

In monkey, decimal is Arbitrary-precision fixed-point decimal numbers. And the code mainly based on decimal.

Please see below examples:

swift
d1 = decimal.fromString("123.45678901234567")  //create decimal from string
d2 = decimal.fromFloat(3)  //create decimal from float

//set decimal division precision.
//Note: this will affect all other code that follows
decimal.setDivisionPrecision(50)

fmt.println("123.45678901234567/3 = ", d1.div(d2))  //print d1/d2
fmt.println(d1.div(d2)) //same as above

fmt.println(decimal.fromString("123.456").trunc(2)) //truncate decimal

//convert string to decimal
d3=decimal("123.45678901234567")
fmt.println(d3)
fmt.println("123.45678901234567/3 = ", d3.div(d2))

Array

In monkey, you could use [] to initialize an empty array:

swift
emptyArr = []
emptyArr[3] = 3 //will auto expand the array
println(emptyArr)

You can create an array with the given size(or length) using below two ways:

swift
//create an array with 10 elements initialized to nil.
//Note: this only support integer literal.
let arr = []10
println(arr)

//use the builtin 'newArray' method.
let anotherArr = newArray(len(arr))
println(anotherArr) //result: [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil]

let arr1 = ["1","a5","5", "5b","4","cc", "7", "dd", "9"]
let arr2 = newArray(6, arr1, 10, 11, 12) //first parameter is the size
println(arr2) //result: ["1", "a5", "5", "5b", "4", "cc", "7", "dd", "9", 10, 11, 12]

let arr3 = newArray(20, arr1, 10, 11, 12)
println(arr3) //result : ["1", "a5", "5", "5b", "4", "cc", "7", "dd", "9", 10, 11, 12, nil, nil, nil, nil, nil, nil, nil, nil]

Array could contain any number of different data types. Note: the last comma before the closing ']' is optional.

swift
mixedArr = [1, 2.5, "Hello", ["Another", "Array"], {"Name": "HHF", "SEX": "Male"}]

You could use index to access array element.

swift
println('mixedArr[2]={mixedArr[2]}')
println(["a", "b",

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Gointerpreted-languagesinterpreterlanguageobject-oriented

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言