source: tags/1.3.0/Target/TargetJavascript.pas

Last change on this file was 162, checked in by chronos, 3 months ago

Merged revision(s) 161 from trunk:

  • Fixed: All targets compilation and run.
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 := '';
38end;
39
40function TTargetJavascript.GetMemoryCell: string;
41begin
42 Result := 'Memory[Pos';
43 if FProgram[FProgramIndex].RelIndex > 0 then
44 Result := Result + ' + ' + IntToStr(FProgram[FProgramIndex].RelIndex)
45 else if FProgram[FProgramIndex].RelIndex < 0 then
46 Result := Result + ' - ' + IntToStr(Abs(FProgram[FProgramIndex].RelIndex));
47 Result := Result + ']';
48end;
49
50procedure TTargetJavascript.Compile;
51begin
52 inherited;
53 Indent := 0;
54 FTargetCode := '';
55
56 AddLine('var Memory = new Array(' + IntToStr(MemorySize) + ').fill(0);');
57 AddLine('var Pos = 0;');
58 AddLine('');
59 FProgramIndex := 0;
60 while FProgramIndex < FProgram.Count do begin
61 case FProgram[FProgramIndex].Command of
62 cmPointerInc: AddLine('Pos = Pos + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
63 cmPointerDec: AddLine('Pos = Pos - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
64 cmInc: AddLine(GetMemoryCell + ' = ' + GetMemoryCell + ' + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
65 cmDec: AddLine(GetMemoryCell + ' = ' + GetMemoryCell + ' - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
66 cmOutput: AddLine('document.getElementById("main").innerHTML += String.fromCharCode(' + GetMemoryCell + ');');
67 cmInput: ; //AddLine(GetMemoryCell + ' = getchar();');
68 cmSet: AddLine(GetMemoryCell + ' = ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
69 cmMultiply: AddLine(GetMemoryCell + ' = ' + GetMemoryCell + ' + Memory[Pos] * ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
70 cmLoopStart: begin
71 AddLine('while(' + GetMemoryCell + ' != 0)');
72 AddLine('{');
73 Inc(Indent);
74 end;
75 cmLoopEnd: begin
76 Dec(Indent);
77 AddLine('}');
78 end;
79 end;
80 Inc(FProgramIndex);
81 end;
82 CompileToFile;
83 CompileHtml;
84end;
85
86procedure TTargetJavascript.CompileHtml;
87var
88 HtmlFile: string;
89begin
90 with TStringList.Create do
91 try
92 Text := '<!DOCTYPE html>' + LineEnding +
93 '<html>' + LineEnding +
94 '<body>' + LineEnding +
95 '<pre id="main"></pre>' + LineEnding +
96 '<script type="text/javascript" src="' + ExtractFileNameOnly(ProjectFileName) + SourceExtension + '"></script>' + LineEnding +
97 '</body>' + LineEnding +
98 '</html>';
99 HtmlFile := ExtractFilePath(ProjectFileName) +
100 CompiledDir + DirectorySeparator + Name + DirectorySeparator +
101 ExtractFileNameOnly(ProjectFileName) + '.html';
102 ForceDirectories(ExtractFilePath(HtmlFile));
103 SaveToFile(HtmlFile);
104 finally
105 Free;
106 end;
107end;
108
109procedure TTargetJavascript.Run;
110var
111 HtmlFile: string;
112begin
113 inherited;
114 HtmlFile := ExtractFilePath(ProjectFileName) +
115 CompiledDir + DirectorySeparator + Name + DirectorySeparator +
116 ExtractFileNameOnly(ProjectFileName) + '.html';
117
118 if FileExists(HtmlFile) then OpenURL(HtmlFile);
119end;
120
121end.
122
Note: See TracBrowser for help on using the repository browser.