{"id":15540352,"url":"https://github.com/rm-hull/byok3","last_synced_at":"2025-04-23T16:43:49.661Z","repository":{"id":145430905,"uuid":"98051048","full_name":"rm-hull/byok3","owner":"rm-hull","description":"A forth interpreter and compiler implemented in scala using typelevel/cats","archived":false,"fork":false,"pushed_at":"2023-12-02T18:16:15.000Z","size":880,"stargazers_count":4,"open_issues_count":9,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-10-03T12:18:00.471Z","etag":null,"topics":["forth","scala"],"latest_commit_sha":null,"homepage":"https://byok3.now.sh","language":"Scala","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/rm-hull.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-07-22T18:41:06.000Z","updated_at":"2023-12-02T15:04:19.000Z","dependencies_parsed_at":null,"dependency_job_id":"68488410-411a-4ca3-ae64-7452d1e65963","html_url":"https://github.com/rm-hull/byok3","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rm-hull%2Fbyok3","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rm-hull%2Fbyok3/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rm-hull%2Fbyok3/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rm-hull%2Fbyok3/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rm-hull","download_url":"https://codeload.github.com/rm-hull/byok3/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250472225,"owners_count":21436102,"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":["forth","scala"],"created_at":"2024-10-02T12:13:35.691Z","updated_at":"2025-04-23T16:43:49.645Z","avatar_url":"https://github.com/rm-hull.png","language":"Scala","funding_links":[],"categories":[],"sub_categories":[],"readme":"# BYOK3\n[![Build Status](https://travis-ci.org/rm-hull/byok3.svg?branch=master)](http://travis-ci.org/rm-hull/byok3)\n[![Coverage Status](https://coveralls.io/repos/github/rm-hull/byok3/badge.svg?branch=master)](https://coveralls.io/github/rm-hull/byok3?branch=master)\n[![Dependency Status](https://www.versioneye.com/user/projects/5973b5240fb24f0056362d48/badge.svg)](https://www.versioneye.com/user/projects/5973b5240fb24f0056362d48)\n[![Docker Pulls](https://img.shields.io/docker/pulls/richardhull/byok3.svg?maxAge=2592000)](https://hub.docker.com/r/richardhull/byok3/)\n[![Maintenance](https://img.shields.io/maintenance/yes/2020.svg?maxAge=2592000)]()\n\nBYOK (_BYE-OK_) is a [Scala](https://www.scala-lang.org/) program that implements a\n[Forth](http://lars.nocrew.org/forth2012/index.html) virtual machine. It can be run\neither as a command-line REPL, or through a browser. It will almost certainly never\nbe useful for any purpose besides that which it has already fulfilled: forcing me to\nthink quite carefully about how forth works.\n\n## Building \u0026 running\n\nDownload [SBT](http://www.scala-sbt.org/), then clone the repository and build the code:\n\n    $ git clone https://github.com/rm-hull/byok3.git\n    $ cd byok3\n    $ sbt repl/assembly\n\nThis produces the REPL as a self-contained jar file `repl/target/scala-2.12/byok3-repl.jar`\nwhich can be executed with:\n\n    $ java -jar repl/target/scala-2.13/byok3-repl.jar\n\nTo build the web service, execute:\n\n    $ sbt web/assembly\n    $ java -jar web/target/scala-2.13/byok3-web.jar\n\n## Demo\n\nA live web service is hosted on a [zeit now](https://zeit.co/now) node: https://byok3-hggauhqcjk.now.sh.\nTry the following commands:\n\n```forth\n: sqrt-closer  ( square guess -- square guess adjustment ) 2dup / over - 2 / ;\n   ok\n: sqrt ( square -- root ) 1 begin sqrt-closer dup while + repeat drop nip ;\n   ok\n```\n\nThese compiled words implement the Newton-Raphson method to finding successively better\napproximations of the root of a number, for example:\n\n```forth\n36 sqrt .\n6   ok\n\n53345 sqrt .\n231   ok\n\n231 dup * .\n53361   ok\n```\n\nWe can see the compiled code with:\n\n```forth\nsee sqrt-closer\n000010AC:  2C 00 00 00  |,...|  : SQRT-CLOSER\n000010B0:  67 00 00 00  |g...|  2DUP\n000010B4:  03 00 00 00  |....|  /\n000010B8:  5E 00 00 00  |^...|  OVER\n000010BC:  01 00 00 00  |....|  -\n000010C0:  4B 00 00 00  |K...|  (LIT)\n000010C4:  02 00 00 00  |....|  2\n000010C8:  03 00 00 00  |....|  /\n000010CC:  2D 00 00 00  |-...|  EXIT\n  ok\nsee sqrt\n000010D0:  2C 00 00 00  |,...|  : SQRT\n000010D4:  4B 00 00 00  |K...|  (LIT)\n000010D8:  01 00 00 00  |....|  1\n000010DC:  09 01 00 00  |....|  SQRT-CLOSER\n000010E0:  66 00 00 00  |f...|  DUP\n000010E4:  33 00 00 00  |3...|  0BRANCH\n000010E8:  10 00 00 00  |....|  16 (==\u003e 0x000010F8)\n000010EC:  00 00 00 00  |....|  +\n000010F0:  32 00 00 00  |2...|  BRANCH\n000010F4:  E8 FF FF FF  |....|  -24 (==\u003e 0x000010DC)\n000010F8:  5C 00 00 00  |\\...|  DROP\n000010FC:  60 00 00 00  |`...|  NIP\n00001100:  2D 00 00 00  |-...|  EXIT\n  ok\n```\n\nNext, try loading one of the included examples (**note:** this might be better\nperformed from the command line, until the web service supports streamed results):\n\n```forth\ninclude forth/examples/mandelbrot.fs\n\nEnter 'mandel' to draw the Mandelbrot Set.\n  ok\nmandel\n\n                                                                                *\n                                                                            *\n                                                                          ****\n                                                                       *********\n                                                                       *********\n                                                        *  *     *  *   *******                *\n                                                          **** ***********************     *\n                                                          ************************************\n                                                    **** ************************************\n                                                       ***************************************    *\n                                                    ***********************************************\n                                 *** ***** ***      ********************************************\n                                 ***************   ***********************************************\n                               ******************************************************************\n                         **********************************************************************\n         **  * * *  ***********************************************************************\n                          *********************************************************************\n                               ******************************************************************\n                                ****************   **********************************************\n                                 *** *********      ********************************************\n                                                    ***********************************************\n                                                       ***************************************\n                                                     *** ************************************\n                                                          ************************************\n                                                          **** ***********************     * * *\n                                                        *  *     *  *   *******   *\n                                                                       *********\n                                                                       *********\n                                                                          ****\n                                                                            *\n                                                                               *\n```\n\nAlternatively, watch a screencast:\n\n![screencast](https://raw.githubusercontent.com/rm-hull/byok3/master/screencast.svg)\n\n## Implementation Notes\n\nThe forth virtual machine is based on a stateful [Context](https://github.com/rm-hull/byok3/blob/master/src/main/scala/byok3/data_structures/Context.scala) which is composed of the following parts\n\n* Core memory address space\n* A data stack\n* A return stack\n* A dictionaty of words\n* A number of flags (compiling state, error)\n\nOrdinarily, a forth machine would most likely represent all these in terms of\nthe core memory, but this implementation does not. That may change in the future.\n\nThe context is wrapped inside [cats](https://github.com/typelevel/cats)' State\nmonad,  specifically the type signature being: `StateT[Try, Context, A]`, and\naliased as `AppState[A]`.\n\nThe `Try` part allows us to force the state to become invalidate either by using\n`inspectF`, `modifyF`, etc. or by letting the implicit `Applicatve[Try]`\nautomatically take care of thrown exceptions.\n\nThis means that all the internal words can be expressed succinctly with for-comprehensions,\nand further composed if necessary. For example:\n\n```scala\n@Documentation(\"Adds x to the single cell number at a-addr\", stackEffect = \"( x a-addr -- )\")\nval +! = for {\n  addr \u003c- dataStack(pop)\n  x \u003c- dataStack(pop)\n  data \u003c- memory(peek(addr))\n  _ \u003c- memory(poke(addr, data + x))\n} yield ()\n```\n\nThe interpreter and compiler both use an [executor](https://github.com/rm-hull/byok3/blob/master/src/main/scala/byok3/Executor.scala)\nto recursively step through the words under consideration.\n\n## TODO\n\n* ~~Move `DOES\u003e` and `(DOES\u003e)` to internal words (current implementation does not work).~~\n* Implement `LOAD` and `LIST` block commands.\n* ~~Investigate if word input can be colorized with [JLine3](https://github.com/jline/jline3).~~\n* Block editor\n* ~~Web API~~\n* Implement full 2012 spec\n* Implement forth test suite\n* Performance improvements - reframe stack in terms of core memory\n* Purely functional IO\n\n## References\n\n* http://lars.nocrew.org/forth2012/index.html\n* https://rwmj.wordpress.com/2010/08/07/jonesforth-git-repository/\n* http://galileo.phys.virginia.edu/classes/551.jvn.fall01/primer.htm\n* https://github.com/nornagon/jonesforth/blob/master/jonesforth.S\n\n## License\n\n### The MIT License (MIT)\n\nCopyright (c) 2017 Richard Hull\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of\nthis software and associated documentation files (the \"Software\"), to deal in\nthe Software without restriction, including without limitation the rights to\nuse, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\nthe Software, and to permit persons to whom the Software is furnished to do so,\nsubject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\nFOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\nCOPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\nIN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\nCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frm-hull%2Fbyok3","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frm-hull%2Fbyok3","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frm-hull%2Fbyok3/lists"}