Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tokenizer in QB64
#19
I do think the code is very un-QB64 and could be structured way better which prevents issues and makes it much more readable.

- Get rid of old basic DECLARE's, WHILE/WEND's, etc.
- Don't make all variables shared unless you have a good reason; use local variables in functions/subs as much as possible
- Use sensible variablenames
- separate small blocks of code in their own functions
- don't use goto TokExit; it's a sign of bad structure in your function
- structure with FOR, SELECT CASE, etc.

A lot of things can go wrong here. For example in your WHILE p <= Len(src) it is very risky to do p=p+1:ch=mid$(src,p,1) since you don't check if p is now pointing outside ch. And there are more of these issues hidden in the structure of your function

something like this (didn't look at all the details):
Code: (Select All)
Const FALSE = 0, TRUE = Not FALSE
Function tokenizer& (src As String)
  srclen& = Len(src)
  srcpos& = 0
  Do While srcpos& < srclen&
    srcpos& = srcpos& + 1
    char% = Asc(src, srcpos&) ' get char
    Select Case char%
      Case 32 'skip space
      Case 9 'skip tab
      Case 13 'skip cr
        skipline% = FALSE: lineCount = lineCount + 1
      Case 39 'skip commentline
        skipline% = TRUE
      Case 10 ' lf
        skipline% = FALSE: lineCount = lineCount + 1
        tp& = addToken("eol")
      Case 34 'double quote
        If Not skipline% Then
          If dquote& = 0 Then
            dquote& = srcpos&
          Else
            tp& = addToken("from dquote& to srcpos&")
            dquote& = 0
          End If
        End If
      Case 48 To 57 '0 to 9
        If Not skipline% Then tp& = addToken("digit")
      Case tkPLUS 'Plus
        If Not skipline% Then tp& = addToken("tkPlus")
      Case tkMINUS 'Minus
        If Not skipline% Then tp& = addToken("tkMINUS")
      Case tkETCETERA 'all other const tk...
        If Not skipline% Then tp& = addToken("something")
      Case 92 To 125 'all other valid chars?
        If Not skipline% Then tp& = addToken("something")
      Case Else
    End Select
  Loop
  tokenizer& = tp&
End Function

Function addToken& (something$)
  If something$ = "allright" then
    tokens = tokens + 1
    ' add token to array
    addToken& = tokens
  Else
    addToken& = errorcode ' Negative
  End If
End Function

Function tokError (errcode%)
  ' show and/or handle error
End Function
45y and 2M lines of MBASIC>BASICA>QBASIC>QBX>QB64 experience
Reply


Messages In This Thread
Tokenizer in QB64 - by aurel - 02-25-2023, 02:52 PM
RE: Tokenizer in QB64 - by bplus - 02-25-2023, 03:36 PM
RE: Tokenizer in QB64 - by aurel - 02-25-2023, 03:40 PM
RE: Tokenizer in QB64 - by bplus - 02-25-2023, 03:42 PM
RE: Tokenizer in QB64 - by aurel - 02-25-2023, 03:43 PM
RE: Tokenizer in QB64 - by mnrvovrfc - 02-25-2023, 05:13 PM
RE: Tokenizer in QB64 - by aurel - 02-25-2023, 03:48 PM
RE: Tokenizer in QB64 - by bplus - 02-25-2023, 03:53 PM
RE: Tokenizer in QB64 - by bplus - 02-25-2023, 03:55 PM
RE: Tokenizer in QB64 - by bplus - 02-25-2023, 04:00 PM
RE: Tokenizer in QB64 - by aurel - 02-25-2023, 04:47 PM
RE: Tokenizer in QB64 - by aurel - 02-25-2023, 04:49 PM
RE: Tokenizer in QB64 - by aurel - 02-25-2023, 07:54 PM
RE: Tokenizer in QB64 - by aurel - 11-21-2023, 04:13 PM
RE: Tokenizer in QB64 - by RhoSigma - 11-21-2023, 04:58 PM
RE: Tokenizer in QB64 - by aurel - 11-21-2023, 06:05 PM
RE: Tokenizer in QB64 - by bplus - 11-21-2023, 06:44 PM
RE: Tokenizer in QB64 - by aurel - 11-22-2023, 10:11 AM
RE: Tokenizer in QB64 - by RhoSigma - 11-22-2023, 12:23 PM
RE: Tokenizer in QB64 - by mnrvovrfc - 11-22-2023, 09:17 PM
RE: Tokenizer in QB64 - by mdijkens - 11-22-2023, 11:25 AM
RE: Tokenizer in QB64 - by bplus - 11-22-2023, 01:30 PM
RE: Tokenizer in QB64 - by SpriggsySpriggs - 11-22-2023, 01:53 PM
RE: Tokenizer in QB64 - by bplus - 11-22-2023, 03:07 PM
RE: Tokenizer in QB64 - by SpriggsySpriggs - 11-22-2023, 03:24 PM
RE: Tokenizer in QB64 - by aurel - 11-22-2023, 04:38 PM
RE: Tokenizer in QB64 - by aurel - 11-22-2023, 04:44 PM
RE: Tokenizer in QB64 - by aurel - 11-22-2023, 04:48 PM
RE: Tokenizer in QB64 - by aurel - 11-22-2023, 05:01 PM
RE: Tokenizer in QB64 - by bplus - 11-22-2023, 05:28 PM
RE: Tokenizer in QB64 - by aurel - 11-22-2023, 07:03 PM
RE: Tokenizer in QB64 - by bplus - 11-22-2023, 09:03 PM
RE: Tokenizer in QB64 - by aurel - 11-23-2023, 10:17 AM
RE: Tokenizer in QB64 - by mnrvovrfc - 11-23-2023, 01:49 PM
RE: Tokenizer in QB64 - by bplus - 11-23-2023, 02:17 PM
RE: Tokenizer in QB64 - by aurel - 11-23-2023, 05:47 PM
RE: Tokenizer in QB64 - by mnrvovrfc - 12-09-2023, 07:52 AM
RE: Tokenizer in QB64 - by SMcNeill - 11-23-2023, 06:54 PM
RE: Tokenizer in QB64 - by aurel - 01-06-2024, 02:07 PM



Users browsing this thread: 5 Guest(s)