source: branches/Void/Generators/UZ80Generator.pas

Last change on this file was 18, checked in by george, 15 years ago
  • Upraveno: Nasazení generického TFPGObjectList pro použité seznamy typu TList.
  • Přidáno: Kostra tříd pro gramatickou analýzu.
File size: 2.1 KB
Line 
1unit UZ80Generator;
2
3{$mode Delphi}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, UModel, StrUtils, UOutputGenerator;
9
10type
11 TZ80Generator = class(TOutputGenerator)
12 private
13 function GenerateVariableValue(Value: TVariableValue): string;
14 public
15 procedure Generate(Model: TModel); override;
16 procedure GenerateModule(Module: TModule);
17 end;
18
19implementation
20
21{ TZ80Generator }
22
23function TZ80Generator.GenerateVariableValue(Value: TVariableValue): string;
24begin
25 case Value.ValueType of
26 vtVariable: if Assigned(Value.VariableDef) then Result := Value.VariableDef.Name
27 else Result := '';
28 vtFloat: Result := FloatToStr(Value.FloatConstant);
29 vtNumber: Result := IntToStr(Value.NumberConstant);
30 vtString: Result := '''' + StringReplace(Value.StringConstant, '''', '''''', [rfReplaceAll]) + '''';
31 vtChar: Result := '''' + Value.CharConstant + '''';
32 end;
33end;
34
35procedure TZ80Generator.Generate(Model: TModel);
36begin
37 inherited;
38 GenerateModule(Model.Module);
39end;
40
41procedure TZ80Generator.GenerateModule(Module: TModule);
42var
43 I, P: Integer;
44 ParameterText: string;
45 Row: string;
46begin
47 with Module do begin
48 // Prepare output
49 Output.Clear;
50
51 // var section
52 for I := 0 to Variables.Count - 1 do
53 with Variables[I] do
54 Output.Add(Name + ': DB ' + VarType.Name + ';');
55
56 // Code block
57 Output.Add('begin:');
58 Inc(IndentCount);
59 for I := 0 to BeginEnd.Commands.Count - 1 do
60 with BeginEnd.Commands[I] do begin
61 if Name = 'Assignment' then Output.Add(' LD ' +
62 Parameters[0].VariableDef.Name + ', ' +
63 GenerateVariableValue(TVariableValue(Parameters[1])))
64 else begin
65 if Parameters.Count > 0 then begin
66 ParameterText := '';
67 for P := 0 to Parameters.Count - 1 do
68 Output.Add(' PUSH ' + GenerateVariableValue(Parameters[P]));
69 end;
70 Output.Add(' CALL ' + Name);
71 end;
72 end;
73 Dec(IndentCount);
74 Output.Add('HALT');
75 end;
76end;
77
78end.
79
80
Note: See TracBrowser for help on using the repository browser.