{"id":13398766,"url":"https://github.com/phreda4/r3d4","last_synced_at":"2025-03-14T02:31:37.801Z","repository":{"id":92168998,"uuid":"231272970","full_name":"phreda4/r3d4","owner":"phreda4","description":"r3 programing language for 64 bits Windows/Linux/Mac/Rasberry Pi 4","archived":false,"fork":false,"pushed_at":"2023-06-24T14:52:50.000Z","size":37365,"stargazers_count":52,"open_issues_count":1,"forks_count":4,"subscribers_count":9,"default_branch":"master","last_synced_at":"2024-02-17T15:34:55.815Z","etag":null,"topics":["colorforth","concatenative-programming-language","forth"],"latest_commit_sha":null,"homepage":"","language":"Forth","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/phreda4.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":"2020-01-01T22:58:28.000Z","updated_at":"2024-07-30T21:37:31.224Z","dependencies_parsed_at":null,"dependency_job_id":"28f44051-2424-4dc9-880e-1e0c9563c7b8","html_url":"https://github.com/phreda4/r3d4","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/phreda4%2Fr3d4","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phreda4%2Fr3d4/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phreda4%2Fr3d4/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phreda4%2Fr3d4/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/phreda4","download_url":"https://codeload.github.com/phreda4/r3d4/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243511660,"owners_count":20302595,"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":["colorforth","concatenative-programming-language","forth"],"created_at":"2024-07-30T19:00:31.490Z","updated_at":"2025-03-14T02:31:33.003Z","avatar_url":"https://github.com/phreda4.png","language":"Forth","readme":"# r3 programming language\n\nr3 is a concatenative language of the forth family, more precisely it takes elements of ColorForth. Word colors are encoded by a prefix: in r3 this prefix is explicit.\n\nThis repository has a windows (r3.exe) and a linux version (r3lin). There are first versions for the web (emscripten) and the Raspberry Pi.\n\nFor Linux and Raspberry Pi, remember to run \"chmod +x\" on the executable file.\n\n## History\n\nI start with r4 in 2005, in 2020 I develop r3 with 64 bits and some internal diferences, more clear vm and some ideas, breaking r4 code, in 2021 I experiment with DLL linking more concise with 64 bits and remove all SO conecction (now are DLL calls), I think it is a real usefull version, but R3d4 (the prev version) has implementationfor WIN,LIN, RPI and MAX, this version can have this SO but need make all the connetion, not for now.\n\n### 2005 :r4 first lang, \n\tlang: https://github.com/phreda4/r4   \n\tvm: https://github.com/phreda4/r4MV\n\n### 2020 r3 version r3d4 (this name is thinking for make complete enviroment), \n\tlang: https://github.com/phreda4/r3d4 \n\tvm: https://github.com/phreda4/r3vm\n\n### 2021 r3 this version\n\tlang: this repository\n\tvm: https://github.com/phreda4/r3evm\n \n## How the language works\n\nA WORD is defined as a sequence of letters separated by spaces, there are three exceptions to improve the expressiveness of the language that are seen later.\n\nEach word can be a number or is searched in the DICTIONARY.\n\nIf it is a valid number, in decimal, binary (%), hexa ($) or fixed point (0.1) its value is pushed to DATA STACK.\n\nLike all FORTH, the DATA STACK is the memory structure used to perform intermediate calculations and pass parameters between words.\n\nIf the word is NOT a valid number, then it is searched in the DICTIONARY, if it is not found, it is an ERROR, the rule is \"every word used must be defined before\".\n\nThe language has a BASIC DICTIONARY that is already defined, from which new WORDS are defined that will be used to build the program.\n\n## BASE word list:\n\nWe use `|` to indicate comment until the end of the line (the first exception to word separation).\n\nEach word can take and/or leave values of the DATA STACK, this is expressed with a state diagram of the stack before -- and after the word.\n\nfor example\n\n```\n+ | a b -- c\n```\nthe word + takes the two elements from the top of the DATA STACK, consumes them and leaves the result.\nIn addition to a modification in the stack, there may also be a lateral action, for example:\n\n```\nREDRAW | --\n```\nIt does not consume or produce values in the stack but it updates the graphic screen with the buffer in memory, this is a side effect.\n\n```\n;\t| End of Word\n(  )\t| Word block to build IF and WHILE\n[  ]\t| Word block to build nameless definitions\nEX\t| Run a word through your address\n```\n\n## Conditional, together with blocks make the control structures\n```\n0? 1?\t| Zero and non-zero conditionals\n+? -?\t| Conditional positive and negative\n\u003c? \u003e?\t| Comparison conditions\n=? \u003e=? \t| Comparison conditions\n\u003c=? \u003c\u003e?\t| Comparison conditions\nAND? NAND?\t| Logical conditioners AND and NOT AND\nBT?\t| Conditional between\n```\n\n## Words to modify the DATA STACK\n```\nDUP\t| a -- a a\nDROP\t| a --\nOVER\t| a b -- a b a\nPICK2\t| a b c -- a b c a\nPICK3\t| a b c d -- a b c d a\nPICK4\t| a b c d e -- a b c d e a\nSWAP\t| a b -- b a\nNIP\t| a b -- b\nROT\t| a b c -- b c a\n2DUP\t| a b -- a b a b\n2DROP\t| a b --\n3DROP\t| a b c --\n4DROP\t| a b c d --\n2OVER\t| a b c d -- a b c d a b\n2SWAP\t| a b c d -- c d a b\n```\n\n## Words to modify the RETURN STACK\n```\n\u003eR\t| a --\t\t; r: -- a\nR\u003e\t| -- a \t\t; r: a --\nR@\t| -- a \t\t; r: a -- a\n```\n\n## Logical operators\n```\nAND\t| a b -- c\nOR\t| a b -- c\nXOR\t| a b -- c\nNOT\t| a -- b\n```\n\n## Arithmetic Operators\n```\n+\t| a b -- c\n-\t| a b -- c\n*\t| a b -- c\n/\t| a b -- c\n\u003c\u003c\t| a b -- c\n\u003e\u003e\t| a b -- c\n\u003e\u003e\u003e\t| a b -- c\nMOD\t| a b -- c\n/MOD\t| a b -- c d\n*/\t| a b c -- d\n*\u003e\u003e\t| a b c -- d\n\u003c\u003c/\t| a b c -- d\nNEG\t| a -- b\nABS\t| a -- b\nSQRT\t| a -- b\nCLZ\t| a -- b\n```\n\n## Access to Memory\n\n`@` fetch a value from memory\n`!` store a value in memory\n\n```\n@\t| a -- [a]\nC@\t| a -- b[a]\nQ@\t| a -- q[a]\n@+\t| a -- b [a]\nC@+\t| a -- b b[a]\nQ@+\t| a -- b q[a]\n!\t| a b --\nC!\t| a b --\nQ!\t| a b --\n!+\t| a b -- c\nC!+\t| a b -- c\nQ!+\t| a b -- c\n+!\t| a b --\nC+!\t| a b --\nQ+!\t| a b --\n```\n\n## Help registers facility\n\nRegisters to traverse memory and read, copy or fill values\n\n```\n\u003eA\t| a --\nA\u003e\t| -- a\nA@\t| -- a\nA!\t| a --\nA+\t| a --\nA@+\t| -- a\nA!+\t| a --\n\u003eB\t| a --\nB\u003e      | -- a\nB@\t| -- a\nB!\t| a --\nB+      | a --\nB@+     | -- a\nB!+     | a --\n```\n\n## Copy and Memory Filling\n\nBlock memory operation, only for data memory\n\n```\nMOVE\t| dst src cnt --\nMOVE\u003e\t| dst src cnt --\nFILL\t| dst fill cnt --\nCMOVE\t| dst src cnt --\nCMOVE\u003e\t| dst src cnt --\nCFILL\t| dst fill cnt --\nQMOVE\t| dst src cnt --\nQMOVE\u003e\t| dst src cnt --\nQFILL\t| dst fill cnt --\n```\n\n## Use and Interaction with the Operating System\n\n```\nUPDATE\t| --\nREDRAW\t| --\nMEM\t| -- a\nSW\t| -- a\nSH\t| -- a\nVFRAME\t| -- a\nXYPEN\t| -- a b\nBPEN\t| -- a\nKEY\t| -- a\nCHAR\t| -- a\nMSEC\t| -- a\nTIME\t| -- a\nDATE\t| -- a\nLOAD\t| m \"filename\" -- lm\nSAVE\t| m cnt \"filename\" --\nAPPEND\t| m cnt \"filename\" --\nFFIRST\t| a -- b\nFNEXT\t| a -- b\nSYS\t| \"sys\" --\n```\n\n## Prefixes in words\n\n* `|` ignored until the end of the line, this is a comment\n* `^` the name of the file to be included is taken until the end of the line, this allows filenames with spaces.\n* `\"` the end of quotation marks is searched to delimit the content, if there is a double quotation mark `\"\"` it is taken as a quotation mark included in the string.\n* `:` define action\n* `::` define action and this definition prevails when a file is included (* exported)\n* `#` define data\n* `##` define exported data\n* `$` define hexadecimal number\n* `%` defines binary number, allows the `.` like `0`\n* `'` means the direction of a word, this address is pushed to DATA STACK, it should be clarified that the words of the BASIC DICTIONARY have NO address, but those defined by the programmer, yes.\n\nProgramming occurs when we define our own words.\nWe can define words as actions with the prefix:\n\n```\n:addmul + * ;\n```\n\nor data with the prefix #\n\n```\n#lives 3\n```\n\n`: ` only is the beginning of the program, a complete program in r3 can be the following\n\n```\n:sum3 dup dup + + ;\n\n: 2 sum3 ;\n```\n\n\n## Conditional and Repeat\n\nThe way to build conditionals and repetitions is through the words `(` and `)`\n\nfor example:\n```\n5 \u003e? ( drop 5 )\n```\n\nThe meaning of these 6 words is: check the top of the stack with 5, if it is greater, remove this value and stack a 5.\n\nThe condition produces a jump at the end of the code block if it is not met. It becomes an IF block.\n\nr3 identifies this construction when there is a conditional word before the word `(`. If this does not happen the block represents a repetition and, a conditional in that this repetition that is not an IF is used with the WHILE condition.\n\nfor example:\n```\n1 ( 10 \u003c?\n\t1 + ) drop\n```\n\naccount from 1 to 9, while the Top of stack is less 10.\n\nYou have to notice some details:\n\nThere is no IF-ELSE construction, this is one of the differences with: r4, on the other hand, ColorForth also does not allow this construction, although it seems limiting, this forces to factor the part of the code that needs this construction, or reformulate the code.\n\nIn: r4 could be constructed as follows\n\n```\n...\n1? ( notzero )( zero )\nfollow\n```\n\nIt must become:\n\n```\n:choice 1? ( nocero ; ) zero ;\n\n...\nchoice\nfollow\n```\n\nSometimes it happens that rethinking code logic avoids ELSE without the need to do this factoring. There are also tricks with bit operations that allow you to avoid conditionals completely but this no longer depends on the language.\n\nAnother feature to note that it is possible to perform a WHILE with multiple output conditions at different points, I do not know that this construction exists in another language, in fact it emerged when the way to detect the IF and WHILE was defined\n\n```\n'list ( c@+\n\t1?\n\t13 \u003c\u003e?\n\temit ) 2drop\n```\n\nDoes this repetition meet that the byte obtained is not 0 ` 1? ` and that is not 13 ` 13 \u003c\u003e? `, in any of the two conditions the WHILE ends\n\nAnother possible construction, that if it is in other FORTH, is the definition that continues in the following.\nFor example, define1 adds 3 to the top of the stack while define2 adds 2.\n\n```\n:define1 | n -- n+3\n\t1 +\n:define2 | n -- n+2\n\t2 + ;\n```\n\n## Recursion\n\nRecursion occurs naturally, when the word is defined with ` : ` it appears in the dictionary and it is possible to call it even when its definition is not closed.\n\n```\n:fibonacci | n -- f\n\t2 \u003c? ( 1 nip ; )\n\t1 - dup 1 - fibonacci swap fibonacci + ;\n```\n\n## Call Optimization\n\nWhen the last word before a `;` is a word defined by the programmer, both in the interpreter and in the compiler, the call is translated into JMP or jump and not with a CALL or call with return, this is commonly called TAIL CALL and saves a return in the chain of words called.\n\nThis feature can convert a recursion into a loop with no callback cost, the following definition has no impact on the return stack.\n\n```\n:loopback | n -- 0\n\t0? ( ; )\n\t1 -\n\tloopback ;\n```\n\n\n\n","funding_links":[],"categories":["Uncategorized"],"sub_categories":["Uncategorized"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphreda4%2Fr3d4","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphreda4%2Fr3d4","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphreda4%2Fr3d4/lists"}