1 | unit UTargetJavascript;
|
---|
2 |
|
---|
3 | {$mode Delphi}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, FileUtil, UTarget, UBFTarget, Dialogs, LazFileUtils,
|
---|
9 | Process, LCLIntf;
|
---|
10 |
|
---|
11 | type
|
---|
12 |
|
---|
13 | { TTargetJavascript }
|
---|
14 |
|
---|
15 | TTargetJavascript = class(TBFTarget)
|
---|
16 | private
|
---|
17 | function GetMemoryCell: string;
|
---|
18 | public
|
---|
19 | WithHtml: Boolean;
|
---|
20 | constructor Create; override;
|
---|
21 | procedure Compile; override;
|
---|
22 | procedure CompileHtml;
|
---|
23 | procedure Run; override;
|
---|
24 | end;
|
---|
25 |
|
---|
26 |
|
---|
27 | implementation
|
---|
28 |
|
---|
29 | { TTargetJavascript }
|
---|
30 |
|
---|
31 | constructor TTargetJavascript.Create;
|
---|
32 | begin
|
---|
33 | inherited;
|
---|
34 | FName := 'Javascript';
|
---|
35 | FSourceExtension := '.js';
|
---|
36 | FImageIndex := 28;
|
---|
37 | FCapabilities := [tcCompile, tcRun];
|
---|
38 | CompilerPath := '';
|
---|
39 | FCompiledExtension := '';
|
---|
40 | FRunExtension := '';
|
---|
41 | end;
|
---|
42 |
|
---|
43 | function TTargetJavascript.GetMemoryCell: string;
|
---|
44 | begin
|
---|
45 | Result := 'Memory[Pos';
|
---|
46 | if FProgram[FProgramIndex].RelIndex > 0 then
|
---|
47 | Result := Result + ' + ' + IntToStr(FProgram[FProgramIndex].RelIndex)
|
---|
48 | else if FProgram[FProgramIndex].RelIndex < 0 then
|
---|
49 | Result := Result + ' - ' + IntToStr(Abs(FProgram[FProgramIndex].RelIndex));
|
---|
50 | Result := Result + ']';
|
---|
51 | end;
|
---|
52 |
|
---|
53 | procedure TTargetJavascript.Compile;
|
---|
54 | begin
|
---|
55 | inherited;
|
---|
56 | Indent := 0;
|
---|
57 | FTargetCode := '';
|
---|
58 |
|
---|
59 | AddLine('var Memory = new Array(' + IntToStr(MemorySize) + ').fill(0);');
|
---|
60 | AddLine('var Pos = 0;');
|
---|
61 | AddLine('');
|
---|
62 | FProgramIndex := 0;
|
---|
63 | while FProgramIndex < FProgram.Count do begin
|
---|
64 | case FProgram[FProgramIndex].Command of
|
---|
65 | cmPointerInc: AddLine('Pos = Pos + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
|
---|
66 | cmPointerDec: AddLine('Pos = Pos - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
|
---|
67 | cmInc: AddLine(GetMemoryCell + ' = ' + GetMemoryCell + ' + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
|
---|
68 | cmDec: AddLine(GetMemoryCell + ' = ' + GetMemoryCell + ' - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
|
---|
69 | cmOutput: AddLine('document.getElementById("main").innerHTML += String.fromCharCode(' + GetMemoryCell + ');');
|
---|
70 | cmInput: ; //AddLine(GetMemoryCell + ' = getchar();');
|
---|
71 | cmSet: AddLine(GetMemoryCell + ' = ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
|
---|
72 | cmMultiply: AddLine(GetMemoryCell + ' = ' + GetMemoryCell + ' + Memory[Pos] * ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
|
---|
73 | cmLoopStart: begin
|
---|
74 | AddLine('while(' + GetMemoryCell + ' != 0)');
|
---|
75 | AddLine('{');
|
---|
76 | Inc(Indent);
|
---|
77 | end;
|
---|
78 | cmLoopEnd: begin
|
---|
79 | Dec(Indent);
|
---|
80 | AddLine('}');
|
---|
81 | end;
|
---|
82 | end;
|
---|
83 | Inc(FProgramIndex);
|
---|
84 | end;
|
---|
85 | CompileToFile;
|
---|
86 | CompileHtml;
|
---|
87 | end;
|
---|
88 |
|
---|
89 | procedure TTargetJavascript.CompileHtml;
|
---|
90 | var
|
---|
91 | HtmlFile: string;
|
---|
92 | begin
|
---|
93 | with TStringList.Create do
|
---|
94 | try
|
---|
95 | Text := '<!DOCTYPE html>' + LineEnding +
|
---|
96 | '<html>' + LineEnding +
|
---|
97 | '<body>' + LineEnding +
|
---|
98 | '<pre id="main"></pre>' + LineEnding +
|
---|
99 | '<script type="text/javascript" src="' + ExtractFileNameOnly(ProjectFileName) + SourceExtension + '"></script>' + LineEnding +
|
---|
100 | '</body>' + LineEnding +
|
---|
101 | '</html>';
|
---|
102 | HtmlFile := ExtractFilePath(ProjectFileName) +
|
---|
103 | CompiledDir + DirectorySeparator + Name + DirectorySeparator +
|
---|
104 | ExtractFileNameOnly(ProjectFileName) + '.html';
|
---|
105 | ForceDirectories(ExtractFilePath(HtmlFile));
|
---|
106 | SaveToFile(HtmlFile);
|
---|
107 | finally
|
---|
108 | Free;
|
---|
109 | end;
|
---|
110 | end;
|
---|
111 |
|
---|
112 | procedure TTargetJavascript.Run;
|
---|
113 | var
|
---|
114 | HtmlFile: string;
|
---|
115 | begin
|
---|
116 | inherited;
|
---|
117 | HtmlFile := ExtractFilePath(ProjectFileName) +
|
---|
118 | CompiledDir + DirectorySeparator + Name + DirectorySeparator +
|
---|
119 | ExtractFileNameOnly(ProjectFileName) + '.html';
|
---|
120 |
|
---|
121 | if FileExists(HtmlFile) then OpenURL(HtmlFile);
|
---|
122 | end;
|
---|
123 |
|
---|
124 | end.
|
---|
125 |
|
---|