program turtlecompile; {$mode objfpc} uses Math, Classes, SysUtils, browserapp, Web, webfilecache, pas2jswebcompiler; Type { TWebCompilerDemo } TWebCompilerDemo = Class(TBrowserApplication) Private btnCloseNotification, BRun : TJSHTMLButtonElement; MSource : TJSHTMLTextAreaElement; MLog: TJSHTMLElement; pnlLog : TJSHTMLElement; RFrame : TJSHTMLIFrameElement; FCompiler : TPas2JSWebCompiler; procedure ClearResult; procedure DoLog(Sender: TObject; const Msg: String); function HideNotification(aEvent: TJSMouseEvent): boolean; procedure LogError(const aMsg: string); procedure OnUnitsLoaded(Sender: TObject; aFileName: String; aError: string); function Prepare(aSource: string): string; function RunClick(aEvent: TJSMouseEvent): boolean; procedure RunResult; Protected function CompileClick(aEvent: TJSMouseEvent): boolean; Procedure LinkElements; Property Compiler : TPas2JSWebCompiler Read FCompiler; Public Constructor Create(aOwner : TComponent); override; Procedure Execute; end; Const // Default run HTML page, shown in IFrame. SHTMLHead = ''+LineEnding+ ''+LineEnding+ ' '+LineEnding+ ' Pas2JS Turtle graphics program output'+LineEnding+ ' '+LineEnding+ ' '+LineEnding+ ''+LineEnding+ ''+LineEnding+ '
'+LineEnding+ '
'+LineEnding+ '

Run program output

'+LineEnding+ '
'+LineEnding+ ' '+LineEnding+ '
'+LineEnding+ '
'+LineEnding+ '
'+LineEnding+ ''+LineEnding+ ''+LineEnding+ ''; { TWebCompilerDemo } procedure TWebCompilerDemo.LogError(const aMsg : string); begin MLog.InnerText:=aMsg; pnlLog.classList.remove('is-hidden'); end; procedure TWebCompilerDemo.OnUnitsLoaded(Sender: TObject; aFileName: String; aError: string); begin BRun.classList.remove('is-loading'); if aError='' then BRun.disabled:=False else begin LogError('Error Loading "'+aFileName+'": '+AError); end; end; procedure TWebCompilerDemo.LinkElements; begin BRun:=TJSHTMLButtonElement(GetHTMLElement('btnRun')); BRun.onClick:=@CompileClick; btnCloseNotification:=TJSHTMLButtonElement(GetHTMLElement('btnCloseNotification')); btnCloseNotification.onClick:=@HideNotification; MSource:=TJSHTMLTextAreaElement(GetHTMLElement('memSource')); MLog:=GetHTMLElement('lblCompilerOutput'); pnlLog:=GetHTMLElement('pnlLog'); RFrame:=TJSHTMLIFrameElement(Document.getElementById('runarea')); end; constructor TWebCompilerDemo.Create(aOwner : TComponent); begin Inherited; FCompiler:=TPas2JSWebCompiler.Create; Compiler.Log.OnLog:=@DoLog; end; function TWebCompilerDemo.RunClick(aEvent: TJSMouseEvent): boolean; Var Src : String; begin Result:=True; end; procedure TWebCompilerDemo.DoLog(Sender: TObject; const Msg: String); begin MLog.InnerHTML:=MLog.InnerHTML+'
'+Msg; end; function TWebCompilerDemo.HideNotification(aEvent: TJSMouseEvent): boolean; begin pnlLog.classList.Add('is-hidden'); end; Procedure TWebCompilerDemo.ClearResult; begin end; function TWebCompilerDemo.Prepare(aSource : string) : string; var Src,un : String; p, pu, pp, ps : Integer; doinsert,withcomma : boolean; begin Result:=aSource; Src:=LowerCase(aSource); p:=pos('begin',Src); p:=Min(P,pos('function ',Src)); p:=Min(P,pos('procedure ',Src)); doinsert:=true; withcomma:=false; pu:=Pos('uses',Src); // No uses if (pu=0) then begin pp:=pos('program',src); if pp=0 then pu:=1 else pu:=pos(';',Src,pp+6)+1; System.Insert(#10'uses ;',result,pu); pu:=pu+6; end else begin pu:=pu+5; ps:=pos(';',Src,pu); if pos('turtlegraphics',Src,pu)