Permalink
Browse files

Simple assignment code generation works.

  • Loading branch information...
1 parent 3a335f0 commit 83f084793cfa940610d6dc8ae1a904f7e6fea912 Szaby Grünwald committed May 11, 2012
Showing with 68 additions and 38 deletions.
  1. +1 −0 codegen.pas
  2. +52 −28 parser.pas
  3. +13 −8 symboltable.pas
  4. +2 −2 tests/cg-assignment.pas
View
@@ -164,6 +164,7 @@ procedure cgAssignmentOperator(leftItem: ptItem; rightItem: ptItem);
// leftItem must be in VAR_MODE, rightItem must be in REG_MODE
cgPut('STW', rightItem^.fReg, leftItem^.fReg, leftItem^.fOffset, 'assignmentOperator');
+ cgReleaseRegister(rightItem^.fReg);
End;
// TODO
View
@@ -18,7 +18,7 @@
procedure parseCodeBlock; forward;
procedure parseDeclaration; forward;
- procedure parseExpression; forward;
+ procedure parseExpression(item: ptItem); forward;
procedure parserErrorStr( errMsg : String);
begin
@@ -341,24 +341,39 @@
- procedure parseVarExtIdentifier;
+ procedure parseVarExtIdentifier(item: ptItem);
var ret : longint;
var again : longint;
var bTry : longint;
var bParse : longint;
begin
(parserDebugStr( 'parseVarExtIdentifier'));
-
(parseVarIdentifier);
ret := gRetLongInt;
if ret = cTrue then begin
+ stFindSymbol(stCurrentScope, id);
+ if stFindSymbolRet <> Nil then begin
+ item^.fMode := mVar;
+ writeln('fo1');
+ item^.fType := stFindSymbolRet^.fType;
+ if stCurrentScope^.fParent = Nil then begin // Global scope
+ item^.fReg := 28;
+ end else begin
+ item^.fReg := 27;
+ end;
+ item^.fOffset := stFindSymbolRet^.fOffset;
+ writeln('fo2');
+ end else begin
+ errorMsg('parseVarExtIdentifier: Variable not found');
+ end;
+
(peekIsSymbol( cLBrak));
bTry := gRetLongInt;
again := bTry;
while (again = cTrue) do begin
if bTry = cTrue then begin
(getSymbol); // "["
- (parseExpression);
+ (parseExpression(item));
bParse := gRetLongInt;
if bParse = cTrue then begin
(parseSymbol( cRBrak));
@@ -402,7 +417,7 @@
ret := gRetLongInt;
if ret = cTrue then begin
- (parseVarExtIdentifier);
+ parseVarExtIdentifier(Nil);
ret := gRetLongInt;
end;
@@ -422,7 +437,7 @@
gRetLongInt := ret;
end;
- procedure parseVariable;
+ procedure parseVariable(item: ptItem);
var ret : longint;
var again : longint;
var bTry : longint;
@@ -431,9 +446,8 @@
var ret : longint;
*)
begin
- (parserDebugStr( 'parseVariable'));
-
- (parseVarExtIdentifier);
+ parserDebugStr( 'parseVariable');
+ parseVarExtIdentifier(item);
ret := gRetLongInt;
if ret = cTrue then begin
(PeekIsVarModifier);
@@ -468,7 +482,7 @@
end;
- procedure parseFactor;
+ procedure parseFactor(item: ptItem);
var ret : longint;
var bTry: longint;
begin
@@ -494,7 +508,7 @@
bTry := gRetLongInt;
if bTry = cTrue then begin
(getSymbol); // LParen
- (parseExpression);
+ parseExpression(Nil);
ret := gRetLongInt;
if ret = cTrue then begin
(parseSymbol( cRParen));
@@ -510,14 +524,17 @@
else begin // longint
(peekSymbol);
if sym = cNumber then begin
+ item^.fMode := mConst;
+ item^.fType := stLongintType;
+ item^.fValue := val;
(getSymbol);
ret := cTrue;
end
else begin // not factor
(peekSymbol);
if sym = cNot then begin
(getSymbol); // not
- (parseFactor);
+ (parseFactor(item));
ret := gRetLongInt;
end
else begin
@@ -529,7 +546,7 @@
(parseVariableTry);
bTry := gRetLongInt;
if bTry = cTrue then begin
- (parseVariable);
+ parseVariable(Nil);
ret := gRetLongInt;
end;
end;
@@ -544,15 +561,15 @@
end;
- procedure parseTerm;
+ procedure parseTerm(item: ptItem);
var ret : longint;
var again : longint;
var bTry : longint;
var bParse : longint;
begin
(parserDebugStr( 'parseTerm'));
- (parseFactor);
+ parseFactor(item);
ret := gRetLongInt;
(peekIsMultOperator);
@@ -561,7 +578,7 @@
while again = cTrue do begin
if bTry = cTrue then begin
(getSymbol); // multOperator
- (parseFactor);
+ parseFactor(item);
bParse := gRetLongInt;
end;
if bParse = cTrue then begin
@@ -586,15 +603,15 @@
end;
- procedure parseSimpleExpression;
+ procedure parseSimpleExpression(item: ptItem);
var ret : longint;
var again : longint;
var bTry : longint;
var bParse : longint;
begin
(parserDebugStr( 'parseSimpleExpression'));
- (parseTerm);
+ parseTerm(item);
ret := gRetLongInt;
(peekIsAddOperator);
@@ -603,7 +620,7 @@
while again = cTrue do begin
if bTry = cTrue then begin
(getSymbol); // addOperator
- (parseTerm);
+ parseTerm(item);
bParse := gRetLongInt;
end;
if bParse = cTrue then begin
@@ -628,12 +645,12 @@
end;
- procedure parseExpression;
+ procedure parseExpression(item: ptItem);
var ret : longint;
var bTry : longint;
begin
(parserDebugStr( 'parseExpression'));
- (parseSimpleExpression);
+ (parseSimpleExpression(item));
ret := gRetLongInt;
if ret = cTrue then begin
@@ -642,7 +659,7 @@
if bTry = cTrue then begin
(getSymbol); // relOperator
- (parseSimpleExpression);
+ parseSimpleExpression(item);
ret := gRetLongInt;
end;
end;
@@ -675,7 +692,7 @@
if ret = cTrue then begin
(getSymbol); // ist '('
- (parseExpression);
+ parseExpression(Nil);
ret := gRetLongInt;
if ret = cTrue then begin
(PeekIsSymbol( cComma));
@@ -684,7 +701,7 @@
while again = cTrue do begin
if bTry = cTrue then begin
(getSymbol); // ","
- (parseExpression);
+ (parseExpression(Nil));
bParse := gRetLongInt;
end;
if bParse = cTrue then begin
@@ -1010,7 +1027,7 @@
ret := gRetLongInt;
if ret = cTrue then begin
- (parseExpression);
+ (parseExpression(Nil));
ret := gRetLongInt;
end;
@@ -1051,7 +1068,7 @@
ret := gRetLongInt;
if ret = cTrue then begin
- (parseExpression);
+ (parseExpression(Nil));
ret := gRetLongInt;
end;
@@ -1096,8 +1113,12 @@
procedure parseSimpleStatement;
var ret : longint;
+ var leftItem: ptItem;
+ var rightItem: ptItem;
begin
- (parseVariable);
+ New(leftItem);
+ New(rightItem);
+ parseVariable(leftItem);
ret := gRetLongInt;
if ret = cTrue then begin
@@ -1113,14 +1134,17 @@
ret := gRetLongInt;
end
else begin
- parseExpression;
+ parseExpression(rightItem);
ret := gRetLongInt;
if ret = cTrue then begin
(parseSymbol( cSemicolon));
ret := gRetLongInt;
end;
end;
end;
+ if ret = cTrue then begin
+ cgAssignmentOperator(leftItem, rightItem);
+ End;
end;
View
@@ -33,7 +33,8 @@
fIsForward: Longint;
fIsParameter: Longint;
fPrev: ptSymbol; // link to the previous symbol. for conveninence
- fNext: ptSymbol // link to the next symbol
+ fNext: ptSymbol; // link to the next symbol
+ fOffset: Longint // To be set when a variable is declared TODO
End;
// type descriptor record for each type (Longint, String, tType, ...)
@@ -47,6 +48,10 @@
Var stCurrentContext: ptSymbol;
Var stProcedureParameters: Longint;
+ var stLongintType: ptType; // TODO Fill!
+ var stStringType: ptType; // TODO Fill!
+
+
Var stCreateSymbolRet: ptSymbol;
Procedure stCreateSymbol;
Begin
@@ -97,22 +102,22 @@
var UCName : String;
Begin
// stFindSymbolRet := Nil; // should be done before calling!
- // writeln('dFS1');
+ writeln('dFS1');
if symbolTable^.fFirst <> Nil then begin
iterator := symbolTable^.fFirst;
whileBool := 0;
// if not yet found
UCFName := upCase(iterator^.fName);
UCName := upCase(name);
if UCFName <> UCName then begin
- // writeln('dFS2');
+ writeln('dFS2');
// AND there's more to look at
if iterator <> symbolTable^.fLast then begin
whileBool := 1;
End;
end;
while whileBool = 1 do begin
- // writeln('dFS3');
+ writeln('dFS3');
// next
iterator := iterator^.fNext;
// prepare whileBool
@@ -128,22 +133,22 @@
End;
end;
end;
- // writeln('dFS5');
+ writeln('dFS5');
if UCFName = UCName then begin
stFindSymbolRet := iterator;
end else begin
if symbolTable^.fParent <> Nil then begin
- // writeln('dFS6');
+ writeln('dFS6');
stFindSymbol(symbolTable^.fParent, name);
end;
end;
end else begin
if symbolTable^.fParent <> Nil then begin
- // writeln('dFS6');
+ writeln('dFS6');
stFindSymbol(symbolTable^.fParent, name);
end;
end;
- // writeln('dFS end');
+ writeln('dFS end');
End;
Var stFindTypeRet: ptType;
View
@@ -2,7 +2,7 @@
Var i: Longint;
Var j: Longint;
begin
- i := 1;
- j := 2;
+ i := 12;
+ j := 13;
end.

0 comments on commit 83f0847

Please sign in to comment.