Skip to content

Commit a20fc3a

Browse files
authored
Fix syntax of several Dana programs (#73)
* 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
1 parent 1c995bd commit a20fc3a

File tree

9 files changed

+67
-73
lines changed

9 files changed

+67
-73
lines changed

dana/programs-erroneous/bsort_error.dana

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,5 @@ def main
5050
writeArray: "Initial array: ", 16, x
5151
# according to the grammar, x here should be a proc-call of function `x` with no arguments
5252
# error should be that x is a variable of type int[16], not a function.
53-
bsort: 16 x
53+
bsort: 16, x
5454
writeArray: "Sorted array: ", 16, x

dana/programs/IntXor.dana

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
1-
# A (not so efficient) program that returns the XOR of 2 ints
1+
# A (not so efficient) program that returns the XOR of 2 ints
2+
3+
def main
24

3-
def main:
4-
55
# A binary number is represented with an array
66
# where every bit is stored as an int
7-
7+
88
# Function definitions
9-
def DecToBin: n as int, N as int[15]
9+
def DecToBin: n as int, N as int[]
1010
var max i is int
1111
max := 16384
1212
i := 14
1313

1414
loop:
1515
N[i] := n / max
16-
n := n % max
16+
n := n % max
1717
max := max / 2
18-
i = i - 1
18+
i := i - 1
1919
if i < 0: break
20-
21-
def BinToDec is int: N as int[15]
20+
21+
def BinToDec is int: N as int[]
2222
var max i n is int
2323
max := 16384
2424
i := 14
2525
n := 0
26-
26+
2727
loop:
28-
n := n + N[i] * max
28+
n := n + N[i] * max
2929
max := max / 2
30-
i = i - 1
30+
i := i - 1
3131
if i < 0: break
32-
return n
32+
return : n
3333

34-
def XOR: A B X is int[15]
34+
def XOR: A B X as int[]
3535
var i is int
3636
i := 14
37-
37+
3838
loop:
39-
X[i] := (A[i] + B[i]) % 2
40-
i = i - 1
39+
X[i] := (A[i] + B[i]) % 2
40+
i := i - 1
4141
if i < 0: break
4242

4343

@@ -47,18 +47,19 @@ def main:
4747

4848
# Read input
4949
writeString: "Input 2 non negative integers"
50-
a := readInteger()
50+
a := readInteger()
5151
b := readInteger()
5252

53-
# Convert to binary
53+
# Convert to binary
5454
DecToBin: a, A
5555
DecToBin: b, B
5656

5757
# Calculate XOR
5858
XOR: A, B, X
59-
59+
6060
# Convert result to int
6161
x := BinToDec(X)
62-
62+
6363
# Print result
6464
writeInteger: x
65+
writeString : "\n"

dana/programs/calculator.dana

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def main
99
if c = '+' or c = '-' or c = '*' or c = '/':
1010
return: true
1111
else:
12-
return false
12+
return: false
1313

1414
(*
1515
Conversion from part of a string to an int
@@ -44,7 +44,7 @@ def main
4444
*)
4545

4646
var buf is byte[256]
47-
var idx len start end a b is int
47+
var idx len start p_end a b is int
4848
var op is byte
4949

5050
loop lines:
@@ -57,27 +57,27 @@ def main
5757
idx := 0
5858

5959
loop a_start:
60-
if idx == len:
60+
if idx = len:
6161
writeString: "Invalid\n"
6262
continue: lines
6363
elif is_number(buf[idx]):
6464
break: a_start
6565
idx := idx + 1
66-
start := idx;
66+
start := idx
6767

6868
loop a_end:
69-
if idx == len:
69+
if idx = len:
7070
writeString: "Invalid\n"
7171
continue: lines
7272
elif not is_number(buf[idx]):
7373
break: a_end
7474
idx := idx + 1
75-
end := idx;
75+
p_end := idx
7676

77-
a := string_to_int(buff, start, end);
77+
a := string_to_int(buff, start, p_end)
7878

7979
loop op_detect:
80-
if idx == len:
80+
if idx = len:
8181
writeString: "Invalid\n"
8282
continue: lines
8383
elif is_operator(buf[idx]):
@@ -86,21 +86,21 @@ def main
8686
op := buf[idx]
8787

8888
loop b_start:
89-
if idx == len:
89+
if idx = len:
9090
writeString: "Invalid\n"
9191
continue: lines
9292
elif is_number(buf[idx]):
9393
break: b_start
9494
idx := idx + 1
95-
start := idx;
95+
start := idx
9696

9797
loop b_end:
98-
if idx == len or not is_number(buf[idx]):
98+
if idx = len or not is_number(buf[idx]):
9999
break: b_end
100100
idx := idx + 1
101-
end := idx;
101+
p_end := idx
102102

103-
b := string_to_int(buff, start, end);
103+
b := string_to_int(buff, start, p_end)
104104

105105
# We have finished parsing now we can calculate
106106

dana/programs/dot_product.dana

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
def main
2-
begin
3-
def dot_product is int : n as int, vector1 as int [], vector2 as int []
4-
begin
5-
var product i is int
6-
product := 0
7-
i := 0
8-
loop:
9-
begin
10-
if i == n : begin break end
11-
product := product + vector1[i]*vector2[i]
12-
i := i + 1
13-
end
14-
return : product
15-
end
2+
def dot_product is int : n as int, vector1 as int [], vector2 as int []
3+
var product i is int
4+
product := 0
5+
i := 0
6+
loop:
7+
if i = n : begin break end
8+
product := product + vector1[i]*vector2[i]
9+
i := i + 1
10+
return : product
1611

1712
var ex_vector1 ex_vector2 is int[4]
1813

@@ -27,8 +22,7 @@ begin
2722
ex_vector2[3] := 7
2823

2924
var result is int
30-
result := dot_product : 4, ex_vector1, ex_vector2
25+
result := dot_product(4, ex_vector1, ex_vector2)
3126
writeString : "The dot product of the two vectors is: "
3227
writeInteger : result
3328
writeChar : '\n'
34-
end

dana/programs/evenChecker.dana

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
def main
22

33
def even: n as int, result as ref int
4-
if n % 2 == 0:
4+
if n % 2 = 0:
55
result := 1
66
else:
77
result := 0
@@ -15,7 +15,7 @@ def main
1515

1616
writeInteger: n
1717
writeString: " is "
18-
if res == 0:
18+
if res = 0:
1919
writeString: "even!"
2020
else:
2121
writeString: "odd!"

dana/programs/factorial.dana

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ def main
88
input := readInteger()
99

1010
output := fact(input)
11-
writeString: "The factorial of ";
11+
writeString: "The factorial of "
1212
writeInteger: input
1313
writeString: " is "
1414
writeInteger: output
15-
writeString "\n"
15+
writeString: "\n"

dana/programs/fibonacci.dana

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
def main
22

33
def fib: n as int, result as ref int
4+
var a is int
5+
var b is int
6+
47
if n <= 0:
58
result := 0
69
elif n = 1:
710
result := 1
811
else:
9-
var a is int
10-
var b is int
1112
fib: n-1, a
1213
fib: n-2, b
1314
result := a + b

dana/programs/gcd.dana

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
11
def main
22

33
def gcd is int: a b as int
4-
if b == 0: return: a
4+
var s is int
5+
6+
if b = 0: return: a
57
else:
6-
begin
7-
var s is int
8-
s := gcd: b, a % b
9-
return: s
10-
end
8+
s := gcd(b, a % b)
9+
return: s
1110

1211
var n1 n2 is int
13-
(* just messing around *) var d is int
14-
12+
(* just messing around *) var d is int
1513
writeString: "Give me two positive integers: \n"
1614
n1 := readInteger()
1715
n2 := readInteger()
1816

19-
# Making sure numbers are non-negative
20-
17+
# Making sure numbers are non-negative
18+
2119
if n1 < 0: n1 := -n1
22-
if not n2 >= 0: n2 := -n2
20+
if not n2 >= 0: n2 := -n2
2321
d := gcd(n1, n2)
2422
writeString: "\nTheir GCD is: "
2523
writeInteger: d
26-
writeString: "\n"
24+
writeString : "\n"

dana/programs/knapsack.dana

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def main
1111
dp[w_c] := 0
1212
w_c := w_c + 1
1313

14-
i := 0;
14+
i := 0
1515
loop:
1616
if i >= n: break
1717
w_c := w_max
@@ -29,7 +29,7 @@ def main
2929
n := readInteger()
3030
weight_max := readInteger()
3131

32-
if n <= 0: return
32+
if n <= 0: exit
3333

3434
i := 0
3535
loop:
@@ -43,8 +43,8 @@ def main
4343
weight[i] := readInteger()
4444
i := i + 1
4545

46-
res := knapsack: n, weight_max, profit, weight
46+
res := knapsack( n, weight_max, profit, weight)
4747

4848
writeInteger: res
49-
writeChar: "\n"
49+
writeChar: '\n'
5050

0 commit comments

Comments
 (0)