read the doc and format skeleton code

This commit is contained in:
ridethepig 2023-03-29 02:03:47 +00:00
parent 040115e812
commit b2142969f8
8 changed files with 909 additions and 985 deletions

View File

@ -234,3 +234,31 @@ PA 2-5 正式写编译器。PA2 写词法分析器,首先读一遍 README 和
- 返回 Object 的情况objectid 未定义、new 未定义类、函数调用中无法确定被调用函数的情况类未定义、函数未定义、函数参数个数不对、loop 返回值。
剩下的基本上能从手册的12章中比较清晰的看出来。
### PA5
#### 读文档
最后一个 PA那必然是要把剩下来的文档全部读完。需要预先看一遍带是 `cool-tour.pdf` 的第7章 Runtime System 和 PA5 handout至于 `cool-manual.pdf` 的第13章语义部分倒是可以一边实现一边看。
先看 handout
- 代码量巨大无比,竟然是 PA4 的2倍
- 文件简述:`cgen.{cc|hh}` 大部分需要写的代码,和 PA4 类似的结构,从 AST 的根节点开始进行 `cgen``cool-tree.h` 和 PA4 类似;`cgen_supp.cc` 定义辅助函数;`emit.h` 里面有一些 MIPS 汇编和符号宏定义;剩下的都是老熟人了。
- 主要任务:
- 生成全局常量prototype objects
- 生成全局表,`class_nameTab` `class_objTab` 还有方法调用表
- 生成每个类的初始化代码
- 生成方法定义
推荐的实现方法还是分两部分,先生成对象布局,然后第二遍在生成每个表达式的代码。
- 提醒注意:这次没有必要去“逆向”参考编译器了,因为它实现了一些高级功能比如寄存器分配优化,这个 PA5 并不要求这个。
- 运行时错误处理manual 规定了6种运行时错误生成的代码需要检测三种(static) dispatch on void, case on void, missing branch除零可以交给模拟器剩下两种由 Runtime 处理。
- GC有一个3个命令行开关控制垃圾回收系统相关的功能。默认情况下不打开 `-g` 开关,此时不启用 GC也就是说这是一个选做功能`-t` 迫使 GC 系统在每次分配对象的时候进行回收;`-T` 的功能交给实现,可能会用来实现一些其他的运行时检查。实现 GC 功能的时候,需要认真阅读 Runtime 手册相关内容。这里可以去看看 CS143 课程网站上的那个单独的 `cool-runtime.pdf`,似乎写的更加详细一些。
- 测试工具:和 PA4 类似,提供了一个 `-c` 选项来设置全局变量 `cgen_debug`。同时提供了一个第三方实现的 `Coolaid` 工具对生成的 MIPS 汇编进行一些检查,说不定会有帮助。最后关于 `Spim` 的 warning 可能会有用。
然后看看 Runtime System
- 首先是对象布局GC Tag 设为-1Object Size 也得填上dispatch pointer 因为不会被 runtime 用到,所以需要自己设计 dispatch 表;对于属性,`Int` 只有一个32位整数、`Bool` 也是如此、`String` 有一个32位的长度+后面全部是 ASCII 字节(最后需要 word 对齐),然后还有空指针 void。
- 然后是一个叫原型对象Prototype Object的东西COOL 里面新建对象的方法是使用 `Object.copy()`,因此我们需要生成这些供其复制的东西,也就是 prototype object。生成的时候需要正确设置前面的头部对于属性三个基本类型有自己的规定其余类型的属性随意设置。
- 栈和寄存器约定:方法调用参数放在栈上、从左到右依次压栈,`a0` 寄存器里面放 `self` 对象指针。指定了一组 Scratch registers 供 runtime routine 存放临时数据,因此需要调用者保存;还有堆指针和堆界限两个寄存器,完全由 runtime 控制。其他的都可以用。
- Label生成的代码需要和 runtime 一起变成最后执行的机器码,因此有一些 label 是指定的,就类似于接口一样的东西。有些 label 是 runtime 提供给我们使用的,也有一些需要我们生成供 runtime 使用。有一句话 `There is no need for code that initializes an object of class Bool if the generated code contains definitions of both Bool objects in the static data area.` 没看懂
- 执行初始化:需要生成一些代码来调用 main 方法。首先通过 Main prototype 生成一个 Main 类的对象并用 `Main_init` 初始化,该初始化方法依次执行 Main 的基类的初始化最后初始化 Main然后调用 `Main.main`,在 `a0` 里面放上 `Main` 的指针并设置 `ra`;执行结束后,`Main.main` 返回,这里打印出提示信息并终止执行。

File diff suppressed because it is too large Load Diff

View File

@ -1,10 +1,11 @@
#include <assert.h>
#include <stdio.h>
#include "emit.h"
#include "cool-tree.h"
#include "emit.h"
#include "symtab.h"
#include <assert.h>
#include <iostream>
#include <stdio.h>
enum Basicness {Basic, NotBasic};
enum Basicness { Basic, NotBasic };
#define TRUE 1
#define FALSE 0
@ -14,17 +15,16 @@ typedef CgenClassTable *CgenClassTableP;
class CgenNode;
typedef CgenNode *CgenNodeP;
class CgenClassTable : public SymbolTable<Symbol,CgenNode> {
class CgenClassTable : public SymbolTable<Symbol, CgenNode> {
private:
List<CgenNode> *nds;
ostream& str;
ostream &str;
int stringclasstag;
int intclasstag;
int boolclasstag;
// The following methods emit code for
// constants and global declarations.
// The following methods emit code for
// constants and global declarations.
void code_global_data();
void code_global_text();
@ -32,23 +32,25 @@ private:
void code_select_gc();
void code_constants();
// The following creates an inheritance graph from
// a list of classes. The graph is implemented as
// a tree of `CgenNode', and class names are placed
// in the base class symbol table.
// The following creates an inheritance graph from
// a list of classes. The graph is implemented as
// a tree of `CgenNode', and class names are placed
// in the base class symbol table.
void install_basic_classes();
void install_class(CgenNodeP nd);
void install_classes(Classes cs);
void build_inheritance_tree();
void set_relations(CgenNodeP nd);
//* New Methods
void dump_inheritance_tree();
public:
CgenClassTable(Classes, ostream& str);
CgenClassTable(Classes, ostream &str);
void code();
CgenNodeP root();
};
class CgenNode : public class__class {
private:
CgenNodeP parentnd; // Parent of class
@ -57,24 +59,29 @@ private:
// `NotBasic' otherwise
public:
CgenNode(Class_ c,
Basicness bstatus,
CgenClassTableP class_table);
CgenNode(Class_ c, Basicness bstatus, CgenClassTableP class_table);
void add_child(CgenNodeP child);
List<CgenNode> *get_children() { return children; }
void set_parentnd(CgenNodeP p);
CgenNodeP get_parentnd() { return parentnd; }
int basic() { return (basic_status == Basic); }
void traverse_dump(int pad) {
for (int i = 0; i < pad; ++i)
std::cerr << " ";
std::cerr << this->name << "\n";
for (auto l = children; l; l = l->tl()) {
l->hd()->traverse_dump(pad + 2);
}
}
};
class BoolConst
{
private:
class BoolConst {
private:
int val;
public:
BoolConst(int);
void code_def(ostream&, int boolclasstag);
void code_ref(ostream&) const;
};
public:
BoolConst(int);
void code_def(ostream &, int boolclasstag);
void code_ref(ostream &) const;
};

View File

@ -1,30 +1,25 @@
#include "stringtab.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "stringtab.h"
static int ascii = 0;
void ascii_mode(ostream& str)
{
if (!ascii)
{
void ascii_mode(ostream &str) {
if (!ascii) {
str << "\t.ascii\t\"";
ascii = 1;
}
}
void byte_mode(ostream& str)
{
if (ascii)
{
void byte_mode(ostream &str) {
if (ascii) {
str << "\"\n";
ascii = 0;
}
}
void emit_string_constant(ostream& str, char* s)
{
void emit_string_constant(ostream &str, char *s) {
ascii = 0;
while (*s) {
@ -39,22 +34,19 @@ void emit_string_constant(ostream& str, char* s)
break;
case '\\':
byte_mode(str);
str << "\t.byte\t" << (int) ((unsigned char) '\\') << endl;
str << "\t.byte\t" << (int)((unsigned char)'\\') << endl;
break;
case '"' :
case '"':
ascii_mode(str);
str << "\\\"";
break;
default:
if (*s >= ' ' && ((unsigned char) *s) < 128)
{
if (*s >= ' ' && ((unsigned char)*s) < 128) {
ascii_mode(str);
str << *s;
}
else
{
} else {
byte_mode(str);
str << "\t.byte\t" << (int) ((unsigned char) *s) << endl;
str << "\t.byte\t" << (int)((unsigned char)*s) << endl;
}
break;
}
@ -63,5 +55,3 @@ void emit_string_constant(ostream& str, char* s)
byte_mode(str);
str << "\t.byte\t0\t" << endl;
}

View File

@ -8,10 +8,8 @@
//
//////////////////////////////////////////////////////////
#include "tree.h"
#include "cool-tree.handcode.h"
#include "tree.h"
// define the class for phylum
// define simple phylum - Program
@ -27,7 +25,6 @@ public:
#endif
};
// define simple phylum - Class_
typedef class Class__class *Class_;
@ -41,7 +38,6 @@ public:
#endif
};
// define simple phylum - Feature
typedef class Feature_class *Feature;
@ -55,7 +51,6 @@ public:
#endif
};
// define simple phylum - Formal
typedef class Formal_class *Formal;
@ -69,7 +64,6 @@ public:
#endif
};
// define simple phylum - Expression
typedef class Expression_class *Expression;
@ -83,7 +77,6 @@ public:
#endif
};
// define simple phylum - Case
typedef class Case_class *Case;
@ -97,44 +90,37 @@ public:
#endif
};
// define the class for phylum - LIST
// define list phlyum - Classes
typedef list_node<Class_> Classes_class;
typedef Classes_class *Classes;
// define list phlyum - Features
typedef list_node<Feature> Features_class;
typedef Features_class *Features;
// define list phlyum - Formals
typedef list_node<Formal> Formals_class;
typedef Formals_class *Formals;
// define list phlyum - Expressions
typedef list_node<Expression> Expressions_class;
typedef Expressions_class *Expressions;
// define list phlyum - Cases
typedef list_node<Case> Cases_class;
typedef Cases_class *Cases;
// define the class for constructors
// define constructor - program
class program_class : public Program_class {
public:
Classes classes;
public:
program_class(Classes a1) {
classes = a1;
}
program_class(Classes a1) { classes = a1; }
Program copy_Program();
void dump(ostream& stream, int n);
void dump(ostream &stream, int n);
#ifdef Program_SHARED_EXTRAS
Program_SHARED_EXTRAS
@ -144,7 +130,6 @@ public:
#endif
};
// define constructor - class_
class class__class : public Class__class {
public:
@ -152,6 +137,7 @@ public:
Symbol parent;
Features features;
Symbol filename;
public:
class__class(Symbol a1, Symbol a2, Features a3, Symbol a4) {
name = a1;
@ -160,7 +146,7 @@ public:
filename = a4;
}
Class_ copy_Class_();
void dump(ostream& stream, int n);
void dump(ostream &stream, int n);
#ifdef Class__SHARED_EXTRAS
Class__SHARED_EXTRAS
@ -170,7 +156,6 @@ public:
#endif
};
// define constructor - method
class method_class : public Feature_class {
public:
@ -178,6 +163,7 @@ public:
Formals formals;
Symbol return_type;
Expression expr;
public:
method_class(Symbol a1, Formals a2, Symbol a3, Expression a4) {
name = a1;
@ -186,7 +172,7 @@ public:
expr = a4;
}
Feature copy_Feature();
void dump(ostream& stream, int n);
void dump(ostream &stream, int n);
#ifdef Feature_SHARED_EXTRAS
Feature_SHARED_EXTRAS
@ -196,13 +182,13 @@ public:
#endif
};
// define constructor - attr
class attr_class : public Feature_class {
public:
Symbol name;
Symbol type_decl;
Expression init;
public:
attr_class(Symbol a1, Symbol a2, Expression a3) {
name = a1;
@ -210,7 +196,7 @@ public:
init = a3;
}
Feature copy_Feature();
void dump(ostream& stream, int n);
void dump(ostream &stream, int n);
#ifdef Feature_SHARED_EXTRAS
Feature_SHARED_EXTRAS
@ -220,19 +206,19 @@ public:
#endif
};
// define constructor - formal
class formal_class : public Formal_class {
public:
Symbol name;
Symbol type_decl;
public:
formal_class(Symbol a1, Symbol a2) {
name = a1;
type_decl = a2;
}
Formal copy_Formal();
void dump(ostream& stream, int n);
void dump(ostream &stream, int n);
#ifdef Formal_SHARED_EXTRAS
Formal_SHARED_EXTRAS
@ -242,13 +228,13 @@ public:
#endif
};
// define constructor - branch
class branch_class : public Case_class {
public:
Symbol name;
Symbol type_decl;
Expression expr;
public:
branch_class(Symbol a1, Symbol a2, Expression a3) {
name = a1;
@ -256,7 +242,7 @@ public:
expr = a3;
}
Case copy_Case();
void dump(ostream& stream, int n);
void dump(ostream &stream, int n);
#ifdef Case_SHARED_EXTRAS
Case_SHARED_EXTRAS
@ -266,19 +252,19 @@ public:
#endif
};
// define constructor - assign
class assign_class : public Expression_class {
public:
Symbol name;
Expression expr;
public:
assign_class(Symbol a1, Expression a2) {
name = a1;
expr = a2;
}
Expression copy_Expression();
void dump(ostream& stream, int n);
void dump(ostream &stream, int n);
#ifdef Expression_SHARED_EXTRAS
Expression_SHARED_EXTRAS
@ -288,7 +274,6 @@ public:
#endif
};
// define constructor - static_dispatch
class static_dispatch_class : public Expression_class {
public:
@ -296,6 +281,7 @@ public:
Symbol type_name;
Symbol name;
Expressions actual;
public:
static_dispatch_class(Expression a1, Symbol a2, Symbol a3, Expressions a4) {
expr = a1;
@ -304,7 +290,7 @@ public:
actual = a4;
}
Expression copy_Expression();
void dump(ostream& stream, int n);
void dump(ostream &stream, int n);
#ifdef Expression_SHARED_EXTRAS
Expression_SHARED_EXTRAS
@ -314,13 +300,13 @@ public:
#endif
};
// define constructor - dispatch
class dispatch_class : public Expression_class {
public:
Expression expr;
Symbol name;
Expressions actual;
public:
dispatch_class(Expression a1, Symbol a2, Expressions a3) {
expr = a1;
@ -328,7 +314,7 @@ public:
actual = a3;
}
Expression copy_Expression();
void dump(ostream& stream, int n);
void dump(ostream &stream, int n);
#ifdef Expression_SHARED_EXTRAS
Expression_SHARED_EXTRAS
@ -338,13 +324,13 @@ public:
#endif
};
// define constructor - cond
class cond_class : public Expression_class {
public:
Expression pred;
Expression then_exp;
Expression else_exp;
public:
cond_class(Expression a1, Expression a2, Expression a3) {
pred = a1;
@ -352,7 +338,7 @@ public:
else_exp = a3;
}
Expression copy_Expression();
void dump(ostream& stream, int n);
void dump(ostream &stream, int n);
#ifdef Expression_SHARED_EXTRAS
Expression_SHARED_EXTRAS
@ -362,19 +348,19 @@ public:
#endif
};
// define constructor - loop
class loop_class : public Expression_class {
public:
Expression pred;
Expression body;
public:
loop_class(Expression a1, Expression a2) {
pred = a1;
body = a2;
}
Expression copy_Expression();
void dump(ostream& stream, int n);
void dump(ostream &stream, int n);
#ifdef Expression_SHARED_EXTRAS
Expression_SHARED_EXTRAS
@ -384,19 +370,19 @@ public:
#endif
};
// define constructor - typcase
class typcase_class : public Expression_class {
public:
Expression expr;
Cases cases;
public:
typcase_class(Expression a1, Cases a2) {
expr = a1;
cases = a2;
}
Expression copy_Expression();
void dump(ostream& stream, int n);
void dump(ostream &stream, int n);
#ifdef Expression_SHARED_EXTRAS
Expression_SHARED_EXTRAS
@ -406,17 +392,15 @@ public:
#endif
};
// define constructor - block
class block_class : public Expression_class {
public:
Expressions body;
public:
block_class(Expressions a1) {
body = a1;
}
block_class(Expressions a1) { body = a1; }
Expression copy_Expression();
void dump(ostream& stream, int n);
void dump(ostream &stream, int n);
#ifdef Expression_SHARED_EXTRAS
Expression_SHARED_EXTRAS
@ -426,7 +410,6 @@ public:
#endif
};
// define constructor - let
class let_class : public Expression_class {
public:
@ -434,6 +417,7 @@ public:
Symbol type_decl;
Expression init;
Expression body;
public:
let_class(Symbol a1, Symbol a2, Expression a3, Expression a4) {
identifier = a1;
@ -442,7 +426,7 @@ public:
body = a4;
}
Expression copy_Expression();
void dump(ostream& stream, int n);
void dump(ostream &stream, int n);
#ifdef Expression_SHARED_EXTRAS
Expression_SHARED_EXTRAS
@ -452,19 +436,19 @@ public:
#endif
};
// define constructor - plus
class plus_class : public Expression_class {
public:
Expression e1;
Expression e2;
public:
plus_class(Expression a1, Expression a2) {
e1 = a1;
e2 = a2;
}
Expression copy_Expression();
void dump(ostream& stream, int n);
void dump(ostream &stream, int n);
#ifdef Expression_SHARED_EXTRAS
Expression_SHARED_EXTRAS
@ -474,19 +458,19 @@ public:
#endif
};
// define constructor - sub
class sub_class : public Expression_class {
public:
Expression e1;
Expression e2;
public:
sub_class(Expression a1, Expression a2) {
e1 = a1;
e2 = a2;
}
Expression copy_Expression();
void dump(ostream& stream, int n);
void dump(ostream &stream, int n);
#ifdef Expression_SHARED_EXTRAS
Expression_SHARED_EXTRAS
@ -496,19 +480,19 @@ public:
#endif
};
// define constructor - mul
class mul_class : public Expression_class {
public:
Expression e1;
Expression e2;
public:
mul_class(Expression a1, Expression a2) {
e1 = a1;
e2 = a2;
}
Expression copy_Expression();
void dump(ostream& stream, int n);
void dump(ostream &stream, int n);
#ifdef Expression_SHARED_EXTRAS
Expression_SHARED_EXTRAS
@ -518,19 +502,19 @@ public:
#endif
};
// define constructor - divide
class divide_class : public Expression_class {
public:
Expression e1;
Expression e2;
public:
divide_class(Expression a1, Expression a2) {
e1 = a1;
e2 = a2;
}
Expression copy_Expression();
void dump(ostream& stream, int n);
void dump(ostream &stream, int n);
#ifdef Expression_SHARED_EXTRAS
Expression_SHARED_EXTRAS
@ -540,17 +524,15 @@ public:
#endif
};
// define constructor - neg
class neg_class : public Expression_class {
public:
Expression e1;
public:
neg_class(Expression a1) {
e1 = a1;
}
neg_class(Expression a1) { e1 = a1; }
Expression copy_Expression();
void dump(ostream& stream, int n);
void dump(ostream &stream, int n);
#ifdef Expression_SHARED_EXTRAS
Expression_SHARED_EXTRAS
@ -560,19 +542,19 @@ public:
#endif
};
// define constructor - lt
class lt_class : public Expression_class {
public:
Expression e1;
Expression e2;
public:
lt_class(Expression a1, Expression a2) {
e1 = a1;
e2 = a2;
}
Expression copy_Expression();
void dump(ostream& stream, int n);
void dump(ostream &stream, int n);
#ifdef Expression_SHARED_EXTRAS
Expression_SHARED_EXTRAS
@ -582,19 +564,19 @@ public:
#endif
};
// define constructor - eq
class eq_class : public Expression_class {
public:
Expression e1;
Expression e2;
public:
eq_class(Expression a1, Expression a2) {
e1 = a1;
e2 = a2;
}
Expression copy_Expression();
void dump(ostream& stream, int n);
void dump(ostream &stream, int n);
#ifdef Expression_SHARED_EXTRAS
Expression_SHARED_EXTRAS
@ -604,19 +586,19 @@ public:
#endif
};
// define constructor - leq
class leq_class : public Expression_class {
public:
Expression e1;
Expression e2;
public:
leq_class(Expression a1, Expression a2) {
e1 = a1;
e2 = a2;
}
Expression copy_Expression();
void dump(ostream& stream, int n);
void dump(ostream &stream, int n);
#ifdef Expression_SHARED_EXTRAS
Expression_SHARED_EXTRAS
@ -626,17 +608,15 @@ public:
#endif
};
// define constructor - comp
class comp_class : public Expression_class {
public:
Expression e1;
public:
comp_class(Expression a1) {
e1 = a1;
}
comp_class(Expression a1) { e1 = a1; }
Expression copy_Expression();
void dump(ostream& stream, int n);
void dump(ostream &stream, int n);
#ifdef Expression_SHARED_EXTRAS
Expression_SHARED_EXTRAS
@ -646,17 +626,15 @@ public:
#endif
};
// define constructor - int_const
class int_const_class : public Expression_class {
public:
Symbol token;
public:
int_const_class(Symbol a1) {
token = a1;
}
int_const_class(Symbol a1) { token = a1; }
Expression copy_Expression();
void dump(ostream& stream, int n);
void dump(ostream &stream, int n);
#ifdef Expression_SHARED_EXTRAS
Expression_SHARED_EXTRAS
@ -666,17 +644,15 @@ public:
#endif
};
// define constructor - bool_const
class bool_const_class : public Expression_class {
public:
Boolean val;
public:
bool_const_class(Boolean a1) {
val = a1;
}
bool_const_class(Boolean a1) { val = a1; }
Expression copy_Expression();
void dump(ostream& stream, int n);
void dump(ostream &stream, int n);
#ifdef Expression_SHARED_EXTRAS
Expression_SHARED_EXTRAS
@ -686,17 +662,15 @@ public:
#endif
};
// define constructor - string_const
class string_const_class : public Expression_class {
public:
Symbol token;
public:
string_const_class(Symbol a1) {
token = a1;
}
string_const_class(Symbol a1) { token = a1; }
Expression copy_Expression();
void dump(ostream& stream, int n);
void dump(ostream &stream, int n);
#ifdef Expression_SHARED_EXTRAS
Expression_SHARED_EXTRAS
@ -706,17 +680,15 @@ public:
#endif
};
// define constructor - new_
class new__class : public Expression_class {
public:
Symbol type_name;
public:
new__class(Symbol a1) {
type_name = a1;
}
new__class(Symbol a1) { type_name = a1; }
Expression copy_Expression();
void dump(ostream& stream, int n);
void dump(ostream &stream, int n);
#ifdef Expression_SHARED_EXTRAS
Expression_SHARED_EXTRAS
@ -726,17 +698,15 @@ public:
#endif
};
// define constructor - isvoid
class isvoid_class : public Expression_class {
public:
Expression e1;
public:
isvoid_class(Expression a1) {
e1 = a1;
}
isvoid_class(Expression a1) { e1 = a1; }
Expression copy_Expression();
void dump(ostream& stream, int n);
void dump(ostream &stream, int n);
#ifdef Expression_SHARED_EXTRAS
Expression_SHARED_EXTRAS
@ -746,15 +716,13 @@ public:
#endif
};
// define constructor - no_expr
class no_expr_class : public Expression_class {
public:
public:
no_expr_class() {
}
no_expr_class() {}
Expression copy_Expression();
void dump(ostream& stream, int n);
void dump(ostream &stream, int n);
#ifdef Expression_SHARED_EXTRAS
Expression_SHARED_EXTRAS
@ -764,17 +732,15 @@ public:
#endif
};
// define constructor - object
class object_class : public Expression_class {
public:
Symbol name;
public:
object_class(Symbol a1) {
name = a1;
}
object_class(Symbol a1) { name = a1; }
Expression copy_Expression();
void dump(ostream& stream, int n);
void dump(ostream &stream, int n);
#ifdef Expression_SHARED_EXTRAS
Expression_SHARED_EXTRAS
@ -784,7 +750,6 @@ public:
#endif
};
// define the prototypes of the interface
Classes nil_Classes();
Classes single_Classes(Class_);
@ -832,5 +797,4 @@ Expression isvoid(Expression);
Expression no_expr();
Expression object(Symbol);
#endif

View File

@ -4,19 +4,20 @@
#ifndef COOL_TREE_HANDCODE_H
#define COOL_TREE_HANDCODE_H
#include <iostream>
#include "tree.h"
#include "cool.h"
#include "stringtab.h"
#include "tree.h"
#include <iostream>
#define yylineno curr_lineno;
extern int yylineno;
inline Boolean copy_Boolean(Boolean b) {return b; }
inline Boolean copy_Boolean(Boolean b) { return b; }
inline void assert_Boolean(Boolean) {}
inline void dump_Boolean(ostream& stream, int padding, Boolean b)
{ stream << pad(padding) << (int) b << "\n"; }
inline void dump_Boolean(ostream &stream, int padding, Boolean b) {
stream << pad(padding) << (int)b << "\n";
}
void dump_Symbol(ostream& stream, int padding, Symbol b);
void dump_Symbol(ostream &stream, int padding, Symbol b);
void assert_Symbol(Symbol b);
Symbol copy_Symbol(Symbol b);
@ -45,65 +46,51 @@ typedef list_node<Case> Cases_class;
typedef Cases_class *Cases;
#define Program_EXTRAS \
virtual void cgen(ostream&) = 0; \
virtual void dump_with_types(ostream&, int) = 0;
virtual void cgen(ostream &) = 0; \
virtual void dump_with_types(ostream &, int) = 0;
#define program_EXTRAS \
void cgen(ostream&); \
void dump_with_types(ostream&, int);
void cgen(ostream &); \
void dump_with_types(ostream &, int);
#define Class__EXTRAS \
virtual Symbol get_name() = 0; \
virtual Symbol get_parent() = 0; \
virtual Symbol get_filename() = 0; \
virtual void dump_with_types(ostream&,int) = 0;
virtual Symbol get_name() = 0; \
virtual Symbol get_parent() = 0; \
virtual Symbol get_filename() = 0; \
virtual void dump_with_types(ostream &, int) = 0;
#define class__EXTRAS \
Symbol get_name() { return name; } \
Symbol get_parent() { return parent; } \
Symbol get_filename() { return filename; } \
void dump_with_types(ostream&,int);
Symbol get_name() { return name; } \
Symbol get_parent() { return parent; } \
Symbol get_filename() { return filename; } \
void dump_with_types(ostream &, int);
#define Feature_EXTRAS virtual void dump_with_types(ostream &, int) = 0;
#define Feature_EXTRAS \
virtual void dump_with_types(ostream&,int) = 0;
#define Feature_SHARED_EXTRAS void dump_with_types(ostream &, int);
#define Formal_EXTRAS virtual void dump_with_types(ostream &, int) = 0;
#define Feature_SHARED_EXTRAS \
void dump_with_types(ostream&,int);
#define formal_EXTRAS void dump_with_types(ostream &, int);
#define Case_EXTRAS virtual void dump_with_types(ostream &, int) = 0;
#define Formal_EXTRAS \
virtual void dump_with_types(ostream&,int) = 0;
#define formal_EXTRAS \
void dump_with_types(ostream&,int);
#define Case_EXTRAS \
virtual void dump_with_types(ostream& ,int) = 0;
#define branch_EXTRAS \
void dump_with_types(ostream& ,int);
#define branch_EXTRAS void dump_with_types(ostream &, int);
#define Expression_EXTRAS \
Symbol type; \
Symbol get_type() { return type; } \
Expression set_type(Symbol s) { type = s; return this; } \
virtual void code(ostream&) = 0; \
virtual void dump_with_types(ostream&,int) = 0; \
void dump_type(ostream&, int); \
Expression_class() { type = (Symbol) NULL; }
Symbol type; \
Symbol get_type() { return type; } \
Expression set_type(Symbol s) { \
type = s; \
return this; \
} \
virtual void code(ostream &) = 0; \
virtual void dump_with_types(ostream &, int) = 0; \
void dump_type(ostream &, int); \
Expression_class() { type = (Symbol)NULL; }
#define Expression_SHARED_EXTRAS \
void code(ostream&); \
void dump_with_types(ostream&,int);
void code(ostream &); \
void dump_with_types(ostream &, int);
#endif

View File

@ -3,7 +3,27 @@
as possible.
*)
class Main {
class A inherits IO {
attr_A_1 : Int <- 10;
attr_A_2 : Bool <- false;
attr_A_3 : A <- self;
method_common(x1: Int) : SELF_TYPE {
self
};
};
class B inherits A {
};
class C inherits A {
attr_C_1 : String <- "This is C\n";
method_common(x1: Int) : SELF_TYPE {
self
};
};
class Main inherits IO {
main():Int { 0 };
};

BIN
handouts/cool-runtime.pdf Normal file

Binary file not shown.