Microsoft Small Basic

Program Listing: XMG163-1
' tr - translate command 0.21
' Copyright (c) 2014 Nonki Takahashi. All rights reserved.
'
' History:
' 0.21 2014-02-19 Minor changed for Silverlight. (XMG163-1)
' 0.2 2014-02-19 Completed. (XMG163-0)
' 0.1a 2014-02-18 Created as alpha version. (XMG163)
'
Not = "False=True;True=False;"
WQ = Text.GetCharacter(34)
TextWindow.Write("Option? ")
buf = TextWindow.Read()
ParseArgs()
string[1] = args[1]
string[2] = args[2]
ExpandStr()
eof = "False"
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()
TranslateLine()
WriteLine()
EndWhile
Sub ReadLine
' return line - text of a line
' return eof - "True" if end of file
If args["stdin"] = "" Then
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
len = Text.GetLength(line)
tr = ""
For p = 1 To len
c = Text.GetSubText(line, p, 1)
i = Text.GetIndexOf(string[1], c)
If 0 < i Then ' found
tr = tr + Text.GetSubText(string[2], i, 1)
Else
tr = tr + c
EndIf
EndFor
line = tr
EndSub
Sub WriteLine
' param line - text of a line
If args["stdout"] = "" Then
TextWindow.WriteLine(line)
Else
' The following line could be harmful and has been automatically commented.
' File.WriteLine(args["stdout"], nLine, line)
EndIf
EndSub
Sub ExpandStr
For i = 1 To 2
p = Text.GetIndexOf(string[i], "-")
While 0 < p
c1 = Text.GetCharacterCode(Text.GetSubText(string[i], p - 1, 1))
c2 = Text.GetCharacterCode(Text.GetSubText(string[i], p + 1, 1))
s1 = Text.GetSubText(string[i], 1, p - 2)
s2 = Text.GetSubTextToEnd(string[i], p + 2)
string[i] = s1
If c2 < c1 Then
c3 = c1
c1 = c2
c2 = c3
EndIf
For code = c1 To c2
string[i] = Text.Append(string[i], Text.GetCharacter(code))
EndFor
string[i] = Text.Append(string[i], s2)
p = Text.GetIndexOf(string[i], "-")
EndWhile
EndFor
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 Dump
TextWindow.WriteLine("p=" + p)
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