Last active
May 11, 2018 20:45
-
-
Save pouyakary/c7fec1c1d178ca70c22ba6cdea93a77e to your computer and use it in GitHub Desktop.
The most basic rewriting system to play with
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Copyright 2018-present by Pouya Kary. Do whatever you like with | |
// it and don't sue me :D | |
// | |
// This is the most simple rewriting system that one can make. | |
// it might look silly but you can experiment all sorts of cool | |
// systems in it. Just check out the sample rules and the result | |
// you're going to get. Believe it or not it was only an accident | |
// to get that result. | |
// | |
// BTW, Do you need a source of inspirations? | |
// * https://www.youtube.com/watch?v=JbseluMTfZQ | |
// * https://www.wolframscience.com/nks/p102--symbolic-systems/ | |
// | |
// Also, the runtime has a maximum iteration variable to keep it | |
// from falling to infinity replacement rule. You can change it to | |
// whatever you want as long as you're happy with it. | |
// | |
// | |
// ─── RULES ────────────────────────────────────────────────────────────────────── | |
// | |
function get_rules ( code ) { | |
const rules = | |
code.trim( ) | |
.split("\n") | |
.map( rule => rule.split( "->" ).map( x => x.trim( ) ) ) | |
const resulting_rules = [ ] | |
for ( const rule of rules ) | |
resulting_rules.push( | |
text => text.replace( new RegExp( rule[ 0 ], "g" ), rule[ 1 ] ) | |
) | |
return resulting_rules | |
} | |
// | |
// ─── EVALUATE ─────────────────────────────────────────────────────────────────── | |
// | |
function evaluate ( rules_code, code ) { | |
const rules = get_rules( rules_code ) | |
const max_iteration = 1000 | |
let iteration = 1 | |
while ( iteration <= max_iteration ) { | |
let new_code = null | |
console.log( "--->", code ) | |
for ( const rule of rules ) | |
new_code = rule( new_code? new_code : code ) | |
if ( new_code == code ) | |
return new_code | |
else | |
code = new_code | |
} | |
} | |
// | |
// ─── MAIN ─────────────────────────────────────────────────────────────────────── | |
// | |
// Edit these rules and remember that the parser is a joke so | |
// don't have errors! | |
const rules = (` | |
-7 -> 62 | |
17 -> 72 | |
27 -> 82 | |
-8 -> 61 | |
18 -> 71 | |
28 -> 81 | |
6- -> 23 | |
61 -> 24 | |
62 -> 25 | |
4- -> -6 | |
41 -> -7 | |
42 -> -8 | |
5- -> 13 | |
51 -> 14 | |
52 -> 15 | |
-3 -> 61 | |
13 -> 71 | |
23 -> 81 | |
`) | |
// this is a text | |
// that all the rules | |
// are applied at | |
evaluate( rules, "-----------------6---------------" ) | |
// | |
// P.S: What you're about to witness is the world's smallest | |
// working and proven Turing Machine. | |
// | |
// ──────────────────────────────────────────────────────────────────────────────── | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment