Microsoft Small Basic

Program Listing: NHX824-0

dbg="False"

dmt="0=Add/Chng...;1=Diamond;2=MultiDiamnds;3=Lace;4=Spyrall;5=Star5;6=HyperLoop;7=HypFlwrGrid;8=Flwr5;9=Flwr4;10=Clock
dmm=11
vcnt=0
scnt=0
deff[1]="{10!S 0!R 108!{5 !R 72!F 80!}!##
deff[2]="{5!S 0!{10!R 108!{5 !R 72!F 80!}!}!R 144!U!F 210!D!##
deff[3]="{25!S 0!F 30!{3 !R 95!F 140!}!##
deff[4]="{95!S 1.025!R 91.5!F 40!##
deff[5]="{5!R 144!F 140!##
deff[6]="{1!L 40!{55 !S 1.025!R 91.5!F 40!}!$!R 90!{55 !R 91.5!F 40!S/1.025!}!&!L 90!{55 !R 91.5!F 40!S/1.025!}!&!R 90!{55 !L 91.5!F 40!S/1.025!}!##
deff[7]="{1!L 40!{55 !S 1.025!R 91.5!F 40!}!$!R 90!{55 !R 91.5!F 40!S/1.025!}!&!L 90!{55 !R 91.5!F 40!S/1.025!}!&!R 90!{55 !L 91.5!F 40!S/1.025!}!&!{55 !L 91.5!F 40!S/1.025!}!&!{55 !R 91.5!F 40!S/1.025!}!&!R 180!{55 !R 91.5!F 40!S/1.025!}!&!R 180!{55 !L 91.5!F 40!S/1.025!}!##
deff[8]="{5!|X1=5!|C2=15!{15 !F %2!R |X1!|X1+1.93!}!##
deff[9]="{4!|X1=5!|C2=15!{15 !F %2!R |X1!|X1+1.76!}!##
deff[10]="{1!D!R 130!{72 !F 11!R 5!}!|V2=1!U!R 140!F 33!L 136!{12 !WRT %1!F 55!R 30!|V2+1!}!R 72!F 20!D!F 100!R 45!F 60!##
_TFL="0=False;1=True;2=True"

Init()

LDDialogs.AddRightClickMenu(dmt,"")
LDDialogs.RightClickMenu=rmm
return=0
args=0

GraphicsWindow.MouseDown=mdd
GraphicsWindow.MouseMove=mww
GraphicsWindow.MouseUp=muu

Main()


Sub muu
nxt=1
endsub

Sub cang
ox= Turtle.X
oy= Turtle.Y
nx= GraphicsWindow. Mousex
ny= GraphicsWindow. Mousey

ta= Turtle.Angle
x=nx-ox
y=ny-oy
Math_ATan2()
aag= math.GetDegrees(a+Math.Pi/2) -ta
If aag>180 Then
aag=aag-360
elseIf aag<-180 Then
aag=aag+360

endif
aag=math.Round(aag)
endsub

Sub mww
If rec=1 Then
cang()

GraphicsWindow.Title = aag+" | old:"+Turtle.angle
endif
EndSub

Sub mdd



If mov=1 Then
Turtle.x= GraphicsWindow.MouseX
Turtle.y=GraphicsWindow.MouseY
mov=0


elseif rec=1 and nxt=1 Then
nxt=0
'cang()

ota=turtle.angle
Turtle.Angle=Turtle.Angle+aag
If Turtle.Angle>180 then
Turtle.Angle=Turtle.Angle-360
elseif Turtle.Angle<-180 then
Turtle.Angle=Turtle.Angle+360
endif
nta=turtle.angle
dta=nta-ota
dst= Math.SquareRoot ( Math.Power(GraphicsWindow.MouseX - Turtle.x, 2)+Math.Power(GraphicsWindow.Mousey - Turtle.y, 2))
dst=math.Round (dst)
Turtle.Move (dst)
dd="R "
If dta<0 then
dd="L "
endif
'TextWindow.WriteLine (aag)
Clipboard.SetText(CRLF+dd+Math.Abs(dta)+CRLF+"F "+dst)
LDFocus.SetFocus(pgm)
'''aw = SPExtra.SendKeys("+{INS}")
EndIf

EndSub


Sub findvar

ar=Text.ConvertToUpperCase(args[1])

For ax=1 To vcnt
If Text.ConvertToUpperCase(mem[ax][0])=ar Then
return=ax
Goto xxx
endif
EndFor
TextWindow.WriteLine (ar+"VARnotfnd!")


return="VARnotfnd!"
xxx:
endsub

Sub rmm
dd= LDDialogs.LastRightClickMenuItem
if dd>0 Then
LDControls .RichTextBoxSetText(pgm, LDText.Replace ( deff[dd],"!",CRLF ),"False")
Else
ib=Dialogs.AskForTextLine ("Name (x=exit, l=load, s=save):","New Menu Item")
If ib="s" Then
' The following line could be harmful and has been automatically commented.
' The following line could be harmful and has been automatically commented.
' File.WriteContents ("i:\defs.txt",deff)
' The following line could be harmful and has been automatically commented.
' The following line could be harmful and has been automatically commented.
' File.WriteContents ("i:\defm.txt",dmt)
Dialogs.ShowMessageBox("Saved ok.","Menu","OK","Information")
elseIf ib="l" Then
' The following line could be harmful and has been automatically commented.
' The following line could be harmful and has been automatically commented.
' deff=File.ReadContents ("i:\defs.txt")
' The following line could be harmful and has been automatically commented.
' The following line could be harmful and has been automatically commented.
' dmt=File.ReadContents ("i:\defm.txt")
Dialogs.ShowMessageBox("Load ok.","Menu","OK","Information")
LDDialogs.AddRightClickMenu(dmt,"")
elseIf ib<>"x" then
li= LDText.Split (src,CRLF)
att=""
For t=1 To Array.GetItemCount (li)
att=att+li[t]+"!"
endfor
deff[dmm]= att
dmt[dmm]=ib
dmm=dmm+1
LDDialogs.AddRightClickMenu(dmt,"")


endif
EndIf

EndSub

Sub hshow
txx= LDText.Split("FWD!BCK!<<>>!LOOP!VAR!CNST!SUB!FOR!GRID!Rec!MvTo!IF!EDIT!OPEN!NEW","!")
For x=1 To 16
bb[x]= Controls.AddButton(txx[x], 2, 5+(x-1)*40)
EndFor
EndSub

Sub Main
nwwp:
nww=0
tree=""
tree[1][0]="Main"
tree[2][1]="Defs
tree[3][2]="Heading
tree[4][3]="0
tree[5][2]="Bgrnd
tree[6][5]="#bbccdd
tree[7][2]="ForeClr
tree[8][7]="AUTO
tree[9][2]="Width
tree[10][9]="4
tree[11][1]="Consts
tree[12][1]="Vars
tree[13][1]="Subs/Lps

mem=0
sbb=0

Goto rr2

rrr:

GraphicsWindow.BackgroundColor =bcll
rr2:
nxt=1
_inn=0
GraphicsWindow.Clear()

Turtle.Show()
GraphicsWindow.PenWidth=4

Turtle.PenUp ()
Turtle.MoveTo (600,400)
Turtle.PenDown ()
GraphicsWindow.FontName="Calibri"
GraphicsWindow.FontSize=14
hshow()
ch=0
_l=1
sc=1
_sbc=0
GraphicsWindow.BrushColor = "DimGray"
LDControls.RichTextBoxFontFamily="Lucida Console"

LDControls .RichTextBoxFontSize = 18
trr=LDControls.AddTreeView(tree,180,350)
nset()
Controls.Move (trr,50,gh-355)

pgm = LDControls.AddRichTextBox (10, 10)
Controls.SetSize(pgm, 180, gh - 360)
Controls.Move (pgm,50,1)
LDControls.RichTextBoxSetText (pgm, src,"False")
LDControls.RichTextBoxDefault(pgm)
GraphicsWindow.BrushColor = "Black"
g=gh-36
c1= Controls.AddButton("RUN", 2, g)
g=g-35
c2=Controls.AddButton("CLR", 2, g)
g=g-35
c3=Controls.AddButton("XPlain", 2, g)
g=g-35
c4=Controls.AddButton("Paste", 2, g)


clicked = "False"
Controls.ButtonClicked = OnButtonClicked
GraphicsWindow.PenColor = "DimGray"

Turtle.Show()
Turtle.PenUp()
Turtle.MoveTo (700,400)
Turtle.PenDown()
Turtle.Angle=0
GraphicsWindow.FontName = "Lucida Console"
GraphicsWindow.FontSize =12
While "True"
If rst=1 Then
src = LDControls .RichTextBoxGetText (pgm)
tree=LDControls.TreeViewGetData(trr)
bcll =tree[6][5]
rst=0
If nww=1 then
Goto nwwp
else
Goto rrr
endif
elseIf clicked Then
clicked = "False"
src = LDControls .RichTextBoxGetText(pgm)
If Text.GetSubText(src,1,1)="{" or Text.GetSubText(src,1,1)="#" then
If Text.GetSubText(src,1,1)="#" then
dbg="True"
Else
dbg="False"
endif
line= LDText.Split (src,CRLF)
rrw=text.GetSubTextToEnd(line[1],2)
nLines =Array.GetItemCount (line)

If dbg then
TextWindow.WriteLine (">>>>>>>>>>>>>>>>>>>>>runn>>>>>>>>>>>>>>>>>>")
endif
tree=LDControls.TreeViewGetData(trr)
'TextWindow.WriteLine(tree)
GraphicsWindow.PenWidth=tree[10][9]
'GraphicsWindow.Title=rrw
tree=LDControls.TreeViewGetData(trr)
Turtle.Angle =tree[4][3]
sbscan()
For tt=1 To rrw
vcnt=0
scnt=0
'TextWindow.WriteLine (line)
For i = 2 To rwx
linee=line[i]
If Text.StartsWith(linee,"##") then
Goto finn
else

DoLine()
endif
EndFor
finn:
EndFor
tree[4][3]=math.Round (math.Remainder (Turtle.Angle,360))
LDControls.TreeViewContent(trr,tree)
nset()
EndIf

Else
Program.Delay(2)
EndIf

EndWhile
EndSub

Sub sbscan
c=1
rwx=nLines
While Text.StartsWith (line[c],"##")<>"True
c=c+1
If c>nLines then
TextWindow.WriteLine("No ##")
goto tt

endif
EndWhile
rwx=c-1

For i=c To nLines
If Text.StartsWith(line[i], "<") Then
_sbc=_sbc+1

sbb[_sbc]["Stt"] = i + 1
For k = i+1 To nLines
If Text.StartsWith(line[k], ">") Then
sbb[_sbc]["End"] = k-1
endif

endfor
tree=LDControls.TreeViewGetData(trr)
tnd=Array.GetAllIndices(tree)
nc=array.GetItemCount (tnd)+1
If dbg then
typ="Sb"
sbb[_sbc][0]=Text.GetSubTextToEnd (linee, 2)
tree[nc][13]=typ+":"+_sbc+">"+sbb[_sbc]["Stt"]+" to "+sbb[_sbc]["End"]
LDControls.TreeViewContent(trr,tree)
nset()
endif
endif
endfor
tt:
Endsub

Sub nset
LDControls.TreeViewExpand(trr,0,"True","True")
nn= LDText.Split("4,6,8,10",",")
For x=1 To Array.GetItemCount(nn)
LDControls.TreeViewEdit(trr,nn[x],"True")
endfor
EndSub

Sub drwgrd
GraphicsWindow.PenWidth=1
GraphicsWindow.PenColor="#aaaaaa
For x=1 To 70
GraphicsWindow.DrawLine(220+x*20,0,220+x*20,800)
EndFor
For x=1 To 40
GraphicsWindow.DrawLine(220,x*20,1400,x*20)
EndFor

EndSub

Sub xplain
TextWindow.Show ()
TextWindow.Clear()
idd=" "
src = LDControls .RichTextBoxGetText (pgm)
TextWindow.WriteLine("Explain program dump list"+CRLF)
line= LDText.Split (Text.ConvertToUpperCase(src),CRLF)
rrw=MathPlus.ToNumber ( text.GetSubTextToEnd(line[1],2))

nLines =Array.GetItemCount (line)
TextWindow.WriteLine ("{Main loop, repeating times:"+rrw)
For i=2 To nLines
linee=line[i]
If Text.StartsWith(linee,"##") then
TextWindow.WriteLine ("} Main loop end.*******************")
else
xpline()
endif
EndFor
endsub

Sub OnButtonClicked
'"FWD!BCK!<<>>!LOOP!VAR!CNST!sub!FOR!SUB!SAVE!LOAD!IF!HLP!NEW"
clb=Controls.LastClickedButton
If clb=c2 then
rst=1
elseIf clb=c3 then
xplain ()
elseIf clb=c4 then

LDControls.RichTextBoxClear (pgm)
LDFocus.SetFocus(pgm)
'''aa = SPExtra.SendKeys("+{INS}")
rst=1
elseif clb=bb[1] then
LDFocus.SetFocus(pgm)
Clipboard.SetText(CRLF+"F ")
'''aa = SPExtra.SendKeys("+{INS}")
elseif clb=bb[2] then
LDFocus.SetFocus(pgm)
Clipboard.SetText(CRLF+"R180"+CRLF+"F "+CRLF+"R180")

'''aa = SPExtra.SendKeys("+{INS}")
elseif clb=bb[3] then
LDFocus.SetFocus(pgm)
Clipboard.SetText(CRLF+"L ")
'''aa = SPExtra.SendKeys("+{INS}")
elseif clb=bb[4] then
LDFocus.SetFocus(pgm)
Clipboard.SetText(CRLF+"R ")
'''aa = SPExtra.SendKeys("+{INS}")
elseif clb=bb[5] then
LDFocus.SetFocus(pgm)
Clipboard.SetText(CRLF+"{1 [")
'''aa = SPExtra.SendKeys("+{INS}")
elseif clb=bb[6] then
LDFocus.SetFocus(pgm)
Clipboard.SetText(CRLF+"|v1=1")
'''aa = SPExtra.SendKeys("+{INS}")
elseif clb=bb[7] then
LDFocus.SetFocus(pgm)
Clipboard.SetText("%1")
'''aa = SPExtra.SendKeys("+{INS}")
elseif clb=bb[8] then
LDFocus.SetFocus(pgm)
Clipboard.SetText(CRLF+""+CRLF)
'''aa = SPExtra.SendKeys("+{INS}")
elseif clb=bb[9] then
LDFocus.SetFocus(pgm)
Clipboard.SetText(CRLF+"|f1=1"+CRLF+"{for |f1;10 ["+CRLF+"]"+CRLF)
'''aa = SPExtra.SendKeys("+{INS}")
elseif clb=bb[10] then
DrwGrd()
elseif clb=bb[11] then
rec=1
GraphicsWindow.Title="Recording moves... Hit RB to stop."
GraphicsWindow.PenWidth=3
GraphicsWindow.PenColor ="Red

elseif clb=bb[12] then
mov=1
elseif clb=bb[13] then
LDFocus.SetFocus(pgm)
Clipboard.SetText(CRLF+"{IF =%1;5 2 "+CRLF+"W#True"+CRLF+":$2 "+CRLF+"W#False"+CRLF)
'''aa = SPExtra.SendKeys("+{INS}")
elseif clb=bb[14] then
Controls.SetSize(pgm,800, gh - 360)
elseif clb=bb[15] then
fnm=LDDialogs.OpenFile("txt","i:\txt")
' The following line could be harmful and has been automatically commented.
' If LDFile.Exists(fnm) then
' The following line could be harmful and has been automatically commented.
' The following line could be harmful and has been automatically commented.
' fcc=File.ReadContents(fnm)
LDControls.RichTextBoxSetText(pgm,fcc,"False")

endif
'GraphicsWindow.FontName="Times New Roman"
' GraphicsWindow.FontSize=540
' GraphicsWindow.FontBold="True
' GraphicsWindow.FontItalic="True
' GraphicsWindow.BrushColor="#bbbbbb
' GraphicsWindow.DrawText(300,150,"F")

elseif clb=bb[16] then
rst=1
nww=1
src="{1"+CRLF
LDControls.RichTextBoxClear (pgm)
LDControls .RichTextBoxSetText (pgm, src,"False")
else
clicked = "True"
endif
EndSub



Sub DoLine '----------------------------------------------LINEPROC-----------------------------
linee= LDText.Trim (linee)
If linee="" Or Text.StartsWith(linee,"//") Then
Goto exx
endif
If tree[8][7]="AUTO" Then
GraphicsWindow.PenColor=LDColours.HSLtoRGB(math.Remainder (ch,360) ,0.9,0.4)
Else
GraphicsWindow.PenColor=tree[8][7]
endif
ch=ch+1
While Text.IsSubText(linee,"%")
dorepl()
endwhile
ree:
If Text.StartsWith(linee, Text.GetCharacter (34)) Then 'virtual mode
If _inn>0 Then
linee=text.GetSubTextToEnd (linee,2)
Goto ree
Endif
elseIf Text.StartsWith(linee, "|") Then
vn=text.GetSubText(linee,2,2)
vv=text.GetSubTextToEnd (linee,5)
dfc=text.GetSubText(linee,4,1)


If dfc="=" then
vcnt=vcnt+1

mem[vcnt][1]=vv
mem[vcnt][0]=vn

elseif dfc="." then
pp= text.GetSubText(linee,5,2)
scnt=scnt+1
Matr[scnt]=Text.GetSubTextToEnd ( vv,4)
elseif dfc=";" then

For scnt=1 to Text.GetLength (vv)

Matr[scnt]=Text.GetSubText(vv,scnt,1)
endfor

elseif dfc="+" then
rr=LDCall.Function("findvar",vn)
mem[rr][1]=mem[rr][1]+vv
elseif dfc="~" then
rr=LDCall.Function("findvar",vn)
mem[rr][1]=vv
elseif dfc="*" then
rr=LDCall.Function("findvar",vn)
mem[rr][1]=mem[rr][1]*vv
elseif dfc="/" then
rr=LDCall.Function("findvar",vn)
mem[rr][1]=mem[rr][1]/vv
endif
ElseIf Text.StartsWith(linee, "~B#") Then 'block wdth
tww= Text.GetSubTextToEnd (linee,4)
ElseIf Text.StartsWith(linee, "WB#") Then 'block txt
ttx= Text.GetSubTextToEnd (linee,4)

GraphicsWindow.DrawBoundText(Turtle.x, Turtle.y,tww,ttx)


ElseIf Text.StartsWith(linee, "W#") Then 'console debug writting

ttx= Text.GetSubTextToEnd (linee,3)
If Text.StartsWith (ttx,"|") then
vn=text.GetSubText(ttx,2,2)
ttx=LDCall.Function("findvar",vn)
Endif
TextWindow.WriteLine(">:"+ttx)
ElseIf Text.StartsWith(linee, "WRT") Then
ttx= Text.GetSubTextToEnd (linee,5)
If Text.StartsWith (ttx,"|") then
vn=text.GetSubText(ttx,2,2)
ttx=LDCall.Function("findvar",vn)
Endif
GraphicsWindow.DrawText(Turtle.x, Turtle.y, ttx)
elseif Text.StartsWith(linee, "F") Then
distance = Text.GetSubTextToEnd(linee, 3)
If Text.StartsWith(distance,"|") Then
rr=LDCall.Function("findvar",vn)
dist=mem[rr][1]
Else
dist=distance
endif
Turtle.Move(math.Floor (dist*sc))
'GraphicsWindow.Title="Dst:"+math.Round ( dist*sc)
if dbg then
TextWindow.WriteLine(math.Floor (dist*sc))
endif

Elseif Text.StartsWith(linee, "S") Then
If Text.GetSubText(linee,2,1)="/" then
sc=sc/ Text.GetSubTextToEnd(linee, 3)
else
sc=sc* Text.GetSubTextToEnd(linee, 3)
endif
If sc=0 then
sc=1
EndIf
ElseIf Text.StartsWith(linee, "$") Then
tx=Turtle.x
ty=Turtle.Y
ta= Turtle.Angle
ts=sc

ElseIf Text.StartsWith(linee, "@") Then ' subbs
scc=Text.GetSubTextToEnd (linee,2)
Sst= sbb[scc]["Stt"]
sen=sbb[scc]["End"]
Stack.PushValue("locali", i)
Stack.PushValue("localj", j)

For i = sSt To sEn
linee=line[i]

_inn=_inn+1
DoLine()
_inn=_inn-1
Endfor
i = Stack.PopValue("locali")+1
j=Stack.PopValue("localj")
ElseIf Text.StartsWith(linee, "&") Then
Turtle.x=tx
Turtle.Y=ty
Turtle.Angle=ta
sc=ts
ElseIf Text.StartsWith(linee, "#F") Then
ff=Text.GetSubTextToEnd(linee, 4)
fn=LDText.Split(ff,";")

GraphicsWindow.FontName=fn[1]
GraphicsWindow.FontBold=_tfl[fn[2]]
GraphicsWindow.FontItalic=_tfl[fn[3]]
GraphicsWindow.FontSize=fn[4]
ElseIf Text.StartsWith(linee, "#P") Then
GraphicsWindow.BrushColor=GraphicsWindow.PenColor
ElseIf Text.StartsWith(linee, "#C") Then
ww = Text.GetSubTextToEnd(linee, 4)
GraphicsWindow.BrushColor=GraphicsWindow.PenColor
If ww=0 then
ww=5
endif

GraphicsWindow.FillEllipse(Turtle.x-ww, Turtle.y-ww,ww*2,ww*2)
ElseIf Text.StartsWith(linee, "`C") Then
ww = Text.GetSubTextToEnd(linee, 3)
GraphicsWindow.BrushColor=ww
ElseIf Text.StartsWith(linee, "~N") Then
scnt=0
vcnt=0
matr=0
mem=0
ch=0
_l=1
sc=1
_sbc=0
count = 0
iStart =0
iEnd= 0
nest = 0


ElseIf Text.StartsWith(linee, "~H") Then

Turtle.x=600
Turtle.Y=400
Turtle.Angle=0

ElseIf Text.StartsWith(linee, "P") Then
GraphicsWindow.PenColor=GraphicsWindow.BackgroundColor
GraphicsWindow.PenWidth=GraphicsWindow.PenWidth+2

distance = Text.GetSubTextToEnd(linee, 3)
If Text.StartsWith(distance,"|") Then
rr=LDCall.Function("findvar",vn)
dist=mem[rr][1]
Else
dist=distance
endif
Turtle.Move(math.Floor (dist*sc))
GraphicsWindow.PenWidth=GraphicsWindow.PenWidth-2

ElseIf Text.StartsWith(linee, "~FN") Then
GraphicsWindow.FontName=Text.GetSubTextToEnd(Linee,5)
ElseIf Text.StartsWith(linee, "~FS") Then
GraphicsWindow.FontSize=Text.GetSubTextToEnd(Linee,5)
ElseIf Text.StartsWith(linee, "~C") Then
ccl=GraphicsWindow.BrushColor
GraphicsWindow.BrushColor =GraphicsWindow.BackgroundColor
GraphicsWindow.FillRectangle (220,200,800,400)
GraphicsWindow.BrushColor=ccl

ElseIf Text.StartsWith(linee, "~T") Then

mem[1][1]=Clock.Hour
mem[1][0]="_H"
mem[2][1]=Clock.Minute
mem[2][0]="_M
mem[3][1]=Clock.Second
mem[3][0]="_S
mem[4][1]=Clock.Date
mem[4][0]="_D"


ElseIf Text.StartsWith(linee, "~") Then
Program.Delay (Text.GetSubTextToEnd(linee, 2))

ElseIf Text.StartsWith(linee, "U") Then
Turtle.PenUp()
ElseIf Text.StartsWith(linee, "D") Then

Turtle.PenDown()
ElseIf Text.StartsWith(linee, "R") Then
distance = Text.GetSubTextToEnd(linee, 3)
If Text.StartsWith(distance,"|") Then
rr=LDCall.Function("findvar",vn)
dist=mem[rr][1]
Else
dist=distance
endif
Turtle.Turn(dist)
ElseIf Text.StartsWith(linee, "L") Then

distance = -Text.GetSubTextToEnd(linee, 3)

If Text.StartsWith(distance,"|") Then
rr=LDCall.Function("findvar",vn)
dist=mem[rr][1]
Else
dist=distance
endif
Turtle.Turn(dist)
ElseIf Text.StartsWith(linee, "A") Then

distance = -Text.GetSubTextToEnd(linee, 3)

If Text.StartsWith(distance,"|") Then
rr=LDCall.Function("findvar",vn)
dist=mem[rr][1]
Else
dist=distance
endif
Turtle.Angle=dist

ElseIf Text.StartsWith(linee, ":$") Then
icc=Text.GetSubTextToEnd (linee, 3)
i=i+icc
ElseIf Text.StartsWith(linee, "IF") Then
icc=Text.GetSubTextToEnd (linee, 4)
vz=text.GetIndexOf(icc,";")
sk=text.GetIndexOf(icc," ")
v1=text.GetSubText (icc,2,vz-2)
v2=LDText.Trim(text.GetSubTexttoend (icc,vz+1))
ss=text.GetIndexOf(v2," ")

'v2=ESLText.Remove (v2,ss-1,text.GetLength(v2)-ss+1)
v2=Text.GetSubText(v2,1,ss-1)
If sk=0 then
skp=2
Else
skp=text.GetSubTextToEnd(icc,sk+1)
endif
cond=0
TextWindow.WriteLine ("Sk:"+Skp)

If Text.StartsWith(icc,"=") then
If v1=v2 then
cond=1
Else
cond=2
endif

elseIf Text.StartsWith(icc,">") then
If v1 cond=1
Else
cond=2
endif
elseIf Text.StartsWith(icc,"<") then
If v1>v2 then
cond=1
Else
cond=2
endif
elseIf Text.StartsWith(icc,"!") then
If v1<>v2 then
cond=1
Else
cond=2
endif

endif

If cond=0 then
TextWindow.WriteLine ("IF invalid cond error!")
else
if cond=1 then 'true part
TextWindow.WriteLine ("TRue")
Else 'false part
TextWindow.WriteLine ("Flss")

i=i+skp
endif
endif

ElseIf Text.StartsWith(linee, "{") Then
count[_l] = Text.GetSubTextToEnd (linee, 2)
iStart[_l] = i + 1
iEnd[_l] = nLines
nest = 0

tree=LDControls.TreeViewGetData(trr)
tnd=Array.GetAllIndices(tree)
nc=array.GetItemCount (tnd)+1


For k = iStart[_l] To nLines
'TextWindow.WriteLine(">>"+k)
If Text.StartsWith(line[k], "{") Then
nest = nest + 1
ElseIf Text.StartsWith(line[k], "]") or Text.StartsWith(line[k], "}") Then
If nest = 0 Then
iEnd[_l] = k - 1
k= nLines
Else
nest = nest - 1
EndIf
EndIf
EndFor


If dbg then

typ="Lp"
tree[nc][13]=typ+":"+nest+">"+iStart[_l]+" to "+iEnd[_l]


LDControls.TreeViewContent(trr,tree)
nset()
endif

j = count[_l]
_l = _l + 1
While j>0
Stack.PushValue("local", j)
_inn=_inn+1
For i = iStart[_l - 1] To iEnd[_l - 1]
linee=line[i]
If Text.IsSubText (linee,"%$$") Then
linee=LDText.Replace(linee,"%$$",j)
elseIf Text.IsSubText (linee,"%$A") Then
linee=LDText.Replace(linee,"%$A",Turtle.Angle)
elseIf Text.IsSubText (linee,"%$C") Then
linee=LDText.Replace(linee,"%$C",GraphicsWindow.PenColor)

elseIf Text.IsSubText (linee,"%$") Then
linee=LDText.Replace(linee,"%$","%"+j)
endif
If dbg then
TextWindow.WriteLine (i+">>"+linee)
endif

DoLine()
EndFor
_inn=_inn-1
j = Stack.PopValue("local")-1
Endwhile

_l = _l - 1
i = iEnd[_l] + 2
EndIf
exx:
EndSub

Sub dorepl
If Text.IsSubText (linee,"%") Then
aq="!!
If Text.IsSubText(linee,aq) Then
m2=Text.Append("0", mem[2][1])
m2=Text.GetSubTextToEnd (m2,text.GetLength( mem[2][1]))
m3=Text.Append("0", mem[3][1])
m3=Text.GetSubTextToEnd (m3,text.GetLength( mem[3][1]))
endif
If Text.IsSubText (linee,"%1") Then
linee=LDText.Replace(linee,"%1",mem[1][1])
elseIf Text.IsSubText (linee,"%2"+aq) Then
linee=LDText.Replace(linee,"%2"+aq,m2)
elseIf Text.IsSubText (linee,"%3"+aq) Then
linee=LDText.Replace(linee,"%3"+aq,m3)
elseIf Text.IsSubText (linee,"%2") Then
linee=LDText.Replace(linee,"%2",mem[2][1])
elseIf Text.IsSubText (linee,"%3") Then
linee=LDText.Replace(linee,"%3",mem[3][1])

elseIf Text.IsSubText (linee,"%4") Then
linee=LDText.Replace(linee,"%4",mem[4][1])
elseIf Text.IsSubText (linee,"%5") Then
linee=LDText.Replace(linee,"%5",mem[5][1])
elseIf Text.IsSubText (linee,"%6") Then
linee=LDText.Replace(linee,"%6",mem[6][1])
elseIf Text.IsSubText (linee,"%7") Then
linee=LDText.Replace(linee,"%7",mem[7][1])
elseIf Text.IsSubText (linee,"%8") Then
linee=LDText.Replace(linee,"%8",mem[8][1])
elseIf Text.IsSubText (linee,"%9") Then
linee=LDText.Replace(linee,"%9",mem[9][1])
elseIf Text.IsSubText (linee,"%A") Then
linee=LDText.Replace(linee,"%A",mem[10][1])
elseIf Text.IsSubText (linee,"%B") Then
linee=LDText.Replace(linee,"%B",mem[11][1])
elseIf Text.IsSubText (linee,"%C") Then
linee=LDText.Replace(linee,"%C",mem[12][1])
elseIf Text.IsSubText (linee,"%D") Then
linee=LDText.Replace(linee,"%D",mem[13][1])
elseIf Text.IsSubText (linee,"%E") Then
linee=LDText.Replace(linee,"%E",mem[14][1])
elseIf Text.IsSubText (linee,"%F") Then
linee=LDText.Replace(linee,"%F",mem[15][1])
elseIf Text.IsSubText (linee,"%M") Then
mm=text.GetSubTextToEnd(linee,text.GetIndexOf(linee,".")+1)
linee=LDText.Replace(linee,"%M."+mm,matr[MathPlus.ToNumber(mm)])

endif

endif
endsub

Sub fWriteLn
TextWindow.WriteLine(idd+args[1])
EndSub

Sub xpline '**************************************xplains prg....*********************

If Text.StartsWith(linee, "|") Then
vn=text.GetSubText(linee,2,2)
vv=text.GetSubTextToEnd (linee,5)
dfc=text.GetSubText(linee,4,1)


If dfc="=" then
LDCall.Function("fWriteLn","Define New VAR "+vn+" and assign value:"+vv)
vcnt=vcnt+1
mem[vcnt][0]=vn

elseif dfc="+" then
LDCall.Function("fWriteLn","Increase VAR "+vn+" by:"+vv)
elseif dfc="~" then
LDCall.Function("fWriteLn","Find VAR "+vn+" and assign value:"+vv)
elseif dfc="*" then
LDCall.Function("fWriteLn","Multiply VAR "+vn+" by:"+vv)
elseif dfc="/" then
LDCall.Function("fWriteLn","Divide VAR "+vn+" by:"+vv)
endif
endif
If Text.StartsWith(linee, "W#") Then 'console debug writting

ttx= Text.GetSubTextToEnd (linee,3)

LDCall.Function("fWriteLn","Write to console:"+ttx)
ElseIf Text.StartsWith(linee, "%") then
LDCall.Function("fWriteLn","Perform "+Text.GetSubTextToEnd (linee,4))
ElseIf Text.StartsWith(linee, "WRT") Then 'turtle writting
ttx= Text.GetSubTextToEnd (linee,5)
LDCall.Function("fWriteLn","Write to screen:"+ttx)
ElseIf Text.StartsWith(linee, "Ht") Then
LDCall.Function("fWriteLn","Reset to Home pos.")
elseif Text.StartsWith(linee, "F") Then

dist = Text.GetSubTextToEnd(linee, 3)

If Text.StartsWith(dist,"%") then
dist = "var value:"+mem[LDMath.Base2Decimal(Text.GetSubTextToEnd(dist, 2),16)][0]
endif
LDCall.Function("fWriteLn","Forward by:"+ dist)
Elseif Text.StartsWith(linee, "<") Then
ttx= Text.GetSubTextToEnd (linee,2)
LDCall.Function("fWriteLn"," idd=idd+" "
Elseif Text.StartsWith(linee, ">") Then
idd=text.GetSubTextToEnd(idd,3)
LDCall.Function("fWriteLn",">END SUB.")
Elseif Text.StartsWith(linee, "]") Then
idd=text.GetSubTextToEnd(idd,4)
LDCall.Function("fWriteLn","] END Repeat block")
Elseif Text.StartsWith(linee, "}") Then
idd=text.GetSubTextToEnd(idd,4)
LDCall.Function("fWriteLn","} END block")

Elseif Text.StartsWith(linee, "S") Then
If sc=0 then
sc=1
LDCall.Function("fWriteLn","Set scale to 1")
else
If Text.GetSubText(linee,2,1)="%" then
LDCall.Function("fWriteLn","Alter scale <(kind +/*) selected by VAR:"+mem[LDMath.Base2Decimal(Text.GetSubText(linee, 3,1),16)][0]+"> by factor:"+Text.GetSubTextToEnd(linee, 5))
elseIf Text.GetSubText(linee,2,1)="/" then
LDCall.Function("fWriteLn","Divide scale by:"+Text.GetSubTextToEnd(linee, 3))
else
LDCall.Function("fWriteLn","Multiply scale by:"+Text.GetSubTextToEnd(linee, 3))
endif

EndIf
ElseIf Text.StartsWith(linee, "$") Then
LDCall.Function("fWriteLn","Save turtle pos.")

ElseIf Text.StartsWith(linee, "@") Then ' subbs
scc=Text.GetSubTextToEnd (linee,2)
LDCall.Function("fWriteLn","Call sub:"+sbb[scc][0])
ElseIf Text.StartsWith(linee, "&") Then
LDCall.Function("fWriteLn","Restore turtle pos.")
ElseIf Text.StartsWith(linee, "U") Then
LDCall.Function("fWriteLn","Pen UP - moving")
ElseIf Text.StartsWith(linee, "D") Then
LDCall.Function("fWriteLn","Pen DN - drawing")
ElseIf Text.StartsWith(linee, "R") Then
dist = Text.GetSubTextToEnd(linee, 3)
LDCall.Function("fWriteLn","Turn RIGHT by:"+dist)
ElseIf Text.StartsWith(linee, "L") Then
dist = Text.GetSubTextToEnd(linee, 3)
LDCall.Function("fWriteLn","Turn LEFT by:"+dist)


ElseIf Text.StartsWith(linee, "{") Then
LDCall.Function("fWriteLn","{Repeat block times:"+Text.GetSubTextToEnd(linee,2))
idd=idd+" "
EndIf
EndSub'---------------------------------xplain**********************



Sub Init
gw = 1000
gh = 800
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
GraphicsWindow.Title = "Turtle Graphics
GraphicsWindow.BackgroundColor="#bbccdd
GraphicsWindow.Top=0
GraphicsWindow.Left=0
Turtle.Speed=10
CRLF = Text.GetCharacter(13)+Text.GetCharacter(10)
src=LDText.Replace ( deff[8],"!",CRLF )
EndSub

Sub Opening

GraphicsWindow.BrushColor = "Black"
GraphicsWindow.FontName = "Tahoma"
GraphicsWindow.FontSize = 15
GraphicsWindow.PenColor="Black
GraphicsWindow.DrawText(310, 30, "S)cale*ff | S/ =S/ff")
GraphicsWindow.DrawText(310, 70, "F distance")
GraphicsWindow.DrawText(310, 110, "L|R angle")
GraphicsWindow.DrawText(310, 150, "D)wn | U)p")
GraphicsWindow.DrawText(310, 190, "rpt:{count [cmds]")
GraphicsWindow.DrawText(310, 220, "$)save pos.")
GraphicsWindow.DrawText(310, 240, "&)load pos.")


EndSub

Sub Math_ATan2
' param y
' param x
' return a - angle [radian]
If x = 0 And 0 < y Then
a = Math.Pi / 2 ' [radian]
ElseIf x = 0 And y < 0 Then
a = -Math.Pi / 2
Else
a = Math.ArcTan(y / x)
EndIf
If x < 0 Then
a = a + Math.Pi
ElseIf 0 < x And y < 0 Then
a = a + Math.Pi * 2
EndIf
EndSub