1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
/*
* This file is part of tela the Tensor Language.
* Copyright (c) 1994-1996 Pekka Janhunen
*/
#ifdef __GNUC__
# pragma implementation "tree.H"
#endif
#include "tree.H"
#include <ctype.h>
typedef Tchar *CP;
Tchar *FuncData[] = {
CP("PLUS"),CP("DIFFERENCE"),CP("MINUS"),
CP("TIMES"),CP("QUOTIENT"),CP("POWER"),CP("MOD"),
CP("EQ"),CP("NE"),CP("GT"),CP("LT"),CP("GE"),CP("LE"),
CP("AND"),CP("OR"),CP("NOT"),
CP("RANGE"),
CP("REF"),CP("MREF"),CP("CALL"),CP("LIST"),
CP("SET"),CP("BLOCK"),CP("DEFUN"),CP("PACKAGE"),
CP("LOCAL"),CP("GLOBAL"),CP("FORMAL"),CP("FORMAL_ELLIPSIS"),CP("GOTO"),CP("LABEL"),
CP("DISP"),
CP("IF"),CP("WHILE"),CP("REPEAT"),CP("FOR"),CP("CONTINUE"),CP("BREAK"),CP("RETURN"),
CP("ARRAY"),CP("EMPTY_ARRAY"),CP("APPEND"),
CP("HARDNOP"),CP("NOP")
};
TNodePool theNodePool;
// --------- Tnode members and friends -----------
void* Tnode::operator new(size_t sz) {
Tnode *r;
if (theNodePool.Last().length < TNodeBlock::SIZE) {
theNodePool.Last().length++;
r = theNodePool.Last().pool + theNodePool.Last().length-1;
} else {
//clog << "new: allocating TNodeBlock\n";
TNodeBlock *p = new TNodeBlock;
p->prev = &theNodePool.Last();
theNodePool.Last().next = p;
theNodePool.SetLast(p);
theNodePool.nblocks++;
p->length = 1;
r = p->pool;
}
//r->next=r->list=0; r->kind=Kbuiltin; r->func=FLIST;
return (void*)r;
}
ostream& operator<<(ostream& o, const Tnode& node) {
if (node.list) {
switch (node.kind) {
case Kconstant:
o << node.ObjectValue(); break;
case Kbuiltin:
o << (char*)FuncData[node.FunctionValue()] /*<< node.LineNumber()*/; break;
case Kvariable:
o << node.SymbolValue(); break;
case Kslot:
o << '$' << node.SlotValue(); break;
case Kcolon:
o << ':'; break;
}
o << '[';
for (Tnode*p=node.list; p; p=p->next) {
o << *p;
if (p->next) o << ',';
}
o << ']';
} else {
switch (node.kind) {
case Kconstant:
o << node.ObjectValue(); break;
case Kbuiltin:
o << (char*)FuncData[node.FunctionValue()]; break;
case Kvariable:
o << node.SymbolValue(); break;
case Kslot:
o << '$' << node.SlotValue(); break;
case Kcolon:
o << ':'; break;
}
}
return o;
}
void TNodePool::DeleteObjects(TNodeBlock*p, int start, int stop) {
// Detect all nodes pointing to an object in node block p.
// Delete those objects.
for (int i=start; i<stop; i++)
if (p->pool[i].kind == Kconstant) {
TObjectPtr op = &(p->pool[i].ObjectValue());
if (op) {
if (op->IsTemporary()) {
op->UnflagTemporary(); // prevent from accidentally deleting it twice
delete op;
}
}
}
}
void TNodePool::release(TNodePoolState& s) {
TNodeBlock *p = last, *q;
while (p != s.Ptr()) {
//clog << "delete: deallocating TNodeBlock\n";
q = p->prev;
DeleteObjects(p,0,p->length);
delete p;
nblocks--;
p = q;
}
DeleteObjects(s.Ptr(),s.Len(),s.Ptr()->length);
last = s.Ptr();
last->length = s.Len();
}
int TNodePool::NodesInUse() {
return TNodeBlock::SIZE*(nblocks-1) + last->length;
}
#if 0
static void skipblanks(istream& i) {
//clog<<"SKIPBLANKS\n";
Tchar ch;
for (;;) {
i >> ch;
if (!isspace(ch)) {i.putback(ch); break;}
}
}
static Tnode* readatom(istream& i) {
//clog << "READATOM\n";
skipblanks(i);
Tchar ch = i.peek();
if (isalpha(ch)) {
Tchar s[80];
for (int j=0; ; j++) {
i.get(ch);
if (isalnum(ch)) s[j] = ch; else {i.putback(ch); break;}
}
s[j] = '\0';
Tsymbol* ptr = theHT.add(s);
return new Tnode(0,ptr,0);
} else {
Tobject* ptr = new Tobject;
return new Tnode(0,ptr,0);
}
}
Tnode* readtree(istream& i) {
//clog<<"READTREE\n";
Tchar ch;
Tnode* nodeptr = readatom(i);
skipblanks(i);
if (i.peek()=='[') {
i >> ch;
int FirstIter = 1;
Tnode*start=0,*prev=0,*newptr=0;
for (;;) {
skipblanks(i);
if (i.peek()==']') {i>>ch; break;}
newptr = readtree(i);
if (FirstIter) {
start = newptr;
prev = newptr;
FirstIter = 0;
} else {
prev->next = newptr;
prev = newptr;
}
skipblanks(i);
i >> ch;
if (ch!=',' && ch!=']') {err<<"Syntax error in readtree()\n"; error();}
if (ch==']') break;
}
nodeptr->list = start;
}
return nodeptr;
}
#endif
|