Changeset 42 for branches/DelphiToC/Produce/UCSource.pas
- Timestamp:
- Aug 5, 2010, 3:13:03 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DelphiToC/Produce/UCSource.pas
r36 r42 7 7 uses 8 8 SysUtils, Variants, Classes, Graphics, Controls, Forms, 9 Dialogs, StdCtrls, UPascalSource, UCodeProducer ;9 Dialogs, StdCtrls, UPascalSource, UCodeProducer, StrUtils; 10 10 11 11 type 12 13 { TCProducer } 14 12 15 TCProducer = class(TCodeProducer) 16 private 17 function TranslateType(Name: string): string; 18 function TranslateOperator(Name: string): string; 19 procedure Emit(Text: string); 20 procedure GenerateCommonBlock(CommonBlock: TCommonBlock; 21 LabelPrefix: string); 22 procedure GenerateProgram(ProgramBlock: TProgram); 23 procedure GenerateBeginEnd(BeginEnd: TBeginEnd); 24 procedure GenerateCommand(Command: TCommand); 25 procedure GenerateWhileDo(WhileDo: TWhileDo); 26 procedure GenerateIfThenElse(IfThenElse: TIfThenElse); 27 procedure GenerateAssignment(Assignment: TAssignment); 28 procedure GenerateMethodCall(MethodCall: TMethodCall); 29 function GenerateExpression(Expression: TExpression): string; 30 public 13 31 TextSource: TStringList; 32 IndentationLength: Integer; 33 Indetation: Integer; 14 34 procedure Produce; override; 15 35 constructor Create; 16 36 destructor Destroy; override; 17 private18 procedure GenerateCommonBlock(CommonBlock: TCommonBlock;19 LabelPrefix: string);20 procedure GenerateProgram(ProgramBlock: TProgram);21 37 end; 22 38 … … 28 44 begin 29 45 TextSource := TStringList.Create; 46 IndentationLength := 2; 30 47 end; 31 48 … … 34 51 TextSource.Free; 35 52 inherited; 53 end; 54 55 function TCProducer.TranslateType(Name: string): string; 56 begin 57 if Name = 'Byte' then Result := 'uint8_t'; 58 if Name = 'Word' then Result := 'uint16_t'; 59 if Name = 'Void' then Result := 'void'; 60 end; 61 62 function TCProducer.TranslateOperator(Name: string): string; 63 begin 64 if Name = '=' then Result := '==' 65 else if Name = 'shl' then Result := '<<' 66 else if Name = 'shr' then Result := '>>' 67 else if Name = 'not' then Result := '!' 68 else if Name = 'mod' then Result := '^' 69 else if Name = ':=' then Result := '=' 70 else if Name = '@' then Result := '*' 71 else if Name = 'and' then Result := '&' 72 else if Name = 'or' then Result := '|' 73 else if Name = 'xor' then Result := '^' 74 else Result := Name; 75 end; 76 77 procedure TCProducer.Emit(Text: string); 78 begin 79 TextSource.Add(DupeString(' ', IndentationLength * Indetation) + Text); 36 80 end; 37 81 … … 47 91 I: Integer; 48 92 begin 93 Indetation := 0;; 49 94 with ProgramBlock do 50 95 for I := 0 to Modules.Count - 1 do 51 96 GenerateCommonBlock(TModule(Modules[I]), ''); 97 end; 98 99 procedure TCProducer.GenerateBeginEnd(BeginEnd: TBeginEnd); 100 var 101 I: Integer; 102 begin 103 Emit('{'); 104 Inc(Indetation); 105 106 107 // Variables 108 if BeginEnd.Parent is TCommonBlock then begin 109 for I := 0 to BeginEnd.CommonBlock.Variables.Count - 1 do 110 with TVariable(BeginEnd.CommonBlock.Variables[I]) do 111 Emit(TranslateType(ValueType.Name) + ' ' + Name); 112 Emit(''); 113 end; 114 115 // Commands 116 for I := 0 to BeginEnd.Commands.Count - 1 do 117 GenerateCommand(TCommand(BeginEnd.Commands[I])); 118 119 Dec(Indetation); 120 Emit('}'); 121 end; 122 123 procedure TCProducer.GenerateCommand(Command: TCommand); 124 begin 125 if Command is TBeginEnd then GenerateBeginEnd(TBeginEnd(Command)) 126 else if Command is TWhileDo then GenerateWhileDo(TWhileDo(Command)) 127 else if Command is TIfThenElse then GenerateIfThenElse(TIfThenElse(Command)) 128 else if Command is TAssignment then GenerateAssignment(TAssignment(Command)) 129 else if Command is TMethodCall then GenerateMethodCall(TMethodCall(Command)); 130 end; 131 132 procedure TCProducer.GenerateWhileDo(WhileDo: TWhileDo); 133 begin 134 Emit('while (' + GenerateExpression(WhileDo.Condition) + ')'); 135 GenerateCommand(WhileDo.Command); 136 end; 137 138 procedure TCProducer.GenerateIfThenElse(IfThenElse: TIfThenElse); 139 begin 140 Emit('if(' + GenerateExpression(IfThenElse.Condition) + ')'); 141 GenerateCommand(IfThenElse.Command); 142 Emit('else '); 143 GenerateCommand(IfThenElse.ElseCommand); 144 end; 145 146 procedure TCProducer.GenerateAssignment(Assignment: TAssignment); 147 begin 148 Emit(Assignment.Target.Name + ' = ' + GenerateExpression(Assignment.Source) + ';'); 149 end; 150 151 procedure TCProducer.GenerateMethodCall(MethodCall: TMethodCall); 152 begin 153 Emit(MethodCall.Method.Name + '();'); 154 end; 155 156 function TCProducer.GenerateExpression(Expression: TExpression): string; 157 begin 158 case Expression.NodeType of 159 ntConstant: Result := Expression.Value; 160 ntVariable: Result := Expression.Variable.Name; 161 ntFunction: Result := Expression.Method.Name; 162 ntOperator: begin 163 Result := GenerateExpression(TExpression(Expression.SubItems.First)) 164 + ' ' + TranslateOperator(Expression.OperatorName) + ' ' + 165 GenerateExpression(TExpression(Expression.SubItems.Last)); 166 end; 167 ntNone: ; 168 end; 52 169 end; 53 170 … … 58 175 begin 59 176 with CommonBlock do begin 60 TextSource.Add('void ' + Name + '()'); 61 TextSource.Add('{'); 62 63 TextSource.Add('}'); 177 Emit('void ' + Name + '()'); 178 GenerateBeginEnd(Code); 64 179 end; 65 180 end;
Note:
See TracChangeset
for help on using the changeset viewer.