Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'Macro for a temp string that's actually a global null string with the header
- 'modified to point ot the middle of an existing string.
- #define SetConst(_sSrc,_iStart,_iLen) *cptr(fbstr ptr, @sGlobalTemp) = type( strptr(_sSrc)+(_iStart) , _iLen , _iLen )
- Dim Shared as String sGlobalTemp
- type fbstr
- pzData as zstring ptr
- iLen as integer
- iSize as integer
- end type
- #if 0
- float MoveCount = 0
- float MoveNum = 32
- Start:
- If MoveCount = MoveNum Goto TurnRobot
- Add MoveCount, 1
- BIOS.Move(1)
- GOTO Start
- TurnRobot:
- BIOS.Turn(1)
- Let MoveCount = 0
- Goto Start
- #endif
- #include once "windows.bi"
- #include once "../include/opcode.bi"
- #include once "HashTable.bi"
- Declare Function ProcessToken() as integer
- Declare Sub EndOfLine()
- Dim Shared as String sSrcBuff
- Dim as String sSourcefile = "test1.txt" 'Command
- dim as integer iFileSz
- If Open (sSourceFile for binary access read as #1) Then
- Print "Error opening source file!"
- Sleep: System
- End If
- iFileSz = LOF(1)
- sSrcBuff = Space(iFileSz)
- Get #1,, sSrcBuff
- close #1
- initHashTable
- enum TokenTypes
- tkUnknown
- tkLabel
- tkVariable
- tkNamespace
- tkNumber
- end enum
- ' State variabes... (if there's multiple threads, must be a UDT)
- dim shared as integer iTokenBegin = -1, iTokenEnd = -1, iTokenType = tkUnknown
- dim as integer iNumParseMode = 0
- dim shared as integer iNumberParse, iDecimalParse
- dim shared as integer iNumberDigits, iDecimalDigits
- dim shared as uByte iTokenHash
- color 14
- print "iTokenBegin", "iTokenEnd", "difference", "sGlobalTemp"
- print String(79, "-")
- color 7
- ' =================================================
- ' ========= Parse File based on the state =========
- ' =================================================
- for iPosi as integer = 0 to iFileSz-1
- var iChar = sSrcBuff[iPosi] 'maybe it wont be needed
- if iTokenType = tkNumber then 'chars that can end numbers...
- select case as const iChar
- case asc("0") to asc("9"),asc(".")
- rem ok so still a number...
- case 32,9,13,10 'Space/Tab/cr/lf (i guess all operators)
- iTokenType = tkNumber
- if ProcessToken()=0 then exit for
- case else
- 'bad number...
- end select
- end if
- select case as const iChar
- case 32,9 'Space/Tab
- if iTokenBegin >= 0 orelse iTokenType<>tkUnknown then
- iTokenEnd = iPosi-1: if ProcessToken()=0 then exit for
- end if
- ' ignore blanks
- case asc("0") to asc("9")
- if iTokenBegin<0 then
- 'not part of a token so must be a number/expression/error
- 'if number parsing... then insert next digit into number
- iTokenType = tkNumber
- if iNumParseMode = 0 then 'whole number
- iNumberParse = (iNumberParse*10)+iChar-asc("0")
- iNumberDigits += 1
- else
- iDecimalParse = (iDecimalParse*10)+iChar-asc("0")
- iDecimalDigits += 1
- end if
- else
- iTokenHash += iChar 'add to the hash of the token
- end if
- case asc("A") to asc("Z")
- iChar += (asc("a")-asc("A")) 'lowercase convert
- sSrcBuff[iPosi] = iChar 'and update on string?
- if iTokenBegin<0 then 'wasnt reading a token
- iTokenBegin = iPosi 'so now token start here
- end if
- iTokenHash += iChar 'add to the hash of the token
- case asc("a") to asc("z"), asc("_")
- iTokenHash += iChar 'add to the hash of the token
- case 13 'possible newline?
- iTokenEnd = iPosi-1
- if sSrcBuff[iPosi+1] = 10 then
- iPosi += 1 'newline so skipping the LF too
- EndOfLine()
- end if
- if ProcessToken()=0 then exit for
- case 10 'newline!
- iTokenEnd = iPosi-1
- EndOfLine()
- if ProcessToken()=0 then exit for
- case asc(":") 'label?
- iTokenType = tkLabel
- iTokenEnd = iPosi-1: if ProcessToken()=0 then exit for
- case asc(".") 'decimal/namespace
- if iTokenBegin < 0 then 'number :)
- if iNumParseMode then 'already decimal or something?
- 'Invalid number? @_@
- else
- iNumParseMode = 1 'decimal
- iTokenType = tkNumber
- end if
- else
- iTokenType = tkNamespace
- iTokenEnd = iPosi-1: if ProcessToken()=0 then exit for
- end if
- case else
- 'probabily this will work as a warning to add new char
- 'since this case never would be entered
- 'unless there is a a syntax error
- iTokenEnd = iPosi-1: if ProcessToken()=0 then exit for
- 'account for unexpected characters?
- end select
- next iPosi
- Sleep
- Function ProcessToken() as integer
- 'if iTokenBegin = -1 orElse iTokenEnd-iTokenBegin = -2 Then return 1
- var pTokenBegin = StrPtr(sSrcBuff)+iTokenBegin
- var pTokenEnd = pTokenBegin+iTokenEnd
- SetConst(sSrcBuff, iTokenBegin, iTokenEnd-iTokenBegin+1)
- print iTokenBegin, iTokenEnd, iTokenEnd-iTokenBegin, sGlobalTemp
- 'print iTokenBegin, iTokenEnd, iTokenType, Hex(iTokenHash)
- 'Search for hash in hashtable of keywords
- for N as integer = 0 to MAX_CONFLICTS
- var iOffset = HashTable(iTokenHash, N)
- if iOffset = INVALID_OFFSET_VALUE then exit for 'not found in table
- if memcmp(pTokenBegin, @Keywords(iOffset), (pTokenEnd-pTokenBegin)+1)=0 then
- print "match found!": exit for 'Found a match!
- end if
- next N
- iTokenBegin = -1: iTokenEnd = -1: iTokenType = tkUnknown: iTokenHash = 0
- return 42
- end function
- sub EndOfLine()
- 'Print "Hello World"
- end sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement