Showing posts with label 2012. Show all posts
Showing posts with label 2012. Show all posts

Tuesday, August 7, 2012

Small Basic Auto Indent (with Subroutine lister)

'Indent - LDH635
'Harry Hardjono
'August 2012
'
CSD=0
indentlevel=0
indenttext="  "
indentarray=""
indentPlus=" sub for while if else elseif "
indentMin=" endsub endfor endwhile endif else elseif "


Filename=Program.Directory+"\indent1.sb"
FileSlurp()
For i=1 To Array.GetItemCount(FileData)
  SkipLeadSpace()
  CatalogSub()
  DoIndent()
  'TextWindow.WriteLine(OutData[i])
endfor


TextWindow.WriteLine("Sub List:")
For i=1 To Array.GetItemCount(SubData)
  TextWindow.Write(SubData[i])
  TextWindow.WriteLine("() ")
EndFor


Sub DoIndent
  StrIn=Text.ConvertToLowerCase(OutData[i])
  SpcInx=text.GetIndexOf(StrIn," ")
  If SpcInx=0 then
    SpcInx=Text.GetLength(StrIn)
  endif 
  StrIn=Text.GetSubText(OutData[i],1,SpcInx)
  StrIn=Text.ConvertToLowerCase(StrIn)
  If (text.GetLength(StrIn) > 1 And Text.GetIndexOf(indentMin,text.Append(" ",StrIn))>0) then
    indentlevel=indentlevel-1
    If indentlevel < 0 then
      indentlevel=0
    endif
    If indentlevel=0 then 
      indentarray[0]=""
    else
      indentarray[indentlevel]=text.Append(indentarray[indentlevel-1],indenttext)
    endif
    'TextWindow.WriteLine("DoIndentMin: "+StrIn)
  endif
  
  TextWindow.WriteLine(indentarray[indentlevel] + OutData[i])
  
  
  If (Text.GetIndexOf(indentPlus,StrIn) > 0) then
    indentlevel=indentlevel+1
    If indentlevel=0 then 
      indentarray[0]=""
    else
      indentarray[indentlevel]=text.Append(indentarray[indentlevel-1],indenttext)
    endif
    'TextWindow.WriteLine("DoIndentPlus: "+StrIn)
  endif
endsub




Sub CatalogSub
  StrIn=Text.ConvertToLowerCase(OutData[i])
  if text.StartsWith(StrIn,"sub ") then
    StrIn=Text.GetSubTextToEnd(StrIn,5)
    SpcInx=text.GetIndexOf(StrIn," ")
    If SpcInx=0 then
      SpcInx=Text.GetLength(StrIn)
    endif 
    StrOut=Text.GetSubText(OutData[i],1,SpcInx+4)
    
    CSD=CSD+1
    SubData[CSD]=StrOut
  endif
endsub


Sub SkipLeadSpace
  StrIn=FileData[i]
  StrOut=""
  For SLS_i=1 to Text.GetLength(StrIn)
    If (Text.GetCharacterCode(Text.GetSubText(StrIn,SLS_i,1))>32) then 'Non-space
      if (StrOut="") then  
        StrOut=Text.GetSubTextToEnd(StrIn,SLS_i)
      EndIf
    EndIf
  endfor  
  OutData[i]=StrOut
endsub




Sub FileSlurp
  'Read a file and assign it to an array
  'Input Filename (string)
  'Output FileData (array)
  FileLength=Text.GetLength(File.ReadContents(Filename))
  FileData=""
  FL=0
  FS_i=1
  While FL
    FileData[FS_i]=File.ReadLine(Filename,FS_i)
    FL=FL+Text.GetLength(FileData[FS_i])+2
    If FileData[FS_i]="" Then 'Fudge for blank lines in file
      FileData[FS_i]=" "
    EndIf
    FS_i=FS_i+1
  Endwhile
EndSub


Tuesday, June 19, 2012

Small Basic Cube Root


'Cuberoot challenge - MLQ436-0
'Harry Hardjono
'June 2012
'Just a 10 minute quickie

Loop:
TextWindow.Write("Enter a number (1-1000): ")
N=TextWindow.ReadNumber()

If N=0 Then
  Program.End()
EndIf

D=1000 'Delta. Decreasing range in calcloop.
S=1 'Starting number. To be modified in calcloop.
E=0.00000000001 ' Epsilon. Desired accuracy.

calcloop:
While (D>E)
  C=(S+D)*(S+D)*(S+D)
  If C<=N Then
    S=S+D
  Else 'Not comfortable skipping else when D isn't a power of two.
    D=D/2
  EndIf
EndWhile

TextWindow.Write("Cube root is: ")
TextWindow.WriteLine(S)
TextWindow.WriteLine(" ")
Goto Loop

Tuesday, June 12, 2012

Small Basic Spell Checker


'Spell checker example - wxv722-1
'Uses Dictionary.GetDefinition(word) - Need internet access
'Harry Hardjono
'June 2012
'
Init:
Str="abcdefghijklmnopqrstuvwxyz'ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Word=""


Loop:
TextWindow.BackgroundColor="black"
TextWindow.ForegroundColor="white"
TextWindow.WriteLine("  ")
TextWindow.WriteLine("Enter your sentence: ")
inputtext=text.Append(TextWindow.Read()," ") 'bug fix. ^_^;

If (inputtext=" ") Then
  Goto End
EndIf


For i=1 To Text.GetLength(inputtext)
  curchar=Text.GetSubText(inputtext,i,1)
  If (Text.GetIndexOf(Str,curchar)=0) Then
    'Not word
    If (Word<>"") Then
      WordDef=Dictionary.GetDefinition(Word)
      If (WordDef="") Then
      TextWindow.BackgroundColor="white"
      TextWindow.ForegroundColor="black"
      TextWindow.Write(Word)
      TextWindow.BackgroundColor="black"
      TextWindow.ForegroundColor="white"
      Else
      TextWindow.Write(Word)
      EndIf
      Word=""
    EndIf
    TextWindow.Write(curchar)
  Else 'Word
    Word=Text.Append(Word,curchar)
  EndIf
endfor

Goto Loop

End:
Program.End()