Changeset 6 for trunk/Compiler
- Timestamp:
- Nov 5, 2010, 11:31:04 AM (14 years ago)
- Location:
- trunk/Compiler
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Compiler/Analyze/UPascalParser.pas
r5 r6 27 27 Expressions: TListExpression): Boolean; 28 28 function ParseExpressionFunctionCall(SourceCode: TExpression; 29 Expressions: TListExpression; var Func: TFunction ): Boolean;29 Expressions: TListExpression; var Func: TFunctionCall): Boolean; 30 30 function ParseUses(SourceCode: TUsedModuleList; AExported: Boolean): Boolean; 31 31 function ParseModule(ProgramCode: TProgram): TModule; … … 225 225 UseConstant: TConstant; 226 226 UseFunction: TFunction; 227 FunctionCall: TFunctionCall; 227 228 NewExpression: TExpression; 228 229 Identifier: string; … … 236 237 if Assigned(UseType) then begin 237 238 ReadToken; 239 if NextToken = '(' then begin 240 Expect('('); 241 // Typecasting 242 NewExpression := TExpression.Create; 243 NewExpression.CommonBlock := SourceCode.CommonBlock; 244 NewExpression.NodeType := ntTypecast; 245 NewExpression.UseType := UseType; 246 ParseExpression(NewExpression); 247 Expect(')'); 248 end else 238 249 if (UseType is TTypeRecord) or (UseType is TTypeClass) then begin 239 // type.variable or type.function250 // Type context 240 251 Expect('.'); 241 252 Identifier := ReadToken; 242 253 UseVariable := TTypeRecord(UseType).CommonBlock.Variables.Search(Identifier); 243 254 if Assigned(UseVariable) then begin 255 // Record or class variable 244 256 NewExpression := TExpression.Create; 245 257 NewExpression.CommonBlock := SourceCode.CommonBlock; … … 250 262 UseFunction := TTypeRecord(UseType).CommonBlock.Functions.Search(Identifier); 251 263 if Assigned(UseFunction) then begin 264 // Record or class functions 265 ParseExpressionFunctionCall(SourceCode, Expressions, FunctionCall); 252 266 NewExpression := TExpression.Create; 253 267 NewExpression.CommonBlock := SourceCode.CommonBlock; 254 268 NewExpression.NodeType := ntFunction; 255 NewExpression.FunctionCall := UseFunction;269 NewExpression.FunctionCall := FunctionCall; 256 270 end; 257 271 end; … … 273 287 if not Assigned(NewExpression) then begin 274 288 // Function call 275 ParseExpressionFunctionCall(SourceCode, Expressions, UseFunction);276 if Assigned( UseFunction) then begin289 ParseExpressionFunctionCall(SourceCode, Expressions, FunctionCall); 290 if Assigned(FunctionCall) then begin 277 291 NewExpression := TExpression.Create; 278 292 NewExpression.CommonBlock := SourceCode.CommonBlock; 279 293 NewExpression.NodeType := ntFunction; 280 NewExpression.FunctionCall := UseFunction;294 NewExpression.FunctionCall := FunctionCall; 281 295 end; 282 296 end; … … 319 333 320 334 function TPascalParser.ParseExpressionFunctionCall(SourceCode: TExpression; 321 Expressions: TListExpression; var Func: TFunction ): Boolean;335 Expressions: TListExpression; var Func: TFunctionCall): Boolean; 322 336 var 323 337 UseFunction: TFunction; 338 NewExpression: TExpression; 339 I: Integer; 324 340 begin 325 341 Func := nil; … … 328 344 if Assigned(UseFunction) then begin 329 345 ReadToken; 330 Func := UseFunction; 346 Func := TFunctionCall.Create; 347 Func.CommonBlock := SourceCode.CommonBlock; 348 Func.FunctionRef := UseFunction; 331 349 if NextToken = '(' then begin 332 350 Expect('('); 333 while NextToken = ',' do begin 334 Expect(','); 335 Expect(')'); 336 end; 351 for I := 0 to Func.FunctionRef.Parameters.Count - 1 do begin 352 if I > 0 then Expect(','); 353 NewExpression := TExpression.Create; 354 NewExpression.CommonBlock := SourceCode.CommonBlock; 355 ParseExpression(NewExpression); 356 Func.ParameterExpression.Add(NewExpression); 357 end; 358 Expect(')'); 337 359 end; 338 360 Result := True; -
trunk/Compiler/Produce/UProducer.pas
r2 r6 10 10 type 11 11 TProducer = class 12 FileExtension: string;13 12 procedure AssignToStringList(Target: TStringList); virtual; abstract; 14 13 procedure Produce(Module: TModule); virtual; abstract; -
trunk/Compiler/Produce/UProducerAsm8051.pas
r2 r6 108 108 begin 109 109 AssemblyCode := TObjectList.Create; 110 FileExtension := '.asm';111 110 end; 112 111 … … 155 154 ntNone: ; 156 155 ntVariable: if Assigned(Variable) then AddInstruction('', 'GETVAR', Variable.Name, ''); 157 nTFunction: AddInstruction('', 'CALL', FunctionCall. Name, '');156 nTFunction: AddInstruction('', 'CALL', FunctionCall.FunctionRef.Name, ''); 158 157 ntConstant: AddInstruction('', 'CONST', '', ''); 159 158 ntOperator: begin … … 192 191 procedure TProducerAsm8051.GenerateModule(Module: TModule); 193 192 begin 194 193 Module.TargetFile := Module.Name + '.asm'; 195 194 end; 196 195 -
trunk/Compiler/Produce/UProducerDynamicC.pas
r5 r6 27 27 procedure GenerateProgram(ProgramBlock: TProgram); 28 28 procedure GenerateFunctions(Functions: TFunctionList; 29 Prefix: string = '' );29 Prefix: string = ''; HeaderOnly: Boolean = False); 30 30 procedure GenerateBeginEnd(BeginEnd: TBeginEnd); 31 31 procedure GenerateVariableList(VariableList: TVariableList); … … 36 36 procedure GenerateIfThenElse(IfThenElse: TIfThenElse); 37 37 procedure GenerateAssignment(Assignment: TAssignment); 38 procedure GenerateFunctionCall(FunctionCall: TFunctionCall);38 function GenerateFunctionCall(FunctionCall: TFunctionCall): string; 39 39 function GenerateExpression(Expression: TExpression): string; 40 40 public … … 55 55 begin 56 56 TextSource := TStringList.Create; 57 FileExtension := '.c';58 57 IndentationLength := 2; 59 58 end; … … 119 118 procedure TProducerDynamicC.GenerateModule(Module: TModule); 120 119 begin 121 EmitLn('#use "platform.lib"');122 EmitLn;123 120 if Module is TModuleProgram then begin 121 Module.TargetFile := Module.Name + '.c'; 122 EmitLn('#use "platform.lib"'); 123 EmitLn; 124 124 TModuleProgram(Module).Body.Name := 'main'; 125 125 GenerateUses(TModuleProgram(Module).UsedModules); … … 127 127 end else 128 128 if Module is TModuleUnit then begin 129 Module.TargetFile := 'Lib\' + Module.Name + '.lib'; 130 EmitLn('/*** BeginHeader */'); 131 EmitLn('#ifndef ' + UpperCase(Module.Name) + '_H'); 132 EmitLn('#define ' + UpperCase(Module.Name) + '_H'); 133 EmitLn; 134 EmitLn('#use "platform.lib"'); 129 135 GenerateUses(TModuleProgram(Module).UsedModules); 136 GenerateTypes(TModuleUnit(Module).Body.Types); 137 EmitLn('/*** EndHeader */'); 138 EmitLn; 139 EmitLn('/*** BeginHeader */'); 140 GenerateFunctions(TModuleUnit(Module).Body.Functions, '', True); 141 EmitLn('/*** EndHeader */'); 142 EmitLn; 143 144 GenerateFunctions(TModuleUnit(Module).Body.Functions); 145 130 146 GenerateCommonBlock(TModuleUnit(Module).Body, ''); 147 148 EmitLn; 149 EmitLn('/*** BeginHeader */'); 150 EmitLn('#endif'); 151 EmitLn('/*** EndHeader */'); 131 152 end; 132 153 end; … … 150 171 151 172 procedure TProducerDynamicC.GenerateFunctions(Functions: TFunctionList; 152 Prefix: string = '' );173 Prefix: string = ''; HeaderOnly: Boolean = False); 153 174 var 154 175 I: Integer; … … 171 192 Line := Line + ')'; 172 193 EmitLn(Line); 173 GenerateBeginEnd(Code);194 if not HeaderOnly then GenerateBeginEnd(Code); 174 195 EmitLn; 175 196 end; … … 218 239 else if Command is TIfThenElse then GenerateIfThenElse(TIfThenElse(Command)) 219 240 else if Command is TAssignment then GenerateAssignment(TAssignment(Command)) 220 else if Command is TFunctionCall then GenerateFunctionCall(TFunctionCall(Command));241 else if Command is TFunctionCall then EmitLn(GenerateFunctionCall(TFunctionCall(Command)) + ';'); 221 242 end; 222 243 … … 254 275 end; 255 276 256 procedure TProducerDynamicC.GenerateFunctionCall(FunctionCall: TFunctionCall);277 function TProducerDynamicC.GenerateFunctionCall(FunctionCall: TFunctionCall): string; 257 278 var 258 279 Line: string; … … 267 288 end; 268 289 end; 269 Line := Line + ') ;';270 EmitLn(Line);271 end;290 Line := Line + ')'; 291 end; 292 Result := Line; 272 293 end; 273 294 … … 285 306 end; 286 307 ntVariable: Result := Expression.Variable.Name; 287 ntFunction: Result := Expression.FunctionCall.Name;308 ntFunction: Result := GenerateFunctionCall(Expression.FunctionCall); 288 309 ntOperator: begin 289 310 Result := GenerateExpression(TExpression(Expression.SubItems.First)) … … 308 329 begin 309 330 with CommonBlock do begin 310 GenerateTypes(Types);311 GenerateFunctions(Functions);312 331 EmitLn('void ' + Name + '()'); 313 332 GenerateBeginEnd(Code); -
trunk/Compiler/Produce/UProducerGCCC.pas
r2 r6 55 55 begin 56 56 TextSource := TStringList.Create; 57 FileExtension := '.c';58 57 IndentationLength := 2; 59 58 end; … … 119 118 procedure TProducerGCCC.GenerateModule(Module: TModule); 120 119 begin 120 Module.TargetFile := Module.Name + '.c'; 121 121 EmitLn('#include "platform.h"'); 122 122 EmitLn; … … 281 281 end; 282 282 ntVariable: Result := Expression.Variable.Name; 283 ntFunction: Result := Expression.FunctionCall. Name;283 ntFunction: Result := Expression.FunctionCall.FunctionRef.Name; 284 284 ntOperator: begin 285 285 Result := GenerateExpression(TExpression(Expression.SubItems.First)) -
trunk/Compiler/Produce/UProducerPascal.pas
r5 r6 51 51 begin 52 52 IndentationLength := 2; 53 FileExtension := '.pas';54 53 TextSource := TStringList.Create; 55 54 end; … … 88 87 procedure TProducerPascal.GenerateModule(Module: TModule); 89 88 begin 89 Module.TargetFile := Module.Name + '.pas'; 90 90 if Module is TModuleProgram then 91 91 with TModuleProgram(Module) do begin … … 320 320 end; 321 321 ntVariable: Result := Expression.Variable.Name; 322 ntFunction: Result := Expression.FunctionCall. Name;322 ntFunction: Result := Expression.FunctionCall.FunctionRef.Name; 323 323 ntOperator: begin 324 324 Result := GenerateExpression(TExpression(Expression.SubItems.First)) -
trunk/Compiler/Produce/UProducerTreeView.pas
r2 r6 152 152 ntConstant: NewNode := TreeView.Items.AddChild(Node, Expression.Value); 153 153 ntVariable: NewNode := TreeView.Items.AddChild(Node, Expression.Variable.Name); 154 ntFunction: NewNode := TreeView.Items.AddChild(Node, Expression.FunctionCall. Name);154 ntFunction: NewNode := TreeView.Items.AddChild(Node, Expression.FunctionCall.FunctionRef.Name); 155 155 ntOperator: begin 156 156 NewNode := TreeView.Items.AddChild(Node, Expression.OperatorName); -
trunk/Compiler/UCompiler.pas
r2 r6 73 73 ProducedCode: TStringList; 74 74 I: Integer; 75 TargetFileName: string; 75 76 begin 76 77 try … … 81 82 //ShowMessage(IntToHex(Integer(Addr(Parser.OnGetSource)), 8)); 82 83 Parser.ParseModule(ProgramCode); 83 for I := 0 to ProgramCode.Modules.Count - 1 do begin 84 Producer.Produce(TModule(ProgramCode.Modules[I])); 84 with ProgramCode do 85 for I := 0 to Modules.Count - 1 do begin 86 Producer.Produce(TModule(Modules[I])); 85 87 Producer.AssignToStringList(ProducedCode); 86 ForceDirectories(TargetFolder + DirectorySeparator + 87 CompiledFolder + DirectorySeparator + Producer.ClassName); 88 ProducedCode.SaveToFile(TargetFolder + DirectorySeparator + 88 TargetFileName := TargetFolder + DirectorySeparator + 89 89 CompiledFolder + DirectorySeparator + Producer.ClassName + 90 DirectorySeparator + TModule(ProgramCode.Modules[I]).Name + Producer.FileExtension); 90 DirectorySeparator + TModule(Modules[I]).TargetFile; 91 ForceDirectories(ExtractFileDir(TargetFileName)); 92 ProducedCode.SaveToFile(TargetFileName); 91 93 end; 92 94 finally -
trunk/Compiler/USourceCode.pas
r5 r6 15 15 16 16 TNodeType = (ntNone, ntVariable, ntFunction, ntConstant, ntOperator, 17 ntValue );17 ntValue, ntTypecast); 18 18 19 19 TTypeVisibility = (tvPublic, tvPublished, tvPrivate, tvProtected); … … 329 329 Variable: TVariable; 330 330 Constant: TConstant; 331 FunctionCall: TFunction; 331 UseType: TType; 332 FunctionCall: TFunctionCall; 332 333 Value: TValue; 333 334 OperatorName: string; … … 396 397 ParentProgram: TProgram; 397 398 Name: string; 399 TargetFile: string; 398 400 UsedModules: TUsedModuleList; 399 401 Body: TCommonBlock;
Note:
See TracChangeset
for help on using the changeset viewer.