- Timestamp:
- Nov 9, 2009, 4:15:39 PM (15 years ago)
- Location:
- branches/Void
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Void/Example.void
r10 r11 1 Define Text 2 Text := Hell 3 WriteLn('Leave me') 4 WriteLn('Heal the world') 5 ReadLn 1 var 2 Text: string; 3 begin; 4 Text := 'Hell'; 5 WriteLn('Leave me'); 6 WriteLn(Text); 7 ReadLn; 8 end; -
branches/Void/UCompilator.pas
r10 r11 17 17 FOnError: TOnErrorEvent; 18 18 procedure DoError(Text: string); 19 procedure ParseBeginEnd; 20 procedure ParseProgram; 21 procedure ParseVariableDefinition; 19 22 public 20 23 Model: TModel; … … 23 26 Parser: TVoidParser; 24 27 procedure Compile; 25 procedure Process;26 28 constructor Create; 27 29 destructor Destroy; override; … … 42 44 end; 43 45 46 procedure TCompilator.ParseProgram; 47 begin 48 while SourceCode.Position < SourceCode.Size do 49 with Model, Module, BeginEnd do begin 50 if Parser.TokenType = ttWhiteSpace then begin 51 end else 52 if Parser.TokenType = ttIdentifier then begin 53 if Parser.TokenValue = 'program' then begin 54 Parser.ParseNextToken; 55 if Parser.TokenType <> ttWhiteSpace then DoError('Expect white space'); 56 Parser.ParseNextToken; 57 if Parser.TokenType <> ttString then DoError('Expect string'); 58 Parser.ParseNextToken; 59 if (Parser.TokenType <> ttSymbol) and (Parser.TokenValue <> ';') then DoError('Expect ;'); 60 Parser.ParseNextToken; 61 end else 62 if Parser.TokenValue = 'var' then begin 63 ParseVariableDefinition; 64 end else 65 if Parser.TokenValue = 'begin' then begin 66 ParseBeginEnd; 67 end; 68 end; 69 end; 70 end; 71 72 procedure TCompilator.ParseVariableDefinition; 73 var 74 VariableName: string; 75 VariableType: string; 76 Variable: TVariable; 77 begin 78 repeat 79 Parser.ParseNextToken; 80 if Parser.TokenType <> ttWhiteSpace then DoError('Expected white space'); 81 Parser.ParseNextToken; 82 if Parser.TokenType <> ttIdentifier then DoError('Expected identifier'); 83 VariableName := Parser.TokenValue; 84 Parser.ParseNextToken; 85 if Parser.TokenType = ttWhiteSpace then Parser.ParseNextToken; 86 if (Parser.TokenType <> ttSymbol) and (Parser.TokenValue = ':') then 87 DoError('Expected :'); 88 Parser.ParseNextToken; 89 if Parser.TokenType = ttWhiteSpace then Parser.ParseNextToken; 90 if Parser.TokenType <> ttIdentifier then DoError('Expected identifier'); 91 VariableType := Parser.TokenValue; 92 93 with Model.Module do begin 94 Variable := FindVariableByName(VariableName); 95 if Assigned(Variable) then DoError('Variable ' + VariableName + ' redefined') 96 else begin 97 Variable := TVariable.Create; 98 with Variable do begin 99 Name := VariableName; 100 VarType := VariableType; 101 end; 102 Variables.Add(Variable); 103 end; 104 end; 105 106 Parser.ParseNextToken; 107 if (Parser.TokenType <> ttSymbol) and (Parser.TokenValue = ';') then 108 DoError('Expected ;'); 109 Parser.ParseNextToken; 110 if Parser.TokenType <> ttWhiteSpace then DoError('Expected white space'); 111 Parser.ParseNextToken; 112 until (Parser.TokenType = ttIdentifier) and ((Parser.TokenValue = 'begin') or 113 (Parser.TokenValue = 'type')); 114 end; 115 44 116 procedure TCompilator.Compile; 45 117 var … … 49 121 SourceCode.Position := 0; 50 122 Parser.Open(SourceCode); 51 52 // Process source lines 53 while SourceCode.Position < SourceCode.Size do begin; 54 Process; 55 Parser.ParseNextToken; 56 end; 57 123 ParseProgram; 58 124 Generator.Generate(Model); 59 125 end; 60 126 61 procedure TCompilator.P rocess;127 procedure TCompilator.ParseBeginEnd; 62 128 var 63 129 CommandName: string; … … 67 133 Value: string; 68 134 begin 69 with Model, BeginEnd do begin 135 with Model, Module, BeginEnd do begin 136 if Parser.TokenType = ttWhiteSpace then begin 137 end else 70 138 if Parser.TokenType = ttIdentifier then begin 71 139 CommandName := Parser.TokenValue; 72 if CommandName = 'Define' then begin 73 Parser.ParseNextToken; 74 if Parser.TokenType <> ttWhiteSpace then DoError('Expected white space'); 75 Parser.ParseNextToken; 76 if Parser.TokenType <> ttIdentifier then DoError('Expected identifier'); 77 VariableName := Parser.TokenValue; 78 Variable := FindVariableByName(VariableName); 79 if Assigned(Variable) then DoError('Variable ' + VariableName + ' redefined') 80 else begin 81 Variable := TVariable.Create; 82 with Variable do begin 83 Name := VariableName; 84 VarType := 'string'; 85 end; 86 Variables.Add(Variable); 87 end; 88 end else begin 140 begin 89 141 Command := FindProcedureByName(CommandName); 90 142 if Assigned(Command) then begin … … 131 183 end; 132 184 end; 185 Parser.ParseNextToken; 186 if Parser.TokenType <> ttWhiteSpace then DoError('Expected white space'); 133 187 end else DoError('Expected identifier'); 134 Parser.ParseNextToken;135 if Parser.TokenType <> ttWhiteSpace then DoError('Expected white space');136 188 end; 137 189 end; -
branches/Void/UModel.pas
r9 r11 36 36 end; 37 37 38 { TModel } 39 40 TModel = class 41 private 38 TModule = class 42 39 public 43 40 Variables: TList; 44 41 Procedures: TList; 45 42 BeginEnd: TBeginEnd; 43 constructor Create; 44 destructor Destroy; override; 45 procedure Clear; 46 function FindProcedureByName(AName: string): TProcedure; 46 47 function FindVariableByName(AName: string): TVariable; 47 function FindProcedureByName(AName: string): TProcedure;48 48 procedure Init; 49 end; 50 51 { TModel } 52 53 TModel = class 54 private 55 public 56 Module: TModule; 49 57 procedure Clear; 50 58 constructor Create; … … 56 64 { TModel } 57 65 66 procedure TModel.Clear; 67 begin 68 Module.Clear; 69 end; 70 58 71 constructor TModel.Create; 72 begin 73 Module := TModule.Create; 74 end; 75 76 destructor TModel.Destroy; 77 begin 78 Module.Destroy; 79 inherited Destroy; 80 end; 81 82 { TBeginEnd } 83 84 constructor TBeginEnd.Create; 85 begin 86 Commands := TList.Create; 87 end; 88 89 destructor TBeginEnd.Destroy; 90 var 91 I: Integer; 92 begin 93 for I := 0 to Commands.Count - 1 do 94 TCommand(Commands[I]).Destroy; 95 Commands.Destroy; 96 inherited Destroy; 97 end; 98 99 procedure TBeginEnd.Clear; 100 var 101 I: Integer; 102 begin 103 for I := 0 to Commands.Count - 1 do 104 TCommand(Commands[I]).Destroy; 105 Commands.Clear; 106 end; 107 108 { TCommand } 109 110 constructor TCommand.Create; 111 begin 112 Parameters := TStringList.Create; 113 end; 114 115 destructor TCommand.Destroy; 116 begin 117 Parameters.Destroy; 118 inherited Destroy; 119 end; 120 121 constructor TProcedure.Create; 122 begin 123 BeginEnd := TBeginEnd.Create; 124 Parameters := TStringList.Create; 125 end; 126 127 destructor TProcedure.Destroy; 128 begin 129 Parameters.Destroy; 130 BeginEnd.Destroy; 131 inherited Destroy; 132 end; 133 134 { TModule } 135 136 constructor TModule.Create; 59 137 begin 60 138 Variables := TList.Create; … … 63 141 end; 64 142 65 destructor TMod el.Destroy;143 destructor TModule.Destroy; 66 144 var 67 145 I: Integer; … … 77 155 end; 78 156 79 function TMod el.FindVariableByName(AName: string): TVariable;157 function TModule.FindVariableByName(AName: string): TVariable; 80 158 var 81 159 I: Integer; … … 87 165 end; 88 166 89 function TMod el.FindProcedureByName(AName: string): TProcedure;167 function TModule.FindProcedureByName(AName: string): TProcedure; 90 168 var 91 169 I: Integer; … … 97 175 end; 98 176 99 procedure TModel.Init; 177 178 procedure TModule.Init; 100 179 begin 101 180 with TProcedure(Procedures[Procedures.Add(TProcedure.Create)]) do begin … … 110 189 end; 111 190 112 procedure TMod el.Clear;191 procedure TModule.Clear; 113 192 var 114 193 I: Integer; … … 121 200 end; 122 201 123 124 { TBeginEnd }125 126 constructor TBeginEnd.Create;127 begin128 Commands := TList.Create;129 end;130 131 destructor TBeginEnd.Destroy;132 var133 I: Integer;134 begin135 for I := 0 to Commands.Count - 1 do136 TCommand(Commands[I]).Destroy;137 Commands.Destroy;138 inherited Destroy;139 end;140 141 procedure TBeginEnd.Clear;142 var143 I: Integer;144 begin145 for I := 0 to Commands.Count - 1 do146 TCommand(Commands[I]).Destroy;147 Commands.Clear;148 end;149 150 { TCommand }151 152 constructor TCommand.Create;153 begin154 Parameters := TStringList.Create;155 end;156 157 destructor TCommand.Destroy;158 begin159 Parameters.Destroy;160 inherited Destroy;161 end;162 163 constructor TProcedure.Create;164 begin165 BeginEnd := TBeginEnd.Create;166 Parameters := TStringList.Create;167 end;168 169 destructor TProcedure.Destroy;170 begin171 Parameters.Destroy;172 BeginEnd.Destroy;173 inherited Destroy;174 end;175 176 202 end. 177 203 -
branches/Void/UOutputGenerator.pas
r10 r11 14 14 TOutputGenerator = class 15 15 private 16 Model: TModel; 16 17 public 17 18 Output: TStringList; … … 25 26 TPascalGenerator = class(TOutputGenerator) 26 27 procedure Generate(Model: TModel); override; 28 procedure GenerateModule(Module: TModule); 27 29 end; 28 30 … … 31 33 TCGenerator = class(TOutputGenerator) 32 34 procedure Generate(Model: TModel); override; 35 procedure GenerateModule(Module: TModule); 33 36 end; 34 37 … … 39 42 procedure TOutputGenerator.Generate(Model: TModel); 40 43 begin 41 44 Self.Model := Model; 42 45 end; 43 46 … … 56 59 57 60 procedure TPascalGenerator.Generate(Model: TModel); 61 begin 62 inherited; 63 GenerateModule(Model.Module); 64 end; 65 66 procedure TPascalGenerator.GenerateModule(Module: TModule); 58 67 var 59 68 I, P: Integer; … … 61 70 Row: string; 62 71 begin 63 inherited; 64 65 with Model do begin 72 with Module do begin 66 73 // Prepare output 67 74 Output.Clear; … … 98 105 99 106 procedure TCGenerator.Generate(Model: TModel); 107 begin 108 inherited; 109 GenerateModule(Model.Module); 110 end; 111 112 procedure TCGenerator.GenerateModule(Module: TModule); 100 113 var 101 114 I, P: Integer; 102 115 Row: string; 103 116 ParameterText: string; 104 begin 105 inherited; 106 107 with Model do begin 117 begin 118 with Module do begin 108 119 // Prepare output 109 120 Output.Clear; -
branches/Void/UVoidParser.pas
r10 r11 60 60 TokenStartPosition := Position; 61 61 if Character in [#13, #10, ' '] then begin 62 TokenType := ttWhiteSpace; 62 63 ParseState := psWhiteSpace; 63 64 end else -
branches/Void/project1.lpi
r10 r11 9 9 <Icon Value="0"/> 10 10 <UseXPManifest Value="True"/> 11 <ActiveEditorIndexAtStart Value=" 2"/>11 <ActiveEditorIndexAtStart Value="1"/> 12 12 </General> 13 13 <VersionInfo> … … 38 38 <IsPartOfProject Value="True"/> 39 39 <UnitName Value="project1"/> 40 <CursorPos X="2 6" Y="17"/>40 <CursorPos X="21" Y="16"/> 41 41 <TopLine Value="1"/> 42 42 <EditorIndex Value="2"/> 43 <UsageCount Value=" 29"/>43 <UsageCount Value="32"/> 44 44 <Loaded Value="True"/> 45 45 </Unit0> … … 53 53 <TopLine Value="68"/> 54 54 <EditorIndex Value="0"/> 55 <UsageCount Value=" 29"/>55 <UsageCount Value="32"/> 56 56 <Loaded Value="True"/> 57 57 </Unit1> … … 59 59 <Filename Value="UCompilator.pas"/> 60 60 <UnitName Value="UCompilator"/> 61 <CursorPos X=" 53" Y="116"/>62 <TopLine Value=" 99"/>61 <CursorPos X="1" Y="60"/> 62 <TopLine Value="50"/> 63 63 <EditorIndex Value="1"/> 64 <UsageCount Value="1 5"/>64 <UsageCount Value="16"/> 65 65 <Loaded Value="True"/> 66 66 </Unit2> … … 69 69 <IsPartOfProject Value="True"/> 70 70 <UnitName Value="UOutputGenerator"/> 71 <CursorPos X=" 72" Y="87"/>72 <TopLine Value=" 71"/>71 <CursorPos X="12" Y="118"/> 72 <TopLine Value="112"/> 73 73 <EditorIndex Value="4"/> 74 <UsageCount Value=" 29"/>74 <UsageCount Value="32"/> 75 75 <Loaded Value="True"/> 76 76 </Unit3> … … 80 80 <CursorPos X="15" Y="4"/> 81 81 <TopLine Value="1"/> 82 <UsageCount Value=" 29"/>82 <UsageCount Value="32"/> 83 83 <SyntaxHighlighter Value="None"/> 84 84 </Unit4> … … 87 87 <IsPartOfProject Value="True"/> 88 88 <UnitName Value="UModel"/> 89 <CursorPos X="2 5" Y="87"/>90 <TopLine Value=" 83"/>89 <CursorPos X="24" Y="48"/> 90 <TopLine Value="25"/> 91 91 <EditorIndex Value="5"/> 92 <UsageCount Value=" 29"/>92 <UsageCount Value="32"/> 93 93 <Loaded Value="True"/> 94 94 </Unit5> … … 122 122 <IsPartOfProject Value="True"/> 123 123 <UnitName Value="UVoidParser"/> 124 <CursorPos X=" 44" Y="54"/>125 <TopLine Value=" 40"/>124 <CursorPos X="9" Y="69"/> 125 <TopLine Value="52"/> 126 126 <EditorIndex Value="3"/> 127 <UsageCount Value=" 29"/>127 <UsageCount Value="32"/> 128 128 <Loaded Value="True"/> 129 129 </Unit10> … … 138 138 <Position1> 139 139 <Filename Value="UCompilator.pas"/> 140 <Caret Line=" 107" Column="1" TopLine="92"/>140 <Caret Line="86" Column="1" TopLine="71"/> 141 141 </Position1> 142 142 <Position2> 143 143 <Filename Value="UCompilator.pas"/> 144 <Caret Line=" 108" Column="1" TopLine="93"/>144 <Caret Line="88" Column="1" TopLine="73"/> 145 145 </Position2> 146 146 <Position3> 147 147 <Filename Value="UCompilator.pas"/> 148 <Caret Line=" 109" Column="1" TopLine="94"/>148 <Caret Line="86" Column="22" TopLine="74"/> 149 149 </Position3> 150 150 <Position4> 151 151 <Filename Value="UCompilator.pas"/> 152 <Caret Line=" 111" Column="1" TopLine="96"/>152 <Caret Line="48" Column="1" TopLine="33"/> 153 153 </Position4> 154 154 <Position5> 155 155 <Filename Value="UCompilator.pas"/> 156 <Caret Line=" 112" Column="1" TopLine="97"/>156 <Caret Line="83" Column="1" TopLine="68"/> 157 157 </Position5> 158 158 <Position6> 159 159 <Filename Value="UCompilator.pas"/> 160 <Caret Line=" 113" Column="1" TopLine="98"/>160 <Caret Line="84" Column="1" TopLine="69"/> 161 161 </Position6> 162 162 <Position7> 163 163 <Filename Value="UCompilator.pas"/> 164 <Caret Line=" 114" Column="1" TopLine="99"/>164 <Caret Line="85" Column="1" TopLine="70"/> 165 165 </Position7> 166 166 <Position8> 167 167 <Filename Value="UCompilator.pas"/> 168 <Caret Line=" 115" Column="1" TopLine="100"/>168 <Caret Line="86" Column="1" TopLine="71"/> 169 169 </Position8> 170 170 <Position9> 171 171 <Filename Value="UCompilator.pas"/> 172 <Caret Line=" 116" Column="1" TopLine="101"/>172 <Caret Line="88" Column="1" TopLine="73"/> 173 173 </Position9> 174 174 <Position10> 175 175 <Filename Value="UCompilator.pas"/> 176 <Caret Line=" 117" Column="1" TopLine="102"/>176 <Caret Line="89" Column="1" TopLine="74"/> 177 177 </Position10> 178 178 <Position11> 179 179 <Filename Value="UCompilator.pas"/> 180 <Caret Line=" 116" Column="33" TopLine="103"/>180 <Caret Line="90" Column="1" TopLine="75"/> 181 181 </Position11> 182 182 <Position12> 183 183 <Filename Value="UCompilator.pas"/> 184 <Caret Line=" 114" Column="23" TopLine="99"/>184 <Caret Line="91" Column="1" TopLine="76"/> 185 185 </Position12> 186 186 <Position13> 187 187 <Filename Value="UCompilator.pas"/> 188 <Caret Line=" 113" Column="1" TopLine="98"/>188 <Caret Line="93" Column="1" TopLine="78"/> 189 189 </Position13> 190 190 <Position14> 191 <Filename Value="U VoidParser.pas"/>192 <Caret Line=" 49" Column="1" TopLine="34"/>191 <Filename Value="UCompilator.pas"/> 192 <Caret Line="94" Column="1" TopLine="79"/> 193 193 </Position14> 194 194 <Position15> 195 <Filename Value="U VoidParser.pas"/>196 <Caret Line=" 51" Column="1" TopLine="36"/>195 <Filename Value="UCompilator.pas"/> 196 <Caret Line="95" Column="1" TopLine="80"/> 197 197 </Position15> 198 198 <Position16> 199 <Filename Value="U VoidParser.pas"/>200 <Caret Line=" 52" Column="1" TopLine="37"/>199 <Filename Value="UCompilator.pas"/> 200 <Caret Line="97" Column="1" TopLine="82"/> 201 201 </Position16> 202 202 <Position17> 203 <Filename Value="U VoidParser.pas"/>204 <Caret Line=" 54" Column="1" TopLine="39"/>203 <Filename Value="UCompilator.pas"/> 204 <Caret Line="99" Column="1" TopLine="84"/> 205 205 </Position17> 206 206 <Position18> 207 <Filename Value="U VoidParser.pas"/>208 <Caret Line=" 55" Column="1" TopLine="40"/>207 <Filename Value="UCompilator.pas"/> 208 <Caret Line="100" Column="1" TopLine="85"/> 209 209 </Position18> 210 210 <Position19> 211 <Filename Value="U VoidParser.pas"/>212 <Caret Line=" 56" Column="1" TopLine="41"/>211 <Filename Value="UCompilator.pas"/> 212 <Caret Line="102" Column="1" TopLine="87"/> 213 213 </Position19> 214 214 <Position20> 215 <Filename Value="U VoidParser.pas"/>216 <Caret Line=" 58" Column="1" TopLine="43"/>215 <Filename Value="UCompilator.pas"/> 216 <Caret Line="106" Column="1" TopLine="91"/> 217 217 </Position20> 218 218 <Position21> 219 <Filename Value="U VoidParser.pas"/>220 <Caret Line=" 60" Column="1" TopLine="45"/>219 <Filename Value="UCompilator.pas"/> 220 <Caret Line="107" Column="1" TopLine="92"/> 221 221 </Position21> 222 222 <Position22> 223 <Filename Value="U VoidParser.pas"/>224 <Caret Line=" 61" Column="1" TopLine="46"/>223 <Filename Value="UCompilator.pas"/> 224 <Caret Line="109" Column="1" TopLine="94"/> 225 225 </Position22> 226 226 <Position23> 227 <Filename Value="U VoidParser.pas"/>228 <Caret Line=" 64" Column="1" TopLine="49"/>227 <Filename Value="UCompilator.pas"/> 228 <Caret Line="110" Column="1" TopLine="95"/> 229 229 </Position23> 230 230 <Position24> 231 <Filename Value="U VoidParser.pas"/>232 <Caret Line=" 65" Column="1" TopLine="50"/>231 <Filename Value="UCompilator.pas"/> 232 <Caret Line="111" Column="1" TopLine="96"/> 233 233 </Position24> 234 234 <Position25> 235 <Filename Value="U VoidParser.pas"/>236 <Caret Line="1 65" Column="1" TopLine="142"/>235 <Filename Value="UCompilator.pas"/> 236 <Caret Line="112" Column="1" TopLine="97"/> 237 237 </Position25> 238 238 <Position26> 239 239 <Filename Value="UCompilator.pas"/> 240 <Caret Line="11 3" Column="1" TopLine="98"/>240 <Caret Line="114" Column="1" TopLine="99"/> 241 241 </Position26> 242 242 <Position27> 243 <Filename Value="U VoidParser.pas"/>244 <Caret Line=" 56" Column="26" TopLine="40"/>243 <Filename Value="UCompilator.pas"/> 244 <Caret Line="68" Column="1" TopLine="38"/> 245 245 </Position27> 246 246 <Position28> 247 <Filename Value="U VoidParser.pas"/>248 <Caret Line=" 54" Column="44" TopLine="40"/>247 <Filename Value="UCompilator.pas"/> 248 <Caret Line="49" Column="1" TopLine="34"/> 249 249 </Position28> 250 250 <Position29> 251 <Filename Value=" project1.lpr"/>252 <Caret Line=" 19" Column="1" TopLine="1"/>251 <Filename Value="UCompilator.pas"/> 252 <Caret Line="50" Column="1" TopLine="35"/> 253 253 </Position29> 254 254 <Position30> 255 <Filename Value=" project1.lpr"/>256 <Caret Line=" 20" Column="36" TopLine="1"/>255 <Filename Value="UCompilator.pas"/> 256 <Caret Line="52" Column="1" TopLine="37"/> 257 257 </Position30> 258 258 </JumpHistory> … … 280 280 </CompilerOptions> 281 281 <Debugging> 282 <BreakPoints Count="2"> 283 <Item1> 284 <Source Value="UCompilator.pas"/> 285 <Line Value="47"/> 286 </Item1> 287 <Item2> 288 <Source Value="UCompilator.pas"/> 289 <Line Value="83"/> 290 </Item2> 291 </BreakPoints> 282 292 <Exceptions Count="3"> 283 293 <Item1>
Note:
See TracChangeset
for help on using the changeset viewer.