Advertisement
Ham62

compiler.bas

Mar 17th, 2017
524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'Macro for a temp string that's actually a global null string with the header
  2. 'modified to point ot the middle of an existing string.
  3. #define SetConst(_sSrc,_iStart,_iLen) *cptr(fbstr ptr, @sGlobalTemp) = type( strptr(_sSrc)+(_iStart) , _iLen , _iLen )
  4. Dim Shared as String sGlobalTemp
  5. type fbstr
  6.   pzData as zstring ptr
  7.   iLen as integer
  8.   iSize as integer
  9. end type
  10. #if 0
  11. float MoveCount = 0
  12. float MoveNum = 32
  13. Start:
  14.     If MoveCount = MoveNum Goto TurnRobot
  15.  
  16.     Add MoveCount, 1
  17.     BIOS.Move(1)
  18.     GOTO Start
  19.  
  20. TurnRobot:
  21.     BIOS.Turn(1)
  22.     Let MoveCount = 0
  23.     Goto Start
  24. #endif
  25. #include once "windows.bi"
  26. #include once "../include/opcode.bi"
  27. #include once "HashTable.bi"
  28. Declare Function ProcessToken() as integer
  29. Declare Sub EndOfLine()
  30. Dim Shared as String sSrcBuff
  31. Dim as String sSourcefile = "test1.txt" 'Command
  32. dim as integer iFileSz
  33.  
  34. If Open (sSourceFile for binary access read as #1) Then
  35.     Print "Error opening source file!"
  36.     Sleep: System
  37. End If
  38.  
  39. iFileSz = LOF(1)
  40. sSrcBuff = Space(iFileSz)
  41. Get #1,, sSrcBuff
  42. close #1
  43.  
  44. initHashTable
  45.  
  46. enum TokenTypes
  47.   tkUnknown
  48.   tkLabel
  49.   tkVariable
  50.   tkNamespace
  51.   tkNumber
  52. end enum
  53.  
  54. ' State variabes... (if there's multiple threads, must be a UDT)
  55. dim shared as integer iTokenBegin = -1, iTokenEnd = -1, iTokenType = tkUnknown
  56. dim as integer iNumParseMode = 0
  57. dim shared as integer iNumberParse, iDecimalParse
  58. dim shared as integer iNumberDigits, iDecimalDigits
  59. dim shared as uByte iTokenHash
  60.  
  61. color 14
  62. print "iTokenBegin", "iTokenEnd", "difference", "sGlobalTemp"
  63. print String(79, "-")
  64. color 7
  65.  
  66. ' =================================================
  67. ' ========= Parse File based on the state =========
  68. ' =================================================
  69. for iPosi as integer = 0 to iFileSz-1
  70.  
  71.   var iChar = sSrcBuff[iPosi] 'maybe it wont be needed
  72.  
  73.   if iTokenType = tkNumber then 'chars that can end numbers...
  74.     select case as const iChar
  75.     case asc("0") to asc("9"),asc(".")
  76.       rem ok so still a number...
  77.     case 32,9,13,10 'Space/Tab/cr/lf (i guess all operators)
  78.       iTokenType = tkNumber
  79.       if ProcessToken()=0 then exit for
  80.     case else
  81.       'bad number...
  82.     end select
  83.   end if
  84.  
  85.   select case as const iChar
  86.   case 32,9 'Space/Tab
  87.     if iTokenBegin >= 0 orelse iTokenType<>tkUnknown then
  88.       iTokenEnd = iPosi-1: if ProcessToken()=0 then exit for
  89.     end if
  90.     ' ignore blanks
  91.    
  92.   case asc("0") to asc("9")
  93.     if iTokenBegin<0 then
  94.       'not part of a token so must be a number/expression/error
  95.       'if number parsing... then insert next digit into number
  96.       iTokenType = tkNumber
  97.       if iNumParseMode = 0 then 'whole number
  98.         iNumberParse = (iNumberParse*10)+iChar-asc("0")
  99.         iNumberDigits += 1
  100.       else
  101.         iDecimalParse = (iDecimalParse*10)+iChar-asc("0")
  102.         iDecimalDigits += 1
  103.       end if
  104.     else
  105.       iTokenHash += iChar 'add to the hash of the token
  106.     end if      
  107.    
  108.   case asc("A") to asc("Z")
  109.     iChar += (asc("a")-asc("A"))  'lowercase convert
  110.     sSrcBuff[iPosi] = iChar  'and update on string?
  111.     if iTokenBegin<0 then 'wasnt reading a token
  112.       iTokenBegin = iPosi 'so now token start here
  113.     end if
  114.     iTokenHash += iChar 'add to the hash of the token
  115.    
  116.   case asc("a") to asc("z"), asc("_")
  117.     iTokenHash += iChar 'add to the hash of the token
  118.    
  119.   case 13                    'possible newline?    
  120.     iTokenEnd = iPosi-1
  121.     if sSrcBuff[iPosi+1] = 10 then      
  122.       iPosi += 1 'newline so skipping the LF too
  123.       EndOfLine()
  124.     end if
  125.     if ProcessToken()=0 then exit for
  126.    
  127.   case 10                    'newline!
  128.     iTokenEnd = iPosi-1
  129.     EndOfLine()
  130.     if ProcessToken()=0 then exit for
  131.    
  132.   case asc(":")              'label?
  133.     iTokenType = tkLabel
  134.     iTokenEnd = iPosi-1: if ProcessToken()=0 then exit for
  135.    
  136.   case asc(".")              'decimal/namespace
  137.     if iTokenBegin < 0 then 'number :)
  138.       if iNumParseMode then 'already decimal or something?
  139.         'Invalid number? @_@
  140.       else
  141.         iNumParseMode = 1 'decimal
  142.         iTokenType = tkNumber
  143.       end if
  144.     else
  145.       iTokenType = tkNamespace
  146.       iTokenEnd = iPosi-1: if ProcessToken()=0 then exit for
  147.     end if
  148.      
  149.   case else
  150.     'probabily this will work as a warning to add new char
  151.     'since this case never would be entered
  152.     'unless there is a a syntax error
  153.     iTokenEnd = iPosi-1: if ProcessToken()=0 then exit for
  154.     'account for unexpected characters?
  155.   end select
  156.  
  157. next iPosi
  158.  
  159. Sleep
  160.  
  161. Function ProcessToken() as integer
  162.     'if iTokenBegin = -1 orElse iTokenEnd-iTokenBegin = -2 Then return 1
  163.  
  164.     var pTokenBegin = StrPtr(sSrcBuff)+iTokenBegin
  165.     var pTokenEnd = pTokenBegin+iTokenEnd
  166.  
  167.     SetConst(sSrcBuff, iTokenBegin, iTokenEnd-iTokenBegin+1)
  168.     print iTokenBegin, iTokenEnd, iTokenEnd-iTokenBegin, sGlobalTemp
  169.     'print iTokenBegin, iTokenEnd, iTokenType, Hex(iTokenHash)
  170.     'Search for hash in hashtable of keywords
  171.     for N as integer = 0 to MAX_CONFLICTS
  172.         var iOffset = HashTable(iTokenHash, N)
  173.         if iOffset = INVALID_OFFSET_VALUE then exit for 'not found in table
  174.         if memcmp(pTokenBegin, @Keywords(iOffset), (pTokenEnd-pTokenBegin)+1)=0 then
  175.             print "match found!": exit for 'Found a match!
  176.         end if
  177.     next N
  178.    
  179.     iTokenBegin = -1: iTokenEnd = -1: iTokenType = tkUnknown: iTokenHash = 0
  180.     return 42
  181. end function
  182.  
  183. sub EndOfLine()
  184.     'Print "Hello World"
  185. end sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement