-
Notifications
You must be signed in to change notification settings - Fork 3
/
cmdloader.pas
139 lines (125 loc) · 3.59 KB
/
cmdloader.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
{
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
}
// Copyright (c) 2010 2011 2012 - J. Aldo G. de Freitas Junior
{$mode objfpc}
{$H+}
Unit
CmdLoader;
Interface
Uses
Classes,
SysUtils,
Tree,
Stacks,
ExprScanner,
ExprParser,
ExprNodes,
CmdParser,
CmdNodes;
Type
TCmdRootNode = Class(TExprNode)
Private
fCommandClassFactory : TCommandFactory;
Public
Constructor Create(Const aOwner : TTreeNode); Override;
Destructor Destroy; Override;
Procedure Evaluate; Override;
Procedure DoLoadCommand(Const aCommandLine : String; Const aStack : TVirtualMachineStack);
Procedure DoLoadDOM(Const aDOMNode : String; Const aStack : TVirtualMachineStack);
Property ClassFactory : TCommandFactory Read fCommandClassFactory;
End;
Implementation
Constructor TCmdRootNode.Create(Const aOwner : TTreeNode);
Begin
Inherited Create(aOwner);
fCommandClassFactory := TCommandFactory.Create;
fCommandClassFactory.DefaultClass := TExprNode;
End;
Destructor TCmdRootNode.Destroy;
Begin
FreeAndNil(fCommandClassFactory);
Inherited Destroy;
End;
Procedure TCmdRootNode.Evaluate;
Begin
// Debug WriteLn('TCmdRootNode');
EvaluateChilds;
End;
Procedure TCmdRootNode.DoLoadCommand(Const aCommandLine : String; Const aStack : TVirtualMachineStack);
Var
lFile : TStringStream;
lSource : TExprSource;
lTokens : TMemoryStream;
lScanner : TExprScanner;
lTokenIterator : TExprTokenIterator;
lParser : TCommandParser;
Begin
lFile := Nil;
lSource := Nil;
lTokens := Nil;
lScanner := Nil;
lTokenIterator := Nil;
lParser := Nil;
Try
lFile := TStringStream.Create(aCommandLine);
lSource := TExprSource.Create(lFile, False);
lTokens := TMemoryStream.Create;
lScanner := TExprScanner.Create(lSource, lTokens, False);
lScanner.Scan;
lTokenIterator := TExprTokenIterator.Create(lTokens, False);
lParser := TCommandParser.Create(lTokenIterator, Self, False);
lParser.ClassFactory := fCommandClassFactory;
lParser.ParseInputCommand;
PropagateStack(aStack);
Finally
FreeAndNil(lFile);
FreeAndNil(lSource);
FreeAndNil(lTokens);
FreeAndNil(lScanner);
FreeAndNil(lTokenIterator);
FreeAndNil(lParser);
End;
End;
Procedure TCmdRootNode.DoLoadDOM(Const aDOMNode : String; Const aStack : TVirtualMachineStack);
Var
lFile : TStringStream;
lSource : TExprSource;
lTokens : TMemoryStream;
lScanner : TExprScanner;
lTokenIterator : TExprTokenIterator;
lParser : TCommandParser;
Begin
lFile := Nil;
lSource := Nil;
lTokens := Nil;
lScanner := Nil;
lTokenIterator := Nil;
lParser := Nil;
Try
lFile := TStringStream.Create(aDOMNode);
lSource := TExprSource.Create(lFile, False);
lTokens := TMemoryStream.Create;
lScanner := TExprScanner.Create(lSource, lTokens, False);
lScanner.Scan;
lTokenIterator := TExprTokenIterator.Create(lTokens, False);
lParser := TCommandParser.Create(lTokenIterator, Self, False);
lParser.ClassFactory := fCommandClassFactory;
lParser.ParseDOMExpression;
PropagateStack(aStack);
Finally
FreeAndNil(lFile);
FreeAndNil(lSource);
FreeAndNil(lTokens);
FreeAndNil(lScanner);
FreeAndNil(lTokenIterator);
FreeAndNil(lParser);
End;
End;
End.