first commit

This commit is contained in:
Jeena Paradies 2011-04-19 11:37:05 +02:00
commit 063194f8be
349 changed files with 36508 additions and 0 deletions

View file

@ -0,0 +1,73 @@
int main() {
printInt(fac(10));
printInt(rfac(10));
printInt(mfac(10));
printInt(ifac(10));
double r ; // just to test blocks
{
int n = 10;
int r = 1;
while (n>0) {
r = r * n;
n--;
}
printInt(r);
}
printDouble(dfac(10.0));
printString ("hello */");
printString ("/* world") ;
return 0 ;
}
int fac(int a) {
int r;
int n;
r = 1;
n = a;
while (n > 0) {
r = r * n;
n = n - 1;
}
return r;
}
int rfac(int n) {
if (n == 0)
return 1;
else
return n * rfac(n-1);
}
int mfac(int n) {
if (n == 0)
return 1;
else
return n * nfac(n-1);
}
int nfac(int n) {
if (n != 0)
return mfac(n-1) * n;
else
return 1;
}
double dfac(double n) {
if (n == 0.0)
return 1.0;
else
return n * dfac(n-1.0);
}
int ifac(int n) { return ifac2f(1,n); }
int ifac2f(int l, int h) {
if (l == h)
return l;
if (l > h)
return 1;
int m;
m = (l + h) / 2;
return ifac2f(l,m) * ifac2f(m+1,h);
}

View file

@ -0,0 +1,8 @@
3628800
3628800
3628800
3628800
3628800
3628800.0
hello */
/* world

View file

@ -0,0 +1,12 @@
/* void expression as statement */
int main() {
foo();
return 0 ;
}
void foo() {
printString("foo");
return;
}

View file

@ -0,0 +1 @@
foo

View file

@ -0,0 +1,23 @@
// Testing the return checker
int f () {
if (true)
return 0;
else
{}
}
int g () {
if (false)
{}
else
return 0;
}
void p () {}
int main() {
p();
return 0;
}

View file

View file

@ -0,0 +1,7 @@
/* allow comparing booleans. */
int main() {
if (true == true) { printInt(42); }
return 0 ;
}

View file

@ -0,0 +1 @@
42

View file

@ -0,0 +1,14 @@
/* usage of variable initialized in both branches. */
int main () {
int x;
int y = 56;
if (y + 45 <= 2) {
x = 1;
} else {
x = 2;
}
printInt(x);
return 0 ;
}

View file

@ -0,0 +1 @@
2

View file

@ -0,0 +1,11 @@
// Declaration of multiple variables of the same type in one statement:
int main () {
int x, y;
x = 45;
y = -36;
printInt(x);
printInt(y);
return 0 ;
}

View file

@ -0,0 +1,2 @@
45
-36

View file

@ -0,0 +1,8 @@
// declaration and initialization in same statement
int main() {
int x = 7;
printInt(x);
return 0 ;
}

View file

@ -0,0 +1 @@
7

View file

@ -0,0 +1,11 @@
// multiple variables of the same type declared
// and possibly initialized in the same statement
int main() {
int x, y = 7;
x = -1234234;
printInt(x);
printInt(y);
return 0 ;
}

View file

@ -0,0 +1,2 @@
-1234234
7

View file

@ -0,0 +1,13 @@
// Calling functions which take zero parameters
int main() {
int x = foo();
printInt(x);
return 0 ;
}
int foo() {
return 10;
}

View file

@ -0,0 +1 @@
10

View file

@ -0,0 +1,19 @@
// count function parameters as initialized
int main() {
printInt(fac(5));
return 0 ;
}
int fac (int a) {
int r;
int n;
r = 1;
n = a;
while (n > 0)
{
r = r * n;
n = n - 1;
}
return r;
}

View file

@ -0,0 +1 @@
120

View file

@ -0,0 +1,6 @@
/* Test pushing -1. */
int main() {
printInt(-1);
return 0 ;
}

View file

@ -0,0 +1 @@
-1

View file

@ -0,0 +1,26 @@
/* Test arithmetic and comparisons. */
int main() {
int x = 56;
int y = -23;
printInt(x+y);
printInt(x-y);
printInt(x*y);
printInt(45/2);
printInt(78%3);
double z = -9.3;
double w = 5.1;
printBool(z+w > z-w);
printBool(z/w <= z*w);
return 0 ;
}
void printBool(boolean b) {
if (b) {
printString("true");
return;
} else {
printString("false");
return;
}
}

View file

@ -0,0 +1,7 @@
33
79
-1288
22
0
true
false

View file

@ -0,0 +1,33 @@
/* Test boolean operators. */
int main() {
printString("&&");
printBool(test(-1) && test(0));
printBool(test(-2) && test(1));
printBool(test(3) && test(-5));
printBool(test(234234) && test(21321));
printString("||");
printBool(test(-1) || test(0));
printBool(test(-2) || test(1));
printBool(test(3) || test(-5));
printBool(test(234234) || test(21321));
printString("!");
printBool(true);
printBool(false);
return 0 ;
}
void printBool(boolean b) {
if (!b) {
printString("false");
} else {
printString("true");
}
return;
}
boolean test(int i) {
printInt(i);
return i > 0;
}

View file

@ -0,0 +1,25 @@
&&
-1
false
-2
false
3
-5
false
234234
21321
true
||
-1
0
false
-2
1
true
3
true
234234
true
!
true
false

View file

@ -0,0 +1,17 @@
/* Fibonacci. */
int main () {
int lo,hi,mx ;
lo = 1 ;
hi = lo ;
mx = 5000000 ;
printInt(lo) ;
while (hi < mx) {
printInt(hi) ;
hi = lo + hi ;
lo = hi - lo ;
}
return 0 ;
}

View file

@ -0,0 +1,33 @@
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
10946
17711
28657
46368
75025
121393
196418
317811
514229
832040
1346269
2178309
3524578

View file

@ -0,0 +1,16 @@
/* parity of positive integers by recursion */
int main () {
printInt(ev(17)) ;
return 0 ;
}
int ev (int y) {
if (y > 0)
return ev (y-2) ;
else
if (y < 0)
return 0 ;
else
return 1 ;
}

View file

@ -0,0 +1 @@
0

View file

@ -0,0 +1,15 @@
/* parity of positive integers by loop */
int main () {
int y = 17;
while (y > 0)
y = y - 2;
if (y < 0) {
printInt(0);
return 0 ;
}
else {
printInt(1);
return 0 ;
}
}

View file

@ -0,0 +1 @@
0

View file

@ -0,0 +1,40 @@
/* Test boolean operators */
int main () {
int x = 4;
if (3 <= x && 4 != 2 && true) {
printBool(true);
} else {
printString("apa");
}
printBool(true == true || dontCallMe(1));
printBool(4.0 < -50.0 && dontCallMe(2));
printBool(4 == x && true == !false && true);
printBool(implies(false,false));
printBool(implies(false,true));
printBool(implies(true,false));
printBool(implies(true,true));
return 0 ;
}
boolean dontCallMe(int x) {
printInt(x);
return true;
}
void printBool(boolean b) {
if (b) {
printString("true");
} else {
printString("false");
}
return;
}
boolean implies(boolean x, boolean y) {
return !x || x == y;
}

View file

@ -0,0 +1,8 @@
true
true
false
true
true
true
false
true

View file

@ -0,0 +1,2 @@
-5673
42.5

View file

@ -0,0 +1,15 @@
/* test input */
int main() {
int x = readInt();
double y = readDouble();
printInt(x-5);
if (y > 42.0 || y < 43.0)
printString("yay!");
else
printString("nay!");
return 0 ;
}

View file

@ -0,0 +1,2 @@
-5678
yay!

View file

@ -0,0 +1,26 @@
int main() {
int i = 78;
{
int i = 1;
printInt(i);
}
printInt(i);
while (i > 76) {
i--;
printInt(i);
// this is a little tricky
// on the right hand side, i refers to the outer i
int i = i + 7;
printInt(i);
}
printInt(i);
if (i > 4) {
int i = 4;
printInt(i);
} else {
printString("foo");
}
printInt(i);
return 0 ;
}

View file

@ -0,0 +1,9 @@
1
78
77
84
76
83
76
4
76

View file

@ -0,0 +1,7 @@
int main() {
p();
printInt(1);
return 0;
}
void p() {}

View file

@ -0,0 +1 @@
1

View file

@ -0,0 +1,6 @@
int main() {
if (true) {
printInt(1);
return 0;
}
}

View file

@ -0,0 +1 @@
1

View file

@ -0,0 +1,7 @@
int main() {
int x;
double y;
printInt(x);
printDouble(y);
return 0;
}

View file

@ -0,0 +1,2 @@
0
0.0