Ignore:
Timestamp:
Jan 17, 2018, 5:27:23 PM (7 years ago)
Author:
chronos
Message:
  • Added: Unfinished support for variable arrays.
File:
1 edited

Legend:

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

    r147 r148  
    99
    1010type
    11   TSourceTokenKind = (stNone, stString, stIdentifier, stEof, stInteger);
     11  TSourceTokenKind = (stNone, stString, stIdentifier, stEof, stInteger,
     12    stSpecialSymbol);
    1213  TSourceToken = record
    1314    Text: string;
     
    4950      ): Boolean;
    5051    function ParseFunctionCall(SourceCode: TSourceCode; out FunctionCall: TCommandFunctionCall): Boolean;
    51     function ParseIfZero(SourceCode: TSourceCode; out IfZero: TCommandIfZero): Boolean;
     52    function ParseIfEqual(SourceCode: TSourceCode; out IfZero: TCommandIfEqual): Boolean;
    5253    function ParseReference(SourceCode: TSourceCode): TSourceReference;
    5354    function ParseReferenceVariable(SourceCode: TSourceCode): TSourceReference;
     
    126127procedure TTokenizer.Tokenize(Text: string; Tokens: TSourceTokens);
    127128type
    128   TTokenizerState = (tsNone, tsIdentifier, tsString, tsInteger);
     129  TTokenizerState = (tsNone, tsIdentifier, tsString, tsInteger, tsSpecialSymbol);
    129130var
    130131  State: TTokenizerState;
     
    153154        Token := '';
    154155      end else
    155       raise Exception.Create('Unexpected character');
     156      if (Text[I] = '[') or (Text[I] = ']') then begin
     157        Tokens.AddToken(stSpecialSymbol, Text[I]);
     158      end else
     159      raise Exception.Create('Unexpected character: ' + Text[I]);
    156160    end else
    157161    if State = tsIdentifier then begin
    158162      if IsAlphaNumeric(Text[I]) then begin
    159163        Token := Token + Text[I];
    160       end else
    161       if IsWhiteSpace(Text[I]) then begin
     164      end else begin
    162165        Tokens.AddToken(stIdentifier, Token);
    163166        State := tsNone;
    164167        Dec(I);
    165       end else
    166       raise Exception.Create('Unexpected character');
     168      end;
    167169    end else
    168170    if State = tsInteger then begin
    169171      if IsDigit(Text[I]) then begin
    170172        Token := Token + Text[I];
    171       end else
    172       if IsWhiteSpace(Text[I]) then begin
     173      end else begin
    173174        Tokens.AddToken(stInteger, Token);
    174175        State := tsNone;
    175176        Dec(I);
    176       end else
    177       raise Exception.Create('Unexpected character');
     177      end;
    178178    end else
    179179    if State = tsString then begin
     
    230230var
    231231  Token: TSourceToken;
     232  Token2: TSourceToken;
    232233  NewReference: TSourceReference;
    233234begin
     
    239240  end else
    240241  if Token.Kind = stIdentifier then begin
    241     NewReference := TSourceReferenceVariable.Create;
    242     TSourceReferenceVariable(NewReference).Variable :=
    243       SourceCode.Variables.Search(Token.Text);
    244     if TSourceReferenceVariable(NewReference).Variable = nil then
    245       raise Exception.Create('Variable not found: ' + Token.Text);
     242    if Tokenizer.CheckNext('[', stSpecialSymbol) then begin
     243      Token2 := Tokenizer.GetNext;
     244      NewReference := TSourceReferenceArray.Create;
     245      TSourceReferenceArray(NewReference).ArrayRef := SourceCode.Variables.Search(Token.Text);
     246      if TSourceReferenceArray(NewReference).ArrayRef = nil then
     247        raise Exception.Create('Variable not found: ' + Token.Text);
     248      TSourceReferenceArray(NewReference).Index := ParseReference(SourceCode);
     249
     250      Token2 := Tokenizer.GetNext;
     251      if (Token2.Text <> ']') or (Token2.Kind <> stSpecialSymbol) then
     252        raise Exception.Create('Expected ]');
     253    end else begin
     254      NewReference := TSourceReferenceVariable.Create;
     255      TSourceReferenceVariable(NewReference).Variable :=
     256        SourceCode.Variables.Search(Token.Text);
     257      if TSourceReferenceVariable(NewReference).Variable = nil then
     258        raise Exception.Create('Variable not found: ' + Token.Text);
     259    end;
    246260  end else
    247261  if Token.Kind = stInteger then begin
     
    257271var
    258272  Token: TSourceToken;
     273  Token2: TSourceToken;
    259274  NewReference: TSourceReference;
    260275begin
    261276  Token := Tokenizer.GetNext;
    262277  if Token.Kind = stIdentifier then begin
    263     NewReference := TSourceReferenceVariable.Create;
    264     TSourceReferenceVariable(NewReference).Variable :=
    265       SourceCode.Variables.Search(Token.Text);
    266     if TSourceReferenceVariable(NewReference).Variable = nil then
    267       raise Exception.Create('Variable not found: ' + Token.Text);
    268 
     278    if Tokenizer.CheckNext('[', stSpecialSymbol) then begin
     279      Token2 := Tokenizer.GetNext;
     280      NewReference := TSourceReferenceArray.Create;
     281      TSourceReferenceArray(NewReference).ArrayRef := SourceCode.Variables.Search(Token.Text);
     282      if TSourceReferenceArray(NewReference).ArrayRef = nil then
     283        raise Exception.Create('Variable not found: ' + Token.Text);
     284      TSourceReferenceArray(NewReference).Index := ParseReference(SourceCode);
     285
     286      Token2 := Tokenizer.GetNext;
     287      if (Token2.Text <> ']') or (Token2.Kind <> stSpecialSymbol) then
     288        raise Exception.Create('Expected ]');
     289    end else begin
     290      NewReference := TSourceReferenceVariable.Create;
     291      TSourceReferenceVariable(NewReference).Variable :=
     292        SourceCode.Variables.Search(Token.Text);
     293      if TSourceReferenceVariable(NewReference).Variable = nil then
     294        raise Exception.Create('Variable not found: ' + Token.Text);
     295    end;
    269296  end else raise Exception.Create('Unexpected parameter');
    270297  Result := NewReference;
     
    300327end;
    301328
    302 function TCompiler.ParseIfZero(SourceCode: TSourceCode; out IfZero: TCommandIfZero): Boolean;
     329function TCompiler.ParseIfEqual(SourceCode: TSourceCode; out IfZero: TCommandIfEqual): Boolean;
    303330var
    304331  Token: TSourceToken;
     
    310337  Token := Tokenizer.GetNext;
    311338  Keyword := LowerCase(Token.Text);
    312   if Keyword = 'ifzero' then begin
    313     IfZero := TCommandIfZero.Create;
    314     IfZero.Variable := TSourceReferenceVariable(ParseReferenceVariable(SourceCode));
     339  if Keyword = 'ifequal' then begin
     340    IfZero := TCommandIfEqual.Create;
     341    IfZero.Reference1 := ParseReference(SourceCode);
     342    IfZero.Reference2 := ParseReference(SourceCode);
    315343    Result := True;
    316344  end;
     
    362390var
    363391  CommandBeginEnd: TCommandBeginEnd;
    364   CommandIfZero: TCommandIfZero;
     392  CommandIfZero: TCommandIfEqual;
    365393  CommandFunctionCall: TCommandFunctionCall;
    366394  CommandBreak: TCommandBreak;
     
    373401    Command := CommandBeginEnd;
    374402  end else
    375   if ParseIfZero(SourceCode, CommandIfZero) then begin
     403  if ParseIfEqual(SourceCode, CommandIfZero) then begin
    376404    Command := CommandIfZero;
    377405  end else
Note: See TracChangeset for help on using the changeset viewer.