diff --git a/fizzbuzz.grsbpl b/fizzbuzz.grsbpl index 2253b4f..ff8b2de 100644 --- a/fizzbuzz.grsbpl +++ b/fizzbuzz.grsbpl @@ -1,9 +1,19 @@ -10 factorial 1 goto exit +1 &i # init loop counter +:start # set start label +@i 100 - not goto finished # if i is 100, finish +@i 15 % not goto print_fizz_buzz # fizzbuzz +@i 5 % not goto print_buzz # buzz +@i 3 % not goto print_fizz # fizz +@i nout '\n' out # normal number +:end # go back here after printing +@i 1 + &i # increment i +1 goto start # go back to the start -function factorial 1 -dup not goto isZero -pop dup 1 - factorial * return -:isZero -1 pop return +:print_fizz_buzz + "Fizzbuzz\n" out goto end +:print_fizz + "Fizz\n" out goto end +:print_buzz + "Buzz\n" out goto end -:exit pop dup nout \ No newline at end of file +:finished 0 \ No newline at end of file diff --git a/src/main/java/com/github/nilstrieb/grsbpl/language/Lexer.java b/src/main/java/com/github/nilstrieb/grsbpl/language/Lexer.java index fc78f5f..80c1694 100644 --- a/src/main/java/com/github/nilstrieb/grsbpl/language/Lexer.java +++ b/src/main/java/com/github/nilstrieb/grsbpl/language/Lexer.java @@ -92,7 +92,7 @@ public class Lexer { value = escape(); } add(CHAR, value); - consume(); + expect('\''); } private void string() { @@ -194,6 +194,13 @@ public class Lexer { advance(); } + private void expect(char c) { + if (peek() != c) { + lexException("Excepted character '" + c + "' but found '" + peek() + "'"); + } + advance(); + } + private char last() { return program[position - 1]; }