{"id":20449575,"url":"https://github.com/skatsuta/monkey-interpreter","last_synced_at":"2025-04-13T02:03:58.404Z","repository":{"id":57503481,"uuid":"78876102","full_name":"skatsuta/monkey-interpreter","owner":"skatsuta","description":"Monkey programming language interpreter designed in \"Writing An Interpreter In Go\".","archived":false,"fork":false,"pushed_at":"2019-04-08T19:02:00.000Z","size":89,"stargazers_count":36,"open_issues_count":0,"forks_count":7,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-06-20T01:55:20.181Z","etag":null,"topics":["go","interpreter","monkey"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/skatsuta.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-01-13T18:40:55.000Z","updated_at":"2024-06-09T22:39:45.000Z","dependencies_parsed_at":"2022-09-13T08:31:05.304Z","dependency_job_id":null,"html_url":"https://github.com/skatsuta/monkey-interpreter","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skatsuta%2Fmonkey-interpreter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skatsuta%2Fmonkey-interpreter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skatsuta%2Fmonkey-interpreter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skatsuta%2Fmonkey-interpreter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/skatsuta","download_url":"https://codeload.github.com/skatsuta/monkey-interpreter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224768986,"owners_count":17366856,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["go","interpreter","monkey"],"created_at":"2024-11-15T10:42:38.662Z","updated_at":"2024-11-15T10:42:39.314Z","avatar_url":"https://github.com/skatsuta.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Monkey interpreter\n\n[![wercker status](https://app.wercker.com/status/20b05c4eb17fc957ff322da01bb157fc/s/master \"wercker status\")](https://app.wercker.com/project/byKey/20b05c4eb17fc957ff322da01bb157fc)\n[![Go Report Card](https://goreportcard.com/badge/github.com/skatsuta/monkey-interpreter)](https://goreportcard.com/report/github.com/skatsuta/monkey-interpreter)\n[![GoDoc](https://godoc.org/github.com/skatsuta/monkey-interpreter?status.svg)](https://godoc.org/github.com/skatsuta/monkey-interpreter)\n\n\nMonkey programming language interpreter designed in [_Writing An Interpreter In Go_](https://interpreterbook.com/).\n\n\n## Usage\n\nInstall the Monkey interpreter using `go get`:\n\n```sh\n$ go get -v -u github.com/skatsuta/monkey-interpreter/...\n```\n\nThen run REPL:\n\n```sh\n$ $GOPATH/bin/monkey-interpreter\nThis is the Monkey programming language!\nFeel free to type in commands\n\u003e\u003e \n```\n\nOr run a Monkey script file (for example `script.monkey` file):\n\n```sh\n$ $GOPATH/bin/monkey-interpreter script.monkey\n```\n\n## Getting started with Monkey\n\n### Variable bindings and number types\n\nYou can define variables using `let` keyword. Supported number types are integers and floating-point numbers.\n\n```sh\n\u003e\u003e let a = 1;\n\u003e\u003e a\n1\n\u003e\u003e let b = 0.5;\n\u003e\u003e b\n0.5\n```\n\n### Arithmetic expressions\n\nYou can do usual arithmetic operations against numbers, such as `+`, `-`, `*` and `/`. \n\n```sh\n\u003e\u003e let a = 10;\n\u003e\u003e let b = a * 2;\n\u003e\u003e (a + b) / 2 - 3;\n12\n\u003e\u003e let c = 2.5;\n\u003e\u003e b + c\n22.5\n```\n\n### If expressions\n\nYou can use `if` and `else` keywords for conditional expressions. The last value in an executed block are returned from the expression.\n\n```sh\n\u003e\u003e let a = 10;\n\u003e\u003e let b = a * 2;\n\u003e\u003e let c = if (b \u003e a) { 99 } else { 100 };\n\u003e\u003e c\n99\n```\n\n### Functions and closures\n\nYou can define functions using `fn` keyword. All functions are closures in Monkey and you must use `let` along with `fn` to bind a closure to a variable. Closures enclose an environment where they are defined, and are evaluated in *the* environment when called. The last value in an executed function body are returned as a return value.\n\n```sh\n\u003e\u003e let multiply = fn(x, y) { x * y };\n\u003e\u003e multiply(50 / 2, 1 * 2)\n50\n\u003e\u003e fn(x) { x + 10 }(10)\n20\n\u003e\u003e let newAdder = fn(x) { fn(y) { x + y }; };\n\u003e\u003e let addTwo = newAdder(2);\n\u003e\u003e addTwo(3);\n5\n\u003e\u003e let sub = fn(a, b) { a - b };\n\u003e\u003e let applyFunc = fn(a, b, func) { func(a, b) };\n\u003e\u003e applyFunc(10, 2, sub);\n8\n```\n\n### Strings\n\nYou can build strings using a pair of double quotes `\"\"`. Strings are immutable values just like numbers. You can concatenate strings with `+` operator.\n\n```sh\n\u003e\u003e let makeGreeter = fn(greeting) { fn(name) { greeting + \" \" + name + \"!\" } };\n\u003e\u003e let hello = makeGreeter(\"Hello\");\n\u003e\u003e hello(\"John\");\nHello John!\n```\n\n### Arrays\n\nYou can build arrays using square brackets `[]`. Arrays can contain any type of values, such as integers, strings, even arrays and functions (closures). To get an element at an index from an array, use `array[index]` syntax.\n\n```sh\n\u003e\u003e let myArray = [\"Thorsten\", \"Ball\", 28, fn(x) { x * x }];\n\u003e\u003e myArray[0]\nThorsten\n\u003e\u003e myArray[4 - 2]\n28\n\u003e\u003e myArray[3](2);\n4\n```\n\n### Hash tables\n\nYou can build hash tables using curly brackets `{}`. Hash literals are `{key1: value1, key2: value2, ...}`. You can use numbers, strings and booleans as keys, and any type of objects as values. To get a value of a key from a hash table, use `hash[key]` syntax.\n\n```sh\n\u003e\u003e let myHash = {\"name\": \"Jimmy\", \"age\": 72, true: \"yes, a boolean\", 99: \"correct, an integer\"};\n\u003e\u003e myHash[\"name\"]\nJimmy\n\u003e\u003e myHash[\"age\"]\n72\n\u003e\u003e myHash[true]\nyes, a boolean\n\u003e\u003e myHash[99]\ncorrect, an integer\n```\n\n### Built-in functions\n\nThere are many built-in functions in Monkey, for example `len()`, `first()` and `last()`. Special function, `quote`, returns an unevaluated code block (think it as an AST). Opposite function to `quote`, `unquote`, evaluates code inside `quote`.\n\n```sh\n\u003e\u003e len(\"hello\");\n5\n\u003e\u003e len(\"∑\");\n3\n\u003e\u003e let myArray = [\"one\", \"two\", \"three\"];\n\u003e\u003e len(myArray)\n3\n\u003e\u003e first(myArray)\none\n\u003e\u003e rest(myArray)\n[two, three]\n\u003e\u003e last(myArray)\nthree\n\u003e\u003e push(myArray, \"four\")\n[one, two, three, four]\n\u003e\u003e puts(\"Hello World\")\nHello World\nnil\n\u003e\u003e quote(2 + 2)\nQuote((2 + 2)) # Unevaluated code\n\u003e\u003e quote(unquote(1 + 2))\nQuote(3)\n```\n\n### Macros\n\nYou can define macros using `macro` keyword. Note that macro definitions must return `Quote` objects generated from `quote` function.\n\n```sh\n# Define `unless` macro which does the opposite to `if`\n\u003e\u003e let unless = macro(condition, consequence, alternative) {\n     quote(\n       if (!(unquote(condition))) {\n         unquote(consequence);\n       } else {\n         unquote(alternative);\n       }\n     );\n   };\n\u003e\u003e unless(10 \u003e 5, puts(\"not greater\"), puts(\"greater\"));\ngreater\nnil\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fskatsuta%2Fmonkey-interpreter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fskatsuta%2Fmonkey-interpreter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fskatsuta%2Fmonkey-interpreter/lists"}