source: trunk/Target/TargetJavascript.pas

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