diff --git a/CS143体验报告.md b/CS143体验报告.md index 9ea03b1..2bebfec 100644 --- a/CS143体验报告.md +++ b/CS143体验报告.md @@ -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 设为-1;Object 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` 返回,这里打印出提示信息并终止执行。 diff --git a/assignments/PA5/cgen.cc b/assignments/PA5/cgen.cc index 08b4220..45624d8 100644 --- a/assignments/PA5/cgen.cc +++ b/assignments/PA5/cgen.cc @@ -24,8 +24,9 @@ #include "cgen.h" #include "cgen_gc.h" +#include -extern void emit_string_constant(ostream& str, char *s); +extern void emit_string_constant(ostream &str, char *s); extern int cgen_debug; // @@ -43,73 +44,46 @@ extern int cgen_debug; // as fixed names used by the runtime system. // ////////////////////////////////////////////////////////////////////// -Symbol - arg, - arg2, - Bool, - concat, - cool_abort, - copy, - Int, - in_int, - in_string, - IO, - length, - Main, - main_meth, - No_class, - No_type, - Object, - out_int, - out_string, - prim_slot, - self, - SELF_TYPE, - Str, - str_field, - substr, - type_name, - val; +Symbol arg, arg2, Bool, concat, cool_abort, copy, Int, in_int, in_string, IO, + length, Main, main_meth, No_class, No_type, Object, out_int, out_string, + prim_slot, self, SELF_TYPE, Str, str_field, substr, type_name, val; // // Initializing the predefined symbols. // -static void initialize_constants(void) -{ - arg = idtable.add_string("arg"); - arg2 = idtable.add_string("arg2"); - Bool = idtable.add_string("Bool"); - concat = idtable.add_string("concat"); - cool_abort = idtable.add_string("abort"); - copy = idtable.add_string("copy"); - Int = idtable.add_string("Int"); - in_int = idtable.add_string("in_int"); - in_string = idtable.add_string("in_string"); - IO = idtable.add_string("IO"); - length = idtable.add_string("length"); - Main = idtable.add_string("Main"); - main_meth = idtable.add_string("main"); -// _no_class is a symbol that can't be the name of any -// user-defined class. - No_class = idtable.add_string("_no_class"); - No_type = idtable.add_string("_no_type"); - Object = idtable.add_string("Object"); - out_int = idtable.add_string("out_int"); - out_string = idtable.add_string("out_string"); - prim_slot = idtable.add_string("_prim_slot"); - self = idtable.add_string("self"); - SELF_TYPE = idtable.add_string("SELF_TYPE"); - Str = idtable.add_string("String"); - str_field = idtable.add_string("_str_field"); - substr = idtable.add_string("substr"); - type_name = idtable.add_string("type_name"); - val = idtable.add_string("_val"); +static void initialize_constants(void) { + arg = idtable.add_string("arg"); + arg2 = idtable.add_string("arg2"); + Bool = idtable.add_string("Bool"); + concat = idtable.add_string("concat"); + cool_abort = idtable.add_string("abort"); + copy = idtable.add_string("copy"); + Int = idtable.add_string("Int"); + in_int = idtable.add_string("in_int"); + in_string = idtable.add_string("in_string"); + IO = idtable.add_string("IO"); + length = idtable.add_string("length"); + Main = idtable.add_string("Main"); + main_meth = idtable.add_string("main"); + // _no_class is a symbol that can't be the name of any + // user-defined class. + No_class = idtable.add_string("_no_class"); + No_type = idtable.add_string("_no_type"); + Object = idtable.add_string("Object"); + out_int = idtable.add_string("out_int"); + out_string = idtable.add_string("out_string"); + prim_slot = idtable.add_string("_prim_slot"); + self = idtable.add_string("self"); + SELF_TYPE = idtable.add_string("SELF_TYPE"); + Str = idtable.add_string("String"); + str_field = idtable.add_string("_str_field"); + substr = idtable.add_string("substr"); + type_name = idtable.add_string("type_name"); + val = idtable.add_string("_val"); } -static char *gc_init_names[] = - { "_NoGC_Init", "_GenGC_Init", "_ScnGC_Init" }; -static char *gc_collect_names[] = - { "_NoGC_Collect", "_GenGC_Collect", "_ScnGC_Collect" }; - +static char *gc_init_names[] = {"_NoGC_Init", "_GenGC_Init", "_ScnGC_Init"}; +static char *gc_collect_names[] = {"_NoGC_Collect", "_GenGC_Collect", + "_ScnGC_Collect"}; // BoolConst is a class that implements code generation for operations // on the two booleans, which are given global names here. @@ -129,18 +103,16 @@ BoolConst truebool(TRUE); // //********************************************************* -void program_class::cgen(ostream &os) -{ +void program_class::cgen(ostream &os) { // spim wants comments to start with '#' os << "# start of generated code\n"; initialize_constants(); - CgenClassTable *codegen_classtable = new CgenClassTable(classes,os); + CgenClassTable *codegen_classtable = new CgenClassTable(classes, os); os << "\n# end of generated code\n"; } - ////////////////////////////////////////////////////////////////////////////// // // emit_* procedures @@ -155,171 +127,171 @@ void program_class::cgen(ostream &os) // ////////////////////////////////////////////////////////////////////////////// -static void emit_load(char *dest_reg, int offset, char *source_reg, ostream& s) -{ - s << LW << dest_reg << " " << offset * WORD_SIZE << "(" << source_reg << ")" +static void emit_load(char *dest_reg, int offset, char *source_reg, + ostream &s) { + s << LW << dest_reg << " " << offset * WORD_SIZE << "(" << source_reg << ")" << endl; } -static void emit_store(char *source_reg, int offset, char *dest_reg, ostream& s) -{ +static void emit_store(char *source_reg, int offset, char *dest_reg, + ostream &s) { s << SW << source_reg << " " << offset * WORD_SIZE << "(" << dest_reg << ")" - << endl; + << endl; } -static void emit_load_imm(char *dest_reg, int val, ostream& s) -{ s << LI << dest_reg << " " << val << endl; } +static void emit_load_imm(char *dest_reg, int val, ostream &s) { + s << LI << dest_reg << " " << val << endl; +} -static void emit_load_address(char *dest_reg, char *address, ostream& s) -{ s << LA << dest_reg << " " << address << endl; } +static void emit_load_address(char *dest_reg, char *address, ostream &s) { + s << LA << dest_reg << " " << address << endl; +} -static void emit_partial_load_address(char *dest_reg, ostream& s) -{ s << LA << dest_reg << " "; } +static void emit_partial_load_address(char *dest_reg, ostream &s) { + s << LA << dest_reg << " "; +} -static void emit_load_bool(char *dest, const BoolConst& b, ostream& s) -{ - emit_partial_load_address(dest,s); +static void emit_load_bool(char *dest, const BoolConst &b, ostream &s) { + emit_partial_load_address(dest, s); b.code_ref(s); s << endl; } -static void emit_load_string(char *dest, StringEntry *str, ostream& s) -{ - emit_partial_load_address(dest,s); +static void emit_load_string(char *dest, StringEntry *str, ostream &s) { + emit_partial_load_address(dest, s); str->code_ref(s); s << endl; } -static void emit_load_int(char *dest, IntEntry *i, ostream& s) -{ - emit_partial_load_address(dest,s); +static void emit_load_int(char *dest, IntEntry *i, ostream &s) { + emit_partial_load_address(dest, s); i->code_ref(s); s << endl; } -static void emit_move(char *dest_reg, char *source_reg, ostream& s) -{ s << MOVE << dest_reg << " " << source_reg << endl; } +static void emit_move(char *dest_reg, char *source_reg, ostream &s) { + s << MOVE << dest_reg << " " << source_reg << endl; +} -static void emit_neg(char *dest, char *src1, ostream& s) -{ s << NEG << dest << " " << src1 << endl; } +static void emit_neg(char *dest, char *src1, ostream &s) { + s << NEG << dest << " " << src1 << endl; +} -static void emit_add(char *dest, char *src1, char *src2, ostream& s) -{ s << ADD << dest << " " << src1 << " " << src2 << endl; } +static void emit_add(char *dest, char *src1, char *src2, ostream &s) { + s << ADD << dest << " " << src1 << " " << src2 << endl; +} -static void emit_addu(char *dest, char *src1, char *src2, ostream& s) -{ s << ADDU << dest << " " << src1 << " " << src2 << endl; } +static void emit_addu(char *dest, char *src1, char *src2, ostream &s) { + s << ADDU << dest << " " << src1 << " " << src2 << endl; +} -static void emit_addiu(char *dest, char *src1, int imm, ostream& s) -{ s << ADDIU << dest << " " << src1 << " " << imm << endl; } +static void emit_addiu(char *dest, char *src1, int imm, ostream &s) { + s << ADDIU << dest << " " << src1 << " " << imm << endl; +} -static void emit_div(char *dest, char *src1, char *src2, ostream& s) -{ s << DIV << dest << " " << src1 << " " << src2 << endl; } +static void emit_div(char *dest, char *src1, char *src2, ostream &s) { + s << DIV << dest << " " << src1 << " " << src2 << endl; +} -static void emit_mul(char *dest, char *src1, char *src2, ostream& s) -{ s << MUL << dest << " " << src1 << " " << src2 << endl; } +static void emit_mul(char *dest, char *src1, char *src2, ostream &s) { + s << MUL << dest << " " << src1 << " " << src2 << endl; +} -static void emit_sub(char *dest, char *src1, char *src2, ostream& s) -{ s << SUB << dest << " " << src1 << " " << src2 << endl; } +static void emit_sub(char *dest, char *src1, char *src2, ostream &s) { + s << SUB << dest << " " << src1 << " " << src2 << endl; +} -static void emit_sll(char *dest, char *src1, int num, ostream& s) -{ s << SLL << dest << " " << src1 << " " << num << endl; } +static void emit_sll(char *dest, char *src1, int num, ostream &s) { + s << SLL << dest << " " << src1 << " " << num << endl; +} -static void emit_jalr(char *dest, ostream& s) -{ s << JALR << "\t" << dest << endl; } +static void emit_jalr(char *dest, ostream &s) { + s << JALR << "\t" << dest << endl; +} -static void emit_jal(char *address,ostream &s) -{ s << JAL << address << endl; } +static void emit_jal(char *address, ostream &s) { s << JAL << address << endl; } -static void emit_return(ostream& s) -{ s << RET << endl; } +static void emit_return(ostream &s) { s << RET << endl; } -static void emit_gc_assign(ostream& s) -{ s << JAL << "_GenGC_Assign" << endl; } +static void emit_gc_assign(ostream &s) { s << JAL << "_GenGC_Assign" << endl; } -static void emit_disptable_ref(Symbol sym, ostream& s) -{ s << sym << DISPTAB_SUFFIX; } +static void emit_disptable_ref(Symbol sym, ostream &s) { + s << sym << DISPTAB_SUFFIX; +} -static void emit_init_ref(Symbol sym, ostream& s) -{ s << sym << CLASSINIT_SUFFIX; } +static void emit_init_ref(Symbol sym, ostream &s) { + s << sym << CLASSINIT_SUFFIX; +} -static void emit_label_ref(int l, ostream &s) -{ s << "label" << l; } +static void emit_label_ref(int l, ostream &s) { s << "label" << l; } -static void emit_protobj_ref(Symbol sym, ostream& s) -{ s << sym << PROTOBJ_SUFFIX; } +static void emit_protobj_ref(Symbol sym, ostream &s) { + s << sym << PROTOBJ_SUFFIX; +} -static void emit_method_ref(Symbol classname, Symbol methodname, ostream& s) -{ s << classname << METHOD_SEP << methodname; } +static void emit_method_ref(Symbol classname, Symbol methodname, ostream &s) { + s << classname << METHOD_SEP << methodname; +} -static void emit_label_def(int l, ostream &s) -{ - emit_label_ref(l,s); +static void emit_label_def(int l, ostream &s) { + emit_label_ref(l, s); s << ":" << endl; } -static void emit_beqz(char *source, int label, ostream &s) -{ +static void emit_beqz(char *source, int label, ostream &s) { s << BEQZ << source << " "; - emit_label_ref(label,s); + emit_label_ref(label, s); s << endl; } -static void emit_beq(char *src1, char *src2, int label, ostream &s) -{ +static void emit_beq(char *src1, char *src2, int label, ostream &s) { s << BEQ << src1 << " " << src2 << " "; - emit_label_ref(label,s); + emit_label_ref(label, s); s << endl; } -static void emit_bne(char *src1, char *src2, int label, ostream &s) -{ +static void emit_bne(char *src1, char *src2, int label, ostream &s) { s << BNE << src1 << " " << src2 << " "; - emit_label_ref(label,s); + emit_label_ref(label, s); s << endl; } -static void emit_bleq(char *src1, char *src2, int label, ostream &s) -{ +static void emit_bleq(char *src1, char *src2, int label, ostream &s) { s << BLEQ << src1 << " " << src2 << " "; - emit_label_ref(label,s); + emit_label_ref(label, s); s << endl; } -static void emit_blt(char *src1, char *src2, int label, ostream &s) -{ +static void emit_blt(char *src1, char *src2, int label, ostream &s) { s << BLT << src1 << " " << src2 << " "; - emit_label_ref(label,s); + emit_label_ref(label, s); s << endl; } -static void emit_blti(char *src1, int imm, int label, ostream &s) -{ +static void emit_blti(char *src1, int imm, int label, ostream &s) { s << BLT << src1 << " " << imm << " "; - emit_label_ref(label,s); + emit_label_ref(label, s); s << endl; } -static void emit_bgti(char *src1, int imm, int label, ostream &s) -{ +static void emit_bgti(char *src1, int imm, int label, ostream &s) { s << BGT << src1 << " " << imm << " "; - emit_label_ref(label,s); + emit_label_ref(label, s); s << endl; } -static void emit_branch(int l, ostream& s) -{ +static void emit_branch(int l, ostream &s) { s << BRANCH; - emit_label_ref(l,s); + emit_label_ref(l, s); s << endl; } // // Push a register on the stack. The stack grows towards smaller addresses. // -static void emit_push(char *reg, ostream& str) -{ - emit_store(reg,0,SP,str); - emit_addiu(SP,SP,-4,str); +static void emit_push(char *reg, ostream &str) { + emit_store(reg, 0, SP, str); + emit_addiu(SP, SP, -4, str); } // @@ -327,34 +299,33 @@ static void emit_push(char *reg, ostream& str) // Emits code to fetch the integer value of the Integer object pointed // to by register source into the register dest // -static void emit_fetch_int(char *dest, char *source, ostream& s) -{ emit_load(dest, DEFAULT_OBJFIELDS, source, s); } +static void emit_fetch_int(char *dest, char *source, ostream &s) { + emit_load(dest, DEFAULT_OBJFIELDS, source, s); +} // // Emits code to store the integer value contained in register source // into the Integer object pointed to by dest. // -static void emit_store_int(char *source, char *dest, ostream& s) -{ emit_store(source, DEFAULT_OBJFIELDS, dest, s); } +static void emit_store_int(char *source, char *dest, ostream &s) { + emit_store(source, DEFAULT_OBJFIELDS, dest, s); +} - -static void emit_test_collector(ostream &s) -{ +static void emit_test_collector(ostream &s) { emit_push(ACC, s); - emit_move(ACC, SP, s); // stack end + emit_move(ACC, SP, s); // stack end emit_move(A1, ZERO, s); // allocate nothing s << JAL << gc_collect_names[cgen_Memmgr] << endl; - emit_addiu(SP,SP,4,s); - emit_load(ACC,0,SP,s); + emit_addiu(SP, SP, 4, s); + emit_load(ACC, 0, SP, s); } -static void emit_gc_check(char *source, ostream &s) -{ - if (source != (char*)A1) emit_move(A1, source, s); +static void emit_gc_check(char *source, ostream &s) { + if (source != (char *)A1) + emit_move(A1, source, s); s << JAL << "_gc_check" << endl; } - /////////////////////////////////////////////////////////////////////////////// // // coding strings, ints, and booleans @@ -379,119 +350,108 @@ static void emit_gc_check(char *source, ostream &s) // // Strings // -void StringEntry::code_ref(ostream& s) -{ - s << STRCONST_PREFIX << index; -} +void StringEntry::code_ref(ostream &s) { s << STRCONST_PREFIX << index; } // // Emit code for a constant String. // You should fill in the code naming the dispatch table. // -void StringEntry::code_def(ostream& s, int stringclasstag) -{ +void StringEntry::code_def(ostream &s, int stringclasstag) { IntEntryP lensym = inttable.add_int(len); // Add -1 eye catcher s << WORD << "-1" << endl; - code_ref(s); s << LABEL // label - << WORD << stringclasstag << endl // tag - << WORD << (DEFAULT_OBJFIELDS + STRING_SLOTS + (len+4)/4) << endl // size - << WORD; + code_ref(s); + s << LABEL // label + << WORD << stringclasstag << endl // tag + << WORD << (DEFAULT_OBJFIELDS + STRING_SLOTS + (len + 4) / 4) + << endl // size + << WORD; + /***** Add dispatch information for class String ******/ - /***** Add dispatch information for class String ******/ - - s << endl; // dispatch table - s << WORD; lensym->code_ref(s); s << endl; // string length - emit_string_constant(s,str); // ascii string - s << ALIGN; // align to word + s << endl; // dispatch table + s << WORD; + lensym->code_ref(s); + s << endl; // string length + emit_string_constant(s, str); // ascii string + s << ALIGN; // align to word } // // StrTable::code_string -// Generate a string object definition for every string constant in the +// Generate a string object definition for every string constant in the // stringtable. // -void StrTable::code_string_table(ostream& s, int stringclasstag) -{ +void StrTable::code_string_table(ostream &s, int stringclasstag) { for (List *l = tbl; l; l = l->tl()) - l->hd()->code_def(s,stringclasstag); + l->hd()->code_def(s, stringclasstag); } // // Ints // -void IntEntry::code_ref(ostream &s) -{ - s << INTCONST_PREFIX << index; -} +void IntEntry::code_ref(ostream &s) { s << INTCONST_PREFIX << index; } // // Emit code for a constant Integer. // You should fill in the code naming the dispatch table. // -void IntEntry::code_def(ostream &s, int intclasstag) -{ +void IntEntry::code_def(ostream &s, int intclasstag) { // Add -1 eye catcher s << WORD << "-1" << endl; - code_ref(s); s << LABEL // label - << WORD << intclasstag << endl // class tag - << WORD << (DEFAULT_OBJFIELDS + INT_SLOTS) << endl // object size - << WORD; + code_ref(s); + s << LABEL // label + << WORD << intclasstag << endl // class tag + << WORD << (DEFAULT_OBJFIELDS + INT_SLOTS) << endl // object size + << WORD; - /***** Add dispatch information for class Int ******/ + /***** Add dispatch information for class Int ******/ - s << endl; // dispatch table - s << WORD << str << endl; // integer value + s << endl; // dispatch table + s << WORD << str << endl; // integer value } - // // IntTable::code_string_table // Generate an Int object definition for every Int constant in the // inttable. // -void IntTable::code_string_table(ostream &s, int intclasstag) -{ +void IntTable::code_string_table(ostream &s, int intclasstag) { for (List *l = tbl; l; l = l->tl()) - l->hd()->code_def(s,intclasstag); + l->hd()->code_def(s, intclasstag); } - // // Bools // BoolConst::BoolConst(int i) : val(i) { assert(i == 0 || i == 1); } -void BoolConst::code_ref(ostream& s) const -{ - s << BOOLCONST_PREFIX << val; -} - +void BoolConst::code_ref(ostream &s) const { s << BOOLCONST_PREFIX << val; } + // // Emit code for a constant Bool. // You should fill in the code naming the dispatch table. // -void BoolConst::code_def(ostream& s, int boolclasstag) -{ +void BoolConst::code_def(ostream &s, int boolclasstag) { // Add -1 eye catcher s << WORD << "-1" << endl; - code_ref(s); s << LABEL // label - << WORD << boolclasstag << endl // class tag - << WORD << (DEFAULT_OBJFIELDS + BOOL_SLOTS) << endl // object size - << WORD; + code_ref(s); + s << LABEL // label + << WORD << boolclasstag << endl // class tag + << WORD << (DEFAULT_OBJFIELDS + BOOL_SLOTS) << endl // object size + << WORD; - /***** Add dispatch information for class Bool ******/ + /***** Add dispatch information for class Bool ******/ - s << endl; // dispatch table - s << WORD << val << endl; // value (0 or 1) + s << endl; // dispatch table + s << WORD << val << endl; // value (0 or 1) } ////////////////////////////////////////////////////////////////////////////// @@ -507,23 +467,32 @@ void BoolConst::code_def(ostream& s, int boolclasstag) // //*************************************************** -void CgenClassTable::code_global_data() -{ - Symbol main = idtable.lookup_string(MAINNAME); - Symbol string = idtable.lookup_string(STRINGNAME); +void CgenClassTable::code_global_data() { + Symbol main = idtable.lookup_string(MAINNAME); + Symbol string = idtable.lookup_string(STRINGNAME); Symbol integer = idtable.lookup_string(INTNAME); - Symbol boolc = idtable.lookup_string(BOOLNAME); + Symbol boolc = idtable.lookup_string(BOOLNAME); str << "\t.data\n" << ALIGN; // // The following global names must be defined first. // str << GLOBAL << CLASSNAMETAB << endl; - str << GLOBAL; emit_protobj_ref(main,str); str << endl; - str << GLOBAL; emit_protobj_ref(integer,str); str << endl; - str << GLOBAL; emit_protobj_ref(string,str); str << endl; - str << GLOBAL; falsebool.code_ref(str); str << endl; - str << GLOBAL; truebool.code_ref(str); str << endl; + str << GLOBAL; + emit_protobj_ref(main, str); + str << endl; + str << GLOBAL; + emit_protobj_ref(integer, str); + str << endl; + str << GLOBAL; + emit_protobj_ref(string, str); + str << endl; + str << GLOBAL; + falsebool.code_ref(str); + str << endl; + str << GLOBAL; + truebool.code_ref(str); + str << endl; str << GLOBAL << INTTAG << endl; str << GLOBAL << BOOLTAG << endl; str << GLOBAL << STRINGTAG << endl; @@ -532,15 +501,11 @@ void CgenClassTable::code_global_data() // We also need to know the tag of the Int, String, and Bool classes // during code generation. // - str << INTTAG << LABEL - << WORD << intclasstag << endl; - str << BOOLTAG << LABEL - << WORD << boolclasstag << endl; - str << STRINGTAG << LABEL - << WORD << stringclasstag << endl; + str << INTTAG << LABEL << WORD << intclasstag << endl; + str << BOOLTAG << LABEL << WORD << boolclasstag << endl; + str << STRINGTAG << LABEL << WORD << stringclasstag << endl; } - //*************************************************** // // Emit code to start the .text segment and to @@ -548,33 +513,29 @@ void CgenClassTable::code_global_data() // //*************************************************** -void CgenClassTable::code_global_text() -{ +void CgenClassTable::code_global_text() { str << GLOBAL << HEAP_START << endl - << HEAP_START << LABEL - << WORD << 0 << endl + << HEAP_START << LABEL << WORD << 0 << endl << "\t.text" << endl << GLOBAL; emit_init_ref(idtable.add_string("Main"), str); str << endl << GLOBAL; - emit_init_ref(idtable.add_string("Int"),str); + emit_init_ref(idtable.add_string("Int"), str); str << endl << GLOBAL; - emit_init_ref(idtable.add_string("String"),str); + emit_init_ref(idtable.add_string("String"), str); str << endl << GLOBAL; - emit_init_ref(idtable.add_string("Bool"),str); + emit_init_ref(idtable.add_string("Bool"), str); str << endl << GLOBAL; emit_method_ref(idtable.add_string("Main"), idtable.add_string("main"), str); str << endl; } -void CgenClassTable::code_bools(int boolclasstag) -{ - falsebool.code_def(str,boolclasstag); - truebool.code_def(str,boolclasstag); +void CgenClassTable::code_bools(int boolclasstag) { + falsebool.code_def(str, boolclasstag); + truebool.code_def(str, boolclasstag); } -void CgenClassTable::code_select_gc() -{ +void CgenClassTable::code_select_gc() { // // Generate GC choice constants (pointers to GC functions) // @@ -589,7 +550,6 @@ void CgenClassTable::code_select_gc() str << WORD << (cgen_Memmgr_Test == GC_TEST) << endl; } - //******************************************************** // // Emit code to reserve space for and initialize all of @@ -603,156 +563,153 @@ void CgenClassTable::code_select_gc() // //******************************************************** -void CgenClassTable::code_constants() -{ +void CgenClassTable::code_constants() { // // Add constants that are required by the code generator. // stringtable.add_string(""); inttable.add_string("0"); - stringtable.code_string_table(str,stringclasstag); - inttable.code_string_table(str,intclasstag); + stringtable.code_string_table(str, stringclasstag); + inttable.code_string_table(str, intclasstag); code_bools(boolclasstag); } +CgenClassTable::CgenClassTable(Classes classes, ostream &s) + : nds(NULL), str(s) { + stringclasstag = 1 /* Change to your String class tag here */; + intclasstag = 2 /* Change to your Int class tag here */; + boolclasstag = 3 /* Change to your Bool class tag here */; -CgenClassTable::CgenClassTable(Classes classes, ostream& s) : nds(NULL) , str(s) -{ - stringclasstag = 0 /* Change to your String class tag here */; - intclasstag = 0 /* Change to your Int class tag here */; - boolclasstag = 0 /* Change to your Bool class tag here */; - - enterscope(); - if (cgen_debug) cout << "Building CgenClassTable" << endl; - install_basic_classes(); - install_classes(classes); - build_inheritance_tree(); - - code(); - exitscope(); + enterscope(); + if (cgen_debug) + cout << "Building CgenClassTable" << endl; + install_basic_classes(); + install_classes(classes); + build_inheritance_tree(); + if (cgen_debug) + dump_inheritance_tree(); + code(); + exitscope(); } -void CgenClassTable::install_basic_classes() -{ +void CgenClassTable::install_basic_classes() { -// The tree package uses these globals to annotate the classes built below. - //curr_lineno = 0; + // The tree package uses these globals to annotate the classes built below. + // curr_lineno = 0; Symbol filename = stringtable.add_string(""); -// -// A few special class names are installed in the lookup table but not -// the class list. Thus, these classes exist, but are not part of the -// inheritance hierarchy. -// No_class serves as the parent of Object and the other special classes. -// SELF_TYPE is the self class; it cannot be redefined or inherited. -// prim_slot is a class known to the code generator. -// + // + // A few special class names are installed in the lookup table but not + // the class list. Thus, these classes exist, but are not part of the + // inheritance hierarchy. + // No_class serves as the parent of Object and the other special classes. + // SELF_TYPE is the self class; it cannot be redefined or inherited. + // prim_slot is a class known to the code generator. + // addid(No_class, - new CgenNode(class_(No_class,No_class,nil_Features(),filename), - Basic,this)); + new CgenNode(class_(No_class, No_class, nil_Features(), filename), + Basic, this)); addid(SELF_TYPE, - new CgenNode(class_(SELF_TYPE,No_class,nil_Features(),filename), - Basic,this)); + new CgenNode(class_(SELF_TYPE, No_class, nil_Features(), filename), + Basic, this)); addid(prim_slot, - new CgenNode(class_(prim_slot,No_class,nil_Features(),filename), - Basic,this)); + new CgenNode(class_(prim_slot, No_class, nil_Features(), filename), + Basic, this)); -// -// The Object class has no parent class. Its methods are -// cool_abort() : Object aborts the program -// type_name() : Str returns a string representation of class name -// copy() : SELF_TYPE returns a copy of the object -// -// There is no need for method bodies in the basic classes---these -// are already built in to the runtime system. -// - install_class( - new CgenNode( - class_(Object, - No_class, - append_Features( - append_Features( - single_Features(method(cool_abort, nil_Formals(), Object, no_expr())), - single_Features(method(type_name, nil_Formals(), Str, no_expr()))), - single_Features(method(copy, nil_Formals(), SELF_TYPE, no_expr()))), - filename), - Basic,this)); + // + // The Object class has no parent class. Its methods are + // cool_abort() : Object aborts the program + // type_name() : Str returns a string representation of class + // name copy() : SELF_TYPE returns a copy of the object + // + // There is no need for method bodies in the basic classes---these + // are already built in to the runtime system. + // + install_class(new CgenNode( + class_( + Object, No_class, + append_Features( + append_Features(single_Features(method(cool_abort, nil_Formals(), + Object, no_expr())), + single_Features(method(type_name, nil_Formals(), + Str, no_expr()))), + single_Features( + method(copy, nil_Formals(), SELF_TYPE, no_expr()))), + filename), + Basic, this)); -// -// The IO class inherits from Object. Its methods are -// out_string(Str) : SELF_TYPE writes a string to the output -// out_int(Int) : SELF_TYPE " an int " " " -// in_string() : Str reads a string from the input -// in_int() : Int " an int " " " -// - install_class( - new CgenNode( - class_(IO, - Object, - append_Features( - append_Features( - append_Features( - single_Features(method(out_string, single_Formals(formal(arg, Str)), - SELF_TYPE, no_expr())), - single_Features(method(out_int, single_Formals(formal(arg, Int)), - SELF_TYPE, no_expr()))), - single_Features(method(in_string, nil_Formals(), Str, no_expr()))), - single_Features(method(in_int, nil_Formals(), Int, no_expr()))), - filename), - Basic,this)); + // + // The IO class inherits from Object. Its methods are + // out_string(Str) : SELF_TYPE writes a string to the output + // out_int(Int) : SELF_TYPE " an int " " " + // in_string() : Str reads a string from the input + // in_int() : Int " an int " " " + // + install_class(new CgenNode( + class_( + IO, Object, + append_Features( + append_Features( + append_Features( + single_Features(method(out_string, + single_Formals(formal(arg, Str)), + SELF_TYPE, no_expr())), + single_Features(method(out_int, + single_Formals(formal(arg, Int)), + SELF_TYPE, no_expr()))), + single_Features( + method(in_string, nil_Formals(), Str, no_expr()))), + single_Features(method(in_int, nil_Formals(), Int, no_expr()))), + filename), + Basic, this)); -// -// The Int class has no methods and only a single attribute, the -// "val" for the integer. -// - install_class( - new CgenNode( - class_(Int, - Object, - single_Features(attr(val, prim_slot, no_expr())), - filename), - Basic,this)); + // + // The Int class has no methods and only a single attribute, the + // "val" for the integer. + // + install_class(new CgenNode( + class_(Int, Object, single_Features(attr(val, prim_slot, no_expr())), + filename), + Basic, this)); -// -// Bool also has only the "val" slot. -// - install_class( - new CgenNode( - class_(Bool, Object, single_Features(attr(val, prim_slot, no_expr())),filename), - Basic,this)); + // + // Bool also has only the "val" slot. + // + install_class(new CgenNode( + class_(Bool, Object, single_Features(attr(val, prim_slot, no_expr())), + filename), + Basic, this)); -// -// The class Str has a number of slots and operations: -// val ??? -// str_field the string itself -// length() : Int length of the string -// concat(arg: Str) : Str string concatenation -// substr(arg: Int, arg2: Int): Str substring -// - install_class( - new CgenNode( - class_(Str, - Object, + // + // The class Str has a number of slots and operations: + // val ??? + // str_field the string itself + // length() : Int length of the string + // concat(arg: Str) : Str string concatenation + // substr(arg: Int, arg2: Int): Str substring + // + install_class(new CgenNode( + class_(Str, Object, append_Features( - append_Features( - append_Features( - append_Features( - single_Features(attr(val, Int, no_expr())), - single_Features(attr(str_field, prim_slot, no_expr()))), - single_Features(method(length, nil_Formals(), Int, no_expr()))), - single_Features(method(concat, - single_Formals(formal(arg, Str)), - Str, - no_expr()))), - single_Features(method(substr, - append_Formals(single_Formals(formal(arg, Int)), - single_Formals(formal(arg2, Int))), - Str, - no_expr()))), - filename), - Basic,this)); - + append_Features( + append_Features( + append_Features( + single_Features(attr(val, Int, no_expr())), + single_Features( + attr(str_field, prim_slot, no_expr()))), + single_Features( + method(length, nil_Formals(), Int, no_expr()))), + single_Features(method(concat, + single_Formals(formal(arg, Str)), + Str, no_expr()))), + single_Features( + method(substr, + append_Formals(single_Formals(formal(arg, Int)), + single_Formals(formal(arg2, Int))), + Str, no_expr()))), + filename), + Basic, this)); } // CgenClassTable::install_class @@ -760,34 +717,30 @@ void CgenClassTable::install_basic_classes() // // install_classes enters a list of classes in the symbol table. // -void CgenClassTable::install_class(CgenNodeP nd) -{ +void CgenClassTable::install_class(CgenNodeP nd) { Symbol name = nd->get_name(); - if (probe(name)) - { - return; - } + if (probe(name)) { + return; + } // The class name is legal, so add it to the list of classes // and the symbol table. - nds = new List(nd,nds); - addid(name,nd); + nds = new List(nd, nds); + addid(name, nd); } -void CgenClassTable::install_classes(Classes cs) -{ - for(int i = cs->first(); cs->more(i); i = cs->next(i)) - install_class(new CgenNode(cs->nth(i),NotBasic,this)); +void CgenClassTable::install_classes(Classes cs) { + for (int i = cs->first(); cs->more(i); i = cs->next(i)) + install_class(new CgenNode(cs->nth(i), NotBasic, this)); } // // CgenClassTable::build_inheritance_tree // -void CgenClassTable::build_inheritance_tree() -{ - for(List *l = nds; l; l = l->tl()) - set_relations(l->hd()); +void CgenClassTable::build_inheritance_tree() { + for (List *l = nds; l; l = l->tl()) + set_relations(l->hd()); } // @@ -796,60 +749,65 @@ void CgenClassTable::build_inheritance_tree() // Takes a CgenNode and locates its, and its parent's, inheritance nodes // via the class table. Parent and child pointers are added as appropriate. // -void CgenClassTable::set_relations(CgenNodeP nd) -{ +void CgenClassTable::set_relations(CgenNodeP nd) { CgenNode *parent_node = probe(nd->get_parent()); nd->set_parentnd(parent_node); parent_node->add_child(nd); } -void CgenNode::add_child(CgenNodeP n) -{ - children = new List(n,children); +void CgenNode::add_child(CgenNodeP n) { + children = new List(n, children); } -void CgenNode::set_parentnd(CgenNodeP p) -{ +void CgenNode::set_parentnd(CgenNodeP p) { assert(parentnd == NULL); assert(p != NULL); parentnd = p; } +void CgenClassTable::dump_inheritance_tree() { + CgenNode *object_node = nullptr; + for (auto l = nds; l; l = l->tl()) { + auto cur_node = l->hd(); + if (cur_node->name == Object) { + object_node = cur_node; + break; + } + } + object_node->traverse_dump(0); +} -void CgenClassTable::code() -{ - if (cgen_debug) cout << "coding global data" << endl; +void CgenClassTable::code() { + if (cgen_debug) + cout << "coding global data" << endl; code_global_data(); - if (cgen_debug) cout << "choosing gc" << endl; + if (cgen_debug) + cout << "choosing gc" << endl; code_select_gc(); - if (cgen_debug) cout << "coding constants" << endl; + if (cgen_debug) + cout << "coding constants" << endl; code_constants(); -// Add your code to emit -// - prototype objects -// - class_nameTab -// - dispatch tables -// + // Add your code to emit + // - prototype objects + // - class_nameTab + // - dispatch tables + // - if (cgen_debug) cout << "coding global text" << endl; + if (cgen_debug) + cout << "coding global text" << endl; code_global_text(); -// Add your code to emit -// - object initializer -// - the class methods -// - etc... - -} - - -CgenNodeP CgenClassTable::root() -{ - return probe(Object); + // Add your code to emit + // - object initializer + // - the class methods + // - etc... } +CgenNodeP CgenClassTable::root() { return probe(Object); } /////////////////////////////////////////////////////////////////////// // @@ -857,16 +815,12 @@ CgenNodeP CgenClassTable::root() // /////////////////////////////////////////////////////////////////////// -CgenNode::CgenNode(Class_ nd, Basicness bstatus, CgenClassTableP ct) : - class__class((const class__class &) *nd), - parentnd(NULL), - children(NULL), - basic_status(bstatus) -{ - stringtable.add_string(name->get_string()); // Add class name to string table +CgenNode::CgenNode(Class_ nd, Basicness bstatus, CgenClassTableP ct) + : class__class((const class__class &)*nd), parentnd(NULL), children(NULL), + basic_status(bstatus) { + stringtable.add_string(name->get_string()); // Add class name to string table } - //****************************************************************** // // Fill in the following methods to produce code for the @@ -877,85 +831,59 @@ CgenNode::CgenNode(Class_ nd, Basicness bstatus, CgenClassTableP ct) : // //***************************************************************** -void assign_class::code(ostream &s) { -} +void assign_class::code(ostream &s) {} -void static_dispatch_class::code(ostream &s) { -} +void static_dispatch_class::code(ostream &s) {} -void dispatch_class::code(ostream &s) { -} +void dispatch_class::code(ostream &s) {} -void cond_class::code(ostream &s) { -} +void cond_class::code(ostream &s) {} -void loop_class::code(ostream &s) { -} +void loop_class::code(ostream &s) {} -void typcase_class::code(ostream &s) { -} +void typcase_class::code(ostream &s) {} -void block_class::code(ostream &s) { -} +void block_class::code(ostream &s) {} -void let_class::code(ostream &s) { -} +void let_class::code(ostream &s) {} -void plus_class::code(ostream &s) { -} +void plus_class::code(ostream &s) {} -void sub_class::code(ostream &s) { -} +void sub_class::code(ostream &s) {} -void mul_class::code(ostream &s) { -} +void mul_class::code(ostream &s) {} -void divide_class::code(ostream &s) { -} +void divide_class::code(ostream &s) {} -void neg_class::code(ostream &s) { -} +void neg_class::code(ostream &s) {} -void lt_class::code(ostream &s) { -} +void lt_class::code(ostream &s) {} -void eq_class::code(ostream &s) { -} +void eq_class::code(ostream &s) {} -void leq_class::code(ostream &s) { -} +void leq_class::code(ostream &s) {} -void comp_class::code(ostream &s) { -} +void comp_class::code(ostream &s) {} -void int_const_class::code(ostream& s) -{ +void int_const_class::code(ostream &s) { // // Need to be sure we have an IntEntry *, not an arbitrary Symbol // - emit_load_int(ACC,inttable.lookup_string(token->get_string()),s); + emit_load_int(ACC, inttable.lookup_string(token->get_string()), s); } -void string_const_class::code(ostream& s) -{ - emit_load_string(ACC,stringtable.lookup_string(token->get_string()),s); +void string_const_class::code(ostream &s) { + emit_load_string(ACC, stringtable.lookup_string(token->get_string()), s); } -void bool_const_class::code(ostream& s) -{ +void bool_const_class::code(ostream &s) { emit_load_bool(ACC, BoolConst(val), s); } -void new__class::code(ostream &s) { -} +void new__class::code(ostream &s) {} -void isvoid_class::code(ostream &s) { -} - -void no_expr_class::code(ostream &s) { -} - -void object_class::code(ostream &s) { -} +void isvoid_class::code(ostream &s) {} +void no_expr_class::code(ostream &s) {} +void object_class::code(ostream &s) {} diff --git a/assignments/PA5/cgen.h b/assignments/PA5/cgen.h index 899a283..923c121 100644 --- a/assignments/PA5/cgen.h +++ b/assignments/PA5/cgen.h @@ -1,10 +1,11 @@ -#include -#include -#include "emit.h" #include "cool-tree.h" +#include "emit.h" #include "symtab.h" +#include +#include +#include -enum Basicness {Basic, NotBasic}; +enum Basicness { Basic, NotBasic }; #define TRUE 1 #define FALSE 0 @@ -14,67 +15,73 @@ typedef CgenClassTable *CgenClassTableP; class CgenNode; typedef CgenNode *CgenNodeP; -class CgenClassTable : public SymbolTable { +class CgenClassTable : public SymbolTable { private: - List *nds; - ostream& str; - int stringclasstag; - int intclasstag; - int boolclasstag; + List *nds; + 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(); + void code_bools(int); + void code_select_gc(); + void code_constants(); - void code_global_data(); - void code_global_text(); - void code_bools(int); - 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(); - void install_basic_classes(); - void install_class(CgenNodeP nd); - void install_classes(Classes cs); - void build_inheritance_tree(); - void set_relations(CgenNodeP nd); public: - CgenClassTable(Classes, ostream& str); - void code(); - CgenNodeP root(); + CgenClassTable(Classes, ostream &str); + void code(); + CgenNodeP root(); }; - class CgenNode : public class__class { -private: - CgenNodeP parentnd; // Parent of class - List *children; // Children of class - Basicness basic_status; // `Basic' if class is basic - // `NotBasic' otherwise +private: + CgenNodeP parentnd; // Parent of class + List *children; // Children of class + Basicness basic_status; // `Basic' if class is basic + // `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 *get_children() { return children; } - void set_parentnd(CgenNodeP p); - CgenNodeP get_parentnd() { return parentnd; } - int basic() { return (basic_status == Basic); } + void add_child(CgenNodeP child); + List *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; +}; diff --git a/assignments/PA5/cgen_supp.cc b/assignments/PA5/cgen_supp.cc index d9fa8cb..f1ff253 100644 --- a/assignments/PA5/cgen_supp.cc +++ b/assignments/PA5/cgen_supp.cc @@ -1,30 +1,25 @@ +#include "stringtab.h" #include #include #include -#include "stringtab.h" static int ascii = 0; -void ascii_mode(ostream& str) -{ - if (!ascii) - { - str << "\t.ascii\t\""; - ascii = 1; - } +void ascii_mode(ostream &str) { + if (!ascii) { + str << "\t.ascii\t\""; + ascii = 1; + } } -void byte_mode(ostream& str) -{ - if (ascii) - { - str << "\"\n"; - ascii = 0; - } +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,23 +34,20 @@ 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) - { - ascii_mode(str); - str << *s; - } - else - { - byte_mode(str); - str << "\t.byte\t" << (int) ((unsigned char) *s) << endl; - } + if (*s >= ' ' && ((unsigned char)*s) < 128) { + ascii_mode(str); + str << *s; + } else { + byte_mode(str); + str << "\t.byte\t" << (int)((unsigned char)*s) << endl; + } break; } s++; @@ -63,5 +55,3 @@ void emit_string_constant(ostream& str, char* s) byte_mode(str); str << "\t.byte\t0\t" << endl; } - - diff --git a/assignments/PA5/cool-tree.h b/assignments/PA5/cool-tree.h index a36a353..a662905 100644 --- a/assignments/PA5/cool-tree.h +++ b/assignments/PA5/cool-tree.h @@ -8,10 +8,8 @@ // ////////////////////////////////////////////////////////// - -#include "tree.h" #include "cool-tree.handcode.h" - +#include "tree.h" // define the class for phylum // define simple phylum - Program @@ -19,772 +17,739 @@ typedef class Program_class *Program; class Program_class : public tree_node { public: - tree_node *copy() { return copy_Program(); } - virtual Program copy_Program() = 0; + tree_node *copy() { return copy_Program(); } + virtual Program copy_Program() = 0; #ifdef Program_EXTRAS - Program_EXTRAS + Program_EXTRAS #endif }; - // define simple phylum - Class_ typedef class Class__class *Class_; class Class__class : public tree_node { public: - tree_node *copy() { return copy_Class_(); } - virtual Class_ copy_Class_() = 0; + tree_node *copy() { return copy_Class_(); } + virtual Class_ copy_Class_() = 0; #ifdef Class__EXTRAS - Class__EXTRAS + Class__EXTRAS #endif }; - // define simple phylum - Feature typedef class Feature_class *Feature; class Feature_class : public tree_node { public: - tree_node *copy() { return copy_Feature(); } - virtual Feature copy_Feature() = 0; + tree_node *copy() { return copy_Feature(); } + virtual Feature copy_Feature() = 0; #ifdef Feature_EXTRAS - Feature_EXTRAS + Feature_EXTRAS #endif }; - // define simple phylum - Formal typedef class Formal_class *Formal; class Formal_class : public tree_node { public: - tree_node *copy() { return copy_Formal(); } - virtual Formal copy_Formal() = 0; + tree_node *copy() { return copy_Formal(); } + virtual Formal copy_Formal() = 0; #ifdef Formal_EXTRAS - Formal_EXTRAS + Formal_EXTRAS #endif }; - // define simple phylum - Expression typedef class Expression_class *Expression; class Expression_class : public tree_node { public: - tree_node *copy() { return copy_Expression(); } - virtual Expression copy_Expression() = 0; + tree_node *copy() { return copy_Expression(); } + virtual Expression copy_Expression() = 0; #ifdef Expression_EXTRAS - Expression_EXTRAS + Expression_EXTRAS #endif }; - // define simple phylum - Case typedef class Case_class *Case; class Case_class : public tree_node { public: - tree_node *copy() { return copy_Case(); } - virtual Case copy_Case() = 0; + tree_node *copy() { return copy_Case(); } + virtual Case copy_Case() = 0; #ifdef Case_EXTRAS - Case_EXTRAS + Case_EXTRAS #endif }; - // define the class for phylum - LIST // define list phlyum - Classes typedef list_node Classes_class; typedef Classes_class *Classes; - // define list phlyum - Features typedef list_node Features_class; typedef Features_class *Features; - // define list phlyum - Formals typedef list_node Formals_class; typedef Formals_class *Formals; - // define list phlyum - Expressions typedef list_node Expressions_class; typedef Expressions_class *Expressions; - // define list phlyum - Cases typedef list_node Cases_class; typedef Cases_class *Cases; - // define the class for constructors // define constructor - program class program_class : public Program_class { public: - Classes classes; + Classes classes; + public: - program_class(Classes a1) { - classes = a1; - } - Program copy_Program(); - void dump(ostream& stream, int n); + program_class(Classes a1) { classes = a1; } + Program copy_Program(); + void dump(ostream &stream, int n); #ifdef Program_SHARED_EXTRAS - Program_SHARED_EXTRAS + Program_SHARED_EXTRAS #endif #ifdef program_EXTRAS - program_EXTRAS + program_EXTRAS #endif }; - // define constructor - class_ class class__class : public Class__class { public: - Symbol name; - Symbol parent; - Features features; - Symbol filename; + Symbol name; + Symbol parent; + Features features; + Symbol filename; + public: - class__class(Symbol a1, Symbol a2, Features a3, Symbol a4) { - name = a1; - parent = a2; - features = a3; - filename = a4; - } - Class_ copy_Class_(); - void dump(ostream& stream, int n); + class__class(Symbol a1, Symbol a2, Features a3, Symbol a4) { + name = a1; + parent = a2; + features = a3; + filename = a4; + } + Class_ copy_Class_(); + void dump(ostream &stream, int n); #ifdef Class__SHARED_EXTRAS - Class__SHARED_EXTRAS + Class__SHARED_EXTRAS #endif #ifdef class__EXTRAS - class__EXTRAS + class__EXTRAS #endif }; - // define constructor - method class method_class : public Feature_class { public: - Symbol name; - Formals formals; - Symbol return_type; - Expression expr; + Symbol name; + Formals formals; + Symbol return_type; + Expression expr; + public: - method_class(Symbol a1, Formals a2, Symbol a3, Expression a4) { - name = a1; - formals = a2; - return_type = a3; - expr = a4; - } - Feature copy_Feature(); - void dump(ostream& stream, int n); + method_class(Symbol a1, Formals a2, Symbol a3, Expression a4) { + name = a1; + formals = a2; + return_type = a3; + expr = a4; + } + Feature copy_Feature(); + void dump(ostream &stream, int n); #ifdef Feature_SHARED_EXTRAS - Feature_SHARED_EXTRAS + Feature_SHARED_EXTRAS #endif #ifdef method_EXTRAS - method_EXTRAS + method_EXTRAS #endif }; - // define constructor - attr class attr_class : public Feature_class { public: - Symbol name; - Symbol type_decl; - Expression init; + Symbol name; + Symbol type_decl; + Expression init; + public: - attr_class(Symbol a1, Symbol a2, Expression a3) { - name = a1; - type_decl = a2; - init = a3; - } - Feature copy_Feature(); - void dump(ostream& stream, int n); + attr_class(Symbol a1, Symbol a2, Expression a3) { + name = a1; + type_decl = a2; + init = a3; + } + Feature copy_Feature(); + void dump(ostream &stream, int n); #ifdef Feature_SHARED_EXTRAS - Feature_SHARED_EXTRAS + Feature_SHARED_EXTRAS #endif #ifdef attr_EXTRAS - attr_EXTRAS + attr_EXTRAS #endif }; - // define constructor - formal class formal_class : public Formal_class { public: - Symbol name; - Symbol type_decl; + 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); + formal_class(Symbol a1, Symbol a2) { + name = a1; + type_decl = a2; + } + Formal copy_Formal(); + void dump(ostream &stream, int n); #ifdef Formal_SHARED_EXTRAS - Formal_SHARED_EXTRAS + Formal_SHARED_EXTRAS #endif #ifdef formal_EXTRAS - formal_EXTRAS + formal_EXTRAS #endif }; - // define constructor - branch class branch_class : public Case_class { public: - Symbol name; - Symbol type_decl; - Expression expr; + Symbol name; + Symbol type_decl; + Expression expr; + public: - branch_class(Symbol a1, Symbol a2, Expression a3) { - name = a1; - type_decl = a2; - expr = a3; - } - Case copy_Case(); - void dump(ostream& stream, int n); + branch_class(Symbol a1, Symbol a2, Expression a3) { + name = a1; + type_decl = a2; + expr = a3; + } + Case copy_Case(); + void dump(ostream &stream, int n); #ifdef Case_SHARED_EXTRAS - Case_SHARED_EXTRAS + Case_SHARED_EXTRAS #endif #ifdef branch_EXTRAS - branch_EXTRAS + branch_EXTRAS #endif }; - // define constructor - assign class assign_class : public Expression_class { public: - Symbol name; - Expression expr; + Symbol name; + Expression expr; + public: - assign_class(Symbol a1, Expression a2) { - name = a1; - expr = a2; - } - Expression copy_Expression(); - void dump(ostream& stream, int n); + assign_class(Symbol a1, Expression a2) { + name = a1; + expr = a2; + } + Expression copy_Expression(); + void dump(ostream &stream, int n); #ifdef Expression_SHARED_EXTRAS - Expression_SHARED_EXTRAS + Expression_SHARED_EXTRAS #endif #ifdef assign_EXTRAS - assign_EXTRAS + assign_EXTRAS #endif }; - // define constructor - static_dispatch class static_dispatch_class : public Expression_class { public: - Expression expr; - Symbol type_name; - Symbol name; - Expressions actual; + Expression expr; + Symbol type_name; + Symbol name; + Expressions actual; + public: - static_dispatch_class(Expression a1, Symbol a2, Symbol a3, Expressions a4) { - expr = a1; - type_name = a2; - name = a3; - actual = a4; - } - Expression copy_Expression(); - void dump(ostream& stream, int n); + static_dispatch_class(Expression a1, Symbol a2, Symbol a3, Expressions a4) { + expr = a1; + type_name = a2; + name = a3; + actual = a4; + } + Expression copy_Expression(); + void dump(ostream &stream, int n); #ifdef Expression_SHARED_EXTRAS - Expression_SHARED_EXTRAS + Expression_SHARED_EXTRAS #endif #ifdef static_dispatch_EXTRAS - static_dispatch_EXTRAS + static_dispatch_EXTRAS #endif }; - // define constructor - dispatch class dispatch_class : public Expression_class { public: - Expression expr; - Symbol name; - Expressions actual; + Expression expr; + Symbol name; + Expressions actual; + public: - dispatch_class(Expression a1, Symbol a2, Expressions a3) { - expr = a1; - name = a2; - actual = a3; - } - Expression copy_Expression(); - void dump(ostream& stream, int n); + dispatch_class(Expression a1, Symbol a2, Expressions a3) { + expr = a1; + name = a2; + actual = a3; + } + Expression copy_Expression(); + void dump(ostream &stream, int n); #ifdef Expression_SHARED_EXTRAS - Expression_SHARED_EXTRAS + Expression_SHARED_EXTRAS #endif #ifdef dispatch_EXTRAS - dispatch_EXTRAS + dispatch_EXTRAS #endif }; - // define constructor - cond class cond_class : public Expression_class { public: - Expression pred; - Expression then_exp; - Expression else_exp; + Expression pred; + Expression then_exp; + Expression else_exp; + public: - cond_class(Expression a1, Expression a2, Expression a3) { - pred = a1; - then_exp = a2; - else_exp = a3; - } - Expression copy_Expression(); - void dump(ostream& stream, int n); + cond_class(Expression a1, Expression a2, Expression a3) { + pred = a1; + then_exp = a2; + else_exp = a3; + } + Expression copy_Expression(); + void dump(ostream &stream, int n); #ifdef Expression_SHARED_EXTRAS - Expression_SHARED_EXTRAS + Expression_SHARED_EXTRAS #endif #ifdef cond_EXTRAS - cond_EXTRAS + cond_EXTRAS #endif }; - // define constructor - loop class loop_class : public Expression_class { public: - Expression pred; - Expression body; + Expression pred; + Expression body; + public: - loop_class(Expression a1, Expression a2) { - pred = a1; - body = a2; - } - Expression copy_Expression(); - void dump(ostream& stream, int n); + loop_class(Expression a1, Expression a2) { + pred = a1; + body = a2; + } + Expression copy_Expression(); + void dump(ostream &stream, int n); #ifdef Expression_SHARED_EXTRAS - Expression_SHARED_EXTRAS + Expression_SHARED_EXTRAS #endif #ifdef loop_EXTRAS - loop_EXTRAS + loop_EXTRAS #endif }; - // define constructor - typcase class typcase_class : public Expression_class { public: - Expression expr; - Cases cases; + Expression expr; + Cases cases; + public: - typcase_class(Expression a1, Cases a2) { - expr = a1; - cases = a2; - } - Expression copy_Expression(); - void dump(ostream& stream, int n); + typcase_class(Expression a1, Cases a2) { + expr = a1; + cases = a2; + } + Expression copy_Expression(); + void dump(ostream &stream, int n); #ifdef Expression_SHARED_EXTRAS - Expression_SHARED_EXTRAS + Expression_SHARED_EXTRAS #endif #ifdef typcase_EXTRAS - typcase_EXTRAS + typcase_EXTRAS #endif }; - // define constructor - block class block_class : public Expression_class { public: - Expressions body; + Expressions body; + public: - block_class(Expressions a1) { - body = a1; - } - Expression copy_Expression(); - void dump(ostream& stream, int n); + block_class(Expressions a1) { body = a1; } + Expression copy_Expression(); + void dump(ostream &stream, int n); #ifdef Expression_SHARED_EXTRAS - Expression_SHARED_EXTRAS + Expression_SHARED_EXTRAS #endif #ifdef block_EXTRAS - block_EXTRAS + block_EXTRAS #endif }; - // define constructor - let class let_class : public Expression_class { public: - Symbol identifier; - Symbol type_decl; - Expression init; - Expression body; + Symbol identifier; + Symbol type_decl; + Expression init; + Expression body; + public: - let_class(Symbol a1, Symbol a2, Expression a3, Expression a4) { - identifier = a1; - type_decl = a2; - init = a3; - body = a4; - } - Expression copy_Expression(); - void dump(ostream& stream, int n); + let_class(Symbol a1, Symbol a2, Expression a3, Expression a4) { + identifier = a1; + type_decl = a2; + init = a3; + body = a4; + } + Expression copy_Expression(); + void dump(ostream &stream, int n); #ifdef Expression_SHARED_EXTRAS - Expression_SHARED_EXTRAS + Expression_SHARED_EXTRAS #endif #ifdef let_EXTRAS - let_EXTRAS + let_EXTRAS #endif }; - // define constructor - plus class plus_class : public Expression_class { public: - Expression e1; - Expression e2; + Expression e1; + Expression e2; + public: - plus_class(Expression a1, Expression a2) { - e1 = a1; - e2 = a2; - } - Expression copy_Expression(); - void dump(ostream& stream, int n); + plus_class(Expression a1, Expression a2) { + e1 = a1; + e2 = a2; + } + Expression copy_Expression(); + void dump(ostream &stream, int n); #ifdef Expression_SHARED_EXTRAS - Expression_SHARED_EXTRAS + Expression_SHARED_EXTRAS #endif #ifdef plus_EXTRAS - plus_EXTRAS + plus_EXTRAS #endif }; - // define constructor - sub class sub_class : public Expression_class { public: - Expression e1; - Expression e2; + Expression e1; + Expression e2; + public: - sub_class(Expression a1, Expression a2) { - e1 = a1; - e2 = a2; - } - Expression copy_Expression(); - void dump(ostream& stream, int n); + sub_class(Expression a1, Expression a2) { + e1 = a1; + e2 = a2; + } + Expression copy_Expression(); + void dump(ostream &stream, int n); #ifdef Expression_SHARED_EXTRAS - Expression_SHARED_EXTRAS + Expression_SHARED_EXTRAS #endif #ifdef sub_EXTRAS - sub_EXTRAS + sub_EXTRAS #endif }; - // define constructor - mul class mul_class : public Expression_class { public: - Expression e1; - Expression e2; + Expression e1; + Expression e2; + public: - mul_class(Expression a1, Expression a2) { - e1 = a1; - e2 = a2; - } - Expression copy_Expression(); - void dump(ostream& stream, int n); + mul_class(Expression a1, Expression a2) { + e1 = a1; + e2 = a2; + } + Expression copy_Expression(); + void dump(ostream &stream, int n); #ifdef Expression_SHARED_EXTRAS - Expression_SHARED_EXTRAS + Expression_SHARED_EXTRAS #endif #ifdef mul_EXTRAS - mul_EXTRAS + mul_EXTRAS #endif }; - // define constructor - divide class divide_class : public Expression_class { public: - Expression e1; - Expression e2; + Expression e1; + Expression e2; + public: - divide_class(Expression a1, Expression a2) { - e1 = a1; - e2 = a2; - } - Expression copy_Expression(); - void dump(ostream& stream, int n); + divide_class(Expression a1, Expression a2) { + e1 = a1; + e2 = a2; + } + Expression copy_Expression(); + void dump(ostream &stream, int n); #ifdef Expression_SHARED_EXTRAS - Expression_SHARED_EXTRAS + Expression_SHARED_EXTRAS #endif #ifdef divide_EXTRAS - divide_EXTRAS + divide_EXTRAS #endif }; - // define constructor - neg class neg_class : public Expression_class { public: - Expression e1; + Expression e1; + public: - neg_class(Expression a1) { - e1 = a1; - } - Expression copy_Expression(); - void dump(ostream& stream, int n); + neg_class(Expression a1) { e1 = a1; } + Expression copy_Expression(); + void dump(ostream &stream, int n); #ifdef Expression_SHARED_EXTRAS - Expression_SHARED_EXTRAS + Expression_SHARED_EXTRAS #endif #ifdef neg_EXTRAS - neg_EXTRAS + neg_EXTRAS #endif }; - // define constructor - lt class lt_class : public Expression_class { public: - Expression e1; - Expression e2; + Expression e1; + Expression e2; + public: - lt_class(Expression a1, Expression a2) { - e1 = a1; - e2 = a2; - } - Expression copy_Expression(); - void dump(ostream& stream, int n); + lt_class(Expression a1, Expression a2) { + e1 = a1; + e2 = a2; + } + Expression copy_Expression(); + void dump(ostream &stream, int n); #ifdef Expression_SHARED_EXTRAS - Expression_SHARED_EXTRAS + Expression_SHARED_EXTRAS #endif #ifdef lt_EXTRAS - lt_EXTRAS + lt_EXTRAS #endif }; - // define constructor - eq class eq_class : public Expression_class { public: - Expression e1; - Expression e2; + Expression e1; + Expression e2; + public: - eq_class(Expression a1, Expression a2) { - e1 = a1; - e2 = a2; - } - Expression copy_Expression(); - void dump(ostream& stream, int n); + eq_class(Expression a1, Expression a2) { + e1 = a1; + e2 = a2; + } + Expression copy_Expression(); + void dump(ostream &stream, int n); #ifdef Expression_SHARED_EXTRAS - Expression_SHARED_EXTRAS + Expression_SHARED_EXTRAS #endif #ifdef eq_EXTRAS - eq_EXTRAS + eq_EXTRAS #endif }; - // define constructor - leq class leq_class : public Expression_class { public: - Expression e1; - Expression e2; + Expression e1; + Expression e2; + public: - leq_class(Expression a1, Expression a2) { - e1 = a1; - e2 = a2; - } - Expression copy_Expression(); - void dump(ostream& stream, int n); + leq_class(Expression a1, Expression a2) { + e1 = a1; + e2 = a2; + } + Expression copy_Expression(); + void dump(ostream &stream, int n); #ifdef Expression_SHARED_EXTRAS - Expression_SHARED_EXTRAS + Expression_SHARED_EXTRAS #endif #ifdef leq_EXTRAS - leq_EXTRAS + leq_EXTRAS #endif }; - // define constructor - comp class comp_class : public Expression_class { public: - Expression e1; + Expression e1; + public: - comp_class(Expression a1) { - e1 = a1; - } - Expression copy_Expression(); - void dump(ostream& stream, int n); + comp_class(Expression a1) { e1 = a1; } + Expression copy_Expression(); + void dump(ostream &stream, int n); #ifdef Expression_SHARED_EXTRAS - Expression_SHARED_EXTRAS + Expression_SHARED_EXTRAS #endif #ifdef comp_EXTRAS - comp_EXTRAS + comp_EXTRAS #endif }; - // define constructor - int_const class int_const_class : public Expression_class { public: - Symbol token; + Symbol token; + public: - int_const_class(Symbol a1) { - token = a1; - } - Expression copy_Expression(); - void dump(ostream& stream, int n); + int_const_class(Symbol a1) { token = a1; } + Expression copy_Expression(); + void dump(ostream &stream, int n); #ifdef Expression_SHARED_EXTRAS - Expression_SHARED_EXTRAS + Expression_SHARED_EXTRAS #endif #ifdef int_const_EXTRAS - int_const_EXTRAS + int_const_EXTRAS #endif }; - // define constructor - bool_const class bool_const_class : public Expression_class { public: - Boolean val; + Boolean val; + public: - bool_const_class(Boolean a1) { - val = a1; - } - Expression copy_Expression(); - void dump(ostream& stream, int n); + bool_const_class(Boolean a1) { val = a1; } + Expression copy_Expression(); + void dump(ostream &stream, int n); #ifdef Expression_SHARED_EXTRAS - Expression_SHARED_EXTRAS + Expression_SHARED_EXTRAS #endif #ifdef bool_const_EXTRAS - bool_const_EXTRAS + bool_const_EXTRAS #endif }; - // define constructor - string_const class string_const_class : public Expression_class { public: - Symbol token; + Symbol token; + public: - string_const_class(Symbol a1) { - token = a1; - } - Expression copy_Expression(); - void dump(ostream& stream, int n); + string_const_class(Symbol a1) { token = a1; } + Expression copy_Expression(); + void dump(ostream &stream, int n); #ifdef Expression_SHARED_EXTRAS - Expression_SHARED_EXTRAS + Expression_SHARED_EXTRAS #endif #ifdef string_const_EXTRAS - string_const_EXTRAS + string_const_EXTRAS #endif }; - // define constructor - new_ class new__class : public Expression_class { public: - Symbol type_name; + Symbol type_name; + public: - new__class(Symbol a1) { - type_name = a1; - } - Expression copy_Expression(); - void dump(ostream& stream, int n); + new__class(Symbol a1) { type_name = a1; } + Expression copy_Expression(); + void dump(ostream &stream, int n); #ifdef Expression_SHARED_EXTRAS - Expression_SHARED_EXTRAS + Expression_SHARED_EXTRAS #endif #ifdef new__EXTRAS - new__EXTRAS + new__EXTRAS #endif }; - // define constructor - isvoid class isvoid_class : public Expression_class { public: - Expression e1; + Expression e1; + public: - isvoid_class(Expression a1) { - e1 = a1; - } - Expression copy_Expression(); - void dump(ostream& stream, int n); + isvoid_class(Expression a1) { e1 = a1; } + Expression copy_Expression(); + void dump(ostream &stream, int n); #ifdef Expression_SHARED_EXTRAS - Expression_SHARED_EXTRAS + Expression_SHARED_EXTRAS #endif #ifdef isvoid_EXTRAS - isvoid_EXTRAS + isvoid_EXTRAS #endif }; - // define constructor - no_expr class no_expr_class : public Expression_class { public: public: - no_expr_class() { - } - Expression copy_Expression(); - void dump(ostream& stream, int n); + no_expr_class() {} + Expression copy_Expression(); + void dump(ostream &stream, int n); #ifdef Expression_SHARED_EXTRAS - Expression_SHARED_EXTRAS + Expression_SHARED_EXTRAS #endif #ifdef no_expr_EXTRAS - no_expr_EXTRAS + no_expr_EXTRAS #endif }; - // define constructor - object class object_class : public Expression_class { public: - Symbol name; + Symbol name; + public: - object_class(Symbol a1) { - name = a1; - } - Expression copy_Expression(); - void dump(ostream& stream, int n); + object_class(Symbol a1) { name = a1; } + Expression copy_Expression(); + void dump(ostream &stream, int n); #ifdef Expression_SHARED_EXTRAS - Expression_SHARED_EXTRAS + Expression_SHARED_EXTRAS #endif #ifdef object_EXTRAS - object_EXTRAS + object_EXTRAS #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 diff --git a/assignments/PA5/cool-tree.handcode.h b/assignments/PA5/cool-tree.handcode.h index 2a6c076..e68e60f 100644 --- a/assignments/PA5/cool-tree.handcode.h +++ b/assignments/PA5/cool-tree.handcode.h @@ -4,19 +4,20 @@ #ifndef COOL_TREE_HANDCODE_H #define COOL_TREE_HANDCODE_H -#include -#include "tree.h" #include "cool.h" #include "stringtab.h" +#include "tree.h" +#include #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); @@ -44,66 +45,52 @@ typedef Expressions_class *Expressions; typedef list_node Cases_class; typedef Cases_class *Cases; -#define Program_EXTRAS \ -virtual void cgen(ostream&) = 0; \ -virtual void dump_with_types(ostream&, int) = 0; +#define Program_EXTRAS \ + 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); +#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; -#define program_EXTRAS \ -void cgen(ostream&); \ -void dump_with_types(ostream&, int); +#define class__EXTRAS \ + Symbol get_name() { return name; } \ + Symbol get_parent() { return parent; } \ + Symbol get_filename() { return filename; } \ + 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; +#define Feature_EXTRAS virtual void dump_with_types(ostream &, int) = 0; +#define Feature_SHARED_EXTRAS void dump_with_types(ostream &, int); -#define class__EXTRAS \ -Symbol get_name() { return name; } \ -Symbol get_parent() { return parent; } \ -Symbol get_filename() { return filename; } \ -void dump_with_types(ostream&,int); +#define Formal_EXTRAS virtual void dump_with_types(ostream &, int) = 0; +#define formal_EXTRAS void dump_with_types(ostream &, int); -#define Feature_EXTRAS \ -virtual void dump_with_types(ostream&,int) = 0; +#define Case_EXTRAS virtual void dump_with_types(ostream &, int) = 0; +#define branch_EXTRAS void dump_with_types(ostream &, int); -#define Feature_SHARED_EXTRAS \ -void dump_with_types(ostream&,int); - - -#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 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; } - -#define Expression_SHARED_EXTRAS \ -void code(ostream&); \ -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; } +#define Expression_SHARED_EXTRAS \ + void code(ostream &); \ + void dump_with_types(ostream &, int); #endif diff --git a/assignments/PA5/example.cl b/assignments/PA5/example.cl index d26ffba..89fd448 100644 --- a/assignments/PA5/example.cl +++ b/assignments/PA5/example.cl @@ -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 }; }; diff --git a/handouts/cool-runtime.pdf b/handouts/cool-runtime.pdf new file mode 100644 index 0000000..e7aaf48 Binary files /dev/null and b/handouts/cool-runtime.pdf differ