Changeset 141 for branches/easy compiler/UCompiler.pas
- Timestamp:
- Jan 16, 2018, 2:44:19 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/easy compiler/UCompiler.pas
r140 r141 9 9 10 10 type 11 TSourceTokenKind = (stNone, stString, stIdentifier, stEof );11 TSourceTokenKind = (stNone, stString, stIdentifier, stEof, stInteger); 12 12 TSourceToken = record 13 13 Text: string; … … 102 102 procedure TTokenizer.Tokenize(Text: string; Tokens: TSourceTokens); 103 103 type 104 TTokenizerState = (tsNone, tsIdentifier, tsString );104 TTokenizerState = (tsNone, tsIdentifier, tsString, tsInteger); 105 105 var 106 106 State: TTokenizerState; … … 114 114 while I < Length(Text) do begin 115 115 if State = tsNone then begin 116 if IsDigit(Text[I]) then begin 117 State := tsInteger; 118 Token := Text[I]; 119 end else 116 120 if IsAlpha(Text[I]) then begin 117 121 State := tsIdentifier; … … 133 137 if IsWhiteSpace(Text[I]) then begin 134 138 Tokens.AddToken(stIdentifier, Token); 139 State := tsNone; 140 Dec(I); 141 end else 142 raise Exception.Create('Unexpected character'); 143 end else 144 if State = tsInteger then begin 145 if IsDigit(Text[I]) then begin 146 Token := Token + Text[I]; 147 end else 148 if IsWhiteSpace(Text[I]) then begin 149 Tokens.AddToken(stInteger, Token); 135 150 State := tsNone; 136 151 Dec(I); … … 156 171 var 157 172 Token: TSourceToken; 173 Token2: TSourceToken; 158 174 Keyword: string; 159 175 Instruction: TSourceInstruction; … … 161 177 Funct: TSourceFunction; 162 178 Param: TSourceFunctionParameter; 179 Variable: TSourceVariable; 180 ValueType: TSourceType; 163 181 164 182 function ParseReference: TSourceReference; … … 168 186 NewReference := TSourceReferenceConstant.Create; 169 187 TSourceReferenceConstant(NewReference).Constant := 170 Code.Constants.AddNew (Token.Text);188 Code.Constants.AddNewString(Token.Text); 171 189 end else 172 190 if Token.Kind = stIdentifier then begin … … 176 194 if TSourceReferenceVariable(NewReference).Variable = nil then 177 195 raise Exception.Create('Variable not found: ' + Token.Text); 178 end else raise Exception.Create('Unexpected parameter'); 196 end else 197 if Token.Kind = stInteger then begin 198 NewReference := TSourceReferenceConstant.Create; 199 TSourceReferenceConstant(NewReference).Constant := 200 Code.Constants.AddNewInteger(StrToInt(Token.Text)); 201 end else 202 raise Exception.Create('Unexpected parameter'); 179 203 Result := NewReference; 180 204 end; … … 187 211 TSourceReferenceVariable(NewReference).Variable := 188 212 Code.Variables.Search(Token.Text); 189 if TSourceReferenceVariable(NewReference).Variable = nil then begin 190 if Initialize then 191 TSourceReferenceVariable(NewReference).Variable := Code.Variables.AddNew(Token.Text) 192 else raise Exception.Create('Variable not found: ' + Token.Text); 193 end; 213 if TSourceReferenceVariable(NewReference).Variable = nil then 214 raise Exception.Create('Variable not found: ' + Token.Text); 194 215 195 216 end else raise Exception.Create('Unexpected parameter'); … … 203 224 if Token.Kind = stIdentifier then begin 204 225 Keyword := LowerCase(Token.Text); 205 Funct := Code.Functions.Search(Keyword); 206 if Assigned(Funct) then begin 207 Instruction := TSourceInstructionFunction.Create; 208 TSourceInstructionFunction(Instruction).Name := Keyword; 209 for Param in Funct.Parameters do 210 if Param.Kind = pkString then begin 211 TSourceInstructionFunction(Instruction).AddParameter(ParseReference) 212 end else 213 if Param.Kind = pkVariable then begin 214 TSourceInstructionFunction(Instruction).AddParameter(ParseReferenceVariable(True)); 215 end else 216 raise Exception.Create('Unsupported parameter type.'); 217 Code.Instructions.Add(Instruction); 218 end else raise Exception.Create('Unsupported keyword: ' + Token.Text); 226 if Keyword = 'var' then begin 227 Token := Tokenizer.GetNext; 228 Token2 := Tokenizer.GetNext; 229 if Token2.Kind <> stIdentifier then 230 raise Exception.Create('Expected type parameter'); 231 Variable := Code.Variables.Search(Token.Text); 232 if not Assigned(Variable) then begin 233 ValueType := Code.Types.Search(Token2.Text); 234 if not Assigned(ValueType) then 235 raise Exception.Create('Unsupported type: ' + Token2.Text); 236 Variable := Code.Variables.AddNew(Token.Text, 237 ValueType); 238 end else raise Exception.Create('Variable redefined'); 239 end else begin 240 Funct := Code.Functions.Search(Keyword); 241 if Assigned(Funct) then begin 242 Instruction := TSourceInstructionFunction.Create; 243 TSourceInstructionFunction(Instruction).Name := Keyword; 244 for Param in Funct.Parameters do 245 if Param.Kind = pkString then begin 246 TSourceInstructionFunction(Instruction).AddParameter(ParseReference) 247 end else 248 if Param.Kind = pkVariable then begin 249 TSourceInstructionFunction(Instruction).AddParameter(ParseReferenceVariable(True)); 250 end else 251 raise Exception.Create('Unsupported parameter type.'); 252 Code.Instructions.Add(Instruction); 253 end else raise Exception.Create('Unsupported keyword: ' + Token.Text); 254 end; 219 255 end else raise Exception.Create('Unsupported token kind.'); 220 256 end;
Note:
See TracChangeset
for help on using the changeset viewer.