1 | unit UFormMain;
|
---|
2 |
|
---|
3 | {$mode delphi}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
|
---|
9 | UTargetCode, USourceCode, USourceExecutor, USourceGenerator, LCLIntf,
|
---|
10 | ComCtrls;
|
---|
11 |
|
---|
12 | type
|
---|
13 |
|
---|
14 | { TForm1 }
|
---|
15 |
|
---|
16 | TForm1 = class(TForm)
|
---|
17 | ButtonBuild: TButton;
|
---|
18 | ButtonSend: TButton;
|
---|
19 | Edit1: TEdit;
|
---|
20 | Label1: TLabel;
|
---|
21 | Label2: TLabel;
|
---|
22 | Label3: TLabel;
|
---|
23 | Label4: TLabel;
|
---|
24 | MemoGenerator: TMemo;
|
---|
25 | MemoOutput: TMemo;
|
---|
26 | MemoSource: TMemo;
|
---|
27 | PageControl1: TPageControl;
|
---|
28 | TabSheetExecutor: TTabSheet;
|
---|
29 | TabSheetGenerator: TTabSheet;
|
---|
30 | procedure ButtonBuildClick(Sender: TObject);
|
---|
31 | procedure ButtonSendClick(Sender: TObject);
|
---|
32 | procedure Edit1KeyPress(Sender: TObject; var Key: char);
|
---|
33 | procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
|
---|
34 | procedure FormCreate(Sender: TObject);
|
---|
35 | procedure FormDestroy(Sender: TObject);
|
---|
36 | procedure FormShow(Sender: TObject);
|
---|
37 | private
|
---|
38 | InputBuffer: TStringList;
|
---|
39 | procedure ExecutorOutput(Text: string);
|
---|
40 | function ExecutorInput: string;
|
---|
41 | public
|
---|
42 | FileName: string;
|
---|
43 | end;
|
---|
44 |
|
---|
45 | var
|
---|
46 | Form1: TForm1;
|
---|
47 |
|
---|
48 | implementation
|
---|
49 |
|
---|
50 | {$R *.lfm}
|
---|
51 |
|
---|
52 | uses
|
---|
53 | UCompiler;
|
---|
54 |
|
---|
55 | { TForm1 }
|
---|
56 |
|
---|
57 | procedure TForm1.FormShow(Sender: TObject);
|
---|
58 | begin
|
---|
59 | FileName := 'Examples' + DirectorySeparator + 'Animal guessing game.lan';
|
---|
60 | MemoSource.Lines.LoadFromFile(FileName);
|
---|
61 | end;
|
---|
62 |
|
---|
63 | procedure TForm1.ButtonBuildClick(Sender: TObject);
|
---|
64 | var
|
---|
65 | Compiler: TCompiler;
|
---|
66 | Target: TTargetCode;
|
---|
67 | Executor: TSourceExecutor;
|
---|
68 | Generator: TSourceGenerator;
|
---|
69 | SourceCode: TSourceCode;
|
---|
70 | begin
|
---|
71 | MemoOutput.Clear;
|
---|
72 | Compiler := TCompiler.Create;
|
---|
73 | Target := TTargetCode.Create;
|
---|
74 | SourceCode := TSourceCode.Create;
|
---|
75 | Executor := TSourceExecutor.Create;
|
---|
76 | Executor.OnOutput := ExecutorOutput;
|
---|
77 | Executor.OnInput := ExecutorInput;
|
---|
78 | Generator := TSourceGenerator.Create;
|
---|
79 |
|
---|
80 | Compiler.Compile(MemoSource.Text, SourceCode);
|
---|
81 | MemoGenerator.Text := Generator.Generate(SourceCode);
|
---|
82 | Executor.Execute(SourceCode);
|
---|
83 |
|
---|
84 | Generator.Free;
|
---|
85 | SourceCode.Free;
|
---|
86 | Executor.Free;
|
---|
87 | Compiler.Free;
|
---|
88 | Target.Free;
|
---|
89 | end;
|
---|
90 |
|
---|
91 | procedure TForm1.ButtonSendClick(Sender: TObject);
|
---|
92 | begin
|
---|
93 | InputBuffer.Add(Edit1.Text);
|
---|
94 | Edit1.Text := '';
|
---|
95 | end;
|
---|
96 |
|
---|
97 | procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: char);
|
---|
98 | begin
|
---|
99 | if Key = #13 then ButtonSend.Click;
|
---|
100 | end;
|
---|
101 |
|
---|
102 | procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
|
---|
103 | begin
|
---|
104 | MemoSource.Lines.SaveToFile(FileName);
|
---|
105 | end;
|
---|
106 |
|
---|
107 | procedure TForm1.FormCreate(Sender: TObject);
|
---|
108 | begin
|
---|
109 | InputBuffer := TStringList.Create;
|
---|
110 | end;
|
---|
111 |
|
---|
112 | procedure TForm1.FormDestroy(Sender: TObject);
|
---|
113 | begin
|
---|
114 | InputBuffer.Free;
|
---|
115 | end;
|
---|
116 |
|
---|
117 | procedure TForm1.ExecutorOutput(Text: string);
|
---|
118 | begin
|
---|
119 | MemoOutput.Text := MemoOutput.Text + Text;
|
---|
120 | MemoOutput.SelStart := Length(MemoOutput.Text);
|
---|
121 | //SendMessage(MemoOutput.Handle, EM_LINESCROLL, 0, MemoOutput.Lines.Count);
|
---|
122 | end;
|
---|
123 |
|
---|
124 | function TForm1.ExecutorInput: string;
|
---|
125 | begin
|
---|
126 | while InputBuffer.Count = 0 do begin
|
---|
127 | if Application.Terminated then Break;
|
---|
128 | Sleep(50);
|
---|
129 | Application.ProcessMessages;
|
---|
130 | end;
|
---|
131 | if InputBuffer.Count > 0 then begin
|
---|
132 | Result := InputBuffer[0];
|
---|
133 | InputBuffer.Delete(0);
|
---|
134 | end else Result := '';
|
---|
135 | end;
|
---|
136 |
|
---|
137 | end.
|
---|
138 |
|
---|