{"id":19496316,"url":"https://github.com/tylfin/writeingo","last_synced_at":"2025-07-30T06:12:50.290Z","repository":{"id":219649445,"uuid":"728803927","full_name":"tylfin/writeingo","owner":"tylfin","description":"Repository covering contents of \"Writing An Interpreter in Go\" and \"Writing a Compiler in Go\"","archived":false,"fork":false,"pushed_at":"2024-01-28T22:12:41.000Z","size":8780,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-25T20:43:15.611Z","etag":null,"topics":["go"],"latest_commit_sha":null,"homepage":"","language":"Go","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/tylfin.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":"2023-12-07T18:22:06.000Z","updated_at":"2024-01-28T22:08:10.000Z","dependencies_parsed_at":"2024-01-28T23:24:33.095Z","dependency_job_id":"4d53ff90-8dbd-4a9f-b403-d272a88c7823","html_url":"https://github.com/tylfin/writeingo","commit_stats":null,"previous_names":["tylfin/writeingo"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/tylfin/writeingo","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tylfin%2Fwriteingo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tylfin%2Fwriteingo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tylfin%2Fwriteingo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tylfin%2Fwriteingo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tylfin","download_url":"https://codeload.github.com/tylfin/writeingo/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tylfin%2Fwriteingo/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267820980,"owners_count":24149296,"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","status":"online","status_checked_at":"2025-07-30T02:00:09.044Z","response_time":70,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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"],"created_at":"2024-11-10T21:40:44.181Z","updated_at":"2025-07-30T06:12:50.270Z","avatar_url":"https://github.com/tylfin.png","language":"Go","readme":"# Write in Go\n\n[![Go](https://github.com/tylfin/writeingo/actions/workflows/go.yml/badge.svg)](https://github.com/tylfin/writeingo/actions/workflows/go.yml)\n\nRepository covers the contents of \"Writing An Interpreter in Go\" and \"Writing a Compiler in Go\".\n\n## REPL\n\nThe monkey language supports a REPL that can be used like so:\n\n```bash\ngo run main.go\nHello tylfin! This is the Monkey programming language!\nFeel free to type in commands\n\u003e\u003e let x = 5;\nlet x = 5;\n\u003e\u003e 5 + 4 * (2 + 4) / 5\n(5 + ((4 * (2 + 4)) / 5))\n\u003e\u003e fn(x, y) { x + y; }\nfn(x, y)(x + y)\n\u003e\u003e let x 5 2\nWoops! We ran into some monkey business here!\n parser errors:\n        expected next token to be =, got INT instead\n```\n\n## Project Structure\n\nThe repository is arranged like:\n\n```bash\n├── main.go\n├── ast\n├── code\n├── compiler\n├── evaluator\n├── lexer\n├── object\n├── parser\n├── repl\n├── token\n└── vm\n```\n\nWhere:\n\n- `main.go`: Entrypoint to the REPL\n- `ast`: Contains the definitions for the Abstract Syntax Tree (AST) nodes. These nodes represent various constructs in the Monkey language such as expressions, statements, etc.\n- `code`: Responsible for generating intermediate code or bytecode, which is a lower-level representation of the AST. This is especially relevant if the interpreter includes a bytecode interpreter or virtual machine.\n- `compiler`: Contains the logic to compile the AST into an executable form. This may involve generating bytecode for a virtual machine or performing just-in-time compilation.\n- `evaluator`: The core component that interprets the AST. This module executes the logic of the language, handling constructs like if-statements, loops, function calls, etc.\n- `lexer`: Performs lexical analysis by converting raw input text into a series of tokens. These tokens are the building blocks for the parsing process.\n- `object`: Defines the various types of objects that the Monkey language supports. This includes basic data types like integers and strings, as well as functions and their interactions.\n- `parser`: Transforms the tokens generated by the lexer into an AST. It follows the grammar rules of the Monkey language, structuring the input into a form that can be easily evaluated or compiled.\n- `repl`: Implements the Read-Eval-Print Loop. It's an interactive shell that takes user input, processes it through the interpreter, and outputs the result.\n- `token`: Defines the raw tokens that the lexer uses. These tokens represent the basic elements of the Monkey language syntax.\n- `vm`: If the interpreter includes a virtual machine, this directory contains its implementation. The VM executes bytecode generated from the Monkey language code.\n\n## More on Monkey\n\nHere is how we bind values to names in Monkey:\n\n```bash\nlet age = 1;\nlet name = \"Monkey\";\nlet result = 10 * (20 / 2);\n```\n\nBesides integers, booleans and strings, the Monkey interpreter supports arrays and hashes.\nHere’s what binding an array of integers to a name looks like:\n\n```bash\nlet myArray = [1, 2, 3, 4, 5];\nAnd here is a hash, where values are associated with keys:\nlet thorsten = {\"name\": \"Thorsten\", \"age\": 28};\nAccessing the elements in arrays and hashes is done with index expressions:\nmyArray[0] // =\u003e 1 thorsten[\"name\"] // =\u003e \"Thorsten\"\n```\n\nThe let statements can also be used to bind functions to names. Here’s a small function that adds two numbers:\n\n```bash\nlet add = fn(a, b) { return a + b; };\n```\n\nImplicit return values are also possible,\n\n```bash\nlet add = fn(a, b) { a + b; };\n```\n\nAnd calling a function is as easy as you’d expect:\n\n```bash\nadd(1, 2);\n```\n\nA more complex function, such as a fibonacci function that returns the Nth Fibonacci number,\nmight look like this:\n\n```bash\nlet fibonacci = fn(x) {\n    if (x == 0) {\n        0\n    } else {\n        if (x == 1) {\n            1\n        } else {\n            fibonacci(x - 1) + fibonacci(x - 2);\n        }\n    }\n};\n```\n\nNote the recursive calls to fibonacci itself!\n\nMonkey also supports a special type of functions, called higher order functions. These are functions that take other\nfunctions as arguments. Here is an example:\n\n```bash\nlet twice = fn(f, x) { return f(f(x)); };\nlet addTwo = fn(x) { return x + 2; };\ntwice(addTwo, 2); // =\u003e 6\n```\n\n## Benchmark\n\nFrom the final chapter of Writing a Compiler in Go, my compiled version was 3.05x as fast:\n\n```bash\n$ go build -o fibonacci ./benchmark\n$ ./fibonacci -engine=eval\nengine=eval, result=9227465, duration=12.267151708s\n$ ./fibonacci -engine=vm\nengine=vm, result=9227465, duration=4.013193583s\n```\n\n## Book References\n\n- [Writing An Interpreter in Go](https://interpreterbook.com/)\n- [Writing a Compiler in Go](https://compilerbook.com/)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftylfin%2Fwriteingo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftylfin%2Fwriteingo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftylfin%2Fwriteingo/lists"}