/*
* p_tree.C
*/
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#include "ctrl.h"
#include "scanparse.h"
#include "symtab.h"
#include "p_tree.h"
extern bool debugFlag;
LstSeqBldrCls :: LstSeqBldrCls() {
if (debugFlag) cout << "LstSeqBldrCls()" << endl;
next = 0;
}
PLstSeqBldrCls LstSeqBldrCls ::
append(PLstSeqBldrCls ToBeAdded) {
if (debugFlag) cout << "LstSeqBldrCls::append()" << endl;
return this -> next = ToBeAdded;
}
/* end-LstSeqBldrCls */
PTreeNodeCls :: PTreeNodeCls() {
if (debugFlag) cout << "PTreeNodeCls" << endl;
extern LexTokCls *lex_tok;
lt = lex_tok;
}
int PTreeNodeCls :: emit() {
cout << "PTreeNodeCls::emit() BASECLASS !!!!!!!" << endl;
return 0;
}
void PTreeNodeCls :: print() {
if (debugFlag) cout << "PTreeNodeCls::print() ";
}
/* end-PTreeNodeCls */
PTreeCls :: PTreeCls(PPTreeNodeCls Root) {
if (debugFlag) cout << "PTreeCls()" << endl;
root = current = Root;
}
void PTreeCls :: print() {
cout << "PTreeCls " << endl;
root -> print();
}
/* end-PTreeCls */
DataItemCls::DataItemCls(PPTreeNodeCls item, PPTreeNodeCls sub)
{
this->item = item;
this->sub = sub;
// error checking would go here
// lookup identifier
// check if array iff sub
}
StatementCls :: StatementCls(char *StmtText) {
if (debugFlag) cout << "StatementCls()" << endl;
stmt_text = new char[strlen(StmtText)+1];
strcpy(stmt_text,StmtText);
}
StatementSeqCls :: StatementSeqCls (PPTreeNodeCls Stmt) {
if (debugFlag) cout << "StatementSeqCls() " << endl;
seq_tail = seq_head = PStatementCls(Stmt);
}
PPTreeNodeCls StatementSeqCls :: append(PPTreeNodeCls Stmt) {
if (debugFlag) cout << "StatementSeqCls::append()" << endl;
if (!seq_tail) {
cerr << "StatementSeqCls::append() -- LOGIC ERROR" << endl;
} else {
seq_tail = PStatementCls(seq_tail ->
LstSeqBldrCls::append(PStatementCls(Stmt)));
}
return this;
}
/* end-StatementSeqCls */
AssignmentStmtCls :: AssignmentStmtCls(
PPTreeNodeCls assignSeq,
PPTreeNodeCls Expr,
char* TextLine):
StatementCls(TextLine) {
if (debugFlag) cout << "AssignmentStmtCls() " << endl;
if (!assignSeq || !Expr) {
cerr << "AssignmentStmtCls() - LOGIC ERROR " << endl;
}
assign_seq = assignSeq;
expr = Expr;
}
AssignmentSeqCls::AssignmentSeqCls(PPTreeNodeCls item)
{
seq_head = seq_tail = PDataItemCls(item);
length = 1;
}
PPTreeNodeCls AssignmentSeqCls::append(PPTreeNodeCls item)
{
if (debugFlag) cout << "AssignmentSeqCls::append()" << endl;
if (!seq_tail) {
cerr << "StatementSeqCls::append() -- LOGIC ERROR" << endl;
} else {
seq_tail = PDataItemCls(seq_tail->LstSeqBldrCls::append(PDataItemCls(item)));
}
length++;
return this;
}
int AssignmentSeqCls::getLength() {
return length;
}
WriteStmtCls :: WriteStmtCls(PPTreeNodeCls Expr,
char* TextLine):
StatementCls(TextLine) {
if (debugFlag) cout << "WriteStmtCls() " << endl;
if (!Expr) {
cerr << "WriteStmtCls() - LOGIC ERROR " << endl;
}
expr = Expr;
}
/* end-WriteStmtCls */
LoopStmtCls::LoopStmtCls(PPTreeNodeCls stmts) {
statements = stmts;
}
IfStmtCls::IfStmtCls(PPTreeNodeCls condition, PPTreeNodeCls statements, PPTreeNodeCls elseStmts)
{
this->condition = condition;
this->statements = statements;
this->elseStmts = elseStmts;
}
ElseStmtCls::ElseStmtCls(PPTreeNodeCls statements)
{
this->statements = statements;
}
RelOpCls::RelOpCls(RelOp op) {
this->op = op;
}
ConditionCls::ConditionCls(PPTreeNodeCls expr1, PPTreeNodeCls relOp, PPTreeNodeCls expr2)
{
this->expr1 = expr1;
this->relOp = relOp;
this->expr2 = expr2;
}
ExprCls :: ExprCls() {
if (debugFlag) cout << "ExprCls" << endl;
}
int ExprCls :: emit() {
cout << "ExprCls::emit() BASE CLASS !!!!!!!" << endl;
return 0;
}
FactorCls :: FactorCls() {
if (debugFlag) cout << "FactorCls() " << endl;
}
/* end-FactorCls */
NumFactorCls :: NumFactorCls(PPTreeNodeCls NumLit) {
if (debugFlag) cout << "NumFactorCls() " << endl;
if (!NumLit) {
cerr << "NumFactorCls - LOGIC ERROR" << endl;
}
ExprCls::value = PNumLiteralCls(NumLit) -> get_value();
if (debugFlag) cout << " value " << ExprCls::value << endl;
}
VarAccessFactorCls :: VarAccessFactorCls(PPTreeNodeCls Item) {
if (debugFlag) cout << "VarAccessFactorCls() " << endl;
if (!Item) {
cerr << "VarAccessFactorCls() - LOGIC ERROR" << endl;
}
item = Item;
}
/* end-VarAccessFactorCls */
ArithmCls::ArithmCls(PPTreeNodeCls left, PPTreeNodeCls right, ArithmOp op)
{
this->left = left;
this->right = right;
this->op = op;
}
NumLiteralCls :: NumLiteralCls() {
if (debugFlag) cout << "NumLiteralCls" << endl;
value = atoi(this -> PTreeNodeCls::lt -> get_lexeme());
}
/* end-NumLiteralCls */
IdentCls :: IdentCls() {
if (debugFlag) cout << "IdentCls" << endl;
}
char *IdentCls :: get_name() {
if (debugFlag) cout << "IdentCls::get_name()" << endl;
return this -> PTreeNodeCls::lt -> get_lexeme();
}
/* end-IdentCls */
IdentSeqCls::IdentSeqCls(PPTreeNodeCls ident)
{
if (debugFlag) cout << "IdentSeqCls::IdentSeqCls()" << endl;
seq_head = seq_tail = PIdentCls(ident);
}
PPTreeNodeCls IdentSeqCls::append(PPTreeNodeCls ident)
{
if (debugFlag) cout << "DeclarationSeqCls::append()" << endl;
if (!seq_tail) {
cerr << "IdentSeqCls::append() -- LOGIC ERROR" << endl;
exit(1);
}
else {
seq_tail = PIdentCls(seq_tail->LstSeqBldrCls::append(PIdentCls(ident)));
}
return this;
}
PIdentCls IdentSeqCls::get_first() {
return seq_head;
}
PIntTypeCls IntTypeCls::copy() {
PIntTypeCls tmp = new IntTypeCls();
return tmp;
}
ArrayTypeCls::ArrayTypeCls(PPTreeNodeCls rangeFrom, PPTreeNodeCls rangeTo,
PPTreeNodeCls dataType)
{
range_from = PNumLiteralCls(rangeFrom);
range_to = PNumLiteralCls(rangeTo);
data_type = PDataTypeCls(dataType);
if (range_from->get_value() != 1) {
cout << "error: array must start at index 1" << endl;
exit(1);
}
if (range_to->get_value() < range_from->get_value()) {
cout << "error: array size <= 0" << endl;
exit(1);
}
}
int ArrayTypeCls::getSize()
{
return (range_to->get_value()-range_from->get_value()+1)*getItemSize();
}
int ArrayTypeCls::getItemSize()
{
return PDataTypeCls(data_type)->getSize();
}
PArrayTypeCls ArrayTypeCls::copy()
{
PArrayTypeCls tmp = new ArrayTypeCls(getRangeFrom(), getRangeTo(), NULL);
tmp->data_type = data_type->copy();
return tmp;
}
PNumLiteralCls ArrayTypeCls::getRangeFrom()
{
return range_from;
}
PNumLiteralCls ArrayTypeCls::getRangeTo()
{
return range_to;
}
PDataTypeCls ArrayTypeCls::getItemDataType()
{
return data_type;
}
DeclarationCls::DeclarationCls(PPTreeNodeCls identSeq, PPTreeNodeCls dataType,
char *declText)
{
if (debugFlag) cout << "DeclarationCls()" << endl;
ident_seq = PIdentSeqCls(identSeq);
data_type = PDataTypeCls(dataType);
decl_text = new char[strlen(declText)+1];
strcpy(decl_text,declText);
PSymtabCls scp = ScopeCls::get_vista();
PIdentCls ident = ident_seq->get_first();
while(ident) {
char *name = ident->get_name();
PSymtabEntryCls found_it = scp -> lookup(name);
if (!found_it) {
PAddressCls address = new AddressCls();
address->setAddr(MMCls::allocate(data_type->getSize()));
PSymbolCls sym = new SymbolCls(name, data_type, address);
scp->insert(sym);
}
else {
cout << "error: multiple declaration of symbol: " << name << endl;
exit(1);
}
ident = PIdentCls(ident->get_next());
}
}
DeclarationSeqCls::DeclarationSeqCls(PPTreeNodeCls decl)
{
if (debugFlag) cout << "DeclarationSeqCls()" << endl;
seq_tail = seq_head = PDeclarationCls(decl);
}
PPTreeNodeCls DeclarationSeqCls::append(PPTreeNodeCls decl)
{
if (debugFlag) cout << "DeclarationSeqCls::append()" << endl;
if (!seq_tail) {
cerr << "DeclarationSeqCls::append() -- LOGIC ERROR" << endl;
exit(1);
}
else {
seq_tail = PDeclarationCls(seq_tail->LstSeqBldrCls::append(PDeclarationCls(decl)));
}
return this;
}
void AddressCls::setAddr(int a)
{
addr=a;
}
int AddressCls::getAddr()
{
return addr;
}
BlockCls :: BlockCls(PPTreeNodeCls StmtSeq) {
if (debugFlag) cout << "BlockCls" << endl;
stmt_seq = StmtSeq;
}
/* end-BlockCls */
extern PPTreeNodeCls prgm_node; //declared in scanparse.C
ProgramCls :: ProgramCls(PPTreeNodeCls Ident, PPTreeNodeCls Block) {
if (debugFlag) cout << "ProgramCls() " << endl;
ident = PIdentCls(Ident);
block = PBlockCls(Block);
std_table = ScopeCls::get_vista();
prgm_node = this;
}
void ProgramCls::print() {
cout << "ProgramCls::print()" << endl;
}
/* end-ProgramCls */
|