{"id":13781050,"url":"https://github.com/abhinav-upadhyay/cmonkey","last_synced_at":"2026-01-17T08:58:48.091Z","repository":{"id":141152181,"uuid":"85458804","full_name":"abhinav-upadhyay/cmonkey","owner":"abhinav-upadhyay","description":"C Implementation of the monkey programming language","archived":false,"fork":false,"pushed_at":"2020-02-23T12:27:20.000Z","size":745,"stargazers_count":72,"open_issues_count":0,"forks_count":8,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-05-05T00:42:51.780Z","etag":null,"topics":["bytecode","c","cmonkey","compiler","interpreter","monkey-language","vm"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/abhinav-upadhyay.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2017-03-19T07:36:27.000Z","updated_at":"2025-04-13T08:32:46.000Z","dependencies_parsed_at":"2024-01-07T23:14:41.175Z","dependency_job_id":null,"html_url":"https://github.com/abhinav-upadhyay/cmonkey","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/abhinav-upadhyay%2Fcmonkey","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abhinav-upadhyay%2Fcmonkey/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abhinav-upadhyay%2Fcmonkey/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abhinav-upadhyay%2Fcmonkey/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/abhinav-upadhyay","download_url":"https://codeload.github.com/abhinav-upadhyay/cmonkey/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253580265,"owners_count":21930911,"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":["bytecode","c","cmonkey","compiler","interpreter","monkey-language","vm"],"created_at":"2024-08-03T18:01:22.489Z","updated_at":"2026-01-17T08:58:48.031Z","avatar_url":"https://github.com/abhinav-upadhyay.png","language":"C","funding_links":[],"categories":["C"],"sub_categories":[],"readme":"# cmonkey\nThis is an implementation of the monkey language in C.\nMonkey is a programming language created by Thorsten Ball in the book\n[Writing an interpreter in go](https://interpreterbook.com/).\n\n[**EDIT**]\nNow this also includes a bytecode compiled version of monkey based on the\nsecond book by the author [Writing a compiler in go](https://compilerbook.com).\n\n## BUILDING\nRequires GNU make to build the code.\nTested mostly on Linux and NetBSD, bug reports welcome for other\noperating systems.\n\nTo build:\n- clone or download the code from github\n- run `make`\n- Binaries are generated in bin/\n\n## TESTS\nTests are implemented in files ending with \\_tests.c. No frameworks are used to write tests. Tests\nare built with the normal build and can be executed by running each of the test programs one by one.\nFor example:\n\n`bin/parser_tests` - will execute the parser tests\n\n## Running as REPL\nexecute `bin/monkey`\n\n![demo](https://github.com/abhinav-upadhyay/cmonkey/blob/master/repl.png)\n\n**Multiline expressions in the REPL**\n\nIt's straightforward to run single line code in the REPL. For more\ncomplex code which requires multiple lines, `\\` can be used at the\nend of the line to indicate continuation so that the REPL waits for\nyou to finish the code. The last line not containing `\\` at the end\nwill be treated as the end of the code after which the evaluation will be performed.\n\n## Running monkey programs\nTo execute a monkey program saved in a file `hello_world.mnk`\n\n`bin/monkey hello_world.mnk`\n\n## Language Features\n\n### Supported data types\nMonkey supports following datatypes natively:\n- integers\n- strings\n- booleans\n- arrays\n- dictionary or hash\n- functions (functions are first class citiznes in monkey)\n\n### Variable bindings\n\n```\nlet x = 10;\nlet str = \"hello, world!\";\nlet bool = x == 10;\n```\n\n### Arithmetic operators\nMonkey supports following arithmetic operators which are similar to other programming languages:\n\n- \\+ (addition) \n- \\- (subtraction)\n- \\* (multiplication)\n- \\/ (division)\n- % (mod)\n\n### Comparision operators\nMonkey supports following comparison operators\n- == (equals)\n- != (not equals)\n- \\\u003e (greater than)\n- \u003c (less than)\n\n### Logical operators\nMonkey supports following logical operators\n- \u0026\u0026 (and)\n- || (or)\n- ! (not)\n\n### Creating string literals\n```\nlet s = \"hello world\";\n```\nWe can also do indexing on string objects, for example:\n```\n\u003e\u003e let s1 = \"hello world\";\n\u003e\u003e let c0 = s1[0]\n\u003e\u003e c0\nh\n\u003e\u003e s1[2]\nl\n```\n\n### Creating array literals\nMonkey arrays can contains objects of any type supported by monkey\n\n```\nlet add = fn(a, b) { a + b};\nlet arr = [1, \"two\", \"3\", true, false, add]\n```\n\nWe can access arrays by using positive indices from `0` to `n - ` (`n` being the length of the array)\n```\nlet first_v = arr[0]\nlet last_v = arr[len(arr) - 1]\n```\n\n### Creating monkey dictionaries\nWe can use integers, strings and boolean types as keys in dictionaries in monkey\n```\nlet d = {\"foo\": 1, \"bar\": 2, true: 3, false: 4, 1: 5};\n```\n\nValues can be accessed from dictionaries as follows:\n\n```\nlet one = d[\"foo\"];\nlet two = d[\"bar\"];\nlet three = d[true];\nlet five = d[1];\n```\n\n### Functions\n```\nlet factorial = fn(n) {\n    if (n == 0) {\n        return 1;\n    }\n    return n * factorial(n - 1);\n}\n```\n\nCalling functions\n```\n\u003e\u003e factorial(3)\n6\n```\n\n### Higher order functions\n\n```\nlet map = fn(arr, f) {\n    let iter = fn(arr, accumulated) {\n        if (len(arr) == 0) {\n            accumulated\n        } else {\n            iter(rest(arr), push(accumulated, f(first(arr))))\n        }\n    };\n    iter(arr, []);\n};\n```\n\n```\n\u003e\u003e let a = [1, 2, 3, 4];\n\u003e\u003e let double = fn(x) { x * 2 };\n\u003e\u003e map(a, double);\n[2, 4, 6, 8]\n```\n\n### if expressions\n`if` statements can produce values in monkey and thus called expressions.\n```\nlet x = 10;\nlet y = 0;\nif (x == 10) {\n    x\n} else {\n    y\n}\n```\n\nThe above would if expression would produce the value 10\n\n### While expressions\nSimilar to `if` expressions, while loops can also be used as expressions in monkey.\n```\nlet x = 10;\nwhile (x \u003c 100) {\n    let x = x * 2;\n    x;\n}\n```\n\n### Builtin functions\n\n**len**\n`len` returns the length of the object, it is supported for strings, arrays and dictionaries\n\n```\n\u003e\u003e let s = \"hello\";\n\u003e\u003e len(s)\n5\n\n\u003e\u003e let arr = [1, 2, 3];\n\u003e\u003e len(arr)\n3\n\n\u003e\u003e let dict = {\"foo\": 1, \"bar\": 2};\n\u003e\u003e len(dict)\n2\n```\n\n**first**\n\n`first` returns the first element from an array, it's not supported for other types\n\n```\n\u003e\u003e let arr = [1, 2, 3]\n\u003e\u003e first(arr)\n1\n```\n\n**last**\n\n`last` returns the last element from an array, it's not supported for other types\n```\n\u003e\u003e let arr = [1, 2, 3]\n\u003e\u003e last(arr)\n3\n```\n\n**rest**\n\n`rest` returns a new array which is a copy of the given array except its first element\n\n```\n\u003e\u003e let arr = [1, 2, 3, 4]\n\u003e\u003e rest(arr)\n[2, 3, 4]\n\n\u003e\u003e rest(arr(arr))\n[3, 4]\n```\n\n**push**\n\n`push` returns a new array by copying the old array and adds new element at its end\n\n```\n\u003e\u003e let arr = [1, 2, 3]\n\u003e\u003e push(arr, 4)\n[1, 2, 3, 4]\n```\n\n**puts**\n\n`puts` prints the value of a monkey object on stdout\n\n```\n\u003e\u003e let s= \"hello\";\n\u003e\u003e puts(s)\nhello\nnull\n\n\u003e\u003e let arr = [1, 2, 3];\n\u003e\u003e puts(arr)\n[1, 2, 3]\nnull\n```\n\n**type**\n\n`type` prints the type of the monkey object\n\n```\n\u003e\u003e let arr = [1, 2, 3]\n\u003e\u003e type(arr)\nARRAY\n\n\u003e\u003e let i = 10\n\u003e\u003e type(i)\nINTEGER\n\n\u003e\u003e let s = \"string\"\n\u003e\u003e type(s)\nSTRING\n\n\u003e\u003e let d = {\"foo\": \"bar\"};\n\u003e\u003e type(d)\nHASH\n\n\u003e\u003e type(true)\n\u003e\u003e BOOLEAN\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fabhinav-upadhyay%2Fcmonkey","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fabhinav-upadhyay%2Fcmonkey","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fabhinav-upadhyay%2Fcmonkey/lists"}