Skip to content

Commit c9c6dc2

Browse files
authored
Fix infinite loop in bsort.dana (#75)
* 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. * 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 ":=". * Fix powint-erroneous.dana Add actual semantic error in powint-erroneous.dana and highlighted it with a comment. * Fix syntax of calculator.dana Fixed various lexical and syntax errors found in the program. * 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. * Fix syntax of evenChecker.dana Use "=" instead of "==" * Fix syntax of factorial.dana * 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. * 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. * 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. * Fix syntax of knapsack.dana * 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. * Fix infinite loop in bsort.dana The 'if not changed: break' condition was previously inside the inner loop. This meant it only broke the inner loop, causing the outer loop to repeat indefinitely if the array was sorted, as 'changed' would be reset at each outer iteration. * Fix linemarket
1 parent 07546e1 commit c9c6dc2

File tree

9 files changed

+441
-412
lines changed

9 files changed

+441
-412
lines changed

dana/programs/N_Queens.dana

Lines changed: 73 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,110 @@
11
def main
22

3-
def printBoard: board as byte[][], n as int
4-
var i j is int
5-
i := 0
3+
var size_main is int
4+
5+
def printBoard: board_param as byte[][], n_param as int
6+
var i_pb j_pb is int
7+
i_pb := 0
68
loop:
7-
if i >= n :
9+
if i_pb >= n_param :
810
break
9-
j := 0
11+
j_pb := 0
1012
loop:
11-
if j >= n :
13+
if j_pb >= n_param :
1214
break
13-
if board[i][j] = 1 :
15+
if board_param[i_pb][j_pb] = true :
1416
writeString: "Q "
1517
else :
1618
writeString: ". "
17-
j := j + 1
19+
j_pb := j_pb + 1
1820
writeString: "\n"
19-
i := i + 1
21+
i_pb := i_pb + 1
2022
writeString: "\n"
2123

22-
def isSafe is byte: board as byte[][], row col n as int
23-
var i j is int
24+
def isSafe is byte: board_param as byte[][], row_param col_param n_param as int
25+
var i_is j_is is int
2426

25-
# check columns
26-
i := 0
27+
i_is := 0
2728
loop:
28-
if i >= row :
29+
if i_is >= row_param :
2930
break
30-
if board[i][col] = 1 :
31-
return : 0
32-
i := i + 1
31+
if board_param[i_is][col_param] = true :
32+
return : false
33+
i_is := i_is + 1
3334

34-
# check left diagonal
35-
i := row - 1
36-
j := col - 1
35+
i_is := row_param - 1
36+
j_is := col_param - 1
3737
loop:
38-
if i < 0 or j < 0 :
38+
if i_is < 0 or j_is < 0 :
3939
break
40-
if board[i][j] = 1 :
41-
return : 0
42-
i := i - 1
43-
j := j - 1
44-
45-
# check right diagonal
46-
i := row - 1
47-
j := col + 1
40+
if board_param[i_is][j_is] = true :
41+
return : false
42+
i_is := i_is - 1
43+
j_is := j_is - 1
44+
45+
i_is := row_param - 1
46+
j_is := col_param + 1
4847
loop:
49-
if i < 0 or j >= n :
48+
if i_is < 0 or j_is >= n_param :
5049
break
51-
if board[i][j] = 1 :
52-
return : 0
53-
i := i - 1
54-
j := j + 1
50+
if board_param[i_is][j_is] = true :
51+
return : false
52+
i_is := i_is - 1
53+
j_is := j_is + 1
5554

56-
return : 1
55+
return : true
5756

58-
def solveNQueensUtil is byte: board as byte[][], row n as int
59-
if row = n :
60-
printBoard: board, n
61-
return : 1
57+
def solveNQueensUtil is byte: board_param as byte[][], row_param n_param as int
58+
var col_util is int
6259

63-
var col is int
64-
col := 0
60+
if row_param = n_param :
61+
printBoard: board_param, n_param
62+
return : true
63+
64+
col_util := 0
6565
loop:
66-
if col >= n :
66+
if col_util >= n_param :
6767
break
68-
if isSafe: board, row, col, n :
69-
board[row][col] := 1
70-
if solveNQueensUtil: board, row + 1, n :
71-
return : 1
72-
board[row][col] := 0
73-
col := col + 1
68+
if isSafe(board_param, row_param, col_util, n_param) :
69+
if row_param < 20 and col_util < 20 : board_param[row_param][col_util] := true
70+
71+
if solveNQueensUtil(board_param, row_param + 1, n_param) :
72+
return : true
73+
74+
if row_param < 20 and col_util < 20 : board_param[row_param][col_util] := false
75+
col_util := col_util + 1
7476

75-
return : 0
77+
return : false
7678

77-
def solveNQueens: n as int
78-
var board is byte[n][n]
79-
var i j is int
79+
def solveNQueens: n_param as int
80+
var board_snq is byte[20][20]
81+
var i_snq j_snq is int
8082

81-
i := 0
83+
i_snq := 0
8284
loop:
83-
if i >= n :
85+
if i_snq >= n_param :
8486
break
85-
j := 0
87+
j_snq := 0
8688
loop:
87-
if j >= n :
89+
if j_snq >= n_param :
8890
break
89-
board[i][j] := 0
90-
j := j + 1
91-
i := i + 1
91+
if i_snq < 20 and j_snq < 20 : board_snq[i_snq][j_snq] := false
92+
j_snq := j_snq + 1
93+
i_snq := i_snq + 1
9294

93-
if solveNQueensUtil: board, 0, n :
94-
writeString: "Solution found.\n"
95+
if solveNQueensUtil(board_snq, 0, n_param) :
96+
skip
9597
else :
9698
writeString: "No solution exists.\n"
9799

98-
var size is int
99100
writeString: "Enter board size: "
100-
size := readInteger()
101-
solveNQueens: size
101+
size_main := readInteger()
102+
if size_main > 20 :
103+
writeString: "Warning: Board size capped at 20 due to program limits.\n"
104+
size_main := 20
105+
if size_main < 1 :
106+
writeString: "Info: Board size must be at least 1. Setting to 1.\n"
107+
size_main := 1
108+
109+
if size_main > 0 : solveNQueens: size_main
110+
else: writeString: "No solution exists for non-positive size.\n"

dana/programs/bsort.dana

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
def main
22

33
def bsort: n as int, x as int []
4-
5-
def swap: x y as ref int
6-
var t is int
7-
t := x
8-
x := y
9-
y := t
10-
114
var changed is byte
125
var i is int
136

14-
loop:
7+
loop outer_loop_label:
158
changed := false
169
i := 0
17-
loop:
10+
loop inner_loop_label:
1811
if i < n-1:
1912
if x[i] > x[i+1]:
2013
swap: x[i], x[i+1]
2114
changed := true
2215
i := i+1
23-
else: break
24-
if not changed: break
16+
else:
17+
break : inner_loop_label
18+
if not changed:
19+
break : outer_loop_label
20+
21+
def swap: x y as ref int
22+
var t is int
23+
t := x
24+
x := y
25+
y := t
2526

2627
def writeArray: msg as byte [], n as int, x as int []
2728
var i is int
@@ -37,16 +38,17 @@ def main
3738
writeString: "\n"
3839

3940
var seed i is int
40-
var x is int [16]
41+
var x is int [16]
4142

4243
seed := 65
4344
i := 0
44-
loop:
45+
loop init_loop:
4546
if i < 16:
4647
seed := (seed * 137 + 220 + i) % 101
4748
x[i] := seed
4849
i := i+1
49-
else: break
50+
else:
51+
break : init_loop
5052
writeArray: "Initial array: ", 16, x
5153
bsort: 16, x
5254
writeArray: "Sorted array: ", 16, x

dana/programs/calculator.dana

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
(* calculator.dana *)
12
def main
23
# Helper Function
34

@@ -11,10 +12,8 @@ def main
1112
else:
1213
return: false
1314

14-
(*
15-
Conversion from part of a string to an int
16-
[from, to)
17-
*)
15+
# Conversion from part of a string to an int
16+
# [from, to)
1817
def string_to_int is int: s as byte[], from to as int
1918
var i res pow is int
2019

@@ -30,18 +29,16 @@ def main
3029

3130
return: res
3231

33-
(*
34-
Simple Calculator
35-
36-
Input: lines of the following form:
37-
<Num1> <Op> <Num2>
38-
where <NumX> is a positive integer
39-
and <Op> is one of +, -, *, /
40-
Any other characters among them get ignored.
41-
42-
Output: lines with the results
43-
or a message "Invalid" in case of failure
44-
*)
32+
# Simple Calculator
33+
#
34+
# Input: lines of the following form:
35+
# <Num1> <Op> <Num2>
36+
# where <NumX> is a positive integer
37+
# and <Op> is one of +, -, *, /
38+
# Any other characters among them get ignored.
39+
#
40+
# Output: lines with the results
41+
# or a message "Invalid" in case of failure
4542

4643
var buf is byte[256]
4744
var idx len start p_end a b is int
@@ -60,60 +57,84 @@ def main
6057
if idx = len:
6158
writeString: "Invalid\n"
6259
continue: lines
63-
elif is_number(buf[idx]):
60+
elif is_digit(buf[idx]):
6461
break: a_start
6562
idx := idx + 1
6663
start := idx
6764

6865
loop a_end:
6966
if idx = len:
70-
writeString: "Invalid\n"
71-
continue: lines
72-
elif not is_number(buf[idx]):
67+
p_end := idx
68+
break: a_end
69+
elif not is_digit(buf[idx]):
70+
p_end := idx
7371
break: a_end
7472
idx := idx + 1
75-
p_end := idx
73+
if idx = len:
74+
p_end := idx
75+
break: a_end
76+
77+
if start = p_end:
78+
writeString: "Invalid\n"
79+
continue: lines
7680

77-
a := string_to_int(buff, start, p_end)
81+
a := string_to_int(buf, start, p_end)
7882

7983
loop op_detect:
8084
if idx = len:
8185
writeString: "Invalid\n"
8286
continue: lines
8387
elif is_operator(buf[idx]):
88+
op := buf[idx]
89+
idx := idx + 1
8490
break: op_detect
8591
idx := idx + 1
86-
op := buf[idx]
92+
93+
if idx = start:
94+
writeString: "Invalid\n"
95+
continue: lines
8796

8897
loop b_start:
8998
if idx = len:
9099
writeString: "Invalid\n"
91100
continue: lines
92-
elif is_number(buf[idx]):
101+
elif is_digit(buf[idx]):
93102
break: b_start
94103
idx := idx + 1
95104
start := idx
96105

97106
loop b_end:
98-
if idx = len or not is_number(buf[idx]):
107+
if idx = len:
108+
p_end := idx
109+
break: b_end
110+
elif not is_digit(buf[idx]):
111+
p_end := idx
99112
break: b_end
100113
idx := idx + 1
101-
p_end := idx
114+
if idx = len:
115+
p_end := idx
116+
break: b_end
102117

103-
b := string_to_int(buff, start, p_end)
118+
if start = p_end: # No digits were found for the second number
119+
writeString: "Invalid\n"
120+
continue: lines
104121

105-
# We have finished parsing now we can calculate
122+
b := string_to_int(buf, start, p_end) # Corrected buff to buf
106123

124+
# We have finished parsing now we can calculate
107125
if op = '+':
108126
writeInteger: a + b
109127
elif op = '-':
110128
writeInteger: a - b
111129
elif op = '*':
112130
writeInteger: a * b
113131
elif op = '/':
114-
writeInteger: a / b
132+
if b = 0:
133+
writeString: "Invalid\n" # Division by zero
134+
else:
135+
writeInteger: a / b
115136
else:
116137
writeString: "Invalid\n"
138+
writeString: "\n"
117139

118140
exit
119-

0 commit comments

Comments
 (0)