Changeset 99 for branches/interpreter
- Timestamp:
- Feb 6, 2017, 12:11:54 AM (8 years ago)
- Location:
- branches/interpreter
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/interpreter/Execute3.pas
r98 r99 5 5 uses 6 6 Source3; 7 8 type 9 TVariableValue = record 10 BaseType: TBaseType; 11 case Integer of 12 0: (ValueChar: Char); 13 1: (ValueInteger: Integer); 14 2: (ValueString: ShortString); 15 3: (ValueBoolean: Boolean); 16 end; 17 PVariableValue = ^TVariableValue; 18 19 TVariableValues = record 20 Items: array of TVariableValue; 21 end; 22 23 { TExecutionContext } 24 25 TExecutionContext = record 26 VariableValues: TVariableValues; 27 procedure LoadFromVariables(Variables: PVariables); 28 end; 29 PExecutionContext = ^TExecutionContext; 30 31 { TExecutionContexts } 32 33 TExecutionContexts = record 34 Items: array of TExecutionContext; 35 function Last: PExecutionContext; 36 procedure Add; 37 procedure RemoveLast; 38 end; 39 40 var 41 ExecutionContexts: TExecutionContexts; 7 42 8 43 procedure ExecuteProgram(ProgramCode: PProgramCode); … … 43 78 I: Integer; 44 79 begin 80 ExecutionContexts.Add; 81 ExecutionContexts.Last^.LoadFromVariables(@Execution^.Func^.Variables); 45 82 { Execution^.Func^.Variables.Add(VariableCreate('Result', Execution^.Func^.ReturnType)); 46 83 for I := 0 to Length(Execution^.Func^.Parameters.Items) - 1 do begin … … 53 90 } 54 91 ExecuteBeginEnd(@Execution^.Func^.BeginEnd); 92 ExecutionContexts.RemoveLast; 55 93 end; 56 94 57 95 procedure ExecuteAssignment(Assignment: PAssignment); 96 var 97 DestVariable: PVariableValue; 58 98 begin 59 case Assignment^.Destination^.DataType^.BaseType of 60 btBoolean: Assignment^.Destination.ValueBoolean := ExecuteExpressionBoolean(@Assignment^.Source); 99 DestVariable := @ExecutionContexts.Last^.VariableValues.Items[Assignment^.Destination^.Index]; 100 case DestVariable^.BaseType of 101 btBoolean: DestVariable^.ValueBoolean := ExecuteExpressionBoolean(@Assignment^.Source); 61 102 //btChar: Assignment^.Destination.ValueBoolean := ExecuteExpressionChar(@Assignment^.Source); 62 103 //btString: Assignment^.Destination.ValueBoolean := ExecuteExpressionString(@Assignment^.Source); … … 78 119 procedure ExecuteProgram(ProgramCode: PProgramCode); 79 120 begin 121 SetLength(ExecutionContexts.Items, 1); 122 ExecutionContexts.Last^.LoadFromVariables(@ProgramCode^.Variables); 80 123 ExecuteBeginEnd(@ProgramCode^.BeginEnd); 124 end; 125 126 { TExecutionContext } 127 128 procedure TExecutionContext.LoadFromVariables(Variables: PVariables); 129 var 130 I: Integer; 131 begin 132 SetLength(VariableValues.Items, Length(Variables.Items)); 133 for I := 0 to Length(Variables.Items) - 1 do begin 134 VariableValues.Items[I].BaseType := Variables.Items[I].DataType.BaseType; 135 end; 136 end; 137 138 { TExecutionContexts } 139 140 function TExecutionContexts.Last: PExecutionContext; 141 begin 142 Result := @ExecutionContexts.Items[Length(ExecutionContexts.Items) - 1]; 143 end; 144 145 procedure TExecutionContexts.Add; 146 begin 147 SetLength(Items, Length(Items) + 1); 148 end; 149 150 procedure TExecutionContexts.RemoveLast; 151 begin 152 SetLength(Items, Length(Items) - 1); 81 153 end; 82 154 -
branches/interpreter/Parser3.pas
r98 r99 27 27 function ParseExecution(Execution: PExecution): Boolean; forward; 28 28 function ParseBeginEnd(BeginEnd: PBeginEnd): Boolean; forward; 29 function ParseGetValue(GetValue: PGetValue): Boolean; forward; 29 30 30 31 … … 165 166 end; 166 167 168 function ParseConstant(out Constant: PConstant): Boolean; 169 var 170 OldPos: Integer; 171 Next: string; 172 begin 173 OldPos := InputTextPos; 174 Next := ReadNext; 175 Constant := MainProgram^.Constants.GetByName(Next); 176 if Constant <> nil then begin 177 Result := True; 178 end else begin 179 Result := False; 180 InputTextPos := OldPos; 181 end; 182 end; 183 167 184 function ParseExpression(Expression: PExpression): Boolean; 168 185 var … … 170 187 OldPos: Integer; 171 188 R: Boolean; 189 GetValue: TGetValue; 190 Execution: TExecution; 172 191 Variable: PVariable; 173 192 SubExpression: TExpression; 174 Execution: TExecution;175 193 begin 176 194 Result := True; … … 180 198 if CheckNext('(') then begin 181 199 Expect('('); 182 R := ParseExpression(@SubExpression); 200 if ParseGetValue(@GetValue) then begin 201 SetLength(Expression^.Items, Length(Expression^.Items) + 1); 202 Expression^.Items[Length(Expression^.Items) - 1] := GetValue; 203 end; 183 204 Expect(')'); 184 205 end else … … 207 228 end; 208 229 230 function ParseGetValue(GetValue: PGetValue): Boolean; 231 var 232 Variable: PVariable; 233 Constant: PConstant; 234 FunctionCall: TExecution; 235 Expression: TExpression; 236 begin 237 if ParseVariable(Variable) then begin 238 GetValue^.Variable := Variable; 239 GetValue^.ReadType := rtVariable; 240 end else 241 if ParseConstant(Constant) then begin 242 GetValue^.Constant := Constant; 243 GetValue^.ReadType := rtConstant; 244 end else 245 if ParseExecution(@FunctionCall) then begin 246 GetValue^.FunctionCall := GetMem(SizeOf(TExecution)); 247 GetValue^.FunctionCall^ := FunctionCall; 248 GetValue^.ReadType := rtFunctionCall; 249 end else 250 if ParseExpression(@Expression) then begin 251 GetValue^.Expression := GetMem(SizeOf(TExpression)); 252 GetValue^.Expression^ := Expression; 253 GetValue^.ReadType := rtExpression; 254 end else 255 ShowError('Expected value'); 256 end; 257 209 258 function ParseAssignment: Boolean; 210 259 var … … 216 265 Assignment.Destination := Variable; 217 266 Expect(':='); 218 Parse Expression(@Assignment.Source);267 ParseGetValue(@Assignment.Source); 219 268 end else begin 220 269 Result := False; … … 228 277 Func: PFunction; 229 278 I: Integer; 230 Expression: TExpression;231 279 begin 232 280 Result := True; … … 235 283 Func := MainProgram.Functions.GetByName(Next); 236 284 if Func <> nil then begin 285 SetLength(Execution^.Parameters.Items, Length(Func^.Parameters.Items)); 237 286 if Length(Func^.Parameters.Items) > 0 then begin 238 287 Expect('('); 239 288 I := 0; 240 289 while I < Length(Func^.Parameters.Items) do begin 241 Parse Expression(@Expression);290 ParseGetValue(@Execution^.Parameters.Items[I]); 242 291 if I < (Length(Func^.Parameters.Items) - 1) then Expect(','); 243 292 I := I + 1; … … 251 300 end; 252 301 253 function ParseCommand : Boolean;302 function ParseCommand(Command: PCommand): Boolean; 254 303 var 255 304 IfThenElse: TIfThenElse; … … 257 306 BeginEnd: TBeginEnd; 258 307 Execution: TExecution; 308 Assignment: TAssignment; 259 309 begin 260 310 Result := True; 261 311 if ParseBeginEnd(@BeginEnd) then begin 312 Command^.BeginEnd := GetMem(SizeOf(TBeginEnd)); 313 Command^.BeginEnd^ := BeginEnd; 314 Command^.CmdType := ctBeginEnd; 262 315 end else 263 316 if ParseAssignment then begin 317 Command^.Assignment := GetMem(SizeOf(TAssignment)); 318 Command^.Assignment^ := Assignment; 319 Command^.CmdType := ctAssignment; 264 320 end else 265 321 if ParseExecution(@Execution) then begin 322 Command^.Execution := GetMem(SizeOf(TExecution)); 323 Command^.Execution^ := Execution; 324 Command^.CmdType := ctExecution; 266 325 end else 267 326 if ParseIfThen(@IfThenElse) then begin 327 Command^.IfThenElse := GetMem(SizeOf(TIfThenElse)); 328 Command^.IfThenElse^ := IfThenElse; 329 Command^.CmdType := ctIfThenElse; 268 330 end else 269 331 if ParseWhileDo(@WhileDo) then begin 332 Command^.WhileDo := GetMem(SizeOf(TWhileDo)); 333 Command^.WhileDo^ := WhileDo; 334 Command^.CmdType := ctWhileDo; 270 335 end else Result := False; 271 336 end; … … 278 343 ParseExpression(@IfThenElse.Condition); 279 344 Expect('then'); 280 ParseCommand ;345 ParseCommand(@IfThenElse^.DoThen); 281 346 if CheckNext('else') then begin 282 347 Expect('else'); 283 ParseCommand ;348 ParseCommand(@IfThenElse^.DoElse); 284 349 end; 285 350 end else Result := False; … … 293 358 ParseExpression(@WhileDo.Condition); 294 359 Expect('do'); 295 ParseCommand ;360 ParseCommand(@WhileDo^.Command); 296 361 end else Result := False; 297 362 end; 298 363 299 364 function ParseBeginEnd(BeginEnd: PBeginEnd): Boolean; 365 var 366 Command: TCommand; 300 367 begin 301 368 if CheckNext('begin') then begin 302 369 Result := True; 303 370 Expect('begin'); 371 SetLength(BeginEnd^.Commands, 0); 304 372 repeat 305 if ParseCommand then begin 373 if ParseCommand(@Command) then begin 374 BeginEnd^.Add; 375 BeginEnd^.GetLast^ := Command; 306 376 Expect(';'); 307 377 end else -
branches/interpreter/Source3.pas
r98 r99 6 6 7 7 type 8 PBeginEnd = ^TBeginEnd; 9 PAssignment = ^TAssignment; 10 PVariable = ^TVariable; 11 PConstant = ^TConstant; 12 PIfThenElse = ^TIfThenElse; 13 PWhileDo = ^TWhileDo; 14 PExpression = ^TExpression; 15 PExecution = ^TExecution; 16 8 17 TBaseType = (btBoolean, btInteger, btChar, btShortString, btArray); 9 18 … … 26 35 Name: string; 27 36 DataType: PType; 28 case Integer of 29 0: (ValueChar: Char); 30 1: (ValueInteger: Integer); 31 2: (ValueString: ShortString); 32 3: (ValueBoolean: Boolean); 33 end; 34 PVariable = ^TVariable; 37 Index: Integer; 38 end; 35 39 36 40 { TVariables } … … 41 45 function GetByName(Name: string): PVariable; 42 46 end; 47 PVariables = ^TVariables; 43 48 44 49 TConstant = record 45 50 Name: string; 46 51 DataType: PType; 47 case Integer of 48 0: (ValueChar: Char); 49 1: (ValueInteger: Integer); 50 2: (ValueString: ShortString); 51 3: (ValueBoolean: Boolean); 52 end; 53 PConstant = ^TConstant; 52 Index: Integer; 53 case TBaseType of 54 btChar: (ValueChar: Char); 55 btInteger: (ValueInteger: Integer); 56 btShortString: (ValueString: ShortString); 57 btBoolean: (ValueBoolean: Boolean); 58 end; 59 60 { TConstants } 61 62 TConstants = record 63 Items: array of TConstant; 64 procedure Add(Constant: TConstant); 65 function GetByName(Name: string): PConstant; 66 end; 67 PConstants = ^TConstants; 54 68 55 69 TFunctionParameter = record … … 72 86 CmdType: TCmdType; 73 87 Ptr: Pointer; 74 {case Integer of75 0: (WhileDo: TWhileDo);76 1: (IfThenElse: TIfThenElse);77 2: (BeginEnd: TBeginEnd);78 3: (Assignment: TAssignment);79 4: (Execution: TExecution);80 }end;88 case Integer of 89 0: (WhileDo: PWhileDo); 90 1: (IfThenElse: PIfThenElse); 91 2: (BeginEnd: PBeginEnd); 92 3: (Assignment: PAssignment); 93 4: (Execution: PExecution); 94 end; 81 95 PCommand = ^TCommand; 96 97 { TBeginEnd } 82 98 83 99 TBeginEnd = record 84 100 Commands: array of TCommand; 85 end; 86 PBeginEnd = ^TBeginEnd; 101 procedure Add; 102 function GetLast: PCommand; 103 end; 104 105 TReadType = (rtVariable, rtConstant, rtExpression, rtFunctionCall); 106 TGetValue = record 107 ReadType: TReadType; 108 case TReadType of 109 rtVariable: (Variable: PVariable); 110 rtConstant: (Constant: PConstant); 111 rtExpression: (Expression: PExpression); 112 rtFunctionCall: (FunctionCall: PExecution); 113 end; 114 PGetValue = ^TGetValue; 115 116 TExpOperand = (eoNone, eoAdd, eoSubtract, eoAnd, eoOr, eoNot); 87 117 88 118 TExpression = record 89 end; 90 PExpression = ^TExpression; 119 Operand: TExpOperand; 120 Items: array of TGetValue; 121 end; 91 122 92 123 TAssignment = record 93 124 Destination: PVariable; 94 Source: TExpression; 95 end; 96 PAssignment = ^TAssignment; 125 Source: TGetValue; 126 end; 97 127 98 128 TIfThenElse = record 99 Condition: T Expression;129 Condition: TGetValue; 100 130 DoThen: TCommand; 101 131 DoElse: TCommand; 102 132 end; 103 PIfThenElse = ^TIfThenElse;104 133 105 134 TWhileDo = record 106 Condition: T Expression;135 Condition: TGetValue; 107 136 Command: TCommand; 108 137 end; 109 PWhileDo = ^TWhileDo;110 138 111 139 { TFunction } … … 130 158 131 159 TExecutionParams = record 132 Items: array of T Expression;160 Items: array of TGetValue; 133 161 end; 134 162 … … 137 165 Parameters: TExecutionParams; 138 166 end; 139 PExecution = ^TExecution;140 167 141 168 TProgramCode = record 142 169 Name: string; 143 170 Variables: TVariables; 171 Constants: TConstants; 144 172 Types: TTypes; 145 173 Functions: TFunctions; … … 180 208 end; 181 209 182 { TVariables } 183 184 procedure TVariables.Add(Variable: TVariable); 185 begin 186 SetLength(Items, Length(Items) + 1); 187 Items[Length(Items) - 1] := Variable; 188 end; 189 190 function TVariables.GetByName(Name: string): PVariable; 210 { TBeginEnd } 211 212 procedure TBeginEnd.Add; 213 begin 214 SetLength(Commands, Length(Commands) + 1); 215 end; 216 217 function TBeginEnd.GetLast: PCommand; 218 begin 219 Result := @Commands[Length(Commands) - 1]; 220 end; 221 222 { TConstants } 223 224 procedure TConstants.Add(Constant: TConstant); 225 begin 226 SetLength(Items, Length(Items) + 1); 227 Items[Length(Items) - 1] := Constant; 228 Items[Length(Items) - 1].Index := Length(Items) - 1; 229 end; 230 231 function TConstants.GetByName(Name: string): PConstant; 191 232 var 192 233 I: Integer; … … 198 239 end; 199 240 200 { TFunctionParameters } 201 202 procedure TFunctionParameters.Add(Param: TFunctionParameter); 203 begin 204 SetLength(Items, Length(Items) + 1); 205 Items[Length(Items) - 1] := Param; 206 end; 207 208 { TTypes } 209 210 procedure TTypes.Add(DataType: TType); 211 begin 212 SetLength(Items, Length(Items) + 1); 213 Items[Length(Items) - 1] := DataType; 214 end; 215 216 function TTypes.GetByName(Name: string): PType; 241 { TVariables } 242 243 procedure TVariables.Add(Variable: TVariable); 244 begin 245 SetLength(Items, Length(Items) + 1); 246 Items[Length(Items) - 1] := Variable; 247 Items[Length(Items) - 1].Index := Length(Items) - 1; 248 end; 249 250 function TVariables.GetByName(Name: string): PVariable; 217 251 var 218 252 I: Integer; … … 224 258 end; 225 259 226 function TTypes.GetLast: PType; 227 begin 228 Result := @Items[Length(Items) - 1]; 229 end; 230 231 { TFunctions } 232 233 procedure TFunctions.Add(Func: TFunction); 234 begin 235 SetLength(Items, Length(Items) + 1); 236 Items[Length(Items) - 1] := Func; 237 end; 238 239 function TFunctions.GetByName(Name: string): PFunction; 260 { TFunctionParameters } 261 262 procedure TFunctionParameters.Add(Param: TFunctionParameter); 263 begin 264 SetLength(Items, Length(Items) + 1); 265 Items[Length(Items) - 1] := Param; 266 end; 267 268 { TTypes } 269 270 procedure TTypes.Add(DataType: TType); 271 begin 272 SetLength(Items, Length(Items) + 1); 273 Items[Length(Items) - 1] := DataType; 274 end; 275 276 function TTypes.GetByName(Name: string): PType; 240 277 var 241 278 I: Integer; … … 247 284 end; 248 285 286 function TTypes.GetLast: PType; 287 begin 288 Result := @Items[Length(Items) - 1]; 289 end; 290 291 { TFunctions } 292 293 procedure TFunctions.Add(Func: TFunction); 294 begin 295 SetLength(Items, Length(Items) + 1); 296 Items[Length(Items) - 1] := Func; 297 end; 298 299 function TFunctions.GetByName(Name: string): PFunction; 300 var 301 I: Integer; 302 begin 303 I := 0; 304 while (I < Length(Items)) and (Items[I].Name <> Name) do I := I + 1; 305 if I < Length(Items) then Result := @Items[I] 306 else Result := nil; 307 end; 308 249 309 function TFunctions.GetLast: PFunction; 250 310 begin
Note:
See TracChangeset
for help on using the changeset viewer.