# HG changeset patch # User Christian Urban # Date 1638568615 0 # Node ID 23273e3a120f60dd12b633ab4b917cb218a146a1 # Parent 1c0a684567d70ce4a89bb6b4872d7415572c2346 updated diff -r 1c0a684567d7 -r 23273e3a120f cwtests/cw02/collatz.while --- a/cwtests/cw02/collatz.while Fri Dec 03 18:10:33 2021 +0000 +++ b/cwtests/cw02/collatz.while Fri Dec 03 21:56:55 2021 +0000 @@ -5,4 +5,4 @@ then n := n/2 else n := 3*n+1 }; -write "Yes" +write "Yes\n" diff -r 1c0a684567d7 -r 23273e3a120f cwtests/cw02/factors.while --- a/cwtests/cw02/factors.while Fri Dec 03 18:10:33 2021 +0000 +++ b/cwtests/cw02/factors.while Fri Dec 03 21:56:55 2021 +0000 @@ -6,6 +6,6 @@ write "\nThe factors of n are"; f := 2; while (f < n / 2 + 1) do { - if ((n / f) * f == n) then { write(f) } else { skip }; + if ((n / f) * f == n) then { write(f); write("\n") } else { skip }; f := f + 1 } \ No newline at end of file diff -r 1c0a684567d7 -r 23273e3a120f cwtests/cw05/fact.fun --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cwtests/cw05/fact.fun Fri Dec 03 21:56:55 2021 +0000 @@ -0,0 +1,21 @@ +// a simple factorial program +// (including a tail recursive version) + + +def fact(n: Int) : Int = + if n == 0 then 1 else n * fact(n - 1); + +def facT(n: Int, acc: Int) : Int = + if n == 0 then acc else facT(n - 1, n * acc); + +def facTi(n: Int) : Int = facT(n, 1); + +def top() : Void = { + print_int(fact(6)); + print_char(','); + print_int(facTi(6)); + print_char('\n') +}; + +top() + diff -r 1c0a684567d7 -r 23273e3a120f cwtests/cw05/hanoi.fun --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cwtests/cw05/hanoi.fun Fri Dec 03 21:56:55 2021 +0000 @@ -0,0 +1,13 @@ +// Towers of Hanoi in Fun + +def hanoi(n: Int, a: Int, b: Int, c: Int) : Void = + if n != 0 then { + hanoi(n - 1, a, c, b); + print_int(a); + print_char('-'); print_char('>'); // prints out "->" + print_int(b); + print_char('\n'); + hanoi(n - 1, c, b, a) + } else skip; + +hanoi(4,1,2,3) \ No newline at end of file diff -r 1c0a684567d7 -r 23273e3a120f cwtests/cw05/mand.fun --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cwtests/cw05/mand.fun Fri Dec 03 21:56:55 2021 +0000 @@ -0,0 +1,36 @@ +// Mandelbrot program (without character constants) + +val Ymin: Double = -1.3; +val Ymax: Double = 1.3; +val Ystep: Double = 0.05; //0.025; + +val Xmin: Double = -2.1; +val Xmax: Double = 1.1; +val Xstep: Double = 0.02; //0.01; + +val Maxiters: Int = 1000; + +def m_iter(m: Int, x: Double, y: Double, + zr: Double, zi: Double) : Void = { + if Maxiters <= m + then print_star() + else { + if 4.0 <= zi*zi+zr*zr then print_space() + else m_iter(m + 1, x, y, x+zr*zr-zi*zi, 2.0*zr*zi+y) + } +}; + +def x_iter(x: Double, y: Double) : Void = { + if x <= Xmax + then { m_iter(0, x, y, 0.0, 0.0) ; x_iter(x + Xstep, y) } + else skip() +}; + +def y_iter(y: Double) : Void = { + if y <= Ymax + then { x_iter(Xmin, y) ; new_line() ; y_iter(y + Ystep) } + else skip() +}; + + +y_iter(Ymin) \ No newline at end of file diff -r 1c0a684567d7 -r 23273e3a120f cwtests/cw05/mand2.fun --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cwtests/cw05/mand2.fun Fri Dec 03 21:56:55 2021 +0000 @@ -0,0 +1,36 @@ +// Mandelbrot program (with character constants) + +val Ymin: Double = -1.3; +val Ymax: Double = 1.3; +val Ystep: Double = 0.05; //0.025; + +val Xmin: Double = -2.1; +val Xmax: Double = 1.1; +val Xstep: Double = 0.02; //0.01; + +val Maxiters: Int = 1000; + +def m_iter(m: Int, x: Double, y: Double, + zr: Double, zi: Double) : Void = { + if Maxiters <= m + then print_char(' ') + else { + if 4.0 <= zi*zi+zr*zr then print_char('0' + (m % 10)) + else m_iter(m + 1, x, y, x+zr*zr-zi*zi, 2.0*zr*zi+y) + } +}; + +def x_iter(x: Double, y: Double) : Void = { + if x <= Xmax + then { m_iter(0, x, y, 0.0, 0.0) ; x_iter(x + Xstep, y) } + else skip() +}; + +def y_iter(y: Double) : Void = { + if y <= Ymax + then { x_iter(Xmin, y) ; print_char('\n') ; y_iter(y + Ystep) } + else skip() +}; + + +y_iter(Ymin) \ No newline at end of file diff -r 1c0a684567d7 -r 23273e3a120f cwtests/cw05/sqr.fun --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cwtests/cw05/sqr.fun Fri Dec 03 21:56:55 2021 +0000 @@ -0,0 +1,12 @@ +val Max : Int = 10; + +def sqr(x: Int) : Int = x * x; + +def all(n: Int) : Void = { + if n <= Max + then { print_int(sqr(n)) ; new_line(); all(n + 1) } + else skip() +}; + +all(0) + \ No newline at end of file diff -r 1c0a684567d7 -r 23273e3a120f cwtests/cw05/sqr.ll --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cwtests/cw05/sqr.ll Fri Dec 03 21:56:55 2021 +0000 @@ -0,0 +1,68 @@ + +declare i32 @printf(i8*, ...) + +@.str_nl = private constant [2 x i8] c"\0A\00" +@.str_star = private constant [2 x i8] c"*\00" +@.str_space = private constant [2 x i8] c" \00" + +define void @new_line() #0 { + %t0 = getelementptr [2 x i8], [2 x i8]* @.str_nl, i32 0, i32 0 + %1 = call i32 (i8*, ...) @printf(i8* %t0) + ret void +} + +define void @print_star() #0 { + %t0 = getelementptr [2 x i8], [2 x i8]* @.str_star, i32 0, i32 0 + %1 = call i32 (i8*, ...) @printf(i8* %t0) + ret void +} + +define void @print_space() #0 { + %t0 = getelementptr [2 x i8], [2 x i8]* @.str_space, i32 0, i32 0 + %1 = call i32 (i8*, ...) @printf(i8* %t0) + ret void +} + +define void @skip() #0 { + ret void +} + +@.str = private constant [4 x i8] c"%d\0A\00" + +define void @print_int(i32 %x) { + %t0 = getelementptr [4 x i8], [4 x i8]* @.str, i32 0, i32 0 + call i32 (i8*, ...) @printf(i8* %t0, i32 %x) + ret void +} + +; END OF BUILD-IN FUNCTIONS (prelude) + +@Max = global i32 10 + +define i32 @sqr (i32 %x) { + %tmp_20 = mul i32 %x, %x + ret i32 %tmp_20 +} + +define void @all (i32 %n) { + %tmp_22 = load i32, i32* @Max + %tmp_21 = icmp sle i32 %n, %tmp_22 + br i1 %tmp_21, label %if_branch_28, label %else_branch_29 + +if_branch_28: + %tmp_23 = call i32 @sqr (i32 %n) + call void @print_int (i32 %tmp_23) + %tmp_25 = add i32 %n, 1 + call void @all (i32 %tmp_25) + ret void + +else_branch_29: + call void @skip () + ret void +} + +define i32 @main() { + call void @all (i32 0) + ret i32 0 +} +