Changeset 10
- Timestamp:
- Nov 9, 2009, 3:02:26 PM (15 years ago)
- Location:
- branches/Void
- Files:
-
- 1 added
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Void/Example.void
r9 r10 1 1 Define Text 2 WriteLn Leave me 3 WriteLn Heal the world4 Assign Text Hell 2 Text := Hell 3 WriteLn('Leave me') 4 WriteLn('Heal the world') 5 5 ReadLn -
branches/Void/UCompilator.pas
r9 r10 6 6 7 7 uses 8 Classes, SysUtils, UOutputGenerator, UModel ;8 Classes, SysUtils, UOutputGenerator, UModel, UVoidParser; 9 9 10 10 type … … 16 16 private 17 17 FOnError: TOnErrorEvent; 18 ParsePosition: TPoint;19 18 procedure DoError(Text: string); 20 19 public 21 20 Model: TModel; 22 SourceCode: TStr ingList;21 SourceCode: TStream; 23 22 Generator: TOutputGenerator; 23 Parser: TVoidParser; 24 24 procedure Compile; 25 procedure Process Line(Line: string);25 procedure Process; 26 26 constructor Create; 27 27 destructor Destroy; override; 28 function Parse(var Text: string; Separator: string = ' '): string;29 28 property OnError: TOnErrorEvent read FOnError write FOnError; 30 29 end; … … 39 38 begin 40 39 Terminate := False; 41 if Assigned(FOnError) then FOnError(Text, Terminate, Parse Position);40 if Assigned(FOnError) then FOnError(Text, Terminate, Parser.TokenStartPosition); 42 41 if Terminate then raise Exception.Create('Compilation terminated'); 43 42 end; … … 48 47 begin 49 48 Model.Clear; 49 SourceCode.Position := 0; 50 Parser.Open(SourceCode); 50 51 51 52 // Process source lines 52 for I := 0 to SourceCode.Count - 1 do begin53 P arsePosition.Y := I;54 P rocessLine(SourceCode[I]);53 while SourceCode.Position < SourceCode.Size do begin; 54 Process; 55 Parser.ParseNextToken; 55 56 end; 56 57 … … 58 59 end; 59 60 60 procedure TCompilator.Process Line(Line: string);61 procedure TCompilator.Process; 61 62 var 62 63 CommandName: string; … … 64 65 Variable: TVariable; 65 66 VariableName: string; 67 Value: string; 66 68 begin 67 69 with Model, BeginEnd do begin 68 CommandName := Parse(Line); 69 if CommandName = 'Define' then begin 70 VariableName := Parse(Line); 71 Variable := FindVariableByName(VariableName); 72 if Assigned(Variable) then DoError('Variable ' + VariableName + ' redefined') 73 else begin 74 Variable := TVariable.Create; 75 with Variable do begin 76 Name := VariableName; 77 VarType := 'string'; 70 if Parser.TokenType = ttIdentifier then begin 71 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); 78 87 end; 79 Variables.Add(Variable); 88 end else begin 89 Command := FindProcedureByName(CommandName); 90 if Assigned(Command) then begin 91 with TCommand(Commands[Commands.Add(TCommand.Create)]) do begin 92 Name := CommandName; 93 Parser.ParseNextToken; 94 if (Parser.TokenType = ttSymbol) and (Parser.TokenValue = '(') then begin 95 Parser.ParseNextToken; 96 if (Parser.TokenType <> ttString) then DoError('Expected string'); 97 Parameters.Add(Parser.TokenValue); 98 Parser.ParseNextToken; 99 if (Parser.TokenType <> ttSymbol) and (Parser.TokenValue = ')') then 100 DoError('Expected )'); 101 end; 102 end; 103 end else begin 104 Variable := FindVariableByName(CommandName); 105 if Assigned(Variable) then begin 106 Parser.ParseNextToken; 107 if Parser.TokenType <> ttWhiteSpace then DoError('Expected white space'); 108 Parser.ParseNextToken; 109 if (Parser.TokenType <> ttSymbol) and (Parser.TokenValue = ':=') then 110 DoError('Expected :='); 111 Parser.ParseNextToken; 112 if Parser.TokenType <> ttWhiteSpace then DoError('Expected white space'); 113 Parser.ParseNextToken; 114 Value := Parser.TokenValue; 115 if Parser.TokenType = ttIdentifier then begin 116 Variable := FindVariableByName(CommandName); 117 if Assigned(Variable) then 118 with TCommand(Commands[Commands.Add(TCommand.Create)]) do begin 119 Name := 'Assignment'; 120 Parameters.Add(CommandName); 121 Parameters.Add(Value); 122 end else DoError('Undefined variable ' + CommandName); 123 end else if Parser.TokenType = ttString then begin 124 with TCommand(Commands[Commands.Add(TCommand.Create)]) do begin 125 Name := 'Assignment'; 126 Parameters.Add(CommandName); 127 Parameters.Add(Value); 128 end; 129 end else DoError('Expected variable or string') 130 end else DoError('Unknown command ' + CommandName); 131 end; 80 132 end; 81 end else if CommandName = 'Assign' then with TCommand(Commands[Commands.Add(TCommand.Create)]) do begin 82 VariableName := Parse(Line); 83 Variable := FindVariableByName(VariableName); 84 if not Assigned(Variable) then DoError('Undefined variable ' + VariableName) 85 else begin 86 Name := 'Assignment'; 87 Parameters.Add(VariableName); 88 Parameters.Add(Parse(Line)); 89 end; 90 end else begin 91 Command := FindProcedureByName(CommandName); 92 if Assigned(Command) then begin 93 with TCommand(Commands[Commands.Add(TCommand.Create)]) do begin 94 Name := CommandName; 95 96 end; 97 end else DoError('Unknown command ' + CommandName); 98 end; 133 end else DoError('Expected identifier'); 134 Parser.ParseNextToken; 135 if Parser.TokenType <> ttWhiteSpace then DoError('Expected white space'); 99 136 end; 100 137 end; … … 102 139 constructor TCompilator.Create; 103 140 begin 104 SourceCode := T StringList.Create;141 SourceCode := TMemoryStream.Create; 105 142 Model := TModel.Create; 143 Parser := TVoidParser.Create; 106 144 end; 107 145 108 146 destructor TCompilator.Destroy; 109 var110 I: Integer;111 147 begin 112 148 SourceCode.Destroy; 113 149 Model.Destroy; 150 Parser.Destroy; 114 151 inherited Destroy; 115 end;116 117 function TCompilator.Parse(var Text: string; Separator: string): string;118 begin119 Text := Trim(Text);120 if Pos(Separator, Text) > 0 then begin121 Result := Copy(Text, 1, Pos(Separator, Text) - 1);122 Delete(Text, 1, Length(Result));123 end else begin124 Result := Text;125 Text := '';126 end;127 Text := Trim(Text);128 152 end; 129 153 -
branches/Void/UMainForm.pas
r9 r10 31 31 procedure FormShow(Sender: TObject); 32 32 private 33 SourceCode: TMemoryStream; 33 34 procedure CompilatorError(Text: string; var Terminate: Boolean; 34 35 Position: TPoint); … … 67 68 68 69 procedure TMainForm.ButtonCompileClick(Sender: TObject); 70 var 71 Code: string; 69 72 begin 70 73 with Compilator do begin 71 74 Memo3.Lines.Clear; 72 SourceCode.Assign(Memo1.Lines); 73 75 Code := Memo1.Lines.Text; 76 SourceCode.Size := 0; 77 SourceCode.Write(Code[1], Length(Code)); 74 78 if Assigned(Generator) then Generator.Destroy; 75 79 if ComboBox1.ItemIndex = 0 then … … 78 82 Compile; 79 83 Memo2.Lines.Assign(Generator.Output); 80 end;84 end; 81 85 end; 82 86 -
branches/Void/UOutputGenerator.pas
r9 r10 85 85 ParameterText := ''; 86 86 for P := 0 to Parameters.Count - 1 do 87 ParameterText := ParameterText + Parameters[P] +', ';87 ParameterText := ParameterText + '''' + Parameters[P] + ''', '; 88 88 Row := Row + '(' + Copy(ParameterText, 1, Length(ParameterText) - 2) + ')'; 89 89 end; … … 129 129 ParameterText := ''; 130 130 for P := 0 to Parameters.Count - 1 do 131 ParameterText := ParameterText + Parameters[P] +', ';131 ParameterText := ParameterText + '''' + Parameters[P] + ''', '; 132 132 Row := Row + Copy(ParameterText, 1, Length(ParameterText) - 2); 133 133 end; -
branches/Void/project1.lpi
r9 r10 33 33 </Item1> 34 34 </RequiredPackages> 35 <Units Count=" 6">35 <Units Count="12"> 36 36 <Unit0> 37 37 <Filename Value="project1.lpr"/> 38 38 <IsPartOfProject Value="True"/> 39 39 <UnitName Value="project1"/> 40 <UsageCount Value="20"/> 40 <CursorPos X="26" Y="17"/> 41 <TopLine Value="1"/> 42 <EditorIndex Value="2"/> 43 <UsageCount Value="29"/> 44 <Loaded Value="True"/> 41 45 </Unit0> 42 46 <Unit1> … … 46 50 <ResourceBaseClass Value="Form"/> 47 51 <UnitName Value="UMainForm"/> 48 <CursorPos X=" 20" Y="71"/>49 <TopLine Value=" 58"/>52 <CursorPos X="44" Y="91"/> 53 <TopLine Value="68"/> 50 54 <EditorIndex Value="0"/> 51 <UsageCount Value="2 0"/>55 <UsageCount Value="29"/> 52 56 <Loaded Value="True"/> 53 57 </Unit1> … … 55 59 <Filename Value="UCompilator.pas"/> 56 60 <UnitName Value="UCompilator"/> 57 <CursorPos X=" 20" Y="39"/>58 <TopLine Value=" 35"/>61 <CursorPos X="53" Y="116"/> 62 <TopLine Value="99"/> 59 63 <EditorIndex Value="1"/> 60 <UsageCount Value="1 0"/>64 <UsageCount Value="15"/> 61 65 <Loaded Value="True"/> 62 66 </Unit2> … … 65 69 <IsPartOfProject Value="True"/> 66 70 <UnitName Value="UOutputGenerator"/> 67 <CursorPos X=" 15" Y="118"/>68 <TopLine Value=" 110"/>69 <EditorIndex Value=" 2"/>70 <UsageCount Value="2 0"/>71 <CursorPos X="72" Y="87"/> 72 <TopLine Value="71"/> 73 <EditorIndex Value="4"/> 74 <UsageCount Value="29"/> 71 75 <Loaded Value="True"/> 72 76 </Unit3> 73 77 <Unit4> 74 78 <Filename Value="Example.void"/> 75 <CursorPos X="1" Y="1"/> 79 <IsPartOfProject Value="True"/> 80 <CursorPos X="15" Y="4"/> 76 81 <TopLine Value="1"/> 77 <EditorIndex Value="4"/> 78 <UsageCount Value="20"/> 79 <Loaded Value="True"/> 82 <UsageCount Value="29"/> 80 83 <SyntaxHighlighter Value="None"/> 81 84 </Unit4> … … 84 87 <IsPartOfProject Value="True"/> 85 88 <UnitName Value="UModel"/> 86 <CursorPos X=" 1" Y="110"/>89 <CursorPos X="25" Y="87"/> 87 90 <TopLine Value="83"/> 91 <EditorIndex Value="5"/> 92 <UsageCount Value="29"/> 93 <Loaded Value="True"/> 94 </Unit5> 95 <Unit6> 96 <Filename Value="UParser.pas"/> 97 <UnitName Value="UParser"/> 98 <CursorPos X="7" Y="11"/> 99 <TopLine Value="1"/> 100 <UsageCount Value="19"/> 101 </Unit6> 102 <Unit7> 103 <Filename Value="..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\classes\classesh.inc"/> 104 <CursorPos X="15" Y="743"/> 105 <TopLine Value="728"/> 106 <UsageCount Value="14"/> 107 </Unit7> 108 <Unit8> 109 <Filename Value="..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\classes\parser.inc"/> 110 <CursorPos X="16" Y="324"/> 111 <TopLine Value="322"/> 112 <UsageCount Value="14"/> 113 </Unit8> 114 <Unit9> 115 <Filename Value="..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\inc\heaph.inc"/> 116 <CursorPos X="10" Y="89"/> 117 <TopLine Value="70"/> 118 <UsageCount Value="9"/> 119 </Unit9> 120 <Unit10> 121 <Filename Value="UVoidParser.pas"/> 122 <IsPartOfProject Value="True"/> 123 <UnitName Value="UVoidParser"/> 124 <CursorPos X="44" Y="54"/> 125 <TopLine Value="40"/> 88 126 <EditorIndex Value="3"/> 89 <UsageCount Value="20"/> 90 <Loaded Value="True"/> 91 </Unit5> 127 <UsageCount Value="29"/> 128 <Loaded Value="True"/> 129 </Unit10> 130 <Unit11> 131 <Filename Value="..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\classes\streams.inc"/> 132 <CursorPos X="5" Y="370"/> 133 <TopLine Value="365"/> 134 <UsageCount Value="10"/> 135 </Unit11> 92 136 </Units> 93 137 <JumpHistory Count="30" HistoryIndex="29"> 94 138 <Position1> 95 <Filename Value="U OutputGenerator.pas"/>96 <Caret Line=" 97" Column="1" TopLine="84"/>139 <Filename Value="UCompilator.pas"/> 140 <Caret Line="107" Column="1" TopLine="92"/> 97 141 </Position1> 98 142 <Position2> 99 143 <Filename Value="UCompilator.pas"/> 100 <Caret Line=" 53" Column="17" TopLine="52"/>144 <Caret Line="108" Column="1" TopLine="93"/> 101 145 </Position2> 102 146 <Position3> 103 147 <Filename Value="UCompilator.pas"/> 104 <Caret Line=" 64" Column="23" TopLine="52"/>148 <Caret Line="109" Column="1" TopLine="94"/> 105 149 </Position3> 106 150 <Position4> 107 151 <Filename Value="UCompilator.pas"/> 108 <Caret Line=" 69" Column="1" TopLine="53"/>152 <Caret Line="111" Column="1" TopLine="96"/> 109 153 </Position4> 110 154 <Position5> 111 155 <Filename Value="UCompilator.pas"/> 112 <Caret Line=" 87" Column="41" TopLine="1"/>156 <Caret Line="112" Column="1" TopLine="97"/> 113 157 </Position5> 114 158 <Position6> 115 <Filename Value="U Model.pas"/>116 <Caret Line=" 30" Column="1" TopLine="2"/>159 <Filename Value="UCompilator.pas"/> 160 <Caret Line="113" Column="1" TopLine="98"/> 117 161 </Position6> 118 162 <Position7> 119 <Filename Value="U Model.pas"/>120 <Caret Line="1 38" Column="7" TopLine="118"/>163 <Filename Value="UCompilator.pas"/> 164 <Caret Line="114" Column="1" TopLine="99"/> 121 165 </Position7> 122 166 <Position8> 123 <Filename Value="U Model.pas"/>124 <Caret Line=" 28" Column="24" TopLine="13"/>167 <Filename Value="UCompilator.pas"/> 168 <Caret Line="115" Column="1" TopLine="100"/> 125 169 </Position8> 126 170 <Position9> 127 <Filename Value="U Model.pas"/>128 <Caret Line=" 29" Column="1" TopLine="13"/>171 <Filename Value="UCompilator.pas"/> 172 <Caret Line="116" Column="1" TopLine="101"/> 129 173 </Position9> 130 174 <Position10> 131 <Filename Value="U Model.pas"/>132 <Caret Line=" 34" Column="5" TopLine="19"/>175 <Filename Value="UCompilator.pas"/> 176 <Caret Line="117" Column="1" TopLine="102"/> 133 177 </Position10> 134 178 <Position11> 135 <Filename Value="U Model.pas"/>136 <Caret Line=" 70" Column="13" TopLine="54"/>179 <Filename Value="UCompilator.pas"/> 180 <Caret Line="116" Column="33" TopLine="103"/> 137 181 </Position11> 138 182 <Position12> 139 183 <Filename Value="UCompilator.pas"/> 140 <Caret Line=" 20" Column="1" TopLine="13"/>184 <Caret Line="114" Column="23" TopLine="99"/> 141 185 </Position12> 142 186 <Position13> 143 187 <Filename Value="UCompilator.pas"/> 144 <Caret Line=" 66" Column="20" TopLine="46"/>188 <Caret Line="113" Column="1" TopLine="98"/> 145 189 </Position13> 146 190 <Position14> 147 <Filename Value="U Compilator.pas"/>148 <Caret Line="49" Column="1 8" TopLine="34"/>191 <Filename Value="UVoidParser.pas"/> 192 <Caret Line="49" Column="1" TopLine="34"/> 149 193 </Position14> 150 194 <Position15> 151 <Filename Value="U Compilator.pas"/>152 <Caret Line=" 67" Column="64" TopLine="47"/>195 <Filename Value="UVoidParser.pas"/> 196 <Caret Line="51" Column="1" TopLine="36"/> 153 197 </Position15> 154 198 <Position16> 155 <Filename Value="U OutputGenerator.pas"/>156 <Caret Line=" 19" Column="19" TopLine="1"/>199 <Filename Value="UVoidParser.pas"/> 200 <Caret Line="52" Column="1" TopLine="37"/> 157 201 </Position16> 158 202 <Position17> 159 <Filename Value="U OutputGenerator.pas"/>160 <Caret Line=" 28" Column="3" TopLine="6"/>203 <Filename Value="UVoidParser.pas"/> 204 <Caret Line="54" Column="1" TopLine="39"/> 161 205 </Position17> 162 206 <Position18> 163 <Filename Value="U OutputGenerator.pas"/>164 <Caret Line=" 35" Column="18" TopLine="13"/>207 <Filename Value="UVoidParser.pas"/> 208 <Caret Line="55" Column="1" TopLine="40"/> 165 209 </Position18> 166 210 <Position19> 167 <Filename Value="U OutputGenerator.pas"/>168 <Caret Line=" 176" Column="1" TopLine="146"/>211 <Filename Value="UVoidParser.pas"/> 212 <Caret Line="56" Column="1" TopLine="41"/> 169 213 </Position19> 170 214 <Position20> 171 <Filename Value="U Compilator.pas"/>172 <Caret Line=" 43" Column="1" TopLine="43"/>215 <Filename Value="UVoidParser.pas"/> 216 <Caret Line="58" Column="1" TopLine="43"/> 173 217 </Position20> 174 218 <Position21> 175 <Filename Value="U Compilator.pas"/>176 <Caret Line=" 20" Column="1" TopLine="5"/>219 <Filename Value="UVoidParser.pas"/> 220 <Caret Line="60" Column="1" TopLine="45"/> 177 221 </Position21> 178 222 <Position22> 179 <Filename Value="U Compilator.pas"/>180 <Caret Line=" 51" Column="3" TopLine="35"/>223 <Filename Value="UVoidParser.pas"/> 224 <Caret Line="61" Column="1" TopLine="46"/> 181 225 </Position22> 182 226 <Position23> 183 <Filename Value="U Model.pas"/>184 <Caret Line=" 47" Column="27" TopLine="21"/>227 <Filename Value="UVoidParser.pas"/> 228 <Caret Line="64" Column="1" TopLine="49"/> 185 229 </Position23> 186 230 <Position24> 187 <Filename Value="U Model.pas"/>188 <Caret Line=" 93" Column="14" TopLine="88"/>231 <Filename Value="UVoidParser.pas"/> 232 <Caret Line="65" Column="1" TopLine="50"/> 189 233 </Position24> 190 234 <Position25> 191 <Filename Value="U Model.pas"/>192 <Caret Line=" 88" Column="53" TopLine="78"/>235 <Filename Value="UVoidParser.pas"/> 236 <Caret Line="165" Column="1" TopLine="142"/> 193 237 </Position25> 194 238 <Position26> 195 <Filename Value="U Model.pas"/>196 <Caret Line=" 49" Column="5" TopLine="28"/>239 <Filename Value="UCompilator.pas"/> 240 <Caret Line="113" Column="1" TopLine="98"/> 197 241 </Position26> 198 242 <Position27> 199 <Filename Value="U OutputGenerator.pas"/>200 <Caret Line=" 19" Column="1" TopLine="13"/>243 <Filename Value="UVoidParser.pas"/> 244 <Caret Line="56" Column="26" TopLine="40"/> 201 245 </Position27> 202 246 <Position28> 203 <Filename Value="U OutputGenerator.pas"/>204 <Caret Line=" 44" Column="1" TopLine="29"/>247 <Filename Value="UVoidParser.pas"/> 248 <Caret Line="54" Column="44" TopLine="40"/> 205 249 </Position28> 206 250 <Position29> 207 <Filename Value=" UOutputGenerator.pas"/>208 <Caret Line=" 97" Column="1" TopLine="83"/>251 <Filename Value="project1.lpr"/> 252 <Caret Line="19" Column="1" TopLine="1"/> 209 253 </Position29> 210 254 <Position30> 211 <Filename Value=" UCompilator.pas"/>212 <Caret Line=" 87" Column="41" TopLine="79"/>255 <Filename Value="project1.lpr"/> 256 <Caret Line="20" Column="36" TopLine="1"/> 213 257 </Position30> 214 258 </JumpHistory> -
branches/Void/project1.lpr
r7 r10 8 8 {$ENDIF}{$ENDIF} 9 9 Interfaces, // this includes the LCL widgetset 10 Forms, UMainForm, UOutputGenerator, LResources, UModel 11 { you can add units after this }; 10 Forms, UMainForm, UOutputGenerator, LResources, UModel, UVoidParser; 12 11 13 12 {$IFDEF WINDOWS}{$R project1.rc}{$ENDIF}
Note:
See TracChangeset
for help on using the changeset viewer.