testcases
authorChristian Urban <christian.urban@kcl.ac.uk>
Fri, 03 Dec 2021 18:00:30 +0000
changeset 854 ce4a7bab6bd8
parent 853 568671822d52
child 855 1c0a684567d7
testcases
progs/fun2/fact.fun
progs/fun2/mand.fun
progs/fun2/mand2.fun
progs/fun2/sqr.fun
progs/fun2/sqr.ll
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/progs/fun2/fact.fun	Fri Dec 03 18:00:30 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()
+
--- a/progs/fun2/mand.fun	Fri Dec 03 17:45:11 2021 +0000
+++ b/progs/fun2/mand.fun	Fri Dec 03 18:00:30 2021 +0000
@@ -1,4 +1,4 @@
-// Mandelbrot program
+// Mandelbrot program (without character constants)
 
 val Ymin: Double = -1.3;
 val Ymax: Double =  1.3;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/progs/fun2/mand2.fun	Fri Dec 03 18:00:30 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
--- a/progs/fun2/sqr.fun	Fri Dec 03 17:45:11 2021 +0000
+++ b/progs/fun2/sqr.fun	Fri Dec 03 18:00:30 2021 +0000
@@ -4,7 +4,7 @@
 
 def all(n: Int) : Void = {
   if n <= Max
-  then print_int(sqr(n)) ; all(n + 1)
+  then { print_int(sqr(n)) ; new_line(); all(n + 1) }
   else skip()
 };
 
--- a/progs/fun2/sqr.ll	Fri Dec 03 17:45:11 2021 +0000
+++ b/progs/fun2/sqr.ll	Fri Dec 03 18:00:30 2021 +0000
@@ -27,10 +27,18 @@
   ret void
 }
 
-@.str = private constant [4 x i8] c"%d\0A\00"
+@.str_int = private constant [3 x i8] c"%d\00"
 
 define void @print_int(i32 %x) {
-   %t0 = getelementptr [4 x i8], [4 x i8]* @.str, i32 0, i32 0
+   %t0 = getelementptr [3 x i8], [3 x i8]* @.str_int, i32 0, i32 0
+   call i32 (i8*, ...) @printf(i8* %t0, i32 %x) 
+   ret void
+}
+
+@.str_char = private constant [3 x i8] c"%c\00"
+
+define void @print_char(i32 %x) {
+   %t0 = getelementptr [3 x i8], [3 x i8]* @.str_char, i32 0, i32 0
    call i32 (i8*, ...) @printf(i8* %t0, i32 %x) 
    ret void
 }
@@ -47,16 +55,17 @@
 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
+   br i1 %tmp_21, label %if_branch_29, label %else_branch_30
 
-if_branch_28:
+if_branch_29:
    %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)
+   call void @new_line ()
+   %tmp_26 = add i32  %n, 1
+   call void @all (i32 %tmp_26)
    ret void
 
-else_branch_29:
+else_branch_30:
    call void @skip ()
    ret void
 }