From e25a81dc12f8071a3fa8aeb294c9823e8d8e1985 Mon Sep 17 00:00:00 2001 From: Orestis Sabethai Date: Thu, 10 Apr 2025 19:06:03 +0300 Subject: [PATCH 01/13] Fix syntax of powint.dana Use"=" instead of "==" for equality and moved "var temp is int" declaration to the beginning of the block. The program still contains a semantic error so I copied it to the dana/programs-erroneous directory. --- dana/programs-erroneous/powint-erroneous.dana | 30 +++++++++++++++++++ dana/programs/powint.dana | 9 +++--- 2 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 dana/programs-erroneous/powint-erroneous.dana diff --git a/dana/programs-erroneous/powint-erroneous.dana b/dana/programs-erroneous/powint-erroneous.dana new file mode 100644 index 0000000..475718c --- /dev/null +++ b/dana/programs-erroneous/powint-erroneous.dana @@ -0,0 +1,30 @@ +def main + + def powint: base as int, exp as int, mod as int, result as ref int + + base := base % mod + var temp is int + + if exp < 1: + result := 1 + else: + if exp % 2 = 0: + powint: base, exp / 2, mod, temp # SEMANTIC ERROR: temp is not a ref variable but being passed to a ref parameter + result := (temp * temp) % mod + else: + powint: base, exp - 1, mod, temp # SEMANTIC ERROR: temp is not a ref variable but being passed to a ref parameter + result := (base * temp) % mod + + var b is int + var e is int + var m is int + var res is int + + b := readInteger() + e := readInteger() + m := readInteger() + + powint: b, e, m, res + + writeInteger: res + writeString: "\n" diff --git a/dana/programs/powint.dana b/dana/programs/powint.dana index b3631be..475718c 100644 --- a/dana/programs/powint.dana +++ b/dana/programs/powint.dana @@ -3,17 +3,16 @@ def main def powint: base as int, exp as int, mod as int, result as ref int base := base % mod + var temp is int if exp < 1: result := 1 else: - if exp % 2 == 0: - var temp is int - powint: base, exp / 2, mod, temp + if exp % 2 = 0: + powint: base, exp / 2, mod, temp # SEMANTIC ERROR: temp is not a ref variable but being passed to a ref parameter result := (temp * temp) % mod else: - var temp is int - powint: base, exp - 1, mod, temp + powint: base, exp - 1, mod, temp # SEMANTIC ERROR: temp is not a ref variable but being passed to a ref parameter result := (base * temp) % mod var b is int From c85f61ababbd9182bfd64a6eee1490b098da1a95 Mon Sep 17 00:00:00 2001 From: Orestis Sabethai Date: Wed, 16 Apr 2025 05:26:09 +0300 Subject: [PATCH 02/13] Fix syntax in binarysearch program Adjusted the bAdded a missing comma in a procedure call, changed function calls to use () instead of :, specifed a fixed size for the local array variable declaration, and corrected the assignment operator from "=" to ":=". --- dana/programs/binarysearch.dana | 10 +++++----- dana/programs/powint.dana | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/dana/programs/binarysearch.dana b/dana/programs/binarysearch.dana index 146f3a8..5d8b013 100644 --- a/dana/programs/binarysearch.dana +++ b/dana/programs/binarysearch.dana @@ -11,13 +11,13 @@ def main i := i + 1 swap: arr[i], arr[j] j := j + 1 - swap: arr[i+1] arr[high] + swap: arr[i+1], arr[high] return: (i+1) def quicksort: arr as int [], low high as int var pi is int if low < high: - pi := (partition: arr, low, high) + pi := partition(arr, low, high) quicksort: arr, low, (pi - 1) quicksort: arr, (pi + 1), high @@ -40,7 +40,7 @@ def main high := mid - 1 return: -1 - var nums is int [] + var nums is int [100] var size k find is int k := 0 writeString: "Give size of array: " @@ -50,7 +50,7 @@ def main loop: if k <= size: nums[k] := readInteger() - k = k + 1 + k := k + 1 else: nums[k] := '\0' break @@ -62,4 +62,4 @@ def main find := readInteger() writeString: "\n" writeString: "Value is: " - writeInteger: (binarySearch: nums, 0, (size - 1), find) + writeInteger: (binarySearch( nums, 0, (size - 1), find)) diff --git a/dana/programs/powint.dana b/dana/programs/powint.dana index 475718c..7e6b6d2 100644 --- a/dana/programs/powint.dana +++ b/dana/programs/powint.dana @@ -9,10 +9,10 @@ def main result := 1 else: if exp % 2 = 0: - powint: base, exp / 2, mod, temp # SEMANTIC ERROR: temp is not a ref variable but being passed to a ref parameter + powint: base, exp / 2, mod, temp result := (temp * temp) % mod else: - powint: base, exp - 1, mod, temp # SEMANTIC ERROR: temp is not a ref variable but being passed to a ref parameter + powint: base, exp - 1, mod, temp result := (base * temp) % mod var b is int From 1428f98881fb8844165ab0cc50219bd64d601a8b Mon Sep 17 00:00:00 2001 From: Orestis Sabethai Date: Wed, 16 Apr 2025 20:45:38 +0300 Subject: [PATCH 03/13] Fix powint-erroneous.dana Add actual semantic error in powint-erroneous.dana and highlighted it with a comment. --- dana/programs-erroneous/powint-erroneous.dana | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/dana/programs-erroneous/powint-erroneous.dana b/dana/programs-erroneous/powint-erroneous.dana index 475718c..02530a6 100644 --- a/dana/programs-erroneous/powint-erroneous.dana +++ b/dana/programs-erroneous/powint-erroneous.dana @@ -1,30 +1,37 @@ def main def powint: base as int, exp as int, mod as int, result as ref int - - base := base % mod + var temp is int if exp < 1: result := 1 + elif exp = 0: + result := 1 else: - if exp % 2 = 0: - powint: base, exp / 2, mod, temp # SEMANTIC ERROR: temp is not a ref variable but being passed to a ref parameter - result := (temp * temp) % mod - else: - powint: base, exp - 1, mod, temp # SEMANTIC ERROR: temp is not a ref variable but being passed to a ref parameter - result := (base * temp) % mod + powint: base, exp / 2, mod, temp + + if exp % 2 = 0: + result := (temp * temp) % mod + else: + result := (base * temp * temp) % mod + + writeString: temp # SEMANTIC ERROR: Calling writeString (expects byte[]) with an int argument (temp) var b is int var e is int var m is int var res is int + writeString: "Enter base: " b := readInteger() + writeString: "Enter exponent: " e := readInteger() + writeString: "Enter modulus: " m := readInteger() powint: b, e, m, res + writeString: "Result: " writeInteger: res writeString: "\n" From 464662dfd80398349643a8faec022bca414ac589 Mon Sep 17 00:00:00 2001 From: Orestis Sabethai Date: Sat, 19 Apr 2025 22:07:45 +0300 Subject: [PATCH 04/13] Fix syntax of calculator.dana Fixed various lexical and syntax errors found in the program. --- dana/programs/calculator.dana | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/dana/programs/calculator.dana b/dana/programs/calculator.dana index 2ba3c6d..a1bcb74 100644 --- a/dana/programs/calculator.dana +++ b/dana/programs/calculator.dana @@ -9,7 +9,7 @@ def main if c = '+' or c = '-' or c = '*' or c = '/': return: true else: - return false + return: false (* Conversion from part of a string to an int @@ -44,7 +44,7 @@ def main *) var buf is byte[256] - var idx len start end a b is int + var idx len start p_end a b is int var op is byte loop lines: @@ -57,27 +57,27 @@ def main idx := 0 loop a_start: - if idx == len: + if idx = len: writeString: "Invalid\n" continue: lines elif is_number(buf[idx]): break: a_start idx := idx + 1 - start := idx; + start := idx loop a_end: - if idx == len: + if idx = len: writeString: "Invalid\n" continue: lines elif not is_number(buf[idx]): break: a_end idx := idx + 1 - end := idx; + p_end := idx - a := string_to_int(buff, start, end); + a := string_to_int(buff, start, p_end) loop op_detect: - if idx == len: + if idx = len: writeString: "Invalid\n" continue: lines elif is_operator(buf[idx]): @@ -86,21 +86,21 @@ def main op := buf[idx] loop b_start: - if idx == len: + if idx = len: writeString: "Invalid\n" continue: lines elif is_number(buf[idx]): break: b_start idx := idx + 1 - start := idx; + start := idx loop b_end: - if idx == len or not is_number(buf[idx]): + if idx = len or not is_number(buf[idx]): break: b_end idx := idx + 1 - end := idx; + p_end := idx - b := string_to_int(buff, start, end); + b := string_to_int(buff, start, p_end) # We have finished parsing now we can calculate From dfba77a6ea1dc8a673f3c0483b77990b388ad076 Mon Sep 17 00:00:00 2001 From: Orestis Sabethai Date: Sat, 19 Apr 2025 22:35:38 +0300 Subject: [PATCH 05/13] Fix syntax of dot_produnct.dana The nested dot_product function definition was moved before the main execution statements within main. The equality comparison was changed from == to =. The begin/end keywords were removed from the bodies of main and dot_product because they contained declarations, requiring layout-based blocks instead. Finally, the call dot_product : ... was changed to dot_product(...) to use the correct function call syntax within an expression. --- dana/programs/dot_product.dana | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/dana/programs/dot_product.dana b/dana/programs/dot_product.dana index 96e789c..56e7860 100644 --- a/dana/programs/dot_product.dana +++ b/dana/programs/dot_product.dana @@ -1,18 +1,15 @@ def main -begin - def dot_product is int : n as int, vector1 as int [], vector2 as int [] - begin - var product i is int - product := 0 - i := 0 - loop: - begin - if i == n : begin break end - product := product + vector1[i]*vector2[i] - i := i + 1 - end - return : product - end + def dot_product is int : n as int, vector1 as int [], vector2 as int [] + var product i is int + product := 0 + i := 0 + loop: + begin + if i = n : begin break end + product := product + vector1[i]*vector2[i] + i := i + 1 + end + return : product var ex_vector1 ex_vector2 is int[4] @@ -27,8 +24,7 @@ begin ex_vector2[3] := 7 var result is int - result := dot_product : 4, ex_vector1, ex_vector2 + result := dot_product(4, ex_vector1, ex_vector2) writeString : "The dot product of the two vectors is: " writeInteger : result writeChar : '\n' -end From 3c13d6e98485f1b536b92ebb976b2c65fc4a5f1b Mon Sep 17 00:00:00 2001 From: Orestis Sabethai Date: Sat, 19 Apr 2025 22:46:09 +0300 Subject: [PATCH 06/13] Fix syntax of evenChecker.dana Use "=" instead of "==" --- dana/programs/evenChecker.dana | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dana/programs/evenChecker.dana b/dana/programs/evenChecker.dana index b3b338b..6afb1ab 100644 --- a/dana/programs/evenChecker.dana +++ b/dana/programs/evenChecker.dana @@ -1,7 +1,7 @@ def main def even: n as int, result as ref int - if n % 2 == 0: + if n % 2 = 0: result := 1 else: result := 0 @@ -15,7 +15,7 @@ def main writeInteger: n writeString: " is " - if res == 0: + if res = 0: writeString: "even!" else: writeString: "odd!" From eb00a72c57b505601852b04fa0d222b5865fcb4b Mon Sep 17 00:00:00 2001 From: Orestis Sabethai Date: Sat, 19 Apr 2025 22:52:48 +0300 Subject: [PATCH 07/13] Fix syntax of factorial.dana --- dana/programs/factorial.dana | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dana/programs/factorial.dana b/dana/programs/factorial.dana index 3a56104..885c321 100644 --- a/dana/programs/factorial.dana +++ b/dana/programs/factorial.dana @@ -8,8 +8,8 @@ def main input := readInteger() output := fact(input) - writeString: "The factorial of "; + writeString: "The factorial of " writeInteger: input writeString: " is " writeInteger: output - writeString "\n" + writeString: "\n" From 059aa226dd3ee781ad717d78639527e56be8c814 Mon Sep 17 00:00:00 2001 From: Orestis Sabethai Date: Sat, 19 Apr 2025 23:01:23 +0300 Subject: [PATCH 08/13] Fix syntax of fibonacci.dana Variable declarations for a and b were moved from inside the else block to the beginning of the fib function's body. --- dana/programs/fibonacci.dana | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/dana/programs/fibonacci.dana b/dana/programs/fibonacci.dana index ef6cfe0..01c0525 100644 --- a/dana/programs/fibonacci.dana +++ b/dana/programs/fibonacci.dana @@ -1,13 +1,14 @@ def main def fib: n as int, result as ref int + var a is int + var b is int + if n <= 0: result := 0 elif n = 1: result := 1 else: - var a is int - var b is int fib: n-1, a fib: n-2, b result := a + b From 4801110ead7c50da8fcd3db06246dd1ec707c5d5 Mon Sep 17 00:00:00 2001 From: Orestis Sabethai Date: Sat, 19 Apr 2025 23:11:10 +0300 Subject: [PATCH 09/13] Fix syntax of gcd.dana The 'var s' declaration was moved to the start of gcd, the else block's begin/end was removed, the equality operator was changed from == to =, and function calls were updated to use parenthesis () syntax. --- dana/programs/gcd.dana | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/dana/programs/gcd.dana b/dana/programs/gcd.dana index c7cc889..fed17d4 100644 --- a/dana/programs/gcd.dana +++ b/dana/programs/gcd.dana @@ -1,26 +1,24 @@ def main def gcd is int: a b as int - if b == 0: return: a + var s is int + + if b = 0: return: a else: - begin - var s is int - s := gcd: b, a % b - return: s - end + s := gcd(b, a % b) + return: s var n1 n2 is int - (* just messing around *) var d is int - + (* just messing around *) var d is int writeString: "Give me two positive integers: \n" n1 := readInteger() n2 := readInteger() - # Making sure numbers are non-negative - + # Making sure numbers are non-negative + if n1 < 0: n1 := -n1 - if not n2 >= 0: n2 := -n2 + if not n2 >= 0: n2 := -n2 d := gcd(n1, n2) writeString: "\nTheir GCD is: " writeInteger: d - writeString: "\n" + writeString : "\n" From d476eaf023469c31968845c41a5b4ab16ab03f36 Mon Sep 17 00:00:00 2001 From: Orestis Sabethai Date: Sat, 19 Apr 2025 23:27:48 +0300 Subject: [PATCH 10/13] Fix syntax of IntXor.dana The colon after main was removed, assignment operators were changed from '=' to ':=', 'return n' became 'return : n', parameter types were defined using 'as' instead of 'is', and the final 'writeString' call was corrected. --- dana/programs/IntXor.dana | 43 ++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/dana/programs/IntXor.dana b/dana/programs/IntXor.dana index 066823d..6badc05 100644 --- a/dana/programs/IntXor.dana +++ b/dana/programs/IntXor.dana @@ -1,43 +1,43 @@ -# A (not so efficient) program that returns the XOR of 2 ints +# A (not so efficient) program that returns the XOR of 2 ints + +def main -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] + def DecToBin: n as int, N as int[] var max i is int max := 16384 i := 14 loop: N[i] := n / max - n := n % max + n := n % max max := max / 2 - i = i - 1 + i := i - 1 if i < 0: break - - def BinToDec is int: N as int[15] + + def BinToDec is int: N as int[] var max i n is int max := 16384 i := 14 n := 0 - + loop: - n := n + N[i] * max + n := n + N[i] * max max := max / 2 - i = i - 1 + i := i - 1 if i < 0: break - return n + return : n - def XOR: A B X is int[15] + def XOR: A B X as int[] var i is int i := 14 - + loop: - X[i] := (A[i] + B[i]) % 2 - i = i - 1 + X[i] := (A[i] + B[i]) % 2 + i := i - 1 if i < 0: break @@ -47,18 +47,19 @@ def main: # Read input writeString: "Input 2 non negative integers" - a := readInteger() + a := readInteger() b := readInteger() - # Convert to binary + # 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 + writeString : "\n" From 760a731bdfe1e5e82c1f661e66ff3c4937175908 Mon Sep 17 00:00:00 2001 From: Orestis Sabethai Date: Mon, 21 Apr 2025 02:09:41 +0300 Subject: [PATCH 11/13] Fix syntax of knapsack.dana --- dana/programs/knapsack.dana | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dana/programs/knapsack.dana b/dana/programs/knapsack.dana index 8379e11..bacfd3d 100644 --- a/dana/programs/knapsack.dana +++ b/dana/programs/knapsack.dana @@ -11,7 +11,7 @@ def main dp[w_c] := 0 w_c := w_c + 1 - i := 0; + i := 0 loop: if i >= n: break w_c := w_max @@ -29,7 +29,7 @@ def main n := readInteger() weight_max := readInteger() - if n <= 0: return + if n <= 0: exit i := 0 loop: @@ -43,7 +43,7 @@ def main weight[i] := readInteger() i := i + 1 - res := knapsack: n, weight_max, profit, weight + res := knapsack( n, weight_max, profit, weight) writeInteger: res writeChar: "\n" From 3b4f9f567bfd352c92b66e31bf44fd05963f57ac Mon Sep 17 00:00:00 2001 From: Orestis Sabethai Date: Tue, 22 Apr 2025 05:05:13 +0300 Subject: [PATCH 12/13] Fix syntax of dana programs --- dana/programs-erroneous/bsort_error.dana | 2 +- dana/programs/dot_product.dana | 2 -- dana/programs/knapsack.dana | 2 +- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/dana/programs-erroneous/bsort_error.dana b/dana/programs-erroneous/bsort_error.dana index c0ef8c0..5534d4e 100644 --- a/dana/programs-erroneous/bsort_error.dana +++ b/dana/programs-erroneous/bsort_error.dana @@ -50,5 +50,5 @@ def main writeArray: "Initial array: ", 16, x # according to the grammar, x here should be a proc-call of function `x` with no arguments # error should be that x is a variable of type int[16], not a function. - bsort: 16 x + bsort: 16, x writeArray: "Sorted array: ", 16, x diff --git a/dana/programs/dot_product.dana b/dana/programs/dot_product.dana index 56e7860..5e0d02d 100644 --- a/dana/programs/dot_product.dana +++ b/dana/programs/dot_product.dana @@ -4,11 +4,9 @@ def main product := 0 i := 0 loop: - begin if i = n : begin break end product := product + vector1[i]*vector2[i] i := i + 1 - end return : product var ex_vector1 ex_vector2 is int[4] diff --git a/dana/programs/knapsack.dana b/dana/programs/knapsack.dana index bacfd3d..3e4d143 100644 --- a/dana/programs/knapsack.dana +++ b/dana/programs/knapsack.dana @@ -46,5 +46,5 @@ def main res := knapsack( n, weight_max, profit, weight) writeInteger: res - writeChar: "\n" + writeChar: '\n' From 15ccad39014ca51b11886d854c1c47f4a5053aed Mon Sep 17 00:00:00 2001 From: Orestis Sabethai Date: Wed, 23 Apr 2025 00:07:58 +0300 Subject: [PATCH 13/13] Fix semantics of binarysearch.dana Added return types to the declerations of 'partition' and 'binarySearch' function definitions. Corrected a type mismatch in the array population loop by changing the termination assignment from the byte literal '\0' to the integer 0. --- dana/programs/binarysearch.dana | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dana/programs/binarysearch.dana b/dana/programs/binarysearch.dana index 5d8b013..a99b58f 100644 --- a/dana/programs/binarysearch.dana +++ b/dana/programs/binarysearch.dana @@ -1,6 +1,6 @@ def main - def partition: arr as int [], low high as int + def partition is int: arr as int [], low high as int var pivot i j is int pivot := arr[high] i := low - 1 @@ -27,7 +27,7 @@ def main a := b b := t - def binarySearch: arr as int [], low high x as int + def binarySearch is int: arr as int [], low high x as int var mid is int loop: if low > high: break @@ -52,7 +52,7 @@ def main nums[k] := readInteger() k := k + 1 else: - nums[k] := '\0' + nums[k] := 0 break writeString: "\n"