Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions dana/programs-erroneous/invalid_parser/assign_error.dana
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def main
x = 1
7 changes: 7 additions & 0 deletions dana/programs-erroneous/invalid_parser/elif_without_if.dana
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Error: 'elif' used without a preceding 'if'

def main
begin
elif condition
writeString: "This should never be reached"
end
2 changes: 2 additions & 0 deletions dana/programs-erroneous/invalid_parser/grammar_error.dana
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def hello (writeInteger)
writeString: "Hello world!\n"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Error: missing ':' in function call syntax

def main
writeString "Hello without a colon"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Error: 'begin' block is never closed with 'end'

def main
begin
writeString: "Hello from an unclosed block"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Error: call to undefined function 'printInt'

def main
begin
printInt: 42
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def main
break # Error: break outside loop
return: 42
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def main
continue # Error: continue outside loop
return: 42
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Error: 'if' condition is not boolean (string used as condition)

def main
if "not a boolean":
writeString: "erroneous"
9 changes: 9 additions & 0 deletions dana/programs-erroneous/invalid_semantic/scope_error.dana
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Error: use of variable 'x' outside its scope (declared inside nested def 'inner')

def main
var y is int

def inner
var x is int
x := 42
y := x
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def main
var s is byte [16]
s := "hello" # should've written: strcpy: s, "hello\n"
writeString: s
43 changes: 43 additions & 0 deletions dana/programs-erroneous/invalid_semantic/strwrite_err.dana
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
(*
This is the powint program containing a semantic error in a call to
the writeString function. This function expects a byte[] argunment,
not an int type (variable temp below).
*)

def main

def powint: base as int, exp as int, mod as int, result as ref int

var temp is int

if exp < 1:
result := 1
elif exp = 0:
result := 1
else:
powint: base, exp / 2, mod, temp

if exp % 2 = 0:
result := (temp * temp) % mod
else:
result := (base * temp * temp) % mod

writeString: temp # SEMANTIC ERROR on this line

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"
5 changes: 5 additions & 0 deletions dana/programs-erroneous/invalid_semantic/type_err.dana
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def main
var x is int
var s is byte[10]

return: x + s # Error: cannot add int and byte[]
54 changes: 54 additions & 0 deletions dana/programs-erroneous/invalid_semantic/type_mismatch.dana
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
(* This is the bsort.dana program with a type error *)

def main

def bsort: n as int, x as int []

def swap: x y as ref int
var t is int
t := x
x := y
y := t

var changed is byte
var i is int

loop:
changed := false
i := 0
loop:
if i < n-1:
if x[i] > x[i+1]:
swap: x[i], x[i+1]
changed := true
i := i+1
else: break
if not changed: break

def writeArray: msg as byte [], n as int, x as int []
var i is int

writeString: msg
i := 0
loop:
if i < n:
if i > 0: writeString: ", "
writeInteger: x[i]
i := i+1
else: break
writeString: "\n"

var seed i is int
var x is int [16]

seed := 65
i := 0
loop:
if i < 16:
seed := (seed * 137 + 221 + i) % 101
x[i] := seed
i := i+1
else: break
writeArray: "Initial array: ", 16, x
bsort: 16, x[0] # ERROR: Type mismatch
writeArray: "Sorted array: ", 16, x
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Error: type mismatch in '+' expression (string + number)

def main
begin
writeString: "Count: " + 1
end
5 changes: 5 additions & 0 deletions dana/programs-erroneous/invalid_semantic/undeclared_func.dana
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def main
var x is int

callMe: 42 # Error: callMe is not declared
return: x
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def main
var x is int

return: foo(x, 42) # Error: foo is not defined
6 changes: 6 additions & 0 deletions dana/programs-erroneous/invalid_semantic/undeclared_id.dana
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def main
var x y is int
var s is byte[10]

x := z # Error: z is not declared
return: 42
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Error: use of undefined identifier 'msg' as an argument

def main
begin
writeString: msg
end
103 changes: 103 additions & 0 deletions dana/programs/def_scopes.dana
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
def main
var counter is int

# Top-level (within main) f
def f
writeString: "main.f\n"

# ---- A: nesting + shadowing + sibling call inside a nested scope
def a


# f in a
def f
writeString: "a.f\n"

# b inside a, with its own f and an inner that calls sibling f
def b

def f
writeString: "a.b.f\n"

def callf
# should resolve to a.b.f (closest scope)
f

writeString: "enter a.b\n"
callf
writeString: "leave a.b\n"

# c inside a, with its own f and an inner that calls it
def c

def f
writeString: "a.c.f\n"

def inner
# should resolve to a.c.f
f

writeString: "enter a.c\n"
inner
writeString: "leave a.c\n"

writeString: "enter a\n"
b # prints "enter a.b", "a.b.f", "leave a.b"
f # should resolve to a.f
c
writeString: "leave a\n"

# ---- B: sibling resolution: inner calls sibling helper, then parent's f
def runner

def f
writeString: "runner.f\n"

def helper
writeString: "runner.helper\n"

def inner
# 'helper' must resolve to sibling under 'runner', not deeper
helper
# 'f' must resolve to runner.f (parent scope), not main.f
f

writeString: "enter runner\n"
inner
writeString: "leave runner\n"

# ---- C: recursion inside a nested function (fact)
def outer
def fact is int: n as int
if n <= 1:
return: 1
else:
return: n * fact(n - 1)

writeInteger: fact(5)
writeChar: '\n'

# ---- D: captured local through multiple levels
def useCounter
def inc
counter := counter + 1
writeInteger: counter
writeChar: '\n'

def callTwice
inc
inc

callTwice



writeString: "BEGIN\n"
counter := 0
# Execute the plan
a
runner
outer
useCounter
f # should resolve to main.f
writeString: "END\n"
19 changes: 19 additions & 0 deletions dana/programs/def_scopes.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
BEGIN
enter a
enter a.b
a.b.f
leave a.b
a.f
enter a.c
a.c.f
leave a.c
leave a
enter runner
runner.helper
runner.f
leave runner
120
1
2
main.f
END
76 changes: 76 additions & 0 deletions dana/programs/matrix_mul.dana
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Matrix multiplication: C(n×p) = A(n×m) × B(m×p)
def main

# Read an n×m matrix into M
def readMatrix: rows cols as int, M as int[][20]
var i j x is int
i := 0
loop:
if i >= rows: break
j := 0
loop:
if j >= cols: break
x := readInteger()
M[i][j] := x
j := j + 1
i := i + 1

# Write an n×m matrix M
def writeMatrix: rows cols as int, M as int[][20]
var i j is int
i := 0
loop:
if i >= rows: break
j := 0
loop:
if j >= cols: break
if j > 0: writeString: " "
writeInteger: M[i][j]
j := j + 1
writeString: "\n"
i := i + 1

# C(n×p) = A(n×m) × B(m×p)
def matmul: n m p as int, A B C as int[][20]
var i j k sum is int
i := 0
loop:
if i >= n: break
j := 0
loop:
if j >= p: break
sum := 0
k := 0
loop:
if k >= m: break
sum := sum + A[i][k] * B[k][j]
k := k + 1
C[i][j] := sum
j := j + 1
i := i + 1

var n m p is int
var A B C is int[20][20]

# writeString: "Enter n m p (max 20): "
n := readInteger()
m := readInteger()
p := readInteger()

if n < 1 or m < 1 or p < 1:
writeString: "Invalid (non-positive) sizes of n m p.\n"
exit
if n > 20: n := 20
if m > 20: m := 20
if p > 20: p := 20

# writeString: "Enter A (n x m) elements row-wise:\n"
readMatrix: n, m, A

# writeString: "Enter B (m x p) elements row-wise:\n"
readMatrix: m, p, B

matmul: n, m, p, A, B, C

writeString: "Result C (n x p):\n"
writeMatrix: n, p, C
Loading