From 860f64c59f58cb928cc3ddb456fbedc2eaa864da Mon Sep 17 00:00:00 2001 From: AntonisAk Date: Fri, 28 Mar 2025 18:20:23 +0200 Subject: [PATCH 1/3] IntegerXor dana program --- dana/programs/IntXor.dana | 64 +++++++++++++++++++++++++++++++++++++ dana/programs/IntXor.input | 1 + dana/programs/IntXor.output | 1 + 3 files changed, 66 insertions(+) create mode 100644 dana/programs/IntXor.dana create mode 100644 dana/programs/IntXor.input create mode 100644 dana/programs/IntXor.output diff --git a/dana/programs/IntXor.dana b/dana/programs/IntXor.dana new file mode 100644 index 0000000..9255b12 --- /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 bianry + 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 From 56a17ce27fa5ce1d1c207a4ac41614c1b0aa5d53 Mon Sep 17 00:00:00 2001 From: AntonisAk Date: Fri, 28 Mar 2025 18:31:58 +0200 Subject: [PATCH 2/3] added scope error program --- dana/programs-erroneous/scope_error.dana | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 dana/programs-erroneous/scope_error.dana 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 From 1032583ab44179835b1ca714993559825e6bdeb7 Mon Sep 17 00:00:00 2001 From: Kostis Sagonas Date: Sat, 29 Mar 2025 08:17:33 +0100 Subject: [PATCH 3/3] Fix a spelling error and prettier comments --- dana/programs/IntXor.dana | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/dana/programs/IntXor.dana b/dana/programs/IntXor.dana index 9255b12..066823d 100644 --- a/dana/programs/IntXor.dana +++ b/dana/programs/IntXor.dana @@ -2,10 +2,10 @@ def main: - #A binary number is represented with an array - #where every bit is stored as an int + # A binary number is represented with an array + # where every bit is stored as an int - # Function Definitions + # Function definitions def DecToBin: n as int, N as int[15] var max i is int max := 16384 @@ -41,24 +41,24 @@ def main: if i < 0: break - #Main program + # Main program var a b x is int var A B X is int[15] - #read input + # Read input writeString: "Input 2 non negative integers" a := readInteger() b := readInteger() - #convert to bianry + # Convert to binary DecToBin: a, A DecToBin: b, B - #Calculate XOR + # Calculate XOR XOR: A, B, X - #Convert result to int + # Convert result to int x := BinToDec(X) - #Print result + # Print result writeInteger: x