aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIgor <pashev.igor@gmail.com>2010-01-17 21:04:30 +0300
committerIgor <pashev.igor@gmail.com>2010-01-17 21:04:30 +0300
commit26301f19486c2b09d922e0317b32a5903b91b792 (patch)
tree2ee627c68f27af13e771f64d9220ee894c7f2c6e
parent480040590d537354528edced47f5c3d9f3909377 (diff)
downloadbrainfuck-26301f19486c2b09d922e0317b32a5903b91b792.tar.gz
Fixed stack. Added: ; - end of code, # - comment
-rw-r--r--README5
-rw-r--r--brainfuck.c31
-rw-r--r--clear-cell.bf5
3 files changed, 35 insertions, 6 deletions
diff --git a/README b/README
index e79272a..7f220c0 100644
--- a/README
+++ b/README
@@ -18,7 +18,10 @@ Output formats for operator '.':
if omitted read stdin
Standard operators: <>+-[].,
-Extensions: ciuox - change format output (same as -c & others, see above)
+Extensions:
+ ciuox - change format output (same as -c & others, see above)
+ ; - end of code (usefull when reading stdin)
+ # - comment to the end of line (usefull when reading files)
Example:
echo '+++[.-]' | ./brainfuck
diff --git a/brainfuck.c b/brainfuck.c
index ba573db..79ff0e5 100644
--- a/brainfuck.c
+++ b/brainfuck.c
@@ -24,17 +24,30 @@ char format = 'u';
unsigned int cp = 0;
unsigned int dp = 0;
unsigned int sp = 0;
+char ignore [] = "\t\r\n ";
+
void read_code()
{
- int allocated, n, c;
+ int allocated, n, c, comment;
allocated = 1000;
n = 0;
code = (char*) malloc(allocated * sizeof(char));
+ comment = 0;
while (EOF != (c = getc(prog)))
{
+ if (c == ';') /* end of code */
+ break;
+ else if (c == '#')
+ comment = 1;
+ else if (c == '\n' || c == '\r')
+ comment = 0;
+
+ if (comment || (NULL != strchr(ignore, c)))
+ continue;
+
if (n >= allocated)
{
allocated <<= 1;
@@ -103,10 +116,15 @@ void pop_cp()
int skip()
{
+ int ob = 0;
while (code[cp] != '\0')
{
- if (code[++cp] == ']')
- return 1;
+ switch (code[cp])
+ {
+ case '[': ++ob; break;
+ case ']': --ob; if (0 == ob) return 1;
+ }
+ ++cp;
}
return 0;
}
@@ -129,7 +147,7 @@ void run_code()
char cmd;
while ('\0' != (cmd = code[cp]))
{
- /*fprintf(stderr, "%c", cmd);*/
+ /* fprintf(stderr, "%c", cmd); */
switch (cmd)
{
case '[': if (data[dp])
@@ -187,7 +205,10 @@ void usage(char * self)
printf(" if omitted read stdin\n");
printf("\n");
printf("Standard operators: <>+-[].,\n");
- printf("Extensions: ciuox - change format output (same as -c & others, see above)\n");
+ printf("Extensions:\n");
+ printf(" ciuox - change format output (same as -c & others, see above)\n");
+ printf(" ; - end of code (usefull when reading stdin)\n");
+ printf(" # - comment to the end of line (usefull when reading files)\n");
printf("\n");
printf("Example:\n");
printf(" echo '+++[.-]' | %s\n", self);
diff --git a/clear-cell.bf b/clear-cell.bf
new file mode 100644
index 0000000..1376939
--- /dev/null
+++ b/clear-cell.bf
@@ -0,0 +1,5 @@
++++++
+.
+[-]
+.
+