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/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);
Note: See TracChangeset for help on using the changeset viewer.