Changeset 90


Ignore:
Timestamp:
Sep 30, 2016, 10:47:25 PM (8 years ago)
Author:
chronos
Message:
  • Added: Support for user defined labels.
Location:
branches/virt simple
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/virt simple/UCompiler.pas

    r89 r90  
    1313  TCompiler = class
    1414  private
     15    Labels: TStringList;
    1516    function GetPart(var Text: string; Separator: string): string;
    1617    procedure ParseLine(Line: string; Instructions: TInstructions);
     
    5253  Value2: Integer;
    5354  OutValue: Integer;
     55  LineLabel: string;
     56  I: Integer;
    5457begin
    5558  Line := Trim(Line);
     
    5760  if Pos(';', Line) > 0 then begin
    5861    Line := Trim(Copy(Line, 1, Pos(';', Line) - 1));
     62  end;
     63  if Pos(':', Line) > 0 then begin
     64    LineLabel := Trim(GetPart(Line, ':'));
     65    if Labels.IndexOf(LineLabel) <> -1 then raise Exception.Create('Label ' + LineLabel + ' already defined');
     66    Labels.AddObject(LineLabel, TObject(Instructions.Count));
    5967  end;
    6068  if Line = '' then Exit;
     
    8492      if TryStrToInt(Param1, OutValue) then begin
    8593        Value1 := OutValue;
    86       end else raise Exception.Create('Unsupported parameter value ' + Param1);
     94      end else begin
     95        I := Labels.IndexOf(Param1);
     96        if I <> -1 then Value1 := Integer(Labels.Objects[I])
     97          else raise Exception.Create('Unsupported parameter value ' + Param1);
     98      end;
    8799    end;
    88100    end else ParamType1 := ptNone;
     
    109121      if TryStrToInt(Param2, OutValue) then begin
    110122        Value2 := OutValue;
    111       end else raise Exception.Create('Unsupported parameter value ' + Param2);
     123      end else begin
     124        I := Labels.IndexOf(Param2);
     125        if I <> -1 then Value2 := Integer(Labels.Objects[I])
     126          else raise Exception.Create('Unsupported parameter value ' + Param2);
     127      end;
    112128    end;
    113129    end else ParamType2 := ptNone;
     
    132148  I: Integer;
    133149begin
     150  Labels.Clear;
    134151  Instructions.Clear;
    135152  for I := 0 to Lines.Count - 1 do begin
     
    141158begin
    142159  Lines := TStringList.Create;
     160  Labels := TStringList.Create;
    143161end;
    144162
    145163destructor TCompiler.Destroy;
    146164begin
     165  FreeAndNil(Labels);
    147166  FreeAndNil(Lines);
    148167  inherited Destroy;
  • branches/virt simple/example.vasm

    r89 r90  
     1Start:
    12  NOP
    23  INC R0
     
    1011  LD R0, 1000
    1112  SYS 2          ; Delay 1 second
    12   JP 0
     13  JP Start
    1314  LD [R4], [R3]
    1415  HALT
Note: See TracChangeset for help on using the changeset viewer.