source code
- list.h
实现了链表,插入操作在constructor中实现
template <class T>
class List {
T *head; // 指向节点内容
List<T>* tail; // 指向下一个节点
public:
List(T *h,List<T>* t = NULL): head(h), tail(t) { } // 构造时使用头插法将新的结点插入到list
};
- stringtab.h:
class Entry(str, len, index)
// 以下*Entry皆为Entry的子类
class StringEntry
class IntEntry
class IdEntry
class IdTable : public StringTable<IdEntry> // store indentifier
class StrTable : public StringTable<StringEntry> // store string constant
class IntTable : public StringTable<IntEntry> // store int constant as string form
- stringtab_functions.h :
StringTable::add_string()
StringTable::add_int() // 将int转化成string存储在StringTable中
-
cool-parse.h
定义了lexer中一部分token的值 -
symtab.h
符号表的实现
template<class SYM, class DAT>
class SymtabEntry { SYM id; DAT *info }
class SymbolTable {
typedef List<ScopeEntry> Scope; // Scope是Entry的链表
typedef List<Scope> ScopeList; // ScopeList是Scope的链表,一个ScopeList中可以有多个Scope
ScopeList *tbl; // 指向当前的Scope
enterscope(); // 新建一个Scope(该Scope的hd为NULL)并且进入这个Scope
exitscope(); // 退出当前Scope
addid(); // 为当前的Scope添加一个item
lookup(); // 在全局的范围内寻找一个特定的item
probe(); // 在当前的Scope寻找一个特定的item
dump(); // 打印SymbolTable的所有内容
}
- tree.h
// 所有node都有copy_list函数,为深拷贝
class tree_node {
int line_number; // 记录对应源代码的行数
}
class list_node {
nil(); //返回一个空的结点(class nil_node)
single(); // 返回只有单个结点的list
append(l1, l2); //return append_node(l1, l2)
}
class nil_node {
len(); // return 0
nth_length(); // return NULL
}
class single_list_node {
len(); // return 1
nth_length(n, len); // 如果n!=0 return elem,如果n==0 return NULL;
}
class append_node {
list_node<Elem> *some, *rest; // append_node将some和rest连接起来
len(); return len(some) + len(rest)
nth_length(n, len); // 返回some和rest组成的list中的第n个元素
}
list(Elem x); // 返回一个single_list_node*
cons(Elem x, list_node *l); // 返回一个连接x和l的append_node,为x->l
xcons(list_node* l, Elem x); // 返回一个连接l和x的append_node,为l->x
- cool-tree.h
定义AST的结点类型
// 基类定义,有typedef Xx_class *Xx
// typedef list_node<Xx> Xxs_class
// typedef Xxs_class *Xxs
class Program_class {
CgenClassTable* class_table; // 只有Program_class有
copy_Program(); // 返回Program_class*
tree_node* copy(); // 调用copy_Program(),并将Program_class*转化为tree_node*
}
class Class_class {
copy_Class(); // 返回Class_class*
tree_node* copy(); // 调用copy_Class(),并将Class_class*转化为tree_node*
}
class Feature_class {
copy_Feature(); // 返回Feature_class*
tree_node* copy(); // 调用copy_Class(),并将Feature_class*转化为tree_node*
}
class Formal_class {
copy_Formal(); // 返回Formal_class*
tree_node* copy(); // 调用copy_Formal(),并将Formal_class*转化为tree_node*
}
class Expression_class {
copy_Expression(); // 返回Expression_class*
tree_node* copy(); // 调用copy_Class(),并将Class_class*转化为tree_node*
}
class Case_class {
copy_Case(); // 返回Case_class*
tree_node* copy(); // 调用copy_Class(),并将Case_class*转化为tree_node*
}
// AST结点具体实现
class program_class : Program_class {
Classes classes;
}
class class_class : Class_class {
Symbol name; // 类名
Symbol parent; // 父类名
Features features; // 成员变量和成员函数
Symbol filename; // 所在文件的名字
}
class method_class : Feature_class { // method属于Feature
Symbol name; // 方法名
Formals formals; // 参数
Symbol return type; // 返回类型
Expression expr; // 方法的body
}
class attr_class : Feature_class { // 成员变量属于类型
Symbol name; // 属性名
Symbol type_decl; // 变量类型
Expression init; // 初始化值
}
class formal_class : Formal_class {
Symbol name; //参数名
Symbol type_decl; //参数类型
}
class branch_class : Case_class { // case的一个分支
Symbol name; // name : type_decl => expr;
Symbol type_decl;
Expression expr;
}
class assign_class : Expression_clas