Ignore:
Timestamp:
Jan 16, 2018, 3:19:23 PM (6 years ago)
Author:
chronos
Message:
  • Added: Support for begin end block.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/easy compiler/UCompiler.pas

    r141 r142  
    4141  private
    4242    Tokenizer: TTokenizer;
    43     procedure Parse(Tokens: TSourceTokens; Code: TSourceCode);
     43    procedure Parse(Code: TSourceCode);
     44    procedure ParseBeginEnd(BeginEnd: TSourceBeginEnd);
    4445  public
    4546    procedure Compile(Source: string; SourceCode: TSourceCode);
     
    168169{ TCompiler }
    169170
    170 procedure TCompiler.Parse(Tokens: TSourceTokens; Code: TSourceCode);
     171procedure TCompiler.Parse(Code: TSourceCode);
     172begin
     173  Tokenizer.TokenIndex := 0;
     174  ParseBeginEnd(Code.Main);
     175end;
     176
     177procedure TCompiler.ParseBeginEnd(BeginEnd: TSourceBeginEnd);
    171178var
    172179  Token: TSourceToken;
     
    186193    NewReference := TSourceReferenceConstant.Create;
    187194    TSourceReferenceConstant(NewReference).Constant :=
    188       Code.Constants.AddNewString(Token.Text);
     195      BeginEnd.SourceCode.Constants.AddNewString(Token.Text);
    189196  end else
    190197  if Token.Kind = stIdentifier then begin
    191198    NewReference := TSourceReferenceVariable.Create;
    192199    TSourceReferenceVariable(NewReference).Variable :=
    193       Code.Variables.Search(Token.Text);
     200      BeginEnd.SourceCode.Variables.Search(Token.Text);
    194201    if TSourceReferenceVariable(NewReference).Variable = nil then
    195202      raise Exception.Create('Variable not found: ' + Token.Text);
     
    198205    NewReference := TSourceReferenceConstant.Create;
    199206    TSourceReferenceConstant(NewReference).Constant :=
    200       Code.Constants.AddNewInteger(StrToInt(Token.Text));
     207      BeginEnd.SourceCode.Constants.AddNewInteger(StrToInt(Token.Text));
    201208  end else
    202209  raise Exception.Create('Unexpected parameter');
     
    210217    NewReference := TSourceReferenceVariable.Create;
    211218    TSourceReferenceVariable(NewReference).Variable :=
    212       Code.Variables.Search(Token.Text);
     219      BeginEnd.SourceCode.Variables.Search(Token.Text);
    213220    if TSourceReferenceVariable(NewReference).Variable = nil then
    214221      raise Exception.Create('Variable not found: ' + Token.Text);
     
    219226
    220227begin
    221   Tokenizer.TokenIndex := 0;
    222   while Tokenizer.TokenIndex < Length(Tokens.Tokens) do begin
     228  while True do begin
    223229    Token := Tokenizer.GetNext;
     230    if Token.Kind = stEof then Break
     231    else
    224232    if Token.Kind = stIdentifier then begin
    225233      Keyword := LowerCase(Token.Text);
     
    229237        if Token2.Kind <> stIdentifier then
    230238          raise Exception.Create('Expected type parameter');
    231         Variable := Code.Variables.Search(Token.Text);
     239        Variable := BeginEnd.SourceCode.Variables.Search(Token.Text);
    232240        if not Assigned(Variable) then begin
    233           ValueType := Code.Types.Search(Token2.Text);
     241          ValueType := BeginEnd.SourceCode.Types.Search(Token2.Text);
    234242          if not Assigned(ValueType) then
    235243            raise Exception.Create('Unsupported type: ' + Token2.Text);
    236           Variable := Code.Variables.AddNew(Token.Text,
     244          Variable := BeginEnd.SourceCode.Variables.AddNew(Token.Text,
    237245            ValueType);
    238         end else raise Exception.Create('Variable redefined');
    239       end else begin
    240         Funct := Code.Functions.Search(Keyword);
     246        end else raise Exception.Create('Variable redefined: ' + Token.Text);
     247      end else
     248      if Keyword = 'begin' then begin
     249        Instruction := TSourceBeginEnd.Create;
     250        BeginEnd.Instructions.Add(Instruction);
     251        TSourceBeginEnd(Instruction).SourceCode := BeginEnd.SourceCode;
     252        ParseBeginEnd(TSourceBeginEnd(Instruction));
     253      end else
     254      if Keyword = 'end' then begin
     255        Break;
     256      end else
     257      begin
     258        Funct := BeginEnd.SourceCode.Functions.Search(Keyword);
    241259        if Assigned(Funct) then begin
    242           Instruction := TSourceInstructionFunction.Create;
    243           TSourceInstructionFunction(Instruction).Name := Keyword;
     260          Instruction := TSourceFunctionCall.Create;
     261          TSourceFunctionCall(Instruction).Name := Keyword;
    244262          for Param in Funct.Parameters do
    245263          if Param.Kind = pkString then begin
    246             TSourceInstructionFunction(Instruction).AddParameter(ParseReference)
     264            TSourceFunctionCall(Instruction).AddParameter(ParseReference)
    247265          end else
    248266          if Param.Kind = pkVariable then begin
    249             TSourceInstructionFunction(Instruction).AddParameter(ParseReferenceVariable(True));
     267            TSourceFunctionCall(Instruction).AddParameter(ParseReferenceVariable(True));
    250268          end else
    251269          raise Exception.Create('Unsupported parameter type.');
    252           Code.Instructions.Add(Instruction);
     270          BeginEnd.Instructions.Add(Instruction);
    253271        end else raise Exception.Create('Unsupported keyword: ' + Token.Text);
    254272      end;
     
    265283
    266284  Tokenizer.Tokenize(Source, SourceTokens);
    267   Parse(SourceTokens, SourceCode);
     285  Parse(SourceCode);
    268286
    269287  Tokenizer.Free;
     
    277295  TargetInstruction: TTargetInstruction;
    278296begin
    279   Target.Instructions.Count := 0;
     297{  Target.Instructions.Count := 0;
    280298  for I := 0 to SourceCode.Instructions.Count - 1 do begin
    281299    Instruction := TSourceInstruction(SourceCode.Instructions[I]);
    282     if Instruction is TSourceInstructionFunction then begin
     300    if Instruction is TSourceFunctionCall then begin
    283301      TargetInstruction := TTargetInstruction.Create;
    284302      TargetInstruction.Kind := tiFunction;
    285       TargetInstruction.Name := TSourceInstructionFunction(Instruction).Name;
     303      TargetInstruction.Name := TSourceFunctionCall(Instruction).Name;
    286304      Target.Instructions.Add(TargetInstruction)
    287305    end else raise Exception.Create('Unsupported source instruction');
    288306  end;
     307  }
    289308end;
    290309
Note: See TracChangeset for help on using the changeset viewer.