diff --git a/test/syntax/handwritten/algorithms.fln b/test/syntax/handwritten/algorithms.fln new file mode 100644 index 00000000..ca251681 --- /dev/null +++ b/test/syntax/handwritten/algorithms.fln @@ -0,0 +1,61 @@ +; A few textbook algorithms written by hand: a substring search and two +; in-place sorts, one typed and one dyn. + +fn find-match(s: str, pattern: str) -> i32 + for i in range(length(s)) + let matched = true + for j in range(length(pattern)) + if s[i + j] != pattern[j] + matched = false + if matched + return i + -1 + +fn selection-sort(coll: [$t]) -> () where is-ordered($t) + let len = length(coll) + for i in range(len) + let min-val = i + for j in range(i + 1, len) + if coll[j] < coll[min-val] + min-val = j + let tmp = coll[min-val] + coll[min-val] = coll[i] + coll[i] = tmp + +fn insertion-sort(coll: [$t]) -> () where is-ordered($t) + let i = 1 + let length = length(coll) + while i < length + let j = i + while j > 0 and coll[j] < coll[dec(j)] + let temp = coll[j] + coll[j] = coll[dec(j)] + coll[dec(j)] = temp + --(j) + ++(i) + +fn insertion-sort-dyn(coll) -> () + let i = 1 + let length = length(coll) + while i < length + let j = i + while j > 0 and coll[j] < coll[dec(j)] + let temp = coll[j] + coll[j] = coll[dec(j)] + coll[dec(j)] = temp + --(j) + ++(i) + +fn main() -> i32 + let nums = [6 2 4 9 1 9 4 5] + selection-sort(slice(nums)) + println("selection-sort", slice(nums)) + let nums2 = [6 2 4 9 1 9 4 5] + insertion-sort(slice(nums2)) + println("insertion-sort", slice(nums2)) + let word = bytes("INSERTIONSORT") + defer free(word) + insertion-sort-dyn(word) + println("insertion-sort-dyn", str(word)) + println("find-match", find-match("aababba", "abba")) + 0 diff --git a/test/syntax/handwritten/algorithms.out b/test/syntax/handwritten/algorithms.out new file mode 100644 index 00000000..30927a0c --- /dev/null +++ b/test/syntax/handwritten/algorithms.out @@ -0,0 +1,4 @@ +selection-sort [1 2 4 4 5 6 9 9] +insertion-sort [1 2 4 4 5 6 9 9] +insertion-sort-dyn EIINNOORRSSTT +find-match 3