Ignore:
Timestamp:
Jan 16, 2018, 2:44:19 PM (7 years ago)
Author:
chronos
Message:
  • Added: Support for multiple value types. String and Integer are supported for the start.
  • Added: "var" keyword for compile time definition of variable and its type.
  • Added: Increment function for integer values.
File:
1 edited

Legend:

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

    r140 r141  
    99
    1010type
    11   TSourceTokenKind = (stNone, stString, stIdentifier, stEof);
     11  TSourceTokenKind = (stNone, stString, stIdentifier, stEof, stInteger);
    1212  TSourceToken = record
    1313    Text: string;
     
    102102procedure TTokenizer.Tokenize(Text: string; Tokens: TSourceTokens);
    103103type
    104   TTokenizerState = (tsNone, tsIdentifier, tsString);
     104  TTokenizerState = (tsNone, tsIdentifier, tsString, tsInteger);
    105105var
    106106  State: TTokenizerState;
     
    114114  while I < Length(Text) do begin
    115115    if State = tsNone then begin
     116      if IsDigit(Text[I]) then begin
     117        State := tsInteger;
     118        Token := Text[I];
     119      end else
    116120      if IsAlpha(Text[I]) then begin
    117121        State := tsIdentifier;
     
    133137      if IsWhiteSpace(Text[I]) then begin
    134138        Tokens.AddToken(stIdentifier, Token);
     139        State := tsNone;
     140        Dec(I);
     141      end else
     142      raise Exception.Create('Unexpected character');
     143    end else
     144    if State = tsInteger then begin
     145      if IsDigit(Text[I]) then begin
     146        Token := Token + Text[I];
     147      end else
     148      if IsWhiteSpace(Text[I]) then begin
     149        Tokens.AddToken(stInteger, Token);
    135150        State := tsNone;
    136151        Dec(I);
     
    156171var
    157172  Token: TSourceToken;
     173  Token2: TSourceToken;
    158174  Keyword: string;
    159175  Instruction: TSourceInstruction;
     
    161177  Funct: TSourceFunction;
    162178  Param: TSourceFunctionParameter;
     179  Variable: TSourceVariable;
     180  ValueType: TSourceType;
    163181
    164182function ParseReference: TSourceReference;
     
    168186    NewReference := TSourceReferenceConstant.Create;
    169187    TSourceReferenceConstant(NewReference).Constant :=
    170       Code.Constants.AddNew(Token.Text);
     188      Code.Constants.AddNewString(Token.Text);
    171189  end else
    172190  if Token.Kind = stIdentifier then begin
     
    176194    if TSourceReferenceVariable(NewReference).Variable = nil then
    177195      raise Exception.Create('Variable not found: ' + Token.Text);
    178   end else raise Exception.Create('Unexpected parameter');
     196  end else
     197  if Token.Kind = stInteger then begin
     198    NewReference := TSourceReferenceConstant.Create;
     199    TSourceReferenceConstant(NewReference).Constant :=
     200      Code.Constants.AddNewInteger(StrToInt(Token.Text));
     201  end else
     202  raise Exception.Create('Unexpected parameter');
    179203  Result := NewReference;
    180204end;
     
    187211    TSourceReferenceVariable(NewReference).Variable :=
    188212      Code.Variables.Search(Token.Text);
    189     if TSourceReferenceVariable(NewReference).Variable = nil then begin
    190       if Initialize then
    191         TSourceReferenceVariable(NewReference).Variable := Code.Variables.AddNew(Token.Text)
    192         else raise Exception.Create('Variable not found: ' + Token.Text);
    193     end;
     213    if TSourceReferenceVariable(NewReference).Variable = nil then
     214      raise Exception.Create('Variable not found: ' + Token.Text);
    194215
    195216  end else raise Exception.Create('Unexpected parameter');
     
    203224    if Token.Kind = stIdentifier then begin
    204225      Keyword := LowerCase(Token.Text);
    205       Funct := Code.Functions.Search(Keyword);
    206       if Assigned(Funct) then begin
    207         Instruction := TSourceInstructionFunction.Create;
    208         TSourceInstructionFunction(Instruction).Name := Keyword;
    209         for Param in Funct.Parameters do
    210         if Param.Kind = pkString then begin
    211           TSourceInstructionFunction(Instruction).AddParameter(ParseReference)
    212         end else
    213         if Param.Kind = pkVariable then begin
    214           TSourceInstructionFunction(Instruction).AddParameter(ParseReferenceVariable(True));
    215         end else
    216         raise Exception.Create('Unsupported parameter type.');
    217         Code.Instructions.Add(Instruction);
    218       end else raise Exception.Create('Unsupported keyword: ' + Token.Text);
     226      if Keyword = 'var' then begin
     227        Token := Tokenizer.GetNext;
     228        Token2 := Tokenizer.GetNext;
     229        if Token2.Kind <> stIdentifier then
     230          raise Exception.Create('Expected type parameter');
     231        Variable := Code.Variables.Search(Token.Text);
     232        if not Assigned(Variable) then begin
     233          ValueType := Code.Types.Search(Token2.Text);
     234          if not Assigned(ValueType) then
     235            raise Exception.Create('Unsupported type: ' + Token2.Text);
     236          Variable := Code.Variables.AddNew(Token.Text,
     237            ValueType);
     238        end else raise Exception.Create('Variable redefined');
     239      end else begin
     240        Funct := Code.Functions.Search(Keyword);
     241        if Assigned(Funct) then begin
     242          Instruction := TSourceInstructionFunction.Create;
     243          TSourceInstructionFunction(Instruction).Name := Keyword;
     244          for Param in Funct.Parameters do
     245          if Param.Kind = pkString then begin
     246            TSourceInstructionFunction(Instruction).AddParameter(ParseReference)
     247          end else
     248          if Param.Kind = pkVariable then begin
     249            TSourceInstructionFunction(Instruction).AddParameter(ParseReferenceVariable(True));
     250          end else
     251          raise Exception.Create('Unsupported parameter type.');
     252          Code.Instructions.Add(Instruction);
     253        end else raise Exception.Create('Unsupported keyword: ' + Token.Text);
     254      end;
    219255    end else raise Exception.Create('Unsupported token kind.');
    220256  end;
Note: See TracChangeset for help on using the changeset viewer.