Skip to content

Commit 2850672

Browse files
authored
Implement function in the correct order (#79)
**Forward declaration enforcement for local functions** - Dana's specification (§1.3.2) states that if a local function calls another local function defined later in the same scope, that called function must either be defined earlier or declared beforehand using `decl`. - In our case, we applied the first option: we reordered the local function definitions so that any function is defined before it is used. Therefore, in the Dana programs, we moved the definition of the `swap` function **above** other functions (such as `quicksort`) that call it.
1 parent f85b1e8 commit 2850672

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

dana/programs/binarysearch.dana

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
def main
2+
def swap: a as ref int, b as ref int
3+
var t is int
4+
t := a
5+
a := b
6+
b := t
27

38
def partition is int: arr as int [], low high as int
49
var pivot i j is int
@@ -20,12 +25,6 @@ def main
2025
pi := partition(arr, low, high)
2126
quicksort: arr, low, (pi - 1)
2227
quicksort: arr, (pi + 1), high
23-
24-
def swap: a as ref int, b as ref int
25-
var t is int
26-
t := a
27-
a := b
28-
b := t
2928

3029
def binarySearch is int: arr as int [], low high x as int
3130
var mid is int

dana/programs/bsort.dana

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
def main
2+
def swap: x y as ref int
3+
var t is int
4+
t := x
5+
x := y
6+
y := t
27

38
def bsort: n as int, x as int []
49
var changed is byte
@@ -18,12 +23,6 @@ def main
1823
if not changed:
1924
break : outer_loop_label
2025

21-
def swap: x y as ref int
22-
var t is int
23-
t := x
24-
x := y
25-
y := t
26-
2726
def writeArray: msg as byte [], n as int, x as int []
2827
var i is int
2928

0 commit comments

Comments
 (0)