Changeset 13 for branches/Void
- Timestamp:
- Nov 10, 2009, 7:14:14 AM (15 years ago)
- Location:
- branches/Void
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Void/Example.void
r12 r13 1 var 2 Text: string;1 var 2 Text: String; 3 3 A: Integer; 4 4 begin -
branches/Void/UCompilator.pas
r12 r13 76 76 VariableType: string; 77 77 Variable: TVariable; 78 VarType: TType; 78 79 begin 79 80 Parser.ParseNextToken; … … 83 84 repeat 84 85 VariableName := Parser.TokenValue; 85 Parser.ParseNextToken;86 if Parser.TokenType = ttWhiteSpace then Parser.ParseNextToken;87 if (Parser.TokenType <> ttSymbol) and (Parser.TokenValue = ':') then88 DoError('Expected :');89 Parser.ParseNextToken;90 if Parser.TokenType = ttWhiteSpace then Parser.ParseNextToken;91 if Parser.TokenType <> ttIdentifier then DoError('Expected identifier');92 VariableType := Parser.TokenValue;93 94 86 with Model.Module do begin 95 Variable := FindVariableByName(VariableName); 96 if Assigned(Variable) then DoError('Variable ' + VariableName + ' redefined') 97 else begin 98 Variable := TVariable.Create; 99 with Variable do begin 100 Name := VariableName; 101 VarType := VariableType; 87 Variable := FindVariableByName(VariableName); 88 if Assigned(Variable) then DoError('Variable ' + VariableName + ' redefined') 89 else begin 90 Parser.ParseNextToken; 91 if Parser.TokenType = ttWhiteSpace then Parser.ParseNextToken; 92 if (Parser.TokenType <> ttSymbol) and (Parser.TokenValue = ':') then 93 DoError('Expected :'); 94 Parser.ParseNextToken; 95 if Parser.TokenType = ttWhiteSpace then Parser.ParseNextToken; 96 if Parser.TokenType <> ttIdentifier then DoError('Expected identifier'); 97 VariableType := Parser.TokenValue; 98 VarType := FindTypeByName(VariableType); 99 if not Assigned(VarType) then DoError('Undefined type ' + VariableType) 100 else begin 101 Variable := TVariable.Create; 102 with Variable do begin 103 Name := VariableName; 104 end; 105 Variable.VarType := VarType; 106 Variables.Add(Variable); 107 end; 102 108 end; 103 Variables.Add(Variable);104 end;105 109 end; 106 110 -
branches/Void/UModel.pas
r11 r13 9 9 10 10 type 11 TType = class 12 Name: string; 13 BaseType: TType; 14 end; 15 11 16 TVariable = class 12 17 Name: string; 13 VarType: string;18 VarType: TType; 14 19 end; 15 20 … … 36 41 end; 37 42 43 { TModule } 44 38 45 TModule = class 39 46 public 47 Types: TList; 40 48 Variables: TList; 41 49 Procedures: TList; … … 44 52 destructor Destroy; override; 45 53 procedure Clear; 54 function FindTypeByName(AName: string): TType; 46 55 function FindProcedureByName(AName: string): TProcedure; 47 56 function FindVariableByName(AName: string): TVariable; … … 136 145 constructor TModule.Create; 137 146 begin 147 Types := TList.Create; 138 148 Variables := TList.Create; 139 149 BeginEnd := TBeginEnd.Create; … … 145 155 I: Integer; 146 156 begin 157 for I := 0 to Types.Count - 1 do 158 TType(Types[I]).Destroy; 159 Types.Destroy; 147 160 for I := 0 to Variables.Count - 1 do 148 161 TVariable(Variables[I]).Destroy; … … 178 191 procedure TModule.Init; 179 192 begin 193 // System types 194 with TType(Types[Types.Add(TType.Create)]) do begin 195 Name := 'Integer'; 196 end; 197 with TType(Types[Types.Add(TType.Create)]) do begin 198 Name := 'String'; 199 end; 200 with TType(Types[Types.Add(TType.Create)]) do begin 201 Name := 'Char'; 202 end; 203 with TType(Types[Types.Add(TType.Create)]) do begin 204 Name := 'Byte'; 205 end; 206 with TType(Types[Types.Add(TType.Create)]) do begin 207 Name := 'Word'; 208 end; 209 210 // System procedures 180 211 with TProcedure(Procedures[Procedures.Add(TProcedure.Create)]) do begin 181 212 Name := 'WriteLn'; 182 213 end; 183 214 with TProcedure(Procedures[Procedures.Add(TProcedure.Create)]) do begin 184 215 Name := 'Exit'; 185 216 end; … … 200 231 end; 201 232 233 function TModule.FindTypeByName(AName: string): TType; 234 var 235 I: Integer; 236 begin 237 I := 0; 238 while (I < Types.Count) and (TType(Types[I]).Name <> AName) do 239 Inc(I); 240 if I < Types.Count then Result := Types[I] else Result := nil; 241 end; 242 202 243 end. 203 244 -
branches/Void/UOutputGenerator.pas
r11 r13 6 6 7 7 uses 8 Classes, SysUtils, UModel ;8 Classes, SysUtils, UModel, StrUtils; 9 9 10 10 type 11 12 { TOutputGenerator }13 14 11 TOutputGenerator = class 15 12 private 13 IndentCount: Integer; 16 14 Model: TModel; 15 function Indent: string; 17 16 public 18 17 Output: TStringList; … … 22 21 end; 23 22 24 { TPascalGenerator }25 26 23 TPascalGenerator = class(TOutputGenerator) 27 24 procedure Generate(Model: TModel); override; … … 29 26 end; 30 27 31 { TCGenerator }32 33 28 TCGenerator = class(TOutputGenerator) 29 private 30 function ConvertType(Name: string): string; 31 public 34 32 procedure Generate(Model: TModel); override; 35 33 procedure GenerateModule(Module: TModule); … … 39 37 40 38 { TOutputGenerator } 39 40 function TOutputGenerator.Indent: string; 41 begin 42 Result := DupeString(' ', IndentCount); 43 end; 41 44 42 45 procedure TOutputGenerator.Generate(Model: TModel); … … 78 81 // var section 79 82 if Variables.Count > 0 then Output.Add('var'); 83 Inc(IndentCount); 80 84 for I := 0 to Variables.Count - 1 do 81 85 with TVariable(Variables[I]) do 82 Output.Add(' ' + Name + ': ' + VarType + ';'); 86 Output.Add(Indent + Name + ': ' + VarType.Name + ';'); 87 Dec(IndentCount); 83 88 84 89 // Code block 85 90 Output.Add('begin'); 91 Inc(IndentCount); 86 92 for I := 0 to BeginEnd.Commands.Count - 1 do 87 93 with TCommand(BeginEnd.Commands[I]) do begin 88 if Name = 'Assignment' then Output.Add( Parameters[0] + ' := ' + Parameters[1] + ';')94 if Name = 'Assignment' then Output.Add(Indent + Parameters[0] + ' := ' + Parameters[1] + ';') 89 95 else begin 90 96 Row := Name; … … 95 101 Row := Row + '(' + Copy(ParameterText, 1, Length(ParameterText) - 2) + ')'; 96 102 end; 97 Output.Add( Row + ';');103 Output.Add(Indent + Row + ';'); 98 104 end; 99 105 end; 106 Dec(IndentCount); 100 107 Output.Add('end.'); 101 108 end; … … 104 111 { TCGenerator } 105 112 113 function TCGenerator.ConvertType(Name: string): string; 114 begin 115 if Name = 'String' then Result := 'char*' 116 else if Name = 'Byte' then Result := 'unsigned char' 117 else if Name = 'ShortInt' then Result := 'signed char' 118 else if Name = 'Word' then Result := 'unsigned int' 119 else if Name = 'Integer' then Result := 'int' 120 else if Name = 'Real' then Result := 'float' 121 else if Name = 'Double' then Result := 'double' 122 else if Name = 'Char' then Result := 'char' 123 else Result := Name; 124 end; 125 106 126 procedure TCGenerator.Generate(Model: TModel); 107 127 begin 108 128 inherited; 129 IndentCount := 0; 109 130 GenerateModule(Model.Module); 110 131 end; … … 122 143 Output.Add('int main()'); 123 144 Output.Add('{'); 145 Inc(IndentCount); 124 146 125 147 // variable section 126 148 for I := 0 to Variables.Count - 1 do 127 149 with TVariable(Variables[I]) do 128 Output.Add(' ' + VarType + ' ' + Name + ';'); 150 Output.Add(Indent + ' ' + ConvertType(VarType.Name) + ' ' + Name + ';'); 151 if Variables.Count > 0 then Output.Add(''); 129 152 130 153 // Code block 131 154 for I := 0 to BeginEnd.Commands.Count - 1 do 132 155 with TCommand(BeginEnd.Commands[I]) do begin 133 if Name = 'Assignment' then Output.Add( Parameters[0] + ' = ' + Parameters[1] + ';')156 if Name = 'Assignment' then Output.Add(Indent + Parameters[0] + ' = ' + Parameters[1] + ';') 134 157 else begin 135 158 if Name = 'WriteLn' then Row := 'printf' … … 143 166 Row := Row + Copy(ParameterText, 1, Length(ParameterText) - 2); 144 167 end; 145 Output.Add( Row + ');');168 Output.Add(Indent + Row + ');'); 146 169 end; 147 170 end; 171 Dec(IndentCount); 148 172 Output.Add('}'); 149 173 end; -
branches/Void/project1.lpi
r12 r13 9 9 <Icon Value="0"/> 10 10 <UseXPManifest Value="True"/> 11 <ActiveEditorIndexAtStart Value=" 1"/>11 <ActiveEditorIndexAtStart Value="4"/> 12 12 </General> 13 13 <VersionInfo> … … 33 33 </Item1> 34 34 </RequiredPackages> 35 <Units Count="1 2">35 <Units Count="14"> 36 36 <Unit0> 37 37 <Filename Value="project1.lpr"/> 38 38 <IsPartOfProject Value="True"/> 39 39 <UnitName Value="project1"/> 40 <CursorPos X=" 21" Y="16"/>40 <CursorPos X="37" Y="7"/> 41 41 <TopLine Value="1"/> 42 <UsageCount Value="39"/> 42 <EditorIndex Value="4"/> 43 <UsageCount Value="40"/> 44 <Loaded Value="True"/> 43 45 </Unit0> 44 46 <Unit1> … … 49 51 <UnitName Value="UMainForm"/> 50 52 <CursorPos X="46" Y="28"/> 51 <TopLine Value=" 52"/>53 <TopLine Value="28"/> 52 54 <EditorIndex Value="0"/> 53 <UsageCount Value=" 39"/>55 <UsageCount Value="40"/> 54 56 <Loaded Value="True"/> 55 57 </Unit1> … … 57 59 <Filename Value="UCompilator.pas"/> 58 60 <UnitName Value="UCompilator"/> 59 <CursorPos X=" 24" Y="80"/>60 <TopLine Value=" 64"/>61 <CursorPos X="33" Y="91"/> 62 <TopLine Value="82"/> 61 63 <EditorIndex Value="1"/> 62 <UsageCount Value=" 19"/>64 <UsageCount Value="20"/> 63 65 <Loaded Value="True"/> 64 66 </Unit2> … … 67 69 <IsPartOfProject Value="True"/> 68 70 <UnitName Value="UOutputGenerator"/> 69 <CursorPos X=" 12" Y="118"/>70 <TopLine Value="1 06"/>71 <CursorPos X="32" Y="151"/> 72 <TopLine Value="143"/> 71 73 <EditorIndex Value="3"/> 72 <UsageCount Value=" 39"/>74 <UsageCount Value="40"/> 73 75 <Loaded Value="True"/> 74 76 </Unit3> … … 76 78 <Filename Value="Example.void"/> 77 79 <IsPartOfProject Value="True"/> 78 <CursorPos X=" 1" Y="1"/>80 <CursorPos X="4" Y="1"/> 79 81 <TopLine Value="1"/> 80 <UsageCount Value=" 39"/>82 <UsageCount Value="40"/> 81 83 <SyntaxHighlighter Value="None"/> 82 84 </Unit4> … … 85 87 <IsPartOfProject Value="True"/> 86 88 <UnitName Value="UModel"/> 87 <CursorPos X=" 24" Y="48"/>88 <TopLine Value=" 25"/>89 <EditorIndex Value=" 4"/>90 <UsageCount Value=" 39"/>89 <CursorPos X="19" Y="18"/> 90 <TopLine Value="1"/> 91 <EditorIndex Value="6"/> 92 <UsageCount Value="40"/> 91 93 <Loaded Value="True"/> 92 94 </Unit5> … … 120 122 <IsPartOfProject Value="True"/> 121 123 <UnitName Value="UVoidParser"/> 122 <CursorPos X=" 9" Y="69"/>123 <TopLine Value=" 7"/>124 <CursorPos X="5" Y="51"/> 125 <TopLine Value="45"/> 124 126 <EditorIndex Value="2"/> 125 <UsageCount Value=" 39"/>127 <UsageCount Value="40"/> 126 128 <Loaded Value="True"/> 127 129 </Unit10> … … 132 134 <UsageCount Value="9"/> 133 135 </Unit11> 136 <Unit12> 137 <Filename Value="..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\inc\strings.pp"/> 138 <UnitName Value="strings"/> 139 <CursorPos X="1" Y="1"/> 140 <TopLine Value="16"/> 141 <UsageCount Value="10"/> 142 </Unit12> 143 <Unit13> 144 <Filename Value="..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\strutils.pp"/> 145 <UnitName Value="strutils"/> 146 <CursorPos X="1" Y="1"/> 147 <TopLine Value="43"/> 148 <EditorIndex Value="5"/> 149 <UsageCount Value="10"/> 150 <Loaded Value="True"/> 151 </Unit13> 134 152 </Units> 135 153 <JumpHistory Count="30" HistoryIndex="29"> 136 154 <Position1> 137 155 <Filename Value="UCompilator.pas"/> 138 <Caret Line=" 110" Column="1" TopLine="94"/>156 <Caret Line="86" Column="1" TopLine="70"/> 139 157 </Position1> 140 158 <Position2> 141 159 <Filename Value="UCompilator.pas"/> 142 <Caret Line=" 111" Column="1" TopLine="95"/>160 <Caret Line="87" Column="1" TopLine="71"/> 143 161 </Position2> 144 162 <Position3> 145 163 <Filename Value="UCompilator.pas"/> 146 <Caret Line=" 112" Column="1" TopLine="96"/>164 <Caret Line="89" Column="1" TopLine="73"/> 147 165 </Position3> 148 166 <Position4> 149 167 <Filename Value="UCompilator.pas"/> 150 <Caret Line=" 106" Column="24" TopLine="97"/>168 <Caret Line="90" Column="1" TopLine="74"/> 151 169 </Position4> 152 170 <Position5> 153 171 <Filename Value="UCompilator.pas"/> 154 <Caret Line=" 80" Column="1" TopLine="64"/>172 <Caret Line="91" Column="1" TopLine="75"/> 155 173 </Position5> 156 174 <Position6> 157 175 <Filename Value="UCompilator.pas"/> 158 <Caret Line=" 81" Column="1" TopLine="65"/>176 <Caret Line="92" Column="1" TopLine="76"/> 159 177 </Position6> 160 178 <Position7> 161 179 <Filename Value="UCompilator.pas"/> 162 <Caret Line=" 82" Column="1" TopLine="66"/>180 <Caret Line="94" Column="1" TopLine="78"/> 163 181 </Position7> 164 182 <Position8> 165 183 <Filename Value="UCompilator.pas"/> 166 <Caret Line=" 83" Column="1" TopLine="67"/>184 <Caret Line="95" Column="1" TopLine="79"/> 167 185 </Position8> 168 186 <Position9> 169 187 <Filename Value="UCompilator.pas"/> 170 <Caret Line=" 84" Column="1" TopLine="68"/>188 <Caret Line="96" Column="1" TopLine="80"/> 171 189 </Position9> 172 190 <Position10> 173 191 <Filename Value="UCompilator.pas"/> 174 <Caret Line=" 85" Column="1" TopLine="69"/>192 <Caret Line="98" Column="1" TopLine="82"/> 175 193 </Position10> 176 194 <Position11> 177 195 <Filename Value="UCompilator.pas"/> 178 <Caret Line=" 86" Column="1" TopLine="70"/>196 <Caret Line="100" Column="1" TopLine="84"/> 179 197 </Position11> 180 198 <Position12> 181 199 <Filename Value="UCompilator.pas"/> 182 <Caret Line=" 87" Column="1" TopLine="71"/>200 <Caret Line="101" Column="1" TopLine="85"/> 183 201 </Position12> 184 202 <Position13> 185 203 <Filename Value="UCompilator.pas"/> 186 <Caret Line=" 89" Column="1" TopLine="73"/>204 <Caret Line="103" Column="1" TopLine="87"/> 187 205 </Position13> 188 206 <Position14> 189 207 <Filename Value="UCompilator.pas"/> 190 <Caret Line=" 90" Column="1" TopLine="74"/>208 <Caret Line="107" Column="1" TopLine="91"/> 191 209 </Position14> 192 210 <Position15> 193 211 <Filename Value="UCompilator.pas"/> 194 <Caret Line=" 91" Column="1" TopLine="75"/>212 <Caret Line="108" Column="1" TopLine="92"/> 195 213 </Position15> 196 214 <Position16> 197 215 <Filename Value="UCompilator.pas"/> 198 <Caret Line=" 92" Column="1" TopLine="76"/>216 <Caret Line="110" Column="1" TopLine="94"/> 199 217 </Position16> 200 218 <Position17> 201 219 <Filename Value="UCompilator.pas"/> 202 <Caret Line=" 94" Column="1" TopLine="78"/>220 <Caret Line="111" Column="1" TopLine="95"/> 203 221 </Position17> 204 222 <Position18> 205 223 <Filename Value="UCompilator.pas"/> 206 <Caret Line=" 95" Column="1" TopLine="79"/>224 <Caret Line="112" Column="1" TopLine="96"/> 207 225 </Position18> 208 226 <Position19> 209 227 <Filename Value="UCompilator.pas"/> 210 <Caret Line=" 96" Column="1" TopLine="80"/>228 <Caret Line="113" Column="1" TopLine="97"/> 211 229 </Position19> 212 230 <Position20> 213 231 <Filename Value="UCompilator.pas"/> 214 <Caret Line=" 98" Column="1" TopLine="82"/>232 <Caret Line="114" Column="1" TopLine="98"/> 215 233 </Position20> 216 234 <Position21> 217 <Filename Value="U Compilator.pas"/>218 <Caret Line=" 100" Column="1" TopLine="84"/>235 <Filename Value="UModel.pas"/> 236 <Caret Line="54" Column="50" TopLine="40"/> 219 237 </Position21> 220 238 <Position22> 221 <Filename Value="U Compilator.pas"/>222 <Caret Line="1 01" Column="1" TopLine="85"/>239 <Filename Value="UOutputGenerator.pas"/> 240 <Caret Line="118" Column="12" TopLine="106"/> 223 241 </Position22> 224 242 <Position23> 225 <Filename Value="U Compilator.pas"/>226 <Caret Line=" 103" Column="1" TopLine="87"/>243 <Filename Value="UOutputGenerator.pas"/> 244 <Caret Line="82" Column="53" TopLine="61"/> 227 245 </Position23> 228 246 <Position24> 229 <Filename Value="U Compilator.pas"/>230 <Caret Line=" 107" Column="1" TopLine="91"/>247 <Filename Value="UOutputGenerator.pas"/> 248 <Caret Line="35" Column="48" TopLine="20"/> 231 249 </Position24> 232 250 <Position25> 233 <Filename Value="U Compilator.pas"/>234 <Caret Line="1 08" Column="1" TopLine="92"/>251 <Filename Value="UOutputGenerator.pas"/> 252 <Caret Line="122" Column="41" TopLine="109"/> 235 253 </Position25> 236 254 <Position26> 237 <Filename Value="U Compilator.pas"/>238 <Caret Line="1 10" Column="1" TopLine="94"/>255 <Filename Value="UOutputGenerator.pas"/> 256 <Caret Line="18" Column="20" TopLine="4"/> 239 257 </Position26> 240 258 <Position27> 241 <Filename Value="U Compilator.pas"/>242 <Caret Line=" 111" Column="1" TopLine="95"/>259 <Filename Value="UOutputGenerator.pas"/> 260 <Caret Line="45" Column="42" TopLine="43"/> 243 261 </Position27> 244 262 <Position28> 245 <Filename Value="U Compilator.pas"/>246 <Caret Line=" 112" Column="1" TopLine="96"/>263 <Filename Value="UOutputGenerator.pas"/> 264 <Caret Line="42" Column="28" TopLine="40"/> 247 265 </Position28> 248 266 <Position29> 249 <Filename Value="U Compilator.pas"/>250 <Caret Line=" 113" Column="1" TopLine="97"/>267 <Filename Value="UOutputGenerator.pas"/> 268 <Caret Line="8" Column="34" TopLine="1"/> 251 269 </Position29> 252 270 <Position30> 253 <Filename Value=" UCompilator.pas"/>254 <Caret Line="1 14" Column="1" TopLine="98"/>271 <Filename Value="project1.lpr"/> 272 <Caret Line="16" Column="21" TopLine="1"/> 255 273 </Position30> 256 274 </JumpHistory> … … 278 296 </CompilerOptions> 279 297 <Debugging> 280 <BreakPoints Count="1">281 <Item1>282 <Source Value="UCompilator.pas"/>283 <Line Value="84"/>284 </Item1>285 </BreakPoints>286 298 <Exceptions Count="3"> 287 299 <Item1>
Note:
See TracChangeset
for help on using the changeset viewer.