Changeset 102 for branches/interpreter
- Timestamp:
- Feb 11, 2017, 6:55:42 PM (8 years ago)
- Location:
- branches/interpreter
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/interpreter/Execute3.pas
r101 r102 52 52 implementation 53 53 54 uses 55 Parser3; 56 54 57 procedure ExecuteCommand(Command: PCommand); forward; 55 58 procedure ExecuteGetValue(GetValue: PGetValue; Value: PVariableValue); forward; … … 74 77 procedure ShowErrorType(Variable: PVariableValue); 75 78 begin 76 ShowError('Not suppo ted type')79 ShowError('Not supported type') 77 80 end; 78 81 … … 81 84 case Result^.BaseType of 82 85 btBoolean: ShowErrorType(Result); 83 btChar: ShowErrorType(Result); 86 btChar: begin 87 Result.BaseType := btShortString; 88 Result.ValueString := Result.ValueString + Operand.ValueChar; 89 end; 84 90 btInteger: Result^.ValueInteger := Result^.ValueInteger + Operand^.ValueInteger; 85 btShortString: Result^.ValueString := Result^.ValueString + Operand^.ValueString; 91 btShortString: begin 92 if Operand^.BaseType = btShortString then 93 Result^.ValueString := Result^.ValueString + Operand^.ValueString 94 else if Operand^.BaseType = btChar then 95 Result^.ValueString := Result^.ValueString + Operand^.ValueChar 96 else ShowErrorType(Result); 97 end; 98 end; 99 end; 100 101 procedure VariableNot(Result, Operand: PVariableValue); 102 begin 103 case Result^.BaseType of 104 btBoolean: Result^.ValueBoolean := not Operand^.ValueBoolean; 105 btChar: ShowErrorType(Result); 106 btInteger: ShowErrorType(Result); 107 btShortString: ShowErrorType(Result); 86 108 end; 87 109 end; … … 141 163 I: Integer; 142 164 SubValue: TVariableValue; 165 Operand1: TVariableValue; 143 166 begin 144 167 if Expression^.NodeType = ntOperator then begin … … 155 178 // Just assign first operand 156 179 case Expression^.OperatorType of 180 opNot: begin 181 Value.BaseType := btBoolean; 182 VariableNot(Value, @SubValue); 183 end; 157 184 opAdd: AssignVariable(Value, @SubValue); 158 185 opSubtract: AssignVariable(Value, @SubValue); 159 186 opAnd: AssignVariable(Value, @SubValue); 160 187 opOr: AssignVariable(Value, @SubValue); 161 opEqual: AssignVariable( Value, @SubValue);162 opNotEqual: AssignVariable( Value, @SubValue);188 opEqual: AssignVariable(@Operand1, @SubValue); 189 opNotEqual: AssignVariable(@Operand1, @SubValue); 163 190 else ShowError('Unsupported operator type'); 164 191 end; … … 169 196 opAnd: VariableAnd(Value, @SubValue); 170 197 opOr: VariableOr(Value, @SubValue); 171 opEqual: VariableEqual(Value, Value, @SubValue); 172 opNotEqual: VariableNotEqual(Value, Value, @SubValue); 198 opEqual: begin 199 Value.BaseType := btBoolean; 200 VariableEqual(Value, @Operand1, @SubValue); 201 end; 202 opNotEqual: begin 203 Value.BaseType := btBoolean; 204 VariableNotEqual(Value, @Operand1, @SubValue); 205 end 173 206 else ShowError('Unsupported operator type'); 174 207 end; … … 262 295 begin 263 296 if Execution^.Func^.Name = 'WriteLn' then begin 264 WriteLn( ExecutionContextCurrent^.VariableValues.GetByName('Text')^.ValueString)297 WriteLn('|' + ExecutionContextCurrent^.VariableValues.GetByName('Text')^.ValueString) 265 298 end else 266 299 if Execution^.Func^.Name = 'Halt' then begin 267 300 Halt; 268 301 end else 302 if Execution^.Func^.Name = 'Read' then begin 303 ExecutionContextCurrent^.VariableValues.GetByName('Output')^.ValueChar := InnerText[InnerTextPos]; 304 InnerTextPos := InnerTextPos + 1; 305 end else 269 306 if Execution^.Func^.Name = 'Eof' then begin 270 307 ExecuteBuildInSetResult(Execution, 'Boolean'); 271 ExecutionContextCurrent^.VariableValues.GetByName('Result')^.ValueBoolean := True;308 ExecutionContextCurrent^.VariableValues.GetByName('Result')^.ValueBoolean := InnerTextPos > Length(InnerText); 272 309 end else ShowError('Unsupported build-in function.'); 273 310 end; … … 297 334 ExecutionContexts.Items[Length(ExecutionContexts.Items) - 1] := NewContext; 298 335 299 //WriteLn('Executed ' + Execution^.Func^.Name);336 WriteLn('Executed ' + Execution^.Func^.Name); 300 337 if IsBuildInFunction(Execution^.Func^.Name) then ExecuteBuildIn(Execution) 301 338 else ExecuteBeginEnd(@Execution^.Func^.BeginEnd); 302 339 if (ReturnValue <> nil) and (Execution^.Func^.ReturnType <> nil) then 303 340 AssignVariable(ReturnValue, ExecutionContextCurrent^.VariableValues.GetByName('Result')); 341 342 WriteLn('Return from ' + Execution^.Func^.Name); 343 344 // Copy output parameters back to variable 345 NewContext := ExecutionContexts.Items[Length(ExecutionContexts.Items) - 1]; 304 346 ExecutionContexts.RemoveLast; 347 348 for I := 0 to Length(Execution^.Func^.Parameters.Items) - 1 do 349 if Execution^.Func^.Parameters.Items[I].Output then begin 350 DestVar := NewContext.VariableValues.GetByName( 351 Execution^.Func^.Parameters.Items[I].Name); 352 Param := @Execution^.Parameters.Items[I]; 353 if (Param.ReadType = rtVariable) then 354 AssignVariable(ExecutionContextCurrent^.VariableValues.GetByName(Param.Variable.Name), DestVar) 355 else ShowError('Function var parameter can be only variable'); 356 end; 305 357 end; 306 358 … … 311 363 begin 312 364 DestVariable := ExecutionContextCurrent^.VariableValues.GetByName(Assignment^.Destination^.Name); 365 WriteLn('Assignment to ' + Assignment^.Destination^.Name); 313 366 FillChar(SrcVariable, SizeOf(TVariableValue), 0); 314 367 ExecuteGetValue(@Assignment^.Source, @SrcVariable); -
branches/interpreter/Parser3.pas
r101 r102 10 10 procedure ParseProgram(ProgramCode: PProgramCode); 11 11 12 13 var 14 InnerText: string; 15 InnerTextPos: Integer; 12 16 13 17 implementation … … 61 65 procedure ReadInputAll; 62 66 var 67 LastC: Char; 63 68 C: Char; 64 begin 69 Inner: Boolean; 70 begin 71 LastC := #0; 65 72 InputTextPos := 1; 66 73 InputText := ''; 74 InnerTextPos := 1; 75 InnerText := ''; 76 Inner := False; 67 77 while not Eof do begin 68 78 Read(C); 69 InputText := InputText + C; 79 if ((LastC = #0) or (LastC = #13) or (LastC = #10)) and (C = '|') then begin 80 Inner := True; 81 LastC := C; 82 Continue; 83 end else 84 if ((LastC = #0) or (LastC = #13) or (LastC = #10)) and (C <> '|') then begin 85 Inner := False; 86 end; 87 if Inner then InnerText := InnerText + C 88 else InputText := InputText + C; 89 LastC := C; 70 90 end; 71 91 end; … … 296 316 I: Integer; 297 317 II: Integer; 298 begin 318 FoundOperator: Boolean; 319 OldPos: Integer; 320 begin 321 OldPos := InputTextPos; 299 322 Result := True; 323 FoundOperator := False; 300 324 repeat 301 325 if CheckNext('(') then begin … … 312 336 Expression^.Items[Length(Expression^.Items) - 1].NodeType := ntOperator; 313 337 Expression^.Items[Length(Expression^.Items) - 1].OperatorType := ExpOperator; 338 FoundOperator := True; 314 339 end else 315 340 if ParseGetValue(@GetValue, True) then begin … … 322 347 end; 323 348 until False; 349 if not FoundOperator then begin 350 Result := False; 351 InputTextPos := OldPos; 352 Exit; 353 end; 324 354 325 355 if Length(Expression^.Items) > 0 then begin … … 340 370 Expression^.Items[I].Associated := True; 341 371 SetLength(Expression^.Items[I].Items, 2); 342 Expression^.Items[I].Items[ 1] := Expression^.Items[I - 1];343 Expression^.Items[I].Items[ 0] := Expression^.Items[I + 1];372 Expression^.Items[I].Items[0] := Expression^.Items[I - 1]; 373 Expression^.Items[I].Items[1] := Expression^.Items[I + 1]; 344 374 DeleteExpressionItem(Expression, I + 1); 345 375 DeleteExpressionItem(Expression, I - 1); … … 686 716 ProgramCode^.Functions.Add(FunctionCreate('Read', nil)); 687 717 FuncRead := ProgramCode^.Functions.GetLast; 688 FuncRead^.Parameters.Add(FunctionParameterCreate('Output', TypeChar)); 718 FuncRead^.Parameters.Add(FunctionParameterCreate('Output', TypeChar, True)); 719 FuncRead^.Variables.Add(VariableCreate('Output', TypeChar)); 689 720 ProgramCode^.Functions.Add(FunctionCreate('Eof', TypeBoolean)); 690 721 ProgramCode^.Functions.Add(FunctionCreate('Length', TypeInteger)); -
branches/interpreter/Source3.pas
r101 r102 72 72 Name: string; 73 73 DataType: PType; 74 Output: Boolean; 74 75 end; 75 76 … … 185 186 function FunctionCreate(Name: string; DataType: PType): TFunction; 186 187 function TypeCreate(Name: string; BaseType: TBaseType): TType; 187 function FunctionParameterCreate(Name: string; DataType: PType ): TFunctionParameter;188 function FunctionParameterCreate(Name: string; DataType: PType; Output: Boolean = False): TFunctionParameter; 188 189 189 190 var … … 216 217 end; 217 218 218 function FunctionParameterCreate(Name: string; DataType: PType ): TFunctionParameter;219 function FunctionParameterCreate(Name: string; DataType: PType; Output: Boolean = False): TFunctionParameter; 219 220 begin 220 221 Result.Name := Name; 221 222 Result.DataType := DataType; 223 Result.Output := Output; 222 224 end; 223 225 -
branches/interpreter/project2.lpr
r95 r102 47 47 end. 48 48 49 |begin 50 |end.
Note:
See TracChangeset
for help on using the changeset viewer.