- Timestamp:
- Nov 10, 2009, 8:19:41 AM (15 years ago)
- Location:
- branches/Void
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Void/UCompilator.pas
r13 r14 10 10 type 11 11 TOnErrorEvent = procedure (Text: string; var Terminate: Boolean; Position: TPoint) of object; 12 13 { TCompilator }14 12 15 13 TCompilator = class … … 135 133 Command: TProcedure; 136 134 Variable: TVariable; 135 SourceVariable: TVariable; 137 136 VariableName: string; 138 137 Value: string; 138 P: Integer; 139 139 begin 140 140 Parser.ParseNextToken; … … 151 151 Name := CommandName; 152 152 Parser.ParseNextToken; 153 if (Parser.TokenType = ttSymbol) and (Parser.TokenValue = '(') then begin 154 Parser.ParseNextToken; 155 if (Parser.TokenType <> ttString) then DoError('Expected string'); 156 Parameters.Add(Parser.TokenValue); 157 Parser.ParseNextToken; 158 if (Parser.TokenType <> ttSymbol) and (Parser.TokenValue = ')') then 159 DoError('Expected )'); 153 if Command.Parameters.Count > 0 then begin 154 if (Parser.TokenType = ttSymbol) and (Parser.TokenValue = '(') then begin 155 Parser.ParseNextToken; 156 for P := 0 to Command.Parameters.Count - 1 do begin 157 if (Parser.TokenType = ttString) then 158 with TVariableValue(Parameters[Parameters.Add(TVariableValue.Create)]) do begin 159 ValueType := vtString; 160 StringConstant := Parser.TokenValue; 161 end else 162 if (Parser.TokenType = ttIdentifier) then 163 with TVariableValue(Parameters[Parameters.Add(TVariableValue.Create)]) do begin 164 ValueType := vtVariable; 165 SourceVariable := FindVariableByName(Parser.TokenValue); 166 if Assigned(SourceVariable) then begin 167 VariableDef := SourceVariable; 168 end else DoError('Unknown identifier ' + Parser.TokenValue); 169 end else DoError('Unknown parameter ' + Parser.TokenValue); 170 if P <> Command.Parameters.Count - 1 then begin 171 Parser.ParseNextToken; 172 if (Parser.TokenType <> ttSymbol) and (Parser.TokenValue = ',') then 173 DoError('Expected ,'); 174 end; 175 Parser.ParseNextToken; 176 end; 177 if (Parser.TokenType <> ttSymbol) and (Parser.TokenValue = ')') then 178 DoError('Expected )'); 179 end else DoError('Expected ('); 160 180 end; 161 181 end; … … 173 193 Value := Parser.TokenValue; 174 194 if Parser.TokenType = ttIdentifier then begin 175 Variable := FindVariableByName(CommandName); 176 if Assigned(Variable) then 177 with TCommand(Commands[Commands.Add(TCommand.Create)]) do begin 178 Name := 'Assignment'; 179 Parameters.Add(CommandName); 180 Parameters.Add(Value); 181 end else DoError('Undefined variable ' + CommandName); 195 SourceVariable := FindVariableByName(Value); 196 if not Assigned(SourceVariable) then DoError('Undefined source variable ' + Value) 197 else begin 198 Variable := FindVariableByName(CommandName); 199 if Assigned(Variable) then 200 with TCommand(Commands[Commands.Add(TCommand.Create)]) do begin 201 Name := 'Assignment'; 202 with TVariableValue(Parameters[Parameters.Add(TVariableValue.Create)]) do begin 203 ValueType := vtVariable; 204 Variable := Variable; 205 end; 206 with TVariableValue(Parameters[Parameters.Add(TVariableValue.Create)]) do begin 207 ValueType := vtVariable; 208 VariableDef := SourceVariable; 209 end; 210 end; 211 end; 182 212 end else if Parser.TokenType = ttString then begin 183 213 with TCommand(Commands[Commands.Add(TCommand.Create)]) do begin 184 214 Name := 'Assignment'; 185 Parameters.Add(CommandName); 186 Parameters.Add(Value); 215 with TVariableValue(Parameters[Parameters.Add(TVariableValue.Create)]) do begin 216 ValueType := vtVariable; 217 VariableDef := Variable; 218 end; 219 with TVariableValue(Parameters[Parameters.Add(TVariableValue.Create)]) do begin 220 ValueType := vtString; 221 StringConstant := Parser.TokenValue; 222 end; 187 223 end; 188 224 end else DoError('Expected variable or string') -
branches/Void/UModel.pas
r13 r14 9 9 10 10 type 11 TValueType = (vtVariable, vtNumber, vtString, vtFloat, vtChar); 12 11 13 TType = class 12 14 Name: string; … … 19 21 end; 20 22 23 TVariableValue = class 24 ValueType: TValueType; 25 VariableDef: TVariable; 26 StringConstant: string; 27 NumberConstant: Integer; 28 FloatConstant: Double; 29 CharConstant: Char; 30 end; 31 21 32 TCommand = class 22 33 Name: string; 23 Parameters: T StringList;34 Parameters: TList; 24 35 constructor Create; 25 36 destructor Destroy; override; … … 35 46 TProcedure = class 36 47 Name: string; 37 Parameters: T StringList;48 Parameters: TList; 38 49 BeginEnd: TBeginEnd; 39 50 constructor Create; 40 51 destructor Destroy; override; 41 52 end; 42 43 { TModule }44 53 45 54 TModule = class … … 119 128 constructor TCommand.Create; 120 129 begin 121 Parameters := T StringList.Create;130 Parameters := TList.Create; 122 131 end; 123 132 124 133 destructor TCommand.Destroy; 125 begin 134 var 135 I: Integer; 136 begin 137 for I := 0 to Parameters.Count - 1 do 138 TVariableValue(Parameters[I]).Destroy; 126 139 Parameters.Destroy; 127 140 inherited Destroy; … … 131 144 begin 132 145 BeginEnd := TBeginEnd.Create; 133 Parameters := T StringList.Create;146 Parameters := TList.Create; 134 147 end; 135 148 136 149 destructor TProcedure.Destroy; 137 begin 150 var 151 I: Integer; 152 begin 153 for I := 0 to Parameters.Count - 1 do 154 TVariableValue(Parameters[I]).Destroy; 138 155 Parameters.Destroy; 139 156 BeginEnd.Destroy; … … 173 190 begin 174 191 I := 0; 175 while (I < Variables.Count) and ( TVariable(Variables[I]).Name <> AName) do192 while (I < Variables.Count) and (LowerCase(TVariable(Variables[I]).Name) <> LowerCase(AName)) do 176 193 Inc(I); 177 194 if I < Variables.Count then Result := Variables[I] else Result := nil; … … 183 200 begin 184 201 I := 0; 185 while (I < Procedures.Count) and ( TProcedure(Procedures[I]).Name <> AName) do202 while (I < Procedures.Count) and (LowerCase(TProcedure(Procedures[I]).Name) <> LowerCase(AName)) do 186 203 Inc(I); 187 204 if I < Procedures.Count then Result := Procedures[I] else Result := nil; … … 211 228 with TProcedure(Procedures[Procedures.Add(TProcedure.Create)]) do begin 212 229 Name := 'WriteLn'; 230 with TVariable(Parameters[Parameters.Add(TVariable.Create)]) do begin 231 Name := 'Text'; 232 VarType := FindTypeByName('String'); 233 end; 213 234 end; 214 235 with TProcedure(Procedures[Procedures.Add(TProcedure.Create)]) do begin … … 236 257 begin 237 258 I := 0; 238 while (I < Types.Count) and ( TType(Types[I]).Name <> AName) do259 while (I < Types.Count) and (LowerCase(TType(Types[I]).Name) <> LowerCase(AName)) do 239 260 Inc(I); 240 261 if I < Types.Count then Result := Types[I] else Result := nil; -
branches/Void/UOutputGenerator.pas
r13 r14 21 21 end; 22 22 23 { TPascalGenerator } 24 23 25 TPascalGenerator = class(TOutputGenerator) 26 private 27 function GenerateVariableValue(Value: TVariableValue): string; 28 public 24 29 procedure Generate(Model: TModel); override; 25 30 procedure GenerateModule(Module: TModule); 26 31 end; 32 33 { TCGenerator } 27 34 28 35 TCGenerator = class(TOutputGenerator) 29 36 private 30 37 function ConvertType(Name: string): string; 38 function GenerateVariableValue(Value: TVariableValue): string; 31 39 public 32 40 procedure Generate(Model: TModel); override; … … 60 68 61 69 { TPascalGenerator } 70 71 function TPascalGenerator.GenerateVariableValue(Value: TVariableValue): string; 72 begin 73 case Value.ValueType of 74 vtVariable: if Assigned(Value.VariableDef) then Result := Value.VariableDef.Name 75 else Result := ''; 76 vtFloat: Result := FloatToStr(Value.FloatConstant); 77 vtNumber: Result := IntToStr(Value.NumberConstant); 78 vtString: Result := '''' + Value.StringConstant + ''''; 79 vtChar: Result := '''' + Value.CharConstant + ''''; 80 end; 81 end; 62 82 63 83 procedure TPascalGenerator.Generate(Model: TModel); … … 92 112 for I := 0 to BeginEnd.Commands.Count - 1 do 93 113 with TCommand(BeginEnd.Commands[I]) do begin 94 if Name = 'Assignment' then Output.Add(Indent + Parameters[0] + ' := ' + Parameters[1] + ';') 114 if Name = 'Assignment' then Output.Add(Indent + 115 TVariableValue(Parameters[0]).VariableDef.Name + ' := ' + 116 GenerateVariableValue(TVariableValue(Parameters[1])) + ';') 95 117 else begin 96 118 Row := Name; … … 98 120 ParameterText := ''; 99 121 for P := 0 to Parameters.Count - 1 do 100 ParameterText := ParameterText + '''' + Parameters[P] + ''', ';122 ParameterText := ParameterText + GenerateVariableValue(Parameters[P]) + ', '; 101 123 Row := Row + '(' + Copy(ParameterText, 1, Length(ParameterText) - 2) + ')'; 102 124 end; … … 124 146 end; 125 147 148 function TCGenerator.GenerateVariableValue(Value: TVariableValue): string; 149 begin 150 case Value.ValueType of 151 vtVariable: if Assigned(Value.VariableDef) then Result := Value.VariableDef.Name 152 else Result := ''; 153 vtFloat: Result := FloatToStr(Value.FloatConstant); 154 vtNumber: Result := IntToStr(Value.NumberConstant); 155 vtString: Result := '"' + Value.StringConstant + '"'; 156 vtChar: Result := '''' + Value.CharConstant + ''''; 157 end; 158 end; 159 126 160 procedure TCGenerator.Generate(Model: TModel); 127 161 begin … … 145 179 Inc(IndentCount); 146 180 147 // variable section181 // Variable section 148 182 for I := 0 to Variables.Count - 1 do 149 183 with TVariable(Variables[I]) do 150 Output.Add(Indent + ' ' +ConvertType(VarType.Name) + ' ' + Name + ';');184 Output.Add(Indent + ConvertType(VarType.Name) + ' ' + Name + ';'); 151 185 if Variables.Count > 0 then Output.Add(''); 152 186 … … 154 188 for I := 0 to BeginEnd.Commands.Count - 1 do 155 189 with TCommand(BeginEnd.Commands[I]) do begin 156 if Name = 'Assignment' then Output.Add(Indent + Parameters[0] + ' = ' + Parameters[1] + ';') 190 if Name = 'Assignment' then Output.Add(Indent + 191 TVariableValue(Parameters[0]).VariableDef.Name + ' = ' + 192 GenerateVariableValue(TVariableValue(Parameters[1])) + ';') 157 193 else begin 158 194 if Name = 'WriteLn' then Row := 'printf' … … 163 199 ParameterText := ''; 164 200 for P := 0 to Parameters.Count - 1 do 165 ParameterText := ParameterText + '''' + Parameters[P] + ''', ';201 ParameterText := ParameterText + GenerateVariableValue(Parameters[P]) + ', '; 166 202 Row := Row + Copy(ParameterText, 1, Length(ParameterText) - 2); 167 203 end; -
branches/Void/project1.lpi
r13 r14 9 9 <Icon Value="0"/> 10 10 <UseXPManifest Value="True"/> 11 <ActiveEditorIndexAtStart Value=" 4"/>11 <ActiveEditorIndexAtStart Value="3"/> 12 12 </General> 13 13 <VersionInfo> … … 33 33 </Item1> 34 34 </RequiredPackages> 35 <Units Count="1 4">35 <Units Count="15"> 36 36 <Unit0> 37 37 <Filename Value="project1.lpr"/> … … 41 41 <TopLine Value="1"/> 42 42 <EditorIndex Value="4"/> 43 <UsageCount Value="4 0"/>43 <UsageCount Value="42"/> 44 44 <Loaded Value="True"/> 45 45 </Unit0> … … 53 53 <TopLine Value="28"/> 54 54 <EditorIndex Value="0"/> 55 <UsageCount Value="4 0"/>55 <UsageCount Value="42"/> 56 56 <Loaded Value="True"/> 57 57 </Unit1> … … 59 59 <Filename Value="UCompilator.pas"/> 60 60 <UnitName Value="UCompilator"/> 61 <CursorPos X=" 33" Y="91"/>62 <TopLine Value=" 82"/>61 <CursorPos X="53" Y="168"/> 62 <TopLine Value="149"/> 63 63 <EditorIndex Value="1"/> 64 <UsageCount Value="2 0"/>64 <UsageCount Value="21"/> 65 65 <Loaded Value="True"/> 66 66 </Unit2> … … 69 69 <IsPartOfProject Value="True"/> 70 70 <UnitName Value="UOutputGenerator"/> 71 <CursorPos X=" 32" Y="151"/>72 <TopLine Value="1 43"/>71 <CursorPos X="7" Y="160"/> 72 <TopLine Value="127"/> 73 73 <EditorIndex Value="3"/> 74 <UsageCount Value="4 0"/>74 <UsageCount Value="42"/> 75 75 <Loaded Value="True"/> 76 76 </Unit3> … … 80 80 <CursorPos X="4" Y="1"/> 81 81 <TopLine Value="1"/> 82 <UsageCount Value="4 0"/>82 <UsageCount Value="42"/> 83 83 <SyntaxHighlighter Value="None"/> 84 84 </Unit4> … … 87 87 <IsPartOfProject Value="True"/> 88 88 <UnitName Value="UModel"/> 89 <CursorPos X=" 19" Y="18"/>90 <TopLine Value=" 1"/>89 <CursorPos X="27" Y="229"/> 90 <TopLine Value="215"/> 91 91 <EditorIndex Value="6"/> 92 <UsageCount Value="4 0"/>92 <UsageCount Value="42"/> 93 93 <Loaded Value="True"/> 94 94 </Unit5> … … 125 125 <TopLine Value="45"/> 126 126 <EditorIndex Value="2"/> 127 <UsageCount Value="4 0"/>127 <UsageCount Value="42"/> 128 128 <Loaded Value="True"/> 129 129 </Unit10> … … 145 145 <UnitName Value="strutils"/> 146 146 <CursorPos X="1" Y="1"/> 147 <TopLine Value=" 43"/>147 <TopLine Value="1"/> 148 148 <EditorIndex Value="5"/> 149 <UsageCount Value="11"/> 150 <Loaded Value="True"/> 151 </Unit13> 152 <Unit14> 153 <Filename Value="..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\sysutils\sysutilh.inc"/> 154 <CursorPos X="4" Y="36"/> 155 <TopLine Value="21"/> 149 156 <UsageCount Value="10"/> 150 <Loaded Value="True"/> 151 </Unit13> 157 </Unit14> 152 158 </Units> 153 159 <JumpHistory Count="30" HistoryIndex="29"> 154 160 <Position1> 155 161 <Filename Value="UCompilator.pas"/> 156 <Caret Line=" 86" Column="1" TopLine="70"/>162 <Caret Line="148" Column="1" TopLine="133"/> 157 163 </Position1> 158 164 <Position2> 159 165 <Filename Value="UCompilator.pas"/> 160 <Caret Line=" 87" Column="1" TopLine="71"/>166 <Caret Line="149" Column="1" TopLine="134"/> 161 167 </Position2> 162 168 <Position3> 163 169 <Filename Value="UCompilator.pas"/> 164 <Caret Line=" 89" Column="1" TopLine="73"/>170 <Caret Line="183" Column="1" TopLine="168"/> 165 171 </Position3> 166 172 <Position4> 167 173 <Filename Value="UCompilator.pas"/> 168 <Caret Line=" 90" Column="1" TopLine="74"/>174 <Caret Line="184" Column="1" TopLine="169"/> 169 175 </Position4> 170 176 <Position5> 171 177 <Filename Value="UCompilator.pas"/> 172 <Caret Line=" 91" Column="1" TopLine="75"/>178 <Caret Line="185" Column="1" TopLine="170"/> 173 179 </Position5> 174 180 <Position6> 175 181 <Filename Value="UCompilator.pas"/> 176 <Caret Line=" 92" Column="1" TopLine="76"/>182 <Caret Line="186" Column="1" TopLine="171"/> 177 183 </Position6> 178 184 <Position7> 179 185 <Filename Value="UCompilator.pas"/> 180 <Caret Line=" 94" Column="1" TopLine="78"/>186 <Caret Line="187" Column="1" TopLine="172"/> 181 187 </Position7> 182 188 <Position8> 183 189 <Filename Value="UCompilator.pas"/> 184 <Caret Line=" 95" Column="1" TopLine="79"/>190 <Caret Line="188" Column="1" TopLine="173"/> 185 191 </Position8> 186 192 <Position9> 187 193 <Filename Value="UCompilator.pas"/> 188 <Caret Line=" 96" Column="1" TopLine="80"/>194 <Caret Line="190" Column="1" TopLine="175"/> 189 195 </Position9> 190 196 <Position10> 191 197 <Filename Value="UCompilator.pas"/> 192 <Caret Line=" 98" Column="1" TopLine="82"/>198 <Caret Line="191" Column="1" TopLine="176"/> 193 199 </Position10> 194 200 <Position11> 195 201 <Filename Value="UCompilator.pas"/> 196 <Caret Line="1 00" Column="1" TopLine="84"/>202 <Caret Line="192" Column="1" TopLine="177"/> 197 203 </Position11> 198 204 <Position12> 199 205 <Filename Value="UCompilator.pas"/> 200 <Caret Line="1 01" Column="1" TopLine="85"/>206 <Caret Line="193" Column="1" TopLine="178"/> 201 207 </Position12> 202 208 <Position13> 203 209 <Filename Value="UCompilator.pas"/> 204 <Caret Line="1 03" Column="1" TopLine="87"/>210 <Caret Line="194" Column="1" TopLine="179"/> 205 211 </Position13> 206 212 <Position14> 207 213 <Filename Value="UCompilator.pas"/> 208 <Caret Line=" 107" Column="1" TopLine="91"/>214 <Caret Line="212" Column="1" TopLine="197"/> 209 215 </Position14> 210 216 <Position15> 211 217 <Filename Value="UCompilator.pas"/> 212 <Caret Line=" 108" Column="1" TopLine="92"/>218 <Caret Line="213" Column="1" TopLine="198"/> 213 219 </Position15> 214 220 <Position16> 215 221 <Filename Value="UCompilator.pas"/> 216 <Caret Line=" 110" Column="1" TopLine="94"/>222 <Caret Line="214" Column="1" TopLine="199"/> 217 223 </Position16> 218 224 <Position17> 219 225 <Filename Value="UCompilator.pas"/> 220 <Caret Line=" 111" Column="1" TopLine="95"/>226 <Caret Line="215" Column="1" TopLine="200"/> 221 227 </Position17> 222 228 <Position18> 223 229 <Filename Value="UCompilator.pas"/> 224 <Caret Line=" 112" Column="1" TopLine="96"/>230 <Caret Line="216" Column="22" TopLine="187"/> 225 231 </Position18> 226 232 <Position19> 227 <Filename Value="U Compilator.pas"/>228 <Caret Line="11 3" Column="1" TopLine="97"/>233 <Filename Value="UOutputGenerator.pas"/> 234 <Caret Line="116" Column="26" TopLine="100"/> 229 235 </Position19> 230 236 <Position20> 231 <Filename Value="U Compilator.pas"/>232 <Caret Line=" 114" Column="1" TopLine="98"/>237 <Filename Value="UOutputGenerator.pas"/> 238 <Caret Line="74" Column="44" TopLine="59"/> 233 239 </Position20> 234 240 <Position21> 235 <Filename Value="U Model.pas"/>236 <Caret Line=" 54" Column="50" TopLine="40"/>241 <Filename Value="UOutputGenerator.pas"/> 242 <Caret Line="114" Column="52" TopLine="99"/> 237 243 </Position21> 238 244 <Position22> 239 245 <Filename Value="UOutputGenerator.pas"/> 240 <Caret Line="1 18" Column="12" TopLine="106"/>246 <Caret Line="150" Column="44" TopLine="135"/> 241 247 </Position22> 242 248 <Position23> 243 <Filename Value="U OutputGenerator.pas"/>244 <Caret Line=" 82" Column="53" TopLine="61"/>249 <Filename Value="UCompilator.pas"/> 250 <Caret Line="216" Column="22" TopLine="187"/> 245 251 </Position23> 246 252 <Position24> 247 <Filename Value="U OutputGenerator.pas"/>248 <Caret Line=" 35" Column="48" TopLine="20"/>253 <Filename Value="UModel.pas"/> 254 <Caret Line="230" Column="15" TopLine="211"/> 249 255 </Position24> 250 256 <Position25> 251 <Filename Value="U OutputGenerator.pas"/>252 <Caret Line=" 122" Column="41" TopLine="109"/>257 <Filename Value="UModel.pas"/> 258 <Caret Line="232" Column="22" TopLine="212"/> 253 259 </Position25> 254 260 <Position26> 255 <Filename Value="U OutputGenerator.pas"/>256 <Caret Line="1 8" Column="20" TopLine="4"/>261 <Filename Value="UModel.pas"/> 262 <Caret Line="192" Column="96" TopLine="187"/> 257 263 </Position26> 258 264 <Position27> 259 <Filename Value="U OutputGenerator.pas"/>260 <Caret Line=" 45" Column="42" TopLine="43"/>265 <Filename Value="UModel.pas"/> 266 <Caret Line="11" Column="16" TopLine="1"/> 261 267 </Position27> 262 268 <Position28> 263 <Filename Value="U OutputGenerator.pas"/>264 <Caret Line="4 2" Column="28" TopLine="40"/>269 <Filename Value="UModel.pas"/> 270 <Caret Line="46" Column="8" TopLine="19"/> 265 271 </Position28> 266 272 <Position29> 267 <Filename Value="U OutputGenerator.pas"/>268 <Caret Line=" 8" Column="34" TopLine="1"/>273 <Filename Value="UModel.pas"/> 274 <Caret Line="229" Column="27" TopLine="215"/> 269 275 </Position29> 270 276 <Position30> 271 <Filename Value=" project1.lpr"/>272 <Caret Line="16 " Column="21" TopLine="1"/>277 <Filename Value="UCompilator.pas"/> 278 <Caret Line="167" Column="107" TopLine="148"/> 273 279 </Position30> 274 280 </JumpHistory>
Note:
See TracChangeset
for help on using the changeset viewer.