-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathradiolang.pas
238 lines (198 loc) · 5.58 KB
/
radiolang.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
unit RadioLang;
{$mode objfpc}{$H+}
{
a LazRadio project is defined by a .lzr file.
lazradio SYS-NAME;
var
VAR-NAME: ModuleName;
...
begin
// connect feature
VAR-NAME :> VAR-NAME [ :> VAR-NAME];
// connect data port 0
VAR-NAME -> VAR-NAME [ -> VAR-NAME];
// connect data port, soure port i, target port j
VAR-NAME[I] -> [J]VAR-NAME;
// connect feature and data port 0
VAR-NAME => VAR-NAME [ => VAR-NAME];
// post a message
VAR-NAME ! {M-ID, PRAMH, PRAML} [! ... ];
end.
}
interface
uses
Classes, SysUtils, superobject, RadioModule, RadioSystem;
type
{ TRadioLangRT }
TRadioLangRT = class
private
FConstTable: ISuperObject;
FModuleTypes: TStringList;
FOnProjNameChanged: TNotifyEvent;
FProjName: string;
procedure LoadMsgConsts;
function CBSendMessage(const Name: string; const V1, V2, V3: PtrUInt): Boolean;
function CBConnectFeature(const Source, Target: string): Boolean;
function CBConnectData(const Source, Target: string; const SourcePort, TargetPort: Integer): Boolean;
function CBMakeStrParam(const S: string): PtrUInt;
function CBMakeStrParam0(const S: string): PtrUInt;
procedure CBEmitMessage(const Line: string);
procedure CBProjName(const Line: string);
procedure CBCreateModules(Sender: TObject);
procedure SetOnProjNameChanged(AValue: TNotifyEvent);
public
constructor Create;
destructor Destroy; override;
procedure Reset;
function Exec(const Fn: string): Boolean;
function Check(const Fn: string): Boolean;
procedure GetRTSymbols(L: TStrings);
property ModuleTypes: TStringList read FModuleTypes;
property ProjName: string read FProjName;
property OnProjNameChanged: TNotifyEvent read FOnProjNameChanged write SetOnProjNameChanged;
end;
implementation
uses
util_config, lzr_interpreter;
{ TRadioLangRT }
procedure TRadioLangRT.LoadMsgConsts;
var
O: ISuperObject;
procedure LoadConsts(Obj: ISuperObject);
var
I: Integer;
A: TSuperArray;
N: string;
begin
if not Assigned(Obj) then Exit;
if Obj.IsType(stObject) then
begin
N := Obj.S['name'];
if N <> '' then
FConstTable.I[N] := Obj.I['id'];
LoadConsts(Obj.O['paramh.vals']);
LoadConsts(Obj.O['paraml.vals']);
end;
if not Obj.IsType(stArray) then Exit;
A := Obj.AsArray;
for I := 0 to A.Length - 1 do
begin
LoadConsts(A.O[I]);
end;
end;
begin
O := TSuperObject.ParseFile(GetResFullName('messages.json'), False);
LoadConsts(O);
end;
function TRadioLangRT.CBSendMessage(const Name: string; const V1, V2,
V3: PtrUInt): Boolean;
begin
Result := RadioPostMessage(V1, V2, V3, Name);
end;
function TRadioLangRT.CBConnectFeature(const Source, Target: string): Boolean;
begin
Result := TRadioSystem.Instance.ConnectFeature(Source, Target);
end;
function TRadioLangRT.CBConnectData(const Source, Target: string;
const SourcePort, TargetPort: Integer): Boolean;
begin
Result := TRadioSystem.Instance.ConnectData(Source, Target, TargetPort, SourcePort);
end;
function TRadioLangRT.CBMakeStrParam(const S: string): PtrUInt;
begin
Result := TRadioSystem.Instance.MakeStrParam(S);
end;
function TRadioLangRT.CBMakeStrParam0(const S: string): PtrUInt;
begin
Result := 0;
end;
procedure TRadioLangRT.CBEmitMessage(const Line: string);
begin
TRadioLogger.Report(llSystem, Line);
end;
procedure TRadioLangRT.CBProjName(const Line: string);
begin
FProjName := Line;
if Assigned(FOnProjNameChanged) then FOnProjNameChanged(Self);
end;
procedure TRadioLangRT.CBCreateModules(Sender: TObject);
var
A: TSuperAvlEntry;
begin
for A in SymTable.AsObject do
begin
if A.Value.AsObject.B['obj'] then
begin
TRadioSystem.Instance.AddModule(A.Value.AsObject.S['disp'], A.Value.AsObject.S['type']);
end;
end;
end;
procedure TRadioLangRT.SetOnProjNameChanged(AValue: TNotifyEvent);
begin
if FOnProjNameChanged = AValue then Exit;
FOnProjNameChanged := AValue;
end;
constructor TRadioLangRT.Create;
begin
FModuleTypes := TStringList.Create;
FConstTable := TSuperObject.ParseFile(GetResFullName('lzr_internal.json'), False);
LoadMsgConsts;
end;
destructor TRadioLangRT.Destroy;
begin
FModuleTypes.Free;
inherited Destroy;
end;
procedure TRadioLangRT.Reset;
var
S: string;
A: TSuperAvlEntry;
begin
ObjTypes := SO('{}');
for S in FModuleTypes do
ObjTypes.O[UpperCase(S)] := SO(Format('{disp: "%s"}', [S]));
RegTable := SO('{}');
SymTable := SO('{}');
for A in FConstTable.AsObject do
PredefineInt(A.Name, A.Value.AsInteger);
end;
function TRadioLangRT.Exec(const Fn: string): Boolean;
begin
Reset;
OnCreateModules := @CBCreateModules;
OnSendMessage := @CBSendMessage;
OnConnectFeature:= @CBConnectFeature;
OnConnectData := @CBConnectData;
OnWriteLn := @CBEmitMessage;
OnProjName := @CBProjName;
OnMakeStrParam := @CBMakeStrParam;
Result := Interpret(Fn);
if Result then
TRadioLogger.Report(llSystem, 'successfully executed.')
else
TRadioLogger.Report(llSystem, 'there are errors.');
end;
function TRadioLangRT.Check(const Fn: string): Boolean;
begin
Reset;
OnCreateModules := nil;
OnSendMessage := nil;
OnConnectFeature:= nil;
OnConnectData := nil;
OnWriteLn := @CBEmitMessage;
OnProjName := @CBProjName;
OnMakeStrParam := @CBMakeStrParam0;
Result := Interpret(Fn);
if Result then
TRadioLogger.Report(llSystem, 'successfully checked.')
else
TRadioLogger.Report(llSystem, 'there are errors.');
end;
procedure TRadioLangRT.GetRTSymbols(L: TStrings);
var
A: TSuperAvlEntry;
begin
for A in FConstTable.AsObject do
L.Add(A.Name);
end;
end.