Skip to content

Commit de518d7

Browse files
committed
fewer and fewer problems to work out. see test.pas
1 parent a34c053 commit de518d7

File tree

7 files changed

+42
-33
lines changed

7 files changed

+42
-33
lines changed

Expression.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,11 @@ While::~While() {
139139
}
140140
Value* While::eval(Frame* frame) {
141141
Value* res = cond->eval(frame);
142+
debug("while_restype=" << res->type);
142143
while (res->asBoolean()) {
143144
delete res;
144145
evalBlock(&block,frame);
146+
res = cond->eval(frame);
145147
}
146148
delete res;
147149
return new Value();

Interpreter.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ Interpreter::~Interpreter() {
1616
}
1717

1818
void Interpreter::run() {
19+
std::map<std::string,int>::iterator iter = names.begin();
20+
while (iter != names.end()) {
21+
debug(iter->first << ">>" << iter->second);
22+
iter++;
23+
}
1924
Frame* frame = new Frame(prog);
2025
evalBlock(&prog->block, frame);
2126
delete frame;

Operator.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@ class FDiv : public Operator {
3434
stack.pop();
3535
Value* a = stack.top();
3636
stack.pop();
37-
double res = a->asReal() - b->asReal();
37+
double res = a->asReal() / b->asReal();
3838
stack.push(new RealValue(res));
3939
delete a;
4040
delete b;
4141
}
4242
};
4343

44+
#include <math.h>
45+
4446
class Mod : public Operator {
4547
public:
4648
Mod() : Operator(OP_MOD) { };
@@ -51,10 +53,10 @@ class Mod : public Operator {
5153
Value* a = stack.top();
5254
stack.pop();
5355
if (a->type == TYPE_REAL || b->type == TYPE_REAL) {
54-
double res = a->asReal() - b->asReal();
56+
double res = fmod(a->asReal(),b->asReal());
5557
stack.push(new RealValue(res));
5658
} else {
57-
int res = a->asInteger() - b->asInteger();
59+
int res = a->asInteger() % b->asInteger();
5860
stack.push(new IntegerValue(res));
5961
}
6062
delete a;
@@ -72,10 +74,10 @@ class Mul : public Operator {
7274
Value* a = stack.top();
7375
stack.pop();
7476
if (a->type == TYPE_REAL || b->type == TYPE_REAL) {
75-
double res = a->asReal() - b->asReal();
77+
double res = a->asReal() * b->asReal();
7678
stack.push(new RealValue(res));
7779
} else {
78-
int res = a->asInteger() - b->asInteger();
80+
int res = a->asInteger() * b->asInteger();
7981
stack.push(new IntegerValue(res));
8082
}
8183
delete a;
@@ -97,10 +99,10 @@ class Add : public Operator {
9799
str.append(b->asString());
98100
stack.push(new StringValue(str));
99101
} else if (a->type == TYPE_REAL || b->type == TYPE_REAL) {
100-
double res = a->asReal() - b->asReal();
102+
double res = a->asReal() + b->asReal();
101103
stack.push(new RealValue(res));
102104
} else {
103-
int res = a->asInteger() - b->asInteger();
105+
int res = a->asInteger() + b->asInteger();
104106
stack.push(new IntegerValue(res));
105107
}
106108
delete a;

Value.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -345,13 +345,13 @@ ArrayValue::ArrayValue(ArrayValue& val) : Value(TYPE_ARRAY,(Type*)val.typeObj) {
345345

346346
ArrayValue::~ArrayValue() {
347347
if (*objrefcount) {
348+
(*objrefcount)--;
349+
} else {
348350
for (int i = 0; i < *asize; i++) {
349351
delete (*array)[i];
350352
}
351353
delete *array;
352354
delete objrefcount;
353-
} else {
354-
(*objrefcount)--;
355355
}
356356
if (*refcount) {
357357
(*refcount)--;
@@ -386,13 +386,13 @@ void ArrayValue::set(Value* val) {
386386
*asize = *((ArrayValue*)val)->asize;
387387
*start = *((ArrayValue*)val)->start;
388388
if (*objrefcount) {
389+
(*objrefcount)--;
390+
} else {
389391
for (int i = 0; i < *asize; i++) {
390392
delete (*array)[i];
391393
}
392394
delete *array;
393395
delete objrefcount;
394-
} else {
395-
(*objrefcount)--;
396396
}
397397
objrefcount = ((ArrayValue*)val)->objrefcount;
398398
(*objrefcount)++;
@@ -526,6 +526,8 @@ RecordValue::RecordValue(RecordValue& val) : Value(TYPE_RECORD,(Type*)val.typeOb
526526

527527
RecordValue::~RecordValue() {
528528
if (*objrefcount) {
529+
(*objrefcount)--;
530+
} else {
529531
delete *types;
530532
std::map<int,Value*>::iterator iter = (*fields)->begin();
531533
std::map<int,Value*>::iterator end = (*fields)->end();
@@ -535,8 +537,6 @@ RecordValue::~RecordValue() {
535537
}
536538
delete *fields;
537539
delete objrefcount;
538-
} else {
539-
(*objrefcount)--;
540540
}
541541
if (*refcount) {
542542
(*refcount)--;
@@ -561,11 +561,11 @@ Value* RecordValue::clone() {
561561

562562
void RecordValue::set(Value* val) {
563563
if (*objrefcount) {
564+
(*objrefcount)--;
565+
} else {
564566
delete *types;
565567
delete *fields;
566568
delete objrefcount;
567-
} else {
568-
(*objrefcount)--;
569569
}
570570
objrefcount = ((RecordValue*)val)->objrefcount;
571571
(*objrefcount)++;

lexer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ char* lex(char* ppg, std::map<std::string,int> &names) {
112112
case 'e':
113113
goto name;
114114
case 'f':
115-
if (ppg[1] == 'a' && ppg[2] == 'l' && ppg[3] == 's' && ppg[4] == 'e' && !namechar(ppg[4])) {
115+
if (ppg[1] == 'a' && ppg[2] == 'l' && ppg[3] == 's' && ppg[4] == 'e' && !namechar(ppg[5])) {
116116
*(toks++) = (char)PBOOLEAN;
117117
*(toks++) = (char)BOOL_FALSE;
118118
ppg += 4;

parser.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ Expression* parseExpr(char* &cur) {
373373
}
374374
if (lastType == POPERATOR && tok[0] == POPERATOR && tok[1] == OP_SUB) //special operator renaming
375375
tok[1] = OP_NEG;
376-
char prec = precedence[OP_ARRAYSET];
376+
char prec = precedence[tok[1]];
377377
while (!oper->empty() && prec <= oper->top().prec) {
378378
expr.push_back(oper->top().op);
379379
oper->pop();
@@ -508,12 +508,12 @@ Case* parseCase(char* &cur) {
508508
For* parseFor(char* &cur) {
509509
debug("parseFor");
510510
cur = next(cur); //skip for
511+
cur = next(cur); //get var
511512
int var = *(int*)(cur+1);
512513
cur = next(cur); //skip :=
513514
Expression* begin = parseExpr(cur);
514515
cur = next(cur); //grap the to/downto
515516
bool inc = cur[1] == RES_TO;
516-
cur = next(cur);
517517
Expression* end = parseExpr(cur);
518518
cur = next(cur); //skip do
519519
//System.out.println("End: " + end);

test.pas

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@ procedure writeln(str: string);
77
begin
88
end;
99

10-
procedure chartostr(str: string);
10+
function chartostr(str: string) : string;
1111
begin
1212
end;
1313

14-
procedure booltostr(str: string);
14+
function booltostr(str: string) : string;
1515
begin
1616
end;
1717

18-
procedure realtostr(str: string);
18+
function realtostr(str: string) : string;
1919
begin
2020
end;
2121

22-
procedure inttostr(str: string);
22+
function inttostr(str: string) : string;
2323
begin
2424
end;
2525

@@ -92,7 +92,7 @@ procedure testwhile;
9292
i:integer;
9393
begin
9494
i:= 0;
95-
while i<10 do
95+
while i < 10 do
9696
begin
9797
writeln(inttostr(i));
9898
i:= i + 1;
@@ -366,19 +366,19 @@ procedure testpointer;
366366
writeln(RealToStr(newReal));
367367
writeln(IntToStr(newInteger));
368368
writeln(BoolToStr(newBoolean));
369-
//testifelse;
369+
testifelse;
370370
testorderoperations;
371371
testtruths;
372-
//testwhile;
373-
//testfor;
374-
//testcase;
375-
//testrepeatuntil;
376-
//writeln('For exec: ' + RealToStr(forlooptime));
377-
//doarraystuff;
372+
testwhile;
373+
testfor;
374+
testcase;
375+
testrepeatuntil;
376+
writeln('For exec: ' + RealToStr(forlooptime));
377+
//doarraystuff; //arrays messed up, takes out three
378378
//testtypes;
379379
//phonebook;
380-
//testtry;
380+
//testtry; //doesn't check for any exception yet, but *works*
381381
testref;
382-
//writeln(inttostr(3*-1));
383-
//testpointer;
382+
writeln(inttostr(3*-1));
383+
//testpointer; //something is wrong with assigning values that contain references (objects)
384384
end;

0 commit comments

Comments
 (0)