RFC (was: Re: Just for fun brainfuck'a'like textprocessor)
davidb
ta-nospam-zz at gmx.at
Wed May 23 14:54:05 PDT 2007
> Gregor Kopp wrote:
...
So, what about a kind of "official" RFC - please comment my code
so I can improve my style? (just put "RFC ..." in the topic)
This way, you could not only learn through your own code
being commented upon, but also through watching other people's
style & improvements!
That said, I'll make a first try @Gregor Kopp's code:
1) You'd definitely want a default case in your switch statement
which btw. fits nicely with "other characters (which are ignored)"
(http://en.wikipedia.org/wiki/Brainfuck)
(currently it exits with an "Error: Switch Default ..."
if it encounters any other symbol than the ones checked for)
2) Just personal preference, but I'd try not to put too much
functionality in main(), makes code more clear and above all:
easier reusable
(okay, its a bf interpreter, no big reuse here ;-).
3) []-handling: not quite the specs implemented, but nicely done with
a stack :-)
4) line 61:
I don't quite see why you use char[] initialized with 1 for cells,
but that's probably just me.
5) line 62:
if "for (;;) ... " has only one statement(?), you don't need
enclosing {} (just cosmetics, but hey - 2 chars less typing *g*)
(or did you use it here as a means to show your intention
more clearly?)
6) descriptive names in addition to clear indentation
=> very easily readable code, nice!
Nice work Gregor Kopp!
Considering the Stack class:
I'm not quite sure about the use of "this.length" in the various
contexts, could someone please explain it to me?
NB: (because I saw it in Gergor Kopps' code)
Just a little gem from the specs:
"Integers can have embedded '_' characters, which are ignored.
The embedded '_' are useful for formatting long literals,
such as using them as a thousands separator"
(http://www.digitalmars.com/d/lex.html#integerliteral)
(e.g. 300_000, 1_925_200, ...)
david
and attached, also a little bf interpreter
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: bf.d
Url: http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20070523/bd4ee86f/attachment.txt
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: upperc.bf
Url: http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20070523/bd4ee86f/attachment-0001.txt
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: 666.bf
Url: http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20070523/bd4ee86f/attachment-0002.txt
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: ascii.bf
Url: http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20070523/bd4ee86f/attachment-0003.txt
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: hellow.bf
Url: http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20070523/bd4ee86f/attachment-0004.txt
More information about the Digitalmars-d-learn
mailing list