Skip to content

Commit

Permalink
*
Browse files Browse the repository at this point in the history
  • Loading branch information
neauoire committed Dec 10, 2024
1 parent 1bf4913 commit c9d3bf1
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 46 deletions.
10 changes: 4 additions & 6 deletions etc/fractran.c.txt
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ parse_symbol(char **dst, char *s)
static char *
parse_rule(char *_s)
{
char c, *sym, *lhs;
char c, *sym, *lhs = NULL;
int side = 0;
Rule *r = _rules++;
r->num = r->den = 1;
Expand All @@ -243,7 +243,7 @@ parse_rule(char *_s)
r->den *= prime_lut[find_symbol(sym) - syms];
}
}
if(side) {
if(side && r->den > 1) {
create(&r->fnum, r->num), create(&r->fden, r->den);
fprintf(stderr, ":: %lu/%lu ", r->num, r->den), print_frag(stderr, &r->fden, 1);
fprintf(stderr, "> "), print_frag(stderr, &r->fnum, 1), fprintf(stderr, "\n");
Expand All @@ -258,9 +258,7 @@ tokenize(char *buf)
char *_buf = buf, *sym;
while(*_buf) {
while(*_buf && *_buf < 0x21) _buf++;
if(*_buf == '(')
while(*(_buf++) != ')');
else if(_buf[0] == ':' && _buf[1] == ':')
if(_buf[0] == ':' && _buf[1] == ':')
_buf = parse_rule(_buf + 2);
else if(*_buf) {
_buf = parse_symbol(&sym, _buf);
Expand Down Expand Up @@ -297,7 +295,7 @@ main(int argc, char *argv[])
FILE *f;
int i, a = 1, repl = 0;
char c, src[SRC_SZ], *_src = src;
if(argc < 2) return !printf("Fractran Rewriting, 30 Sep 2024.\nusage: fractran [-i] input.fra [arguments..]\n");
if(argc < 2) return !printf("Fractran Rewriting, 9 Dec 2024.\nusage: fractran [-i] input.fra [arguments..]\n");
if(argv[a][0] == '-' && argv[a][1] == 'i') repl = 1, a++;
if(!(f = fopen(argv[a], "r"))) return !printf("Source missing: %s\n", argv[a]);
if(!fread(&src, 1, SRC_SZ, f)) return !printf("Source empty: %s\n", argv[a]);
Expand Down
2 changes: 1 addition & 1 deletion etc/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
af16 <a href='nasu.tal.txt'>nasu.tal.txt</a>
16b5 <a href='wireworld.tal.txt'>wireworld.tal.txt</a>
2ca3 <a href='uxnasm.c.txt'>uxnasm.c.txt</a>
1a69 <a href='fractran.c.txt'>fractran.c.txt</a>
1a49 <a href='fractran.c.txt'>fractran.c.txt</a>
0dde <a href='bifurcan.tal.txt'>bifurcan.tal.txt</a>
5706 <a href='left.c.txt'>left.c.txt</a>
cf92 <a href='m291.tal.txt'>m291.tal.txt</a>
Expand Down
27 changes: 14 additions & 13 deletions site/fractran.html
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ <h3>Programming In Fractran</h3>
<p>In a rule definition, which is a fraction where prime factorization is written as names, we find names to the left-side of the spacer(>) to be rewritten by names found on the right-side. Each new name is added to the dictionary and represented internally as a prime number.</p>

<pre>
<i>:: > A rule with no left-side is a comment.</i>
:: flour sugar apples > apple-cake
:: apples oranges cherries > fruit-salad
:: fruit-salad apple-cake > fruit-cake
Expand Down Expand Up @@ -380,24 +381,24 @@ <h3 id='fib'>Example: Fibonacci</h3>
<p>Let's have a look at a <i>real program</i> to generate the Fibonacci Sequence(1, 1, 2, 3, 5, 8, 13, 21, 34..). This program uses catalysts(fib, fib.shift, fib.move) to keep the program state which has 3 phases(shift, move and back to fib) and ensures the correct evaluation order:</p>

<pre>
<i>(Shift the scrolling window to show two numbers)</i>
<i>:: > Shift the scrolling window to show two numbers</i>

:: fib n last > fib n B
:: fib n res > fib n A B
:: fib n > fibrec

<i>(Move the temporary registers back by one number)</i>
<i>:: > Move the temporary registers back by one number</i>

:: fibrec A > fibrec last
:: fibrec B > fibrec res
:: fibrec > fib

<i>(Cleanup temporary registers at the end of the program)</i>
<i>:: > Cleanup temporary registers at the end of the program</i>

:: last >
:: fib >

<i>(Find the fib number equal to the value in n register)</i>
<i>:: > Find the fib number equal to the value in n register</i>

AC 6240, n^5 last res fib
..
Expand All @@ -409,17 +410,17 @@ <h3>Example: Tic-Tac-Toe</h3>
<p>Fractran's <b>output</b> capability is limited to the resulting accumulator at the end of an evaluation. The advantage of symbolic rewriting is that registers are already assigned names, so we shall print those instead. As for <b>input</b>, we can type in new symbol tokens and appending their value to the accumulator between evaluations. We can implement a <i>tic-tac-toe</i> in a mere 16 rules:</p>

<pre>
<i>(Reserve the first registers for the player moves)</i>
<i>:: > Reserve the first registers for the player moves</i>

:: x#a o#a x#b o#b x#c o#c
:: x#d o#d x#e o#e x#f o#f
:: x#g o#g x#h o#h x#i o#i

<i>(This register remains active until the game ends)</i>
<i>:: > This register remains active until the game ends</i>

game

<i>(A symbol to draw the value of registers in a grid)</i>
<i>:: > A symbol to draw the value of registers in a grid</i>

"

Expand All @@ -431,7 +432,7 @@ <h3>Example: Tic-Tac-Toe</h3>

"

<i>(Rules for each possible victory states)</i>
<i>:: > Rules for each possible victory states</i>

:: game x#a x#b x#c > x#a x#b x#c "Player X wins!"
:: game o#a o#b o#c > o#a o#b o#c "Player O wins!"
Expand Down Expand Up @@ -470,27 +471,27 @@ <h3>Example: Fizzbuzz</h3>
<p>Alternatively to getting the resulting program state at the end of an evaluation, we can emit the accumulator at a specific rate during the evaluation by checking if a register is active or not.</p>

<pre>
<i>(Reserve the first registers for the increments and base-10)</i>
<i>:: > Reserve the first registers for the increments and base-10</i>

:: +5 +3
:: 1# 2# 3# 4# 5# 6# 7# 8# 9# 0
:: 1 2 3 4 5 6 7 8 9

<i>(Leave the printing register for no more than one rewrite)</i>
<i>:: > Leave the printing register for no more than one rewrite</i>

:: print: fizz >
:: print: buzz >
:: print: fizzbuzz >
:: print: "{1# 2# 3# 4# 5# 6# 7# 8# 9# 0}{1 2 3 4 5 6 7 8 9}" >

<i>(Fizzbuzz logic)</i>
<i>:: > Fizzbuzz logic</i>

:: eval +3 +3 +3 +5 +5 +5 +5 +5 > print: fizzbuzz
:: eval +3 +3 +3 > print: fizz
:: eval +5 +5 +5 +5 +5 > print: buzz
:: eval > print: "{1# 2# 3# 4# 5# 6# 7# 8# 9# 0}{1 2 3 4 5 6 7 8 9}"

<i>(Base-10 numbers)</i>
<i>:: > Base-10 numbers</i>

:: 1# 9 > 2# 0 +3 +5 eval
:: 2# 9 > 3# 0 +3 +5 eval
Expand All @@ -513,7 +514,7 @@ <h3>Example: Fizzbuzz</h3>
:: 8 > 9 +3 +5 eval
:: 9 > 1# 0 +3 +5 eval

<i>(The initial state)</i>
<i>:: > The initial state</i>

0
</pre>
Expand Down
27 changes: 14 additions & 13 deletions site/rewriting.html
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ <h3>Programming In Fractran</h3>
<p>In a rule definition, which is a fraction where prime factorization is written as names, we find names to the left-side of the spacer(>) to be rewritten by names found on the right-side. Each new name is added to the dictionary and represented internally as a prime number.</p>

<pre>
<i>:: > A rule with no left-side is a comment.</i>
:: flour sugar apples > apple-cake
:: apples oranges cherries > fruit-salad
:: fruit-salad apple-cake > fruit-cake
Expand Down Expand Up @@ -424,24 +425,24 @@ <h3 id='fib'>Example: Fibonacci</h3>
<p>Let's have a look at a <i>real program</i> to generate the Fibonacci Sequence(1, 1, 2, 3, 5, 8, 13, 21, 34..). This program uses catalysts(fib, fib.shift, fib.move) to keep the program state which has 3 phases(shift, move and back to fib) and ensures the correct evaluation order:</p>

<pre>
<i>(Shift the scrolling window to show two numbers)</i>
<i>:: > Shift the scrolling window to show two numbers</i>

:: fib n last > fib n B
:: fib n res > fib n A B
:: fib n > fibrec

<i>(Move the temporary registers back by one number)</i>
<i>:: > Move the temporary registers back by one number</i>

:: fibrec A > fibrec last
:: fibrec B > fibrec res
:: fibrec > fib

<i>(Cleanup temporary registers at the end of the program)</i>
<i>:: > Cleanup temporary registers at the end of the program</i>

:: last >
:: fib >

<i>(Find the fib number equal to the value in n register)</i>
<i>:: > Find the fib number equal to the value in n register</i>

AC 6240, n^5 last res fib
..
Expand All @@ -453,17 +454,17 @@ <h3>Example: Tic-Tac-Toe</h3>
<p>Fractran's <b>output</b> capability is limited to the resulting accumulator at the end of an evaluation. The advantage of symbolic rewriting is that registers are already assigned names, so we shall print those instead. As for <b>input</b>, we can type in new symbol tokens and appending their value to the accumulator between evaluations. We can implement a <i>tic-tac-toe</i> in a mere 16 rules:</p>

<pre>
<i>(Reserve the first registers for the player moves)</i>
<i>:: > Reserve the first registers for the player moves</i>

:: x#a o#a x#b o#b x#c o#c
:: x#d o#d x#e o#e x#f o#f
:: x#g o#g x#h o#h x#i o#i

<i>(This register remains active until the game ends)</i>
<i>:: > This register remains active until the game ends</i>

game

<i>(A symbol to draw the value of registers in a grid)</i>
<i>:: > A symbol to draw the value of registers in a grid</i>

"

Expand All @@ -475,7 +476,7 @@ <h3>Example: Tic-Tac-Toe</h3>

"

<i>(Rules for each possible victory states)</i>
<i>:: > Rules for each possible victory states</i>

:: game x#a x#b x#c > x#a x#b x#c "Player X wins!"
:: game o#a o#b o#c > o#a o#b o#c "Player O wins!"
Expand Down Expand Up @@ -514,27 +515,27 @@ <h3>Example: Fizzbuzz</h3>
<p>Alternatively to getting the resulting program state at the end of an evaluation, we can emit the accumulator at a specific rate during the evaluation by checking if a register is active or not.</p>

<pre>
<i>(Reserve the first registers for the increments and base-10)</i>
<i>:: > Reserve the first registers for the increments and base-10</i>

:: +5 +3
:: 1# 2# 3# 4# 5# 6# 7# 8# 9# 0
:: 1 2 3 4 5 6 7 8 9

<i>(Leave the printing register for no more than one rewrite)</i>
<i>:: > Leave the printing register for no more than one rewrite</i>

:: print: fizz >
:: print: buzz >
:: print: fizzbuzz >
:: print: "{1# 2# 3# 4# 5# 6# 7# 8# 9# 0}{1 2 3 4 5 6 7 8 9}" >

<i>(Fizzbuzz logic)</i>
<i>:: > Fizzbuzz logic</i>

:: eval +3 +3 +3 +5 +5 +5 +5 +5 > print: fizzbuzz
:: eval +3 +3 +3 > print: fizz
:: eval +5 +5 +5 +5 +5 > print: buzz
:: eval > print: "{1# 2# 3# 4# 5# 6# 7# 8# 9# 0}{1 2 3 4 5 6 7 8 9}"

<i>(Base-10 numbers)</i>
<i>:: > Base-10 numbers</i>

:: 1# 9 > 2# 0 +3 +5 eval
:: 2# 9 > 3# 0 +3 +5 eval
Expand All @@ -557,7 +558,7 @@ <h3>Example: Fizzbuzz</h3>
:: 8 > 9 +3 +5 eval
:: 9 > 1# 0 +3 +5 eval

<i>(The initial state)</i>
<i>:: > The initial state</i>

0
</pre>
Expand Down
27 changes: 14 additions & 13 deletions src/htm/fractran.htm
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ <h3>Programming In Fractran</h3>
<p>In a rule definition, which is a fraction where prime factorization is written as names, we find names to the left-side of the spacer(>) to be rewritten by names found on the right-side. Each new name is added to the dictionary and represented internally as a prime number.</p>

<pre>
<i>:: > A rule with no left-side is a comment.</i>
:: flour sugar apples > apple-cake
:: apples oranges cherries > fruit-salad
:: fruit-salad apple-cake > fruit-cake
Expand Down Expand Up @@ -373,24 +374,24 @@ <h3 id='fib'>Example: Fibonacci</h3>
<p>Let's have a look at a <i>real program</i> to generate the Fibonacci Sequence(1, 1, 2, 3, 5, 8, 13, 21, 34..). This program uses catalysts(fib, fib.shift, fib.move) to keep the program state which has 3 phases(shift, move and back to fib) and ensures the correct evaluation order:</p>

<pre>
<i>(Shift the scrolling window to show two numbers)</i>
<i>:: > Shift the scrolling window to show two numbers</i>

:: fib n last > fib n B
:: fib n res > fib n A B
:: fib n > fibrec

<i>(Move the temporary registers back by one number)</i>
<i>:: > Move the temporary registers back by one number</i>

:: fibrec A > fibrec last
:: fibrec B > fibrec res
:: fibrec > fib

<i>(Cleanup temporary registers at the end of the program)</i>
<i>:: > Cleanup temporary registers at the end of the program</i>

:: last >
:: fib >

<i>(Find the fib number equal to the value in n register)</i>
<i>:: > Find the fib number equal to the value in n register</i>

AC 6240, n^5 last res fib
..
Expand All @@ -402,17 +403,17 @@ <h3>Example: Tic-Tac-Toe</h3>
<p>Fractran's <b>output</b> capability is limited to the resulting accumulator at the end of an evaluation. The advantage of symbolic rewriting is that registers are already assigned names, so we shall print those instead. As for <b>input</b>, we can type in new symbol tokens and appending their value to the accumulator between evaluations. We can implement a <i>tic-tac-toe</i> in a mere 16 rules:</p>

<pre>
<i>(Reserve the first registers for the player moves)</i>
<i>:: > Reserve the first registers for the player moves</i>

:: x#a o#a x#b o#b x#c o#c
:: x#d o#d x#e o#e x#f o#f
:: x#g o#g x#h o#h x#i o#i

<i>(This register remains active until the game ends)</i>
<i>:: > This register remains active until the game ends</i>

game

<i>(A symbol to draw the value of registers in a grid)</i>
<i>:: > A symbol to draw the value of registers in a grid</i>

"

Expand All @@ -424,7 +425,7 @@ <h3>Example: Tic-Tac-Toe</h3>

"

<i>(Rules for each possible victory states)</i>
<i>:: > Rules for each possible victory states</i>

:: game x#a x#b x#c > x#a x#b x#c "Player X wins!"
:: game o#a o#b o#c > o#a o#b o#c "Player O wins!"
Expand Down Expand Up @@ -463,27 +464,27 @@ <h3>Example: Fizzbuzz</h3>
<p>Alternatively to getting the resulting program state at the end of an evaluation, we can emit the accumulator at a specific rate during the evaluation by checking if a register is active or not.</p>

<pre>
<i>(Reserve the first registers for the increments and base-10)</i>
<i>:: > Reserve the first registers for the increments and base-10</i>

:: +5 +3
:: 1# 2# 3# 4# 5# 6# 7# 8# 9# 0
:: 1 2 3 4 5 6 7 8 9

<i>(Leave the printing register for no more than one rewrite)</i>
<i>:: > Leave the printing register for no more than one rewrite</i>

:: print: fizz >
:: print: buzz >
:: print: fizzbuzz >
:: print: "{1# 2# 3# 4# 5# 6# 7# 8# 9# 0}{1 2 3 4 5 6 7 8 9}" >

<i>(Fizzbuzz logic)</i>
<i>:: > Fizzbuzz logic</i>

:: eval +3 +3 +3 +5 +5 +5 +5 +5 > print: fizzbuzz
:: eval +3 +3 +3 > print: fizz
:: eval +5 +5 +5 +5 +5 > print: buzz
:: eval > print: "{1# 2# 3# 4# 5# 6# 7# 8# 9# 0}{1 2 3 4 5 6 7 8 9}"

<i>(Base-10 numbers)</i>
<i>:: > Base-10 numbers</i>

:: 1# 9 > 2# 0 +3 +5 eval
:: 2# 9 > 3# 0 +3 +5 eval
Expand All @@ -506,7 +507,7 @@ <h3>Example: Fizzbuzz</h3>
:: 8 > 9 +3 +5 eval
:: 9 > 1# 0 +3 +5 eval

<i>(The initial state)</i>
<i>:: > The initial state</i>

0
</pre>
Expand Down

0 comments on commit c9d3bf1

Please sign in to comment.