| 1 | unit TargetJavascript;
|
|---|
| 2 |
|
|---|
| 3 | interface
|
|---|
| 4 |
|
|---|
| 5 | uses
|
|---|
| 6 | Classes, SysUtils, FileUtil, Target, BFTarget, Dialogs, LazFileUtils,
|
|---|
| 7 | Process, LCLIntf;
|
|---|
| 8 |
|
|---|
| 9 | type
|
|---|
| 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 |
|
|---|
| 25 | implementation
|
|---|
| 26 |
|
|---|
| 27 | { TTargetJavascript }
|
|---|
| 28 |
|
|---|
| 29 | constructor TTargetJavascript.Create;
|
|---|
| 30 | begin
|
|---|
| 31 | inherited;
|
|---|
| 32 | FName := 'Javascript';
|
|---|
| 33 | FSourceExtension := '.js';
|
|---|
| 34 | FImageIndex := 28;
|
|---|
| 35 | FCapabilities := [tcCompile, tcRun];
|
|---|
| 36 | CompilerPath := '';
|
|---|
| 37 | FCompiledExtension := '';
|
|---|
| 38 | end;
|
|---|
| 39 |
|
|---|
| 40 | function TTargetJavascript.GetMemoryCell: string;
|
|---|
| 41 | begin
|
|---|
| 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 + ']';
|
|---|
| 48 | end;
|
|---|
| 49 |
|
|---|
| 50 | procedure TTargetJavascript.Compile;
|
|---|
| 51 | begin
|
|---|
| 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;
|
|---|
| 84 | end;
|
|---|
| 85 |
|
|---|
| 86 | procedure TTargetJavascript.CompileHtml;
|
|---|
| 87 | var
|
|---|
| 88 | HtmlFile: string;
|
|---|
| 89 | begin
|
|---|
| 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;
|
|---|
| 107 | end;
|
|---|
| 108 |
|
|---|
| 109 | procedure TTargetJavascript.Run;
|
|---|
| 110 | var
|
|---|
| 111 | HtmlFile: string;
|
|---|
| 112 | begin
|
|---|
| 113 | inherited;
|
|---|
| 114 | HtmlFile := ExtractFilePath(ProjectFileName) +
|
|---|
| 115 | CompiledDir + DirectorySeparator + Name + DirectorySeparator +
|
|---|
| 116 | ExtractFileNameOnly(ProjectFileName) + '.html';
|
|---|
| 117 |
|
|---|
| 118 | if FileExists(HtmlFile) then OpenURL(HtmlFile);
|
|---|
| 119 | end;
|
|---|
| 120 |
|
|---|
| 121 | end.
|
|---|
| 122 |
|
|---|