Skip to content

Commit

Permalink
All leaks (besides types) fixed. Ready to start implementing in Simba...
Browse files Browse the repository at this point in the history
  • Loading branch information
BenLand100 committed Jan 24, 2010
1 parent d2604e7 commit 517084f
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 63 deletions.
8 changes: 8 additions & 0 deletions Exceptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
#include <cstring>
#include <math.h>

int InterpEx::getType(int cause) {
return (cause / 100) * 100;
}

InterpEx::InterpEx(int cause_impl) : cause(cause_impl) {

}
Expand All @@ -34,6 +38,10 @@ void InterpEx::addTrace(int pos) {
trace.push_back(pos);
}

int InterpEx::getType() {
return getType(cause);
}

int InterpEx::getCause() {
return cause;
}
Expand Down
52 changes: 32 additions & 20 deletions Exceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,48 @@ class InterpEx;
#ifndef _EXCEPTIONS_H
#define _EXCEPTIONS_H

#define E_DIV_ZERO 0
#define E_NOT_INTEGER 1
#define E_NOT_REAL 2
#define E_NOT_CHAR 3
#define E_NOT_STRING 4
#define E_NOT_BOOLEAN 5
#define E_NOT_RECORD 6
#define E_NOT_ARRAY 7
#define E_NOT_POINTER 8
#define E_NULL_VAL 9
#define E_NON_NUMERIC 10
#define E_INDEX_BOUNDS 11
#define E_NO_FIELD 12
#define E_UNRESOLVABLE 13
#define E_STATIC_ARRAY 14
#define E_NOT_METHOD 15
#define E_WRONG_NUM_ARG 16
#define E_REF_TYPE 17
#define E_EXIT 18
#define E_BREAK 19

//Runtime Exceptions
#define E_RUNTIME 0
#define E_DIV_ZERO E_RUNTIME + 0
#define E_NOT_INTEGER E_RUNTIME + 1
#define E_NOT_REAL E_RUNTIME + 2
#define E_NOT_CHAR E_RUNTIME + 3
#define E_NOT_STRING E_RUNTIME + 4
#define E_NOT_BOOLEAN E_RUNTIME + 5
#define E_NOT_RECORD E_RUNTIME + 6
#define E_NOT_ARRAY E_RUNTIME + 7
#define E_NOT_POINTER E_RUNTIME + 8
#define E_NULL_VAL E_RUNTIME + 9
#define E_NON_NUMERIC E_RUNTIME + 10
#define E_INDEX_BOUNDS E_RUNTIME + 11
#define E_NO_FIELD E_RUNTIME + 12
#define E_UNRESOLVABLE E_RUNTIME + 13
#define E_STATIC_ARRAY E_RUNTIME + 14
#define E_NOT_METHOD E_RUNTIME + 15
#define E_WRONG_NUM_ARG E_RUNTIME + 16
#define E_REF_TYPE E_RUNTIME + 17

//Non-catchable exceptions
#define E_NOCATCH 100
#define E_EXIT E_NOCATCH + 0
#define E_BREAK E_NOCATCH + 1

//Parser exceptions
#define E_PARSER 200

#include <exception>
#include <list>

class InterpEx : public std::exception {
public:
static int getType(int cause);

InterpEx(int cause);
~InterpEx() throw ();

void addTrace(int pos);
int getType();
int getCause();
std::list<int> getTrace();
const char* what() const throw();
Expand Down
118 changes: 88 additions & 30 deletions Expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,24 @@ Until::~Until() {
}
void Until::eval(Frame* frame, std::stack<Value*>& stack) throw(InterpEx*, int) {
Value* res = 0;
do {
if (res) Value::decref(res);
evalBlock(&block,frame,stack);
res = evalExpr(cond,frame,stack);
} while (!res->asBoolean());
Value::decref(res);
try {
do {
if (res) Value::decref(res);
evalBlock(&block,frame,stack);
res = evalExpr(cond,frame,stack);
} while (!res->asBoolean());
Value::decref(res);
} catch (int exi) {
if (exi == E_BREAK)
return;
throw exi;
} catch (InterpEx* ex) {
if (ex->getCause() == E_BREAK) {
delete ex;
return;
}
throw ex;
}
}

Case::Case(Expression* condition, int offset_impl) : Expression(offset_impl), value(condition) { }
Expand Down Expand Up @@ -98,10 +110,22 @@ void Case::eval(Frame* frame, std::stack<Value*>& stack) throw(InterpEx*, int) {
Value* val = evalExpr(value,frame,stack);
int i = val->asInteger();
Value::decref(val);
if (branches.find(i) != branches.end()) {
evalBlock(branches[i],frame,stack);
} else if (def.elems) {
evalBlock(&def,frame,stack);
try {
if (branches.find(i) != branches.end()) {
evalBlock(branches[i],frame,stack);
} else if (def.elems) {
evalBlock(&def,frame,stack);
}
} catch (int exi) {
if (exi == E_BREAK)
return;
throw exi;
} catch (InterpEx* ex) {
if (ex->getCause() == E_BREAK) {
delete ex;
return;
}
throw ex;
}
}

Expand All @@ -118,26 +142,40 @@ void For::eval(Frame* frame, std::stack<Value*>& stack) throw(InterpEx*, int) {
Value* temp = evalExpr(begin,frame,stack);
varval->set(temp);
Value::decref(temp);
if (inc) { //simply reduces tests and increases code size
temp = evalExpr(end,frame,stack);
while (varval->asInteger() <= temp->asInteger()) {
Value::decref(temp);
evalBlock(&block,frame,stack);
try {
if (inc) { //simply reduces tests and increases code size
temp = evalExpr(end,frame,stack);
varval->incr();
}
Value::decref(temp);
} else {
temp = evalExpr(end,frame,stack);
while (varval->asInteger() >= temp->asInteger()) {
while (varval->asInteger() <= temp->asInteger()) {
Value::decref(temp);
evalBlock(&block,frame,stack);
temp = evalExpr(end,frame,stack);
varval->incr();
}
Value::decref(temp);
evalBlock(&block,frame,stack);
} else {
temp = evalExpr(end,frame,stack);
varval->decr();
while (varval->asInteger() >= temp->asInteger()) {
Value::decref(temp);
evalBlock(&block,frame,stack);
temp = evalExpr(end,frame,stack);
varval->decr();
}
Value::decref(temp);
}
Value::decref(varval);
} catch (int exi) {
Value::decref(varval);
if (exi == E_BREAK)
return;
throw exi;
} catch (InterpEx* ex) {
Value::decref(varval);
if (ex->getCause() == E_BREAK) {
delete ex;
return;
}
Value::decref(temp);
throw ex;
}
Value::decref(varval);
}

While::While(Expression* cond_impl, std::list<Expression*> block_impl, int offset_impl) : Expression(offset_impl), cond(cond_impl) {
Expand All @@ -150,12 +188,24 @@ While::~While() {
void While::eval(Frame* frame, std::stack<Value*>& stack) throw(InterpEx*, int) {
Value* res = evalExpr(cond,frame,stack);
debug("while_restype=" << res->type);
while (res->asBoolean()) {
try {
while (res->asBoolean()) {
Value::decref(res);
evalBlock(&block,frame,stack);
res = evalExpr(cond,frame,stack);
}
Value::decref(res);
evalBlock(&block,frame,stack);
res = evalExpr(cond,frame,stack);
} catch (int exi) {
if (exi == E_BREAK)
return;
throw exi;
} catch (InterpEx* ex) {
if (ex->getCause() == E_BREAK) {
delete ex;
return;
}
throw ex;
}
Value::decref(res);
}

If::If(Expression* cond_impl, std::list<Expression*> block_impl, int offset_impl) : Expression(offset_impl) {
Expand Down Expand Up @@ -209,9 +259,17 @@ Try::~Try() {
void Try::eval(Frame* frame, std::stack<Value*>& stack) throw(InterpEx*, int) {
try {
evalBlock(&danger,frame,stack);
} catch (int) {
} catch (int exi) {
if (InterpEx::getType(exi) == E_NOCATCH) {
evalBlock(&always,frame,stack);
throw exi;
}
evalBlock(&saftey,frame,stack);
} catch (InterpEx* ex) {
if (ex->getType() == E_NOCATCH) {
evalBlock(&always,frame,stack);
throw ex;
}
delete ex;
evalBlock(&saftey,frame,stack);
}
Expand Down
10 changes: 1 addition & 9 deletions Interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,7 @@ void Interpreter::run() {
delete exception;
exception = 0;
}
std::cout << "Symbols: " << names.size() << '\n';
//std::map<std::string, int>::iterator iter = names.begin();
//while (iter != names.end()) {
// debug(iter->first << ">>" << iter->second);
// iter++;
//}
debug("Symbols: " << names.size() << '\n');
Frame* frame = new Frame(names.size(), prog);
try {
evalBlock(&prog->block, frame);
Expand Down Expand Up @@ -228,9 +223,6 @@ Frame::~Frame() {

Value* Frame::resolve(int symbol) throw(int, InterpEx*) {
debug("resolve_symbol=" << symbol);
//std::map<int,Value*>::iterator iter = slots.find(symbol);
//if (iter != slots.end()) {
//return Value::incref(iter->second);
Value* res = slots[symbol];
if (res) return Value::incref(res);
throw E_UNRESOLVABLE;
Expand Down
5 changes: 2 additions & 3 deletions Value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,8 @@ Value* Value::incref(Value *val) throw(int,InterpEx*) {
}

Value* Value::decref(Value *val) throw(int,InterpEx*) {
if (val->refcount > 5)
std::cout << "test " << val->refcount << '\n';
if (--val->refcount) return val;
if (--val->refcount)
return val;
delete val;
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion fpc/maze.pas
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ procedure benland100_solver(var jacks_path: TPointArray; var jills_routes: Integ
//writeln('d1:'+inttostr(length(maze)));
//writeln('d2:'+inttostr(length(maze[0])));
writeln('preparing to solve maze');
//thin(input,start.x,start.y,finish.x,finish.y,length(input),length(input[0]));
thin(input,start.x,start.y,finish.x,finish.y,length(input),length(input[0]));
writeln('thinned the maze');
//writeln('start: (' + inttostr(start.x) + ',' + inttostr(start.y) + ') finish: (' + inttostr(finish.x) + ',' + inttostr(finish.y) + ')');
all_paths:= flood(input,start.x,start.y,finish.x,finish.y,length(input),length(input[0]));
Expand Down

0 comments on commit 517084f

Please sign in to comment.