source: tags/1.2.0/Target/UTargetJavascript.pas

Last change on this file was 135, checked in by chronos, 3 years ago
  • Modified: Code cleanup.
  • Modified: More translated strings.
File size: 3.5 KB
Line 
1unit UTargetJavascript;
2
3{$mode Delphi}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, FileUtil, UTarget, UBFTarget, Dialogs, LazFileUtils,
9 Process, LCLIntf;
10
11type
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
27implementation
28
29{ TTargetJavascript }
30
31constructor TTargetJavascript.Create;
32begin
33 inherited;
34 FName := 'Javascript';
35 FSourceExtension := '.js';
36 FImageIndex := 28;
37 FCapabilities := [tcCompile, tcRun];
38 CompilerPath := '';
39 FCompiledExtension := '';
40 FRunExtension := '';
41end;
42
43function TTargetJavascript.GetMemoryCell: string;
44begin
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 + ']';
51end;
52
53procedure TTargetJavascript.Compile;
54begin
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;
87end;
88
89procedure TTargetJavascript.CompileHtml;
90var
91 HtmlFile: string;
92begin
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;
110end;
111
112procedure TTargetJavascript.Run;
113var
114 HtmlFile: string;
115begin
116 inherited;
117 HtmlFile := ExtractFilePath(ProjectFileName) +
118 CompiledDir + DirectorySeparator + Name + DirectorySeparator +
119 ExtractFileNameOnly(ProjectFileName) + '.html';
120
121 if FileExists(HtmlFile) then OpenURL(HtmlFile);
122end;
123
124end.
125
Note: See TracBrowser for help on using the repository browser.