Skip to content

Commit 5161db8

Browse files
committed
add fizzbuff
1 parent f064e5c commit 5161db8

8 files changed

Lines changed: 108 additions & 6 deletions

File tree

‎nodes.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -715,8 +715,8 @@ def type_mismatch(v: BinaryOperation) -> str:
715715
self.operation = operations.get(self.operation, self.operation)
716716
assert left_type in ("INTEGER", "REAL"), type_mismatch(self)
717717

718-
elif self.operation in ("<", "<=", ">", ">=", "=", "<>", "|", "XOR"):
719-
operations = {"=": "==", "<>": "!=", "|": "||", "XOR": "^"}
718+
elif self.operation in ("<", "<=", ">", ">=", "=", "<>", "&", "|", "XOR"):
719+
operations = {"=": "==", "<>": "!=", "&": "&&", "|": "||", "XOR": "^"}
720720
self.operation = operations.get(self.operation, self.operation)
721721
assert left_type in ("BOOLEAN", "INTEGER", "REAL"), type_mismatch(self)
722722

@@ -729,7 +729,7 @@ def type_mismatch(v: BinaryOperation) -> str:
729729
return f"({self.left.c()} {self.operation} {self.right.c()})"
730730

731731
def py(self) -> str:
732-
operations = {"AND": "and", "OR": "or", "=": "==", "<>": "!=", "||": "or", "XOR": "^"}
732+
operations = {"AND": "and", "OR": "or", "=": "==", "<>": "!=", "&&": "and", "||": "or", "XOR": "^"}
733733
operation = operations.get(self.operation, self.operation)
734734
rhs = self.right.py()
735735
if operation == "==" and rhs == "True":

‎runtime.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ void output(const char *fmt, ...)
9191
}
9292
va_end(args);
9393

94-
if (n > 1)
94+
if (n > 1 || fmt[-1] == 'i')
9595
putchar('\n');
9696
}
9797
void pause(double seconds)

‎runtime.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
def runtime_print(*args, **kwargs) -> None:
2+
if not args:
3+
return
24
print(*args, end="", sep="")
3-
if args and len(str(args[-1])) > 1:
5+
last = args[-1]
6+
if len(str(last)) > 1 or isinstance(last, int):
47
print()
58

69

‎tests/fizzbuzz/test.easy‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
PROGRAM FizzBuzz:
2+
DECLARE n INTEGER;
3+
4+
PROCEDURE FizzBuzz(n INTEGER):
5+
DECLARE i INTEGER;
6+
FOR i := 1 TO n DO
7+
SELECT TRUE OF
8+
CASE (i MOD 3 = 0 & i MOD 5 = 0): OUTPUT "FizzBuzz";
9+
CASE (i MOD 3 = 0): OUTPUT "Fizz";
10+
CASE (i MOD 5 = 0): OUTPUT "Buzz";
11+
OTHERWISE: OUTPUT i;
12+
END SELECT;
13+
END FOR;
14+
END PROCEDURE FizzBuzz;
15+
16+
CALL FizzBuzz(20);
17+
END PROGRAM FizzBuzz;

‎tests/fizzbuzz/x/test.c‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "runtime.c"
2+
int n = {0};
3+
void FizzBuzz(int n)
4+
{
5+
int i = {0};
6+
for (i = 1; i <= n; i += 1)
7+
{
8+
if (((i % 3) == 0) && ((i % 5) == 0))
9+
{
10+
output("A", from_cstring("FizzBuzz"));
11+
}
12+
else if ((i % 3) == 0)
13+
{
14+
output("A", from_cstring("Fizz"));
15+
}
16+
else if ((i % 5) == 0)
17+
{
18+
output("A", from_cstring("Buzz"));
19+
}
20+
else
21+
{
22+
output("i", i);
23+
}
24+
}
25+
}
26+
int main()
27+
{
28+
FizzBuzz(20);
29+
}

‎tests/fizzbuzz/x/test.output‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
1
2+
2
3+
Fizz
4+
4
5+
Buzz
6+
Fizz
7+
7
8+
8
9+
Fizz
10+
Buzz
11+
11
12+
Fizz
13+
13
14+
14
15+
FizzBuzz
16+
16
17+
17
18+
Fizz
19+
19
20+
Buzz

‎tests/fizzbuzz/x/test.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from runtime import runtime_print
2+
3+
n = 0
4+
5+
6+
def FizzBuzz(n):
7+
i = 0
8+
i = 1
9+
while True:
10+
if i > n:
11+
break
12+
if (((i % 3) == 0) and ((i % 5) == 0)):
13+
runtime_print('FizzBuzz')
14+
elif ((i % 3) == 0):
15+
runtime_print('Fizz')
16+
elif ((i % 5) == 0):
17+
runtime_print('Buzz')
18+
else:
19+
runtime_print(i)
20+
i += 1
21+
22+
23+
FizzBuzz(20)

‎tests/hw/x/test.output‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,12 @@
11
abc 100
2-
012345678910
2+
0
3+
1
4+
2
5+
3
6+
4
7+
5
8+
6
9+
7
10+
8
11+
9
12+
10

0 commit comments

Comments
 (0)