stanford cs143 Compilers 2 Cool Overview and Examples

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

class Main{
	i : IO <- new IO;
	main(): Int { { i.out_string("Hello World!\n"); 1; } } ;

Unix 中 !! 表示执行上一条命令,!c 表示执行上一次以 c 开头的命令。

可以改成:

class Main{
	i : IO <- new IO;
	main(): IO { i.out_string("Hello World!\n") } ;

也可以改成:

class Main{
	i : IO <- new IO;
	main(): Object { i.out_string("Hello World!\n") } ;

也可以省掉外面的声明:

class Main{
	main(): Object { (new IO).out_string("Hello World!\n") };

例子2:

// fact.cl
class Main {
	main() : Object {
		(new IO).out_string("1\n")
	};
};

输入:

class Main{
	main() : Object {
		(new IO).out_string((new IO).in_string().concat("\n"))
	};
};

string 转 int

class Main inherits A2I{
	main() : Object {
		(new IO).out_string(i2a(a2i((new IO).in_string())+1).concat("\n"))
	};
};

在这里插入图片描述
注意编译的时候要 coolc fact.cl atoi.cl ,atoi 这个文件在给的虚拟机的 cool 目录下面的 example 里面有。

ok ,递归的阶乘函数:

class Main inherits A2I {
	main() : Object {
		(new IO).out_string(i2a(fact(a2i((new IO).in_string()))).concat("\n"))
	};
	fact(i: Int): Int {
		if(i==0) then 1 else i * fact(i-1) fi
	};
};

在这里插入图片描述

迭代的阶乘函数:

class Main inherites A2I{
	main() : Object {
		(new IO).out_string(i2a(fact(a2i((new IO).in_string()))).concat("\n"))
	};
	fact(i: Int): Int {
		let fact: Int <- 1 in {
			while (not (i = 0)) loop
				{
					fact <- fact * i;
					i <- i - 1;
				}
			pool;
			fact;
		}
	};
};

在这里插入图片描述
注意 i <- i - 1 不能写成 i = i - 1 ,因为 cool 中的 = 是比较的意思。

例子3:

class Main inherits IO {
	main(): Object {
		let hello: String <- "Hello ",
			world: String <- "World!",
			newline: String <- "\n"
		in
			out_string(hello.concat(world.concat(newline)))
	};
};

在这里插入图片描述
创建一个链表:

class List{
	item: String;
	next: List;
	
	init(i: String, n: List): List {
		{
			item <- i;
			next <- n;
			self;
		}
	};

	flatten(): String {
		if (isvoid next) then 
			item 
		else 
			item.concat(next.flatten()) 
		fi
	};
};
class Main inherits IO {
	main(): Object {
		let hello: String <- "Hello ",
			world: String <- "World!",
			newline: String <- "\n",
			nil: List,
			list: List <-
				(new List).init(hello,
					(new List).init(world,
						(new List).init(newline, nil)))
		in
			out_string(list.flatten())
	};
};

在这里插入图片描述
使 List 能够存任意的 Object

class List inherits A2I{
	item: Object;
	next: List;
	
	init(i: Object, n: List): List {
		{
			item <- i;
			next <- n;
			self;
		}
	};

	flatten(): String {
		let string: String <-
			case item of
				i: Int => i2a(i);
				s: String => s;
				o: Object => { abort(); ""; };
			esac
		in
			if (isvoid next) then 
				string
			else 
				string.concat(next.flatten()) 
			fi
	};
};
class Main inherits IO {
	main(): Object {
		let hello: String <- "Hello ",
			world: String <- "World!",
			newline: String <- "\n",
			nil: List,
			list: List <-
				(new List).init(hello,
					(new List).init(world,
						(new List).init(newline, nil)))
		in
			out_string(list.flatten())
	};
};

在这里插入图片描述
此时就可以加数字啦:

class List inherits A2I{
	item: Object;
	next: List;
	
	init(i: Object, n: List): List {
		{
			item <- i;
			next <- n;
			self;
		}
	};

	flatten(): String {
		let string: String <-
			case item of
				i: Int => i2a(i);
				s: String => s;
				o: Object => { abort(); ""; };
			esac
		in
			if (isvoid next) then 
				string
			else 
				string.concat(next.flatten()) 
			fi
	};
};
class Main inherits IO {
	main(): Object {
		let hello: String <- "Hello ",
			world: String <- "World!",
			newline: String <- "\n",
			i: Int <- 42,
			nil: List,
			list: List <-
				(new List).init(hello,
					(new List).init(world,
						(new List).init(i,
							(new List).init(newline, nil)))
		in
			out_string(list.flatten())
	};
};

在这里插入图片描述

posted @ 2020-05-13 11:43  winechord  阅读(128)  评论(0编辑  收藏  举报