Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,55 @@
include "cowgol.coh";
# Comparator interface, on the model of C, i.e:
# foo < bar => -1, foo == bar => 0, foo > bar => 1
typedef CompRslt is int(-1, 1);
interface Comparator(foo: intptr, bar: intptr): (rslt: CompRslt);
# Bubble sort an array of pointer-sized integers given a comparator function
# (This is the closest you can get to polymorphism in Cowgol).
sub bubbleSort(A: [intptr], len: intptr, comp: Comparator) is
loop
var swapped: uint8 := 0;
var i: intptr := 1;
var a := @next A;
while i < len loop
if comp([@prev a], [a]) == 1 then
var t := [a];
[a] := [@prev a];
[@prev a] := t;
swapped := 1;
end if;
a := @next a;
i := i + 1;
end loop;
if swapped == 0 then
return;
end if;
end loop;
end sub;
# Test: sort a list of numbers
sub NumComp implements Comparator is
# Compare the inputs as numbers
if foo < bar then rslt := -1;
elseif foo > bar then rslt := 1;
else rslt := 0;
end if;
end sub;
# Numbers
var numbers: intptr[] := {
65,13,4,84,29,5,96,73,5,11,17,76,38,26,44,20,36,12,44,51,79,8,99,7,19,95,26
};
# Sort the numbers in place
bubbleSort(&numbers as [intptr], @sizeof numbers, NumComp);
# Print the numbers (hopefully in order)
var i: @indexof numbers := 0;
while i < @sizeof numbers loop
print_i32(numbers[i] as uint32);
print_char(' ');
i := i + 1;
end loop;
print_nl();