Microsoft Small Basic

Program Listing: XDS207
Initialize()
While "True"
Repeat = 1
TextWindow.Write("logo>")
Input = TextWindow.Read()
If Text.StartsWith(Text.ConvertToLowerCase(Input), "repeat") Then
Repeat()
EndIf
If Text.GetLength(Input) <> 0 Then
For i = 1 to Repeat
_CharactersToParse = Input
Parse()
ExcuteLineOfInput()
EndFor
EndIf
EndWhile
Program.End()

Sub Initialize
GraphicsWindow.Width=800
GraphicsWindow.Height=600
GraphicsWindow.BackgroundColor = "LightGray"
GraphicsWindow.Title = "Logo - by Joseph Fischetti"
GraphicsWindow.Clear()
GraphicsWindow.Show()
GraphicsWindow.PenColor="black"
Turtle.Speed = 10
Turtle.X = GraphicsWindow.Width/2
Turtle.Y = GraphicsWindow.Height/2
TextWindow.Show()
EndSub

Sub Repeat
Repeat = 0
PosL = Text.GetIndexOf(Input, "[")
PosR = Text.GetIndexOf(Input, "]")
If PosL > 0 then
If PosR > PosL then
_CharactersToParse = Text.GetSubText(Input,7,PosL-7)
Input = Text.GetSubText(Input,PosL+1,PosR-PosL-1)
Parse()
t = Stack.PopValue(_ParseStack)
If Stack.GetCount(_ParseStack) = 0 then
If t > 0 then
Repeat = t
EndIf
Else
For i = 1 To Stack.GetCount(_ParseStack)
t = Stack.PopValue(_ParseStack)
EndFor
EndIf
Endif
EndIf
If Repeat = 0 then
TextWindow.WriteLine("ERROR")
EndIf
EndSub


Sub ExcuteLineOfInput
While Stack.GetCount(_ParseStack) > 0
_Arg1 = Text.ConvertToLowerCase(Stack.PopValue(_ParseStack))
If _Arg1 = "st" then
Turtle.Show()
ElseIf _Arg1 = "ht" then
Turtle.Hide()
ElseIf _Arg1 = "cs" then
GraphicsWindow.Clear()
Turtle.X = GraphicsWindow.Width/2
Turtle.Y = GraphicsWindow.Height/2
Turtle.Angle=0
ElseIf _Arg1 = "pu" then
Turtle.PenUp()
ElseIf _Arg1 = "pd" then
Turtle.PenDown()
ElseIf _Arg1 = "fd" then
Get2Arg()
If _Arg2<> "" then
Turtle.Move(_Arg2)
EndIf
ElseIf _Arg1 = "bk" then
Get2Arg()
If _Arg2<> "" then
Turtle.Move(-_Arg2)
EndIf
ElseIf _Arg1 = "rt" then
Get2Arg()
If _Arg2<> "" then
Turtle.Turn(_Arg2)
EndIf
ElseIf _Arg1 = "lt" then
Get2Arg()
If _Arg2<> "" then
Turtle.Turn(-_Arg2)
EndIf
Else
TextWindow.WriteLine("ERROR")
EndIf
EndWhile
EndSub

Sub Get2Arg
_Arg2 = ""
If Stack.GetCount(_ParseStack)>0 Then
_Arg2 = Stack.PopValue(_ParseStack)
If _Arg2 > 0 then
Else
_Arg2 = ""
TextWindow.WriteLine("ERROR")
EndIf
Else
TextWindow.WriteLine("ERROR")
EndIf
EndSub

Sub Parse
_CharactersToStack = _CharactersToParse
_CharacterStack = "Characters""
PushCharactersOnStack()
_CharacterCount = Stack.GetCount(_CharacterStack)
_ParseStack = "BackwardArguments"
_Arg = ""
For _i = 1 To _CharacterCount
_Char = Stack.PopValue(_CharacterStack)
If _Char <> " " Then
_Arg = Text.Append(_Char, _Arg)
ElseIf _Char = " " Then
If Text.GetLength(_Arg)>0 then
Stack.PushValue(_ParseStack,_Arg)
_Arg = ""
EndIf
ElseIf _i = _CharacterCount Then
If Text.GetLength(_Arg)>0 then
Stack.PushValue(_ParseStack,_Arg)
EndIf
EndIf
EndFor
If _Char <> " " then
Stack.PushValue(_ParseStack,_Arg)
EndIf
EndSub

Sub PushCharactersOnStack
_Len = Text.GetLength(_CharactersToStack)
For _i = 1 to _Len
Stack.PushValue(_CharacterStack, Text.GetSubText(_CharactersToStack,_i,1))
EndFor
EndSub