Microsoft Small Basic

Program Listing: VFD117
'Matts Programming Language to SmallBasic Code Interpreter
'By DaCodeDude, December 2009

'Program Variables
filePath = "C:\mpl\" ' set this to the path from which you want to load the file (or leave blank and specify full path when entering filename)
dQuotes = Text.GetCharacter(34) 'Code for the " character

'StartUp Window Code
TextWindow.Title = "MPL to SmallBasic Code Interpreter"
TextWindow.WriteLine("MPL to SmallBasic Code Interpreter by DaCodeDude")
TextWindow.WriteLine("Built on some code from: 'SB Textfile Editor by Davey~Wavey 2009'")
TextWindow.WriteLine("")

'Main Code-----------------------------------------------------------------------------------------
TextWindow.Write("Enter name of file to load: " + filePath)
fileName = TextWindow.Read()
' only attempt to open file if a filename was specified
If fileName <> "" Then
openFile()
EndIf

TextWindow.WriteLine("Interpreting code...")

'Interpreter Counting Variables
lineCount = 1
lineCount2 = 0
lineCount3 = 1
numLines = Array.GetItemCount(thisFile)

While numLines <> 0
'Window Commands
If (thisFile[lineCount] = "windowsize") Then 'Edit size of window, Graphics window only and uses given hight and widths in pixels
lineCount2 = lineCount + 1
newFile[lineCount3] = "GraphicsWindow.Height = " + thisFile[lineCount2]
lineCount3 = lineCount3 + 1
lineCount = lineCount + 1
newFile[lineCount3] = "GraphicsWindow.Width = " + thisFile[lineCount2]
lineCount = lineCount + 1
numLines = numLines - 3
ElseIf (thisFile[lineCount] = "windowname") Then 'Edit window name 1=text window and 2=graphics window and uses given name
lineCount2 = lineCount + 1
If (thisFile[lineCount2] = "1") Then
lineCount2 = lineCount2 + 1
newFile[lineCount3] = "TextWindow.Title = " + dQuotes + thisFile[lineCount2] + dQuotes
ElseIf (thisFile[lineCount2] = "2") Then
lineCount2 = lineCount2 + 1
newFile[lineCount3] = "GraphicsWindow.Title = " + dQuotes + thisFile[lineCount2] + dQuotes
EndIf
lineCount = lineCount + 2
numLines = numLines - 3
ElseIf (thisFile[lineCount] = "backcolor") Then 'Edit window background color 1=text window and 2=graphics window and uses given color
lineCount2 = lineCount + 1
If (thisFile[lineCount2] = "1") Then
lineCount2 = lineCount2 + 1
newFile[lineCount3] = "TextWindow.BackgroundColor = " + dQuotes + thisFile[lineCount2] + dQuotes
ElseIf (thisFile[lineCount2] = "2") Then
lineCount2 = lineCount2 + 1
newFile[lineCount3] = "GraphicsWindow.BackgroundColor = " + thisFile[lineCount2]
EndIf
lineCount = lineCount + 2
numLines = numLines - 3
EndIf
'Input/Output Commands
If (thisFile[lineCount] = "var") Then 'Declares a given variable name and gives it a starting value
lineCount2 = lineCount + 1
lineCount4 = lineCount2 + 1
newFile[lineCount3] = thisFile[lineCount2] + " = " + thisFile[lineCount4]
lineCount = lineCount + 2
numLines = numLines - 3
ElseIf (thisFile[lineCount] = "print") Then 'Prints given text/number to the screen, text window only
lineCount2 = lineCount + 1
newFile[lineCount3] = "TextWindow.WriteLine(" + dQuotes + thisFile[lineCount2] + dQuotes + ")"
lineCount = lineCount + 1
numLines = numLines - 2
ElseIf (thisFile[lineCount] = "printvar") Then 'Prints a given variable to the screen, text window only
lineCount2 = lineCount + 1
newFile[lineCount3] = "textWindow.WriteLine(" + thisFile[lineCount2] + ")"
lineCount = lineCount + 1
numLines = numLines - 2
ElseIf (thisFile[lineCount] = "ask") Then 'Displays a curser and accept input then when Enter is pressed return input in named variable
lineCount2 = lineCount + 1
newFile[lineCount3] = thisFile[lineCount2] + " = TextWindow.Read()"
lineCount = lineCount + 1
numLines = numLines - 2
EndIf
'Other Commands
If (thisFile[lineCount] = "sub") Then 'Starts a sub named after given name
lineCount2 = lineCount + 1
newFile[lineCount3] = "Sub " + thisFile[lineCount2]
lineCount = lineCount + 1
numLines = numLines - 2
ElseIf (thisFile[lineCount] = "endsub") Then 'Ends a sub
newFile[lineCount3] = "EndSub"
numLines = numLines - 1
ElseIf (thisFile[lineCount] = "runsub") Then 'Runs given sub
lineCount2 = lineCount + 1
newFile[lineCount3] = thisFile[lineCount2] + "()"
lineCount = lineCount + 1
numLines = numLines - 2
EndIf
lineCount3 = lineCount3 + 1
lineCount2 = 0
lineCount = lineCount + 1
EndWhile

TextWindow.Write("Enter name of file to save as: " + filePath)
fileName = TextWindow.Read()
' only attempt to save file if a filename was specified
If fileName <> "" Then
saveFile()
EndIf

'Sub Codes-------------------------------------------------------------------------
Sub openFile
thisFile = "" ' blank the array to contain file data
fileLength = 0

' read each line from the file into our array
' and keep track of how many lines were read in 'fileLength' variable
' The following line could be harmful and has been automatically commented.
' thisLine = File.ReadLine( filePath + fileName, 1 )

While thisLine <> "Terminate"
fileLength = fileLength + 1
thisFile[fileLength] = thisLine
' The following line could be harmful and has been automatically commented.
' thisLine = File.ReadLine( filePath + fileName, fileLength+1 )
EndWhile
EndSub

Sub saveFile
fileLength = Array.GetItemCount(newFile)
For thisLine = 1 To fileLength
' The following line could be harmful and has been automatically commented.
' File.WriteLine(filePath + fileName, thisLine, newFile[thisLine])
EndFor

TextWindow.WriteLine("")
TextWindow.WriteLine( "FILE WRITTEN TO DISK.")

EndSub