BrainF*** Interpreter in Batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
einstein1969
Expert
Posts: 960
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: BrainF*** Interpreter in Batch

#61 Post by einstein1969 » 05 Aug 2014 11:23

Hi Antonio,

I have probed the version 4.1 and work well on hanoi.bf.

The time misured on my machine , from version 4, down from 62sec to 46sec (about 25% less)

I have then replicated the Swapout mechanism. And with the previus mechanism the speed go down to 23sec (about 50% less). The same speed of 4.0 MOD.

I'm developing two new version of swapout. The first is very fast and i reached 21sec :!: (about another 10% less)

The second version uses the SIZE of environment and the TIME elapsed for swapout. This is more robust and work well for generic dos batch applications.

The test on this version is 22sec.

I have probed the NEGATIVE FOR instead IF but the speed seem the same. More test needed!

NEGATIVE FOR:

Code: Select all

 (for /f "tokens=1 delims=S" %%h in ("%%b") do @
 ) || set /A "%%c, ip+=1"



einstein1969

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: BrainF*** Interpreter in Batch

#62 Post by penpen » 06 Aug 2014 14:46

This is an interesting thread.

I'm not sure how deep you want to "dig into" simplifying bf code,
so i don't know if this is of much benefit for you.

Maybe you even want to do something like that:

Code: Select all

Repeat apply (Rule 1 > Rule 2 > ... > Rule 8)
until  (no rule changes the source)
Apply rules means to replace the left side of the rule with the right side:
"Rule n: left side => right side"

To simplify computational (nonsense) counters you may use something like these rules:

Code: Select all

Rule 0: "+"^256 ==> ""
Rule 1: exists m: <start of file> (">"^m | "<"^m) "[" <any expression 1> "]" <any expression 2> ==> <start of file> (">"^m | "<"^m) <any expression 2>
Rule 2: "[" <any expr 1> "]" "[" <any expr 2> "]" ==> "[" <any expr 1> "]"
Rule 3: ("><" | "<>") ==> ""
Rule 4: ("+"|"-")* "[-]" ==> "[-]"

   Rule 5-8 needs:
   for_all m in [1 : 255] :
   <Exp1> ::= "[-]" ("+"^m) "[" <Exp2> "-" <Exp2> "]"
   <Exp2> ::= <Exp2> "<" <Exp2> ">" <Exp2> | <Exp2> ">" <Exp2> "<" <Exp2> | <Exp2> <Exp2> | "[-]" | ""
Rule 5:   if exists n, "[-]" ("+"^m) "[" (">"^n) "[-]" <M> "]" in Exp1: Exp1 ==> "[-]" ("+"^m) "["(">"^n) <M> "]" (">"^n) "[-]" ("<"^n)
Rule 6:   if exists n, "[-]" ("+"^m) "[" ("<"^n) "[-]" <M> "]" in Exp1: Exp1 ==> "[-]" ("+"^m) "["(">"^n) <M> "]" (">"^n) "[-]" ("<"^n)
Rule 7, 8: analougous to Rules 5, and 6 but right side extraction
These rules are sketched only (using something like regular expressions and grammar in (some idiom) of BNF).
I hope you see how these rules should work, as i don't have the much time (these days...; sorry for that if not).

Example 1: The "delay" macro of the "hanoi.bf" file.

Code: Select all

                                  [-]+++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++[>[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++[>[-]+++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++[-]<-]<-]
Simplification result of example 1:

Code: Select all

[-]>>[-]<[-]<
(Although in this case you can simplify to nothing, as these memory cells are never used uninitialized for other things.)

Example 2: Small bf program (just counts a little bit and finally displays "OK") http://stackoverflow.com/questions/6853548/**censored**

Code: Select all

++++++++[->-[->-[->-[-]<]<]<]>++++++++[<++++++++++>-]<[>+>+<<-]>-.>-----.>
Simplification result of example 2:

Code: Select all

>++++++++[<++++++++++>-]<[>+>+<<-]>-.>-----.>

But i fear there are multpiple additional rules to optimize bf code.

(You don't have to implement multiple simplification algorithm (snippets) as all simplifications
may be described using such rules.)

penpen

Post Reply