source: branches/easy compiler/UFormMain.pas

Last change on this file was 150, checked in by chronos, 7 years ago
  • Added: Load/Save example source code from/to file instead of hardcoded text in application source code.
File size: 3.2 KB
Line 
1unit UFormMain;
2
3{$mode delphi}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
9 UTargetCode, USourceCode, USourceExecutor, USourceGenerator, LCLIntf,
10 ComCtrls;
11
12type
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
45var
46 Form1: TForm1;
47
48implementation
49
50{$R *.lfm}
51
52uses
53 UCompiler;
54
55{ TForm1 }
56
57procedure TForm1.FormShow(Sender: TObject);
58begin
59 FileName := 'Examples' + DirectorySeparator + 'Animal guessing game.lan';
60 MemoSource.Lines.LoadFromFile(FileName);
61end;
62
63procedure TForm1.ButtonBuildClick(Sender: TObject);
64var
65 Compiler: TCompiler;
66 Target: TTargetCode;
67 Executor: TSourceExecutor;
68 Generator: TSourceGenerator;
69 SourceCode: TSourceCode;
70begin
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;
89end;
90
91procedure TForm1.ButtonSendClick(Sender: TObject);
92begin
93 InputBuffer.Add(Edit1.Text);
94 Edit1.Text := '';
95end;
96
97procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: char);
98begin
99 if Key = #13 then ButtonSend.Click;
100end;
101
102procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
103begin
104 MemoSource.Lines.SaveToFile(FileName);
105end;
106
107procedure TForm1.FormCreate(Sender: TObject);
108begin
109 InputBuffer := TStringList.Create;
110end;
111
112procedure TForm1.FormDestroy(Sender: TObject);
113begin
114 InputBuffer.Free;
115end;
116
117procedure TForm1.ExecutorOutput(Text: string);
118begin
119 MemoOutput.Text := MemoOutput.Text + Text;
120 MemoOutput.SelStart := Length(MemoOutput.Text);
121 //SendMessage(MemoOutput.Handle, EM_LINESCROLL, 0, MemoOutput.Lines.Count);
122end;
123
124function TForm1.ExecutorInput: string;
125begin
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 := '';
135end;
136
137end.
138
Note: See TracBrowser for help on using the repository browser.