Ignore:
Timestamp:
Apr 17, 2020, 10:16:25 PM (5 years ago)
Author:
chronos
Message:
  • Added: Simple pascal code generator.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/interpreter2/UParser.pas

    r202 r203  
    2727    function ParseExpressionOperation(Block: TBlock; out ExpressionOperation: TExpressionOperation): Boolean;
    2828    function ParseExpressionOperand(Block: TBlock; out ExpressionOperand: TExpressionOperand): Boolean;
     29    function ParseConstantRef(Block: TBlock; out ConstantRef: TConstant): Boolean;
    2930    function ParseConstant(Block: TBlock; out ConstantRef: TConstant): Boolean;
    3031    function ParseVariable(Block: TBlock; out VariableRef: TVariable): Boolean;
     
    342343          else Error('Expression operands needs to be same type.');
    343344      end else Error('Missing operand.');
    344     end;
     345    end else Operand.Free;
    345346  end;
    346347  if not Result then Tokenizer.Pos := LastPos;
     
    364365    Result := True;
    365366    ExpressionOperand := TExpressionOperand.Create;
     367    ExpressionOperand.ConstantDirect := Constant;
     368    ExpressionOperand.OperandType := otConstantDirect;
     369  end else
     370  if ParseConstantRef(Block, Constant) then begin
     371    Result := True;
     372    ExpressionOperand := TExpressionOperand.Create;
    366373    ExpressionOperand.ConstantRef := Constant;
    367     ExpressionOperand.OperandType := otConstant;
     374    ExpressionOperand.OperandType := otConstantRef;
    368375  end else
    369376  if ParseVariable(Block, Variable) then begin
     
    371378    ExpressionOperand := TExpressionOperand.Create;
    372379    ExpressionOperand.VariableRef := Variable;
    373     ExpressionOperand.OperandType := otVariable;
     380    ExpressionOperand.OperandType := otVariableRef;
    374381  end else Error('Expected expression operand.');
    375382end;
    376383
    377 function TParser.ParseConstant(Block: TBlock; out ConstantRef: TConstant
     384function TParser.ParseConstantRef(Block: TBlock; out ConstantRef: TConstant
    378385  ): Boolean;
    379386var
     
    389396      Result := True;
    390397    end;
    391   end else
     398  end;
     399  if not Result then Tokenizer.Pos := LastPos;
     400end;
     401
     402function TParser.ParseConstant(Block: TBlock; out ConstantRef: TConstant
     403  ): Boolean;
     404var
     405  LastPos: TTokenizerPos;
     406  Token: TToken;
     407begin
     408  Result := False;
     409  LastPos := Tokenizer.Pos;
     410  Token := Tokenizer.GetNext;
    392411  if Token.Kind = tkNumber then begin
    393412    Result := True;
    394     ConstantRef := Block.Constants.AddNew('_C' + IntToStr(Block.Constants.Count));
     413    ConstantRef := TConstant.Create;
    395414    ConstantRef.TypeRef := Block.GetType('Integer');
    396415    ConstantRef.Value := TValueInteger.Create;
     
    399418  if Token.Kind = tkString then begin
    400419    Result := True;
    401     ConstantRef := Block.Constants.AddNew('_C' + IntToStr(Block.Constants.Count));
     420    ConstantRef := TConstant.Create;
    402421    ConstantRef.TypeRef := Block.GetType('string');
    403422    ConstantRef.Value := TValueString.Create;
Note: See TracChangeset for help on using the changeset viewer.