Microsoft Small Basic

Program Listing: WQN588
' Remove Comments
' Copyright © 2014 Nonki Takahashi. The MIT License.
' Usage: Filename? input.sb [>output.sb]
' Last update 2014-12-20
' Version (pilot)
'
Not = "False=True;True=False;"
CRLF = Text.GetCharacter(13) + Text.GetCharacter(10)
WQ = Text.GetCharacter(34)
TextWindow.Write("Filename? ")
buf = TextWindow.Read()
ParseArgs()
If args[1] <> "" Then
args["stdin"] = args[1]
EndIf
eof = "False"
atFirst = "True"
nLine = 0
nChar = 0
If args["stdin"] <> "" Then
max = 0 ' dummy for remote
' The following line could be harmful and has been automatically commented.
' max = Text.GetLength(File.ReadContents(args["stdin"]))
EndIf
While Not[eof]
ReadLine()
If Not[eof] Then
TranslateLine()
WriteLine()
EndIf
EndWhile
Sub ReadLine
' return line - text of a line
' return eof - "True" if end of file
' return nLine - number of line
If args["stdin"] = "" Then
nLine = nLine + 1
line = TextWindow.Read()
If line = "" Then
eof = "True"
EndIf
Else
If nChar < max Then
nLine = nLine + 1
' The following line could be harmful and has been automatically commented.
' line = File.ReadLine(args["stdin"], nLine)
nChar = nChar + Text.GetLength(line) + 2 ' 2 for CR and LF
Else
eof = "True"
EndIf
EndIf
EndSub
Sub TranslateLine
If 5 < nLine Then
SkipLeadingSpace()
While Text.GetSubText(line, p, 1) = "'"
ReadLine()
SkipLeadingSpace()
EndWhile
EndIf
EndSub
Sub SkipLeadingSpace
' param line - text of a line
' return p - pointer to non space character in line
p = 1
While Text.GetSubText(line, p, 1) = " "
p = p + 1
EndWhile
EndSub
Sub WriteLine
' param line - text of a line
If args["stdout"] = "" Then
TextWindow.WriteLine(line)
Else
If atFirst Then
atFirst = "False"
' The following line could be harmful and has been automatically commented.
' File.WriteContents(args["stdout"], line + CRLF)
Else
' The following line could be harmful and has been automatically commented.
' File.AppendContents(args["stdout"], line)
EndIf
EndIf
EndSub
Sub ParseArgs
' param buf - command line
' return args[] - array of arguments
p = 1
l = Text.GetLength(buf)
n = 1
While p <= l
SkipSpace()
GetToken()
EndWhile
EndSub
Sub SkipSpace
' param buf - command line
' param l - length of buf
' param p - pointer in buf
' return p - pointer to not space in buf
isSpace = "True"
While p <= l And isSpace
c = Text.GetSubText(buf, p, 1)
If c = " " Then
p = p + 1
Else
isSpace = "False"
EndIf
EndWhile
EndSub
Sub GetToken
' param buf - command line
' param l - length of buf
' param p - pointer in buf
' return args[] - array of arguments
' return p - pointer to not space in buf
isDelim = "False"
s = p ' p at start
n = Array.GetItemCount(args)
While p <= l And Not[isDelim]
c = Text.GetSubText(buf, p, 1)
If c = " " Then
isDelim = "True"
ElseIf c = "<" Or c = ">" Then
isDelim = "True"
d = c ' save delimiter
If p = s Then
p = p + 1
SkipSpace()
GetToken()
If d = "<" Then
args["stdin"] = args[n + 1]
ElseIf d = ">" Then
args["stdout"] = args[n + 1]
EndIf
args[n + 1] = ""
EndIf
ElseIf c = WQ Then
p2 = Text.GetIndexOf(Text.GetSubTextToEnd(buf, p + 1), WQ)
If 0 < p2 Then
args[n + 1] = Text.Append(args[n + 1], Text.GetSubText(buf, p + 1, p2 - 1))
p = p + p2 + 1
Else
TextWindow.WriteLine("Error: Missing quote")
EndIf
Else
args[n + 1] = Text.Append(args[n + 1], c)
p = p + 1
EndIf
EndWhile
EndSub