Skip to content

Commit e600841

Browse files
committed
Pointers work. Next step: exceptions
1 parent f2f81c1 commit e600841

File tree

5 files changed

+34
-12
lines changed

5 files changed

+34
-12
lines changed

Operator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ class Addr : public Operator {
311311
stack.pop();
312312
Pointer* pt = Type::getPointerType((Type*)val->typeObj);
313313
PointerValue* ref = new PointerValue(pt,frame->typemap);
314-
ref->set(val);
314+
ref->setRef(val);
315315
stack.push(ref);
316316
delete val;
317317
}

Value.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ void Value::set(Value* val) { }
4040
Value* Value::clone() { return new Value(); }
4141
Value* Value::duplicate() { return new Value(); }
4242
Value* Value::getRef() { return 0; }
43+
void Value::setRef(Value* ref) { }
4344
void Value::negate() { }
4445
void Value::incr() { }
4546
void Value::decr() { }
@@ -490,7 +491,7 @@ PointerValue::PointerValue(Pointer* pt, std::map<int,Type*> &typemap) : Value(TY
490491
refType = new Type*;
491492
*refType = (Type*)pt->pointsTo;
492493
ref = new Value*;
493-
*ref = 0;
494+
*ref = new Value();
494495
}
495496

496497

@@ -529,14 +530,19 @@ Value* PointerValue::clone() {
529530

530531
void PointerValue::set(Value* val) {
531532
*refType = *((PointerValue*)val)->refType;
532-
if (*ref) delete *ref;
533+
delete *ref;
533534
*ref = (*((PointerValue*)val)->ref)->duplicate();
534535
}
535536

536537
Value* PointerValue::getRef() {
537538
return *ref;
538539
}
539540

541+
void PointerValue::setRef(Value* ref_impl) {
542+
delete *ref;
543+
*ref = ref_impl->duplicate();
544+
}
545+
540546
//**** BEGIN RECORDVALUE DEFINITION ***
541547

542548
RecordValue::RecordValue(Record* rec, std::map<int,Type*> &typemap) : Value(TYPE_RECORD,rec) {

Value.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class Value : public Element {
2929
virtual Value* clone();
3030
virtual Value* duplicate();
3131
virtual Value* getRef();
32+
virtual void setRef(Value* ref);
3233
virtual void negate();
3334
virtual void incr();
3435
virtual void decr();
@@ -197,6 +198,7 @@ class PointerValue : public Value {
197198
Value* clone();
198199
void set(Value* val);
199200
Value* getRef();
201+
void setRef(Value* ref);
200202

201203
private:
202204
Value** ref;

parser.cpp

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -223,15 +223,14 @@ Method* parseMethod(char* &cur) {
223223

224224
std::map<char,char> precedence_map() {
225225
std::map <char,char> prec;
226-
prec[OP_ARRAYGET] = 6; //Careful
227-
prec[OP_ARRAYSET] = 6; //Careful
228-
prec[OP_FIELDGET] = 6; //Careful
229-
prec[OP_FIELDSET] = 6; //Careful
230-
prec[OP_DEREFGET] = 6; //Careful
231-
prec[OP_DEREFSET] = 6; //Careful
232-
233-
prec[OP_ADDR] = 5; //Careful
226+
prec[OP_ARRAYGET] = 5; //Careful
227+
prec[OP_ARRAYSET] = 5; //Careful
228+
prec[OP_FIELDGET] = 5; //Careful
229+
prec[OP_FIELDSET] = 5; //Careful
230+
prec[OP_DEREFGET] = 5; //Careful
231+
prec[OP_DEREFSET] = 5; //Careful
234232

233+
prec[OP_ADDR] = 4; //Careful
235234
prec[OP_NOT] = 4;
236235
prec[OP_NEG] = 4; //Careful, this has to be parsed from a urinary -
237236

@@ -279,6 +278,7 @@ Expression* parseExpr(char* &cur) {
279278
std::stack<std::stack<opprec>*> parseStack;
280279
std::stack<opprec>* oper = new std::stack<opprec>();
281280
char lastType = -1, nextType = -1;
281+
bool prefix = false;
282282
while (*cur) {
283283
char* tok = next(cur);
284284
lastType = nextType;
@@ -378,6 +378,14 @@ Expression* parseExpr(char* &cur) {
378378
if (lastType == POPERATOR && tok[0] == POPERATOR && tok[1] == OP_SUB) //special operator renaming
379379
tok[1] = OP_NEG;
380380
char prec = precedence[tok[1]];
381+
switch (tok[1]) {
382+
case OP_NEG:
383+
case OP_NOT:
384+
case OP_ADDR:
385+
prefix = true;
386+
oper->push(mk_opprec(Operator::get(tok[1]),prec));
387+
goto next_exprparse;
388+
}
381389
while (!oper->empty() && prec <= oper->top().prec) {
382390
expr.push_back(oper->top().op);
383391
oper->pop();
@@ -452,6 +460,11 @@ Expression* parseExpr(char* &cur) {
452460
next_exprparse:
453461
debug("next_exprparse");
454462
cur = tok;
463+
if (!prefix && !oper->empty() && oper->top().prec == 4) {
464+
expr.push_back(oper->top().op);
465+
oper->pop();
466+
}
467+
if (prefix) prefix = false;
455468
}
456469
end_exprparse:
457470
debug("end_exprparse");

test.pas

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,8 @@ procedure testpointer;
363363
phonebook;
364364
//testtry; //doesn't check for any exception yet, but *works*
365365
testref;
366+
testpointer;
366367
writeln(inttostr(3*-1));
367-
//testpointer; //needs some work
368+
writeln(inttostr(3+-(2*6)));
368369
writeln('great success!');
369370
end;

0 commit comments

Comments
 (0)