diff --git a/dana/programs-erroneous/scope_error.dana b/dana/programs-erroneous/scope_error.dana new file mode 100644 index 0000000..7a5c5c2 --- /dev/null +++ b/dana/programs-erroneous/scope_error.dana @@ -0,0 +1,12 @@ +# x is declared in the loop but called outside +# the loop -> out of scope error + +def main + var i is int + i := 10 + loop: + var x is byte + x := 'A' + i := i - 1 + if i=0 : break + x := 42 diff --git a/dana/programs/IntXor.dana b/dana/programs/IntXor.dana new file mode 100644 index 0000000..066823d --- /dev/null +++ b/dana/programs/IntXor.dana @@ -0,0 +1,64 @@ +# A (not so efficient) program that returns the XOR of 2 ints + +def main: + + # A binary number is represented with an array + # where every bit is stored as an int + + # Function definitions + def DecToBin: n as int, N as int[15] + var max i is int + max := 16384 + i := 14 + + loop: + N[i] := n / max + n := n % max + max := max / 2 + i = i - 1 + if i < 0: break + + def BinToDec is int: N as int[15] + var max i n is int + max := 16384 + i := 14 + n := 0 + + loop: + n := n + N[i] * max + max := max / 2 + i = i - 1 + if i < 0: break + return n + + def XOR: A B X is int[15] + var i is int + i := 14 + + loop: + X[i] := (A[i] + B[i]) % 2 + i = i - 1 + if i < 0: break + + + # Main program + var a b x is int + var A B X is int[15] + + # Read input + writeString: "Input 2 non negative integers" + a := readInteger() + b := readInteger() + + # Convert to binary + DecToBin: a, A + DecToBin: b, B + + # Calculate XOR + XOR: A, B, X + + # Convert result to int + x := BinToDec(X) + + # Print result + writeInteger: x diff --git a/dana/programs/IntXor.input b/dana/programs/IntXor.input new file mode 100644 index 0000000..5d60d4f --- /dev/null +++ b/dana/programs/IntXor.input @@ -0,0 +1 @@ +10472 31738 diff --git a/dana/programs/IntXor.output b/dana/programs/IntXor.output new file mode 100644 index 0000000..ebe1d19 --- /dev/null +++ b/dana/programs/IntXor.output @@ -0,0 +1 @@ +21266