Skip to content
Merged
2 changes: 1 addition & 1 deletion dana/programs-erroneous/bsort_error.dana
Original file line number Diff line number Diff line change
Expand Up @@ -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
43 changes: 22 additions & 21 deletions dana/programs/IntXor.dana
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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"
26 changes: 13 additions & 13 deletions dana/programs/calculator.dana
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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]):
Expand All @@ -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

Expand Down
26 changes: 10 additions & 16 deletions dana/programs/dot_product.dana
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
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:
if i = n : begin break end
product := product + vector1[i]*vector2[i]
i := i + 1
return : product

var ex_vector1 ex_vector2 is int[4]

Expand All @@ -27,8 +22,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
4 changes: 2 additions & 2 deletions dana/programs/evenChecker.dana
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -15,7 +15,7 @@ def main

writeInteger: n
writeString: " is "
if res == 0:
if res = 0:
writeString: "even!"
else:
writeString: "odd!"
Expand Down
4 changes: 2 additions & 2 deletions dana/programs/factorial.dana
Original file line number Diff line number Diff line change
Expand Up @@ -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"
5 changes: 3 additions & 2 deletions dana/programs/fibonacci.dana
Original file line number Diff line number Diff line change
@@ -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
Expand Down
22 changes: 10 additions & 12 deletions dana/programs/gcd.dana
Original file line number Diff line number Diff line change
@@ -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"
8 changes: 4 additions & 4 deletions dana/programs/knapsack.dana
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,7 +29,7 @@ def main
n := readInteger()
weight_max := readInteger()

if n <= 0: return
if n <= 0: exit

i := 0
loop:
Expand All @@ -43,8 +43,8 @@ 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"
writeChar: '\n'