Changeset 141 for branches/easy compiler


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.
Location:
branches/easy compiler
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • branches/easy compiler/Steps.txt

    r140 r141  
    1515- change from static functions to dynamic predefined
    1616Add inputln function
     17Add integer type
     18- support for mutiple types
  • 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;
  • branches/easy compiler/UFormMain.lfm

    r140 r141  
    4242    Top = 304
    4343    Width = 200
     44    OnKeyPress = Edit1KeyPress
    4445    TabOrder = 3
    4546  end
  • branches/easy compiler/UFormMain.pas

    r140 r141  
    2626    procedure ButtonBuildClick(Sender: TObject);
    2727    procedure ButtonSendClick(Sender: TObject);
     28    procedure Edit1KeyPress(Sender: TObject; var Key: char);
    2829    procedure FormCreate(Sender: TObject);
    2930    procedure FormDestroy(Sender: TObject);
     
    5152begin
    5253  with MemoSource.Lines do begin
     54    Add('PrintLn ''Super Calculator''');
     55    Add('var Value1 Integer');
     56    Add('var Value2 Integer');
     57    Add('var Result Integer');
     58    Add('Print ''Enter value 1: ''');
     59    Add('Assign Value1 0');
     60    Add('InputLn Value1');
     61    Add('Print ''Enter value 2: ''');
     62    Add('Assign Value2 0');
     63    Add('InputLn Value2');
     64
     65    Add('Assign Result Value1');
     66    Add('Increment Result Value2');
     67    Add('Print ''Sum of two values is: ''');
     68    Add('PrintLn Result');
     69  end;
     70{  with MemoSource.Lines do begin
    5371    Add('PrintLn ''Hello World!''');
    5472    Add('print ''Hello'' PRINT '' World!''');
    5573    Add('PrintLn ''Live your life.''');
     74    Add('var Text1 String');
    5675    Add('Assign Text1 ''Live your life.''');
     76    Add('var Text2 String');
    5777    Add('Assign Text2 ''Live your life.''');
    5878    Add('PrintLn Text1');
    5979    Add('PrintLn Text2');
     80
     81    Add('var Value1 Integer');
     82    Add('Assign Value1 123');
     83    Add('Increment Value1 2');
     84    Add('PrintLn Value1');
     85
     86    Add('var Text3 String');
     87    Add('Assign Text3 ''''');
     88    Add('Print ''Enter your name:''');
    6089    Add('InputLn Text3');
    6190    Add('PrintLn Text3');
    6291  end;
     92
     93  with MemoSource.Lines do begin
     94    Add('Assign AnimalName[0] ''an elephant''');
     95    Add('Assign AnimalProperty[0] ''It is big and slow''');
     96    Add('Assign AnimalName[1] ''a cat''');
     97    Add('Assign AnimalProperty[1] ''It meows and purrs''');
     98    Add('Print ''An animal guessing game.''');
     99    Add('Assign AnimalCount 2');
     100    Add('Repeat');
     101    Add('BeginBlock');
     102      Add('PrintLn ''Think an animal.''');
     103      Add('Assign I 0');
     104      Add('Repeat');
     105      Add('BeginBlock');
     106        Add('Print AnimalProperty[I]');
     107        Add('Print ''? (y/n)''');
     108        Add('InputLn Answer');
     109        Add('IfEqual Answer ''y''');
     110        Add('BeginBlock');
     111          Add('PrintLn ''That''s clear. It is ''');
     112          Add('PrintLn AnimalName[I]');
     113          Add('Break');
     114        Add('EndBlock');
     115        Add('Increment I');
     116        Add('IfHigherOrEqual I AnimalCount');
     117        Add('Break');
     118      Add('EndBlock');
     119      Add('PrintLn ''I am lost. What is the animal?''');
     120      Add('InputLn AnimalName[I]');
     121      Add('PrintLn ''Describe the animal for me. What is it like?''');
     122      Add('InputLn AnimalProperty[I]');
     123      Add('PrintLn ''Thank you. I will remember that animal.''');
     124      Add('Increment AnimalCount 1');
     125      Add('PrintLn ''''');
     126    Add('EndBlock');
     127  end;
     128  }
    63129end;
    64130
     
    97163end;
    98164
     165procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: char);
     166begin
     167  if Key = #13 then ButtonSend.Click;
     168end;
     169
    99170procedure TForm1.FormCreate(Sender: TObject);
    100171begin
     
    115186begin
    116187  while InputBuffer.Count = 0 do begin
     188    if Application.Terminated then Break;
    117189    Sleep(50);
    118190    Application.ProcessMessages;
    119191  end;
    120   Result := InputBuffer[0];
    121   InputBuffer.Delete(0);
     192  if InputBuffer.Count > 0 then begin
     193    Result := InputBuffer[0];
     194    InputBuffer.Delete(0);
     195  end else Result := '';
    122196end;
    123197
  • branches/easy compiler/USourceCode.pas

    r140 r141  
    1818  end;
    1919
     20  { TSourceValue }
     21
     22  TSourceValue = class
     23    procedure Assign(Source: TSourceValue); virtual;
     24  end;
     25
     26  TSourceValueClass = class of TSourceValue;
     27
     28  { TSourceType }
     29
     30  TSourceType = class
     31    Name: string;
     32    ValueClass: TSourceValueClass;
     33  end;
     34
     35  { TSourceTypes }
     36
     37  TSourceTypes = class(TObjectList)
     38    function AddNew(Name: string; ClassType: TSourceValueClass): TSourceType;
     39    function Search(Name: string): TSourceType;
     40  end;
     41
     42  { TSourceVariable }
     43
    2044  TSourceVariable = class
    2145    Name: string;
     46    ValueType: TSourceType;
    2247  end;
    2348
     
    2550
    2651  TSourceVariables = class(TObjectList)
    27     function AddNew(Name: string = ''): TSourceVariable;
     52    function AddNew(Name: string; ValueType: TSourceType): TSourceVariable;
    2853    function Search(Name: string): TSourceVariable;
    2954  end;
     
    3358  end;
    3459
     60  { TSourceValueString }
     61
     62  TSourceValueString = class(TSourceValue)
     63    Value: string;
     64    procedure Assign(Source: TSourceValue); override;
     65  end;
     66
     67  { TSourceValueInteger }
     68
     69  TSourceValueInteger = class(TSourceValue)
     70    Value: Integer;
     71    procedure Assign(Source: TSourceValue); override;
     72  end;
     73
    3574  TSourceConstant = class
    3675    Name: string;
    37     Value: string;
    38   end;
    39 
    40   TSourceParameterKind = (pkString, pkVariable);
     76    Value: TSourceValue;
     77  end;
     78
     79  TSourceParameterKind = (pkString, pkVariable, pkType);
    4180
    4281  TSourceFunctionParameter = class
     
    68107
    69108  TSourceConstants = class(TObjectList)
    70     function AddNew(Value: string; Name: string = ''): TSourceConstant;
     109    function AddNewString(Value: string; Name: string = ''): TSourceConstant;
     110    function AddNewInteger(Value: Integer; Name: string = ''): TSourceConstant;
     111    function Search(Name: string): TSourceConstant;
    71112  end;
    72113
     
    89130    procedure InitFunctions;
    90131  public
     132    Types: TSourceTypes;
    91133    Variables: TSourceVariables;
    92134    Constants: TSourceConstants;
     
    100142implementation
    101143
    102 { TSourceFunctions }
    103 
    104 function TSourceFunctions.AddNew(Name: string): TSourceFunction;
    105 begin
    106   Result := TSourceFunction.Create;
     144{ TSourceTypes }
     145
     146function TSourceTypes.AddNew(Name: string; ClassType: TSourceValueClass): TSourceType;
     147begin
     148  Result := TSourceType.Create;
    107149  Result.Name := Name;
    108   Add(Result);
    109 end;
    110 
    111 function TSourceFunctions.Search(Name: string): TSourceFunction;
    112 var
    113   Item: TSourceFunction;
     150  Result.ValueClass := ClassType;
     151  Add(Result);
     152end;
     153
     154function TSourceTypes.Search(Name: string): TSourceType;
     155var
     156  Item: TSourceType;
    114157begin
    115158  Result := nil;
     
    121164end;
    122165
     166{ TSourceValue }
     167
     168procedure TSourceValue.Assign(Source: TSourceValue);
     169begin
     170  raise Exception.Create('Value assign not implemented');
     171end;
     172
     173{ TSourceValueInteger }
     174
     175procedure TSourceValueInteger.Assign(Source: TSourceValue);
     176begin
     177  if Source is TSourceValueInteger then
     178  Value := TSourceValueInteger(Source).Value
     179  else raise Exception.Create('Type for assignment not matches');
     180end;
     181
     182{ TSourceValueString }
     183
     184procedure TSourceValueString.Assign(Source: TSourceValue);
     185begin
     186  if Source is TSourceValueString then
     187  Value := TSourceValueString(Source).Value
     188  else raise Exception.Create('Type for assignment not matches');
     189end;
     190
     191{ TSourceFunctions }
     192
     193function TSourceFunctions.AddNew(Name: string): TSourceFunction;
     194begin
     195  Result := TSourceFunction.Create;
     196  Result.Name := Name;
     197  Add(Result);
     198end;
     199
     200function TSourceFunctions.Search(Name: string): TSourceFunction;
     201var
     202  Item: TSourceFunction;
     203begin
     204  Result := nil;
     205  for Item in Self do
     206  if Item.Name = Name then begin
     207    Result := Item;
     208    Break;
     209  end;
     210end;
     211
    123212{ TSourceFunction }
    124213
     
    147236{ TSourceVariables }
    148237
    149 function TSourceVariables.AddNew(Name: string): TSourceVariable;
     238function TSourceVariables.AddNew(Name: string;ValueType: TSourceType): TSourceVariable;
    150239begin
    151240  Result := TSourceVariable.Create;
    152241  Result.Name := Name;
     242  Result.ValueType := ValueType;
    153243  Add(Result);
    154244end;
     
    156246function TSourceVariables.Search(Name: string): TSourceVariable;
    157247var
    158   Variable: TSourceVariable;
     248  Item: TSourceVariable;
    159249begin
    160250  Result := nil;
    161   for Variable in Self do
    162   if Variable.Name = Name then begin
    163     Result := Variable;
     251  for Item in Self do
     252  if Item.Name = Name then begin
     253    Result := Item;
    164254    Break;
    165255  end;
     
    168258{ TSourceConstants }
    169259
    170 function TSourceConstants.AddNew(Value: string; Name: string): TSourceConstant;
     260function TSourceConstants.AddNewString(Value: string; Name: string
     261  ): TSourceConstant;
    171262begin
    172263  Result := TSourceConstant.Create;
    173   Result.Value := Value;
     264  Result.Value := TSourceValueString.Create;
     265  TSourceValueString(Result.Value).Value := Value;
    174266  Result.Name := '';
    175267  Add(Result);
     268end;
     269
     270function TSourceConstants.AddNewInteger(Value: Integer; Name: string
     271  ): TSourceConstant;
     272begin
     273  Result := TSourceConstant.Create;
     274  Result.Value := TSourceValueInteger.Create;
     275  TSourceValueInteger(Result.Value).Value := Value;
     276  Result.Name := '';
     277  Add(Result);
     278end;
     279
     280function TSourceConstants.Search(Name: string): TSourceConstant;
     281var
     282  Item: TSourceConstant;
     283begin
     284  Result := nil;
     285  for Item in Self do
     286  if Item.Name = Name then begin
     287    Result := Item;
     288    Break;
     289  end;
    176290end;
    177291
     
    191305begin
    192306  Functions.Clear;
     307
     308  // Init types
     309  Types.AddNew('Integer', TSourceValueInteger);
     310  Types.AddNew('String', TSourceValueString);
     311
     312  // Init functions
    193313  Funct := Functions.AddNew('print');
    194314  Funct.AddParameter('Text', pkString);
     315
    195316  Funct := Functions.AddNew('println');
    196317  Funct.AddParameter('Text', pkString);
     318
     319  Funct := Functions.AddNew('var');
     320  Funct.AddParameter('Variable', pkVariable);
     321  Funct.AddParameter('Type', pkType);
     322
    197323  Funct := Functions.AddNew('assign');
    198324  Funct.AddParameter('Destination', pkVariable);
    199325  Funct.AddParameter('Source', pkString);
     326
    200327  Funct := Functions.AddNew('inputln');
    201328  Funct.AddParameter('Text', pkVariable);
     329
     330  Funct := Functions.AddNew('increment');
     331  Funct.AddParameter('Variable', pkVariable);
     332  Funct.AddParameter('Addition', pkString);
    202333end;
    203334
    204335constructor TSourceCode.Create;
    205336begin
     337  Types := TSourceTypes.Create;
    206338  Variables := TSourceVariables.Create;
    207339  Constants := TSourceConstants.Create;
     340  Functions := TSourceFunctions.Create;
    208341  Instructions := TSourceInstructions.Create;
    209   Functions := TSourceFunctions.Create;
    210342  InitFunctions;
    211343end;
     
    213345destructor TSourceCode.Destroy;
    214346begin
     347  Instructions.Free;
    215348  Functions.Free;
    216349  Variables.Free;
    217350  Constants.Free;
    218   Instructions.Free;
     351  Types.Free;
    219352  inherited Destroy;
    220353end;
  • branches/easy compiler/USourceExecutor.pas

    r140 r141  
    66
    77uses
    8   Classes, SysUtils, USourceCode;
     8  Classes, SysUtils, USourceCode, Contnrs;
    99
    1010type
    1111  TOutputEvent = procedure (Text: string) of object;
    1212  TInputEvent = function: string of object;
     13
     14  TExecutorVariable = class
     15    Variable: TSourceVariable;
     16    Value: TSourceValue;
     17  end;
     18
     19  TExecutorVariables = class(TObjectList)
     20    function Search(Variable: TSourceVariable): TExecutorVariable;
     21  end;
    1322
    1423  { TSourceExecutor }
     
    1827    FOnInput: TInputEvent;
    1928    FOnOutput: TOutputEvent;
    20     Variables: TStringList;
     29    Variables: TExecutorVariables;
    2130  public
    2231    constructor Create;
     
    3039implementation
    3140
     41
     42{ TExecutorVariables }
     43
     44function TExecutorVariables.Search(Variable: TSourceVariable): TExecutorVariable;
     45var
     46  Item: TExecutorVariable;
     47begin
     48  Result := nil;
     49  for Item in Self do
     50  if Item.Variable = Variable then begin
     51    Result := Item;
     52    Break;
     53  end;
     54end;
     55
    3256{ TSourceExecutor }
    3357
    3458constructor TSourceExecutor.Create;
    3559begin
    36   Variables := TStringList.Create;
     60  Variables := TExecutorVariables.Create;
    3761end;
    3862
     
    4872  Instruction: TSourceInstruction;
    4973  Variable: TSourceVariable;
     74  Value: TSourceValue;
     75  ExecutorVar: TExecutorVariable;
    5076
    51 function ReadStringReference(Reference: TSourceReference): string;
     77function ReadValueReference(Reference: TSourceReference): TSourceValue;
    5278begin
    53   Result := '';
     79  Result := nil;
    5480  if Reference is TSourceReferenceConstant then begin
    5581    Result := TSourceReferenceConstant(Reference).Constant.Value;
    5682  end else
    5783  if Reference is TSourceReferenceVariable then begin
    58     Result := Variables.Values[TSourceReferenceVariable(Reference).Variable.Name];
     84    Result := Variables.Search(TSourceReferenceVariable(Reference).Variable).Value;
    5985  end else raise Exception.Create('Unsupported reference');
    6086end;
     
    75101    with TSourceInstructionFunction(Instruction) do begin
    76102      if Name = 'print' then begin
    77         if Assigned(FOnOutput) then FOnOutput(ReadStringReference(Parameters[0]));
     103        if Assigned(FOnOutput) then begin
     104          Value := ReadValueReference(Parameters[0]);
     105          if Value is TSourceValueString then
     106            FOnOutput(TSourceValueString(Value).Value)
     107          else if Value is TSourceValueInteger then
     108            FOnOutput(IntToStr(TSourceValueInteger(Value).Value))
     109          else raise Exception.Create('Unsupported value type');
     110        end;
    78111      end else
    79112      if Name = 'println' then begin
    80         if Assigned(FOnOutput) then FOnOutput(ReadStringReference(Parameters[0]) +
    81           LineEnding);
     113        if Assigned(FOnOutput) then begin
     114          Value := ReadValueReference(Parameters[0]);
     115          if Value is TSourceValueString then
     116            FOnOutput(TSourceValueString(Value).Value + LineEnding)
     117          else if Value is TSourceValueInteger then
     118            FOnOutput(IntToStr(TSourceValueInteger(Value).Value) + LineEnding)
     119          else raise Exception.Create('Unsupported value type');
     120        end;
    82121      end else
    83122      if Name = 'inputln' then begin
    84123        if Assigned(FOnInput) then begin
    85124          Variable := ReadVarReference(Parameters[0]);
    86           Variables.Values[Variable.Name] := FOnInput;
     125          ExecutorVar := Variables.Search(Variable);
     126          if ExecutorVar.Value is TSourceValueString then begin
     127            TSourceValueString(ExecutorVar.Value).Value := FOnInput;
     128            FOnOutput(TSourceValueString(ExecutorVar.Value).Value + LineEnding);
     129          end else
     130          if ExecutorVar.Value is TSourceValueInteger then begin
     131            TSourceValueInteger(ExecutorVar.Value).Value := StrToInt(FOnInput);
     132            FOnOutput(IntToStr(TSourceValueInteger(ExecutorVar.Value).Value) + LineEnding);
     133          end else
     134          raise Exception.Create('Unsupported value type');
    87135        end;
    88136      end else
    89137      if Name = 'assign' then begin
    90138        Variable := ReadVarReference(Parameters[0]);
    91         Variables.Values[Variable.Name] := ReadStringReference(Parameters[1]);
    92       end else raise Exception.Create('Unsupported function: ' + TSourceInstructionFunction(Instruction).Name);
     139        Value := ReadValueReference(Parameters[1]);
     140        ExecutorVar := Variables.Search(Variable);
     141        if not Assigned(ExecutorVar) then begin
     142          ExecutorVar := TExecutorVariable.Create;
     143          ExecutorVar.Variable := Variable;
     144          Variables.Add(ExecutorVar);
     145          ExecutorVar.Value := Variable.ValueType.ValueClass.Create;
     146        end;
     147        ExecutorVar.Value.Assign(Value);
     148      end else
     149      if Name = 'increment' then begin
     150        Variable := ReadVarReference(Parameters[0]);
     151        Value := ReadValueReference(Parameters[1]);
     152        ExecutorVar := Variables.Search(Variable);
     153        if not Assigned(ExecutorVar) then raise Exception.Create('Variable not found');
     154        if (ExecutorVar.Value is TSourceValueInteger) and (Value is TSourceValueInteger) then
     155          Inc(TSourceValueInteger(ExecutorVar.Value).Value, TSourceValueInteger(Value).Value)
     156        else raise Exception.Create('Wrong type for increment');
     157      end else
     158      raise Exception.Create('Unsupported function: ' + TSourceInstructionFunction(Instruction).Name);
    93159    end else raise Exception.Create('Unsupported instruction');
    94160    Inc(IP);
  • branches/easy compiler/USourceGenerator.pas

    r140 r141  
    2525  Instruction: TSourceInstruction;
    2626  I: Integer;
     27  Value: TSourceValue;
    2728
    2829function GenerateRef(Reference: TSourceReference): string;
     
    3031  Result := '';
    3132  if Reference is TSourceReferenceConstant then begin
    32     Result := Result + '''' + TSourceReferenceConstant(Reference).Constant.Value + '''';
     33    Value := TSourceReferenceConstant(Reference).Constant.Value;
     34    if Value is TSourceValueString then
     35      Result := Result + '''' + TSourceValueString(Value).Value + ''''
     36    else
     37    if Value is TSourceValueInteger then
     38      Result := Result + IntToStr(TSourceValueInteger(Value).Value);
    3339  end else
    3440  if Reference is TSourceReferenceVariable then begin
     
    4349  with SourceCode do
    4450  for I := 0 to Variables.Count - 1 do
    45     Output := Output + '  ' + TSourceVariable(Variables[I]).Name + ': string;' + LineEnding;
     51  with TSourceVariable(Variables[I]) do begin
     52    Output := Output + '  ' + Name + ': ';
     53    if ValueType.Name = 'String' then
     54      Output := Output + 'string'
     55    else if ValueType.Name = 'Integer' then
     56      Output := Output + 'Integer'
     57    else raise Exception.Create('Unsupported type');
     58    Output := Output + ';' + LineEnding;
     59  end;
    4660  Output := Output + 'begin' + LineEnding;
    4761  with SourceCode do
     
    6377      end else
    6478      if Name = 'inputln' then begin
    65         Output := Output + '  ' + GenerateRef(Parameters[0]) + ' := ReadLn;' + LineEnding;
     79        Output := Output + '  ReadLn(' + GenerateRef(Parameters[0]) + ');' + LineEnding;
     80      end else
     81      if Name = 'increment' then begin
     82        Output := Output + '  Inc(' + GenerateRef(Parameters[0]) + ', ' +
     83          GenerateRef(Parameters[1]) + ');' + LineEnding;
    6684      end else
    6785      raise Exception.Create('Unsupported instruction name.');
Note: See TracChangeset for help on using the changeset viewer.