Variable Handling in a QB Scripter?

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
Rocket Boy
Coder
Posts: 19
Joined: Thu Sep 08, 2005 3:14 am

Variable Handling in a QB Scripter?

Post by Rocket Boy »

I'm curious on how you guys think you might handle variables in a scripting language made in Qb? So far I don't feel like I've come up with any great amazing ways and I'm looking for some fresh ideas :)
MystikShadows
Veteran
Posts: 703
Joined: Sun Nov 14, 2004 7:36 am
Contact:

Post by MystikShadows »

I do it in 3 steps so to speak:

Code: Select all


TYPE VariableRecord
     VariableNumber AS INTEGER
     VariableName   AS STRING * 32
     VariableType   AS INTEGER
     VariableValue  AS STRING * 255
END TYPE

DIM SHARED Variables()       AS VariableRecord
DIM SHARED VariableCount     AS INTEGER
DIM SHARED VariablePointer   AS INTEGER

SELECT CASE WorkWord
        CASE "@VAR"
             CALL ProcessDeclaration(WorkString)
        CASE "@SET"
             CALL ProcessAssignment(WorkString)
        CASE "@GET"
             WorkValue = ProcessVariableRead(WorkString)
END SELECT
And these 3 functions are coded as:

Code: Select all

' ============================================================
'  NAME........: ProcessDeclaration()
'  PARAMETERS..: ScriptLine AS STRING
'  RETURN......: No Value
'  ASSUMES.....: ScriptLine is valid GenesisBasic Script Line
'  CALLED FROM.: Anywhere
' ------------------------------------------------------------
'  DESCRIPTION.: This subroutine takes a script line and
'                performs appropriate code to add a variable
'                to the variables array and assign it's
'                different field values.
' ------------------------------------------------------------
'  SCRIPT LABEL: @VAR
' ============================================================
SUB ProcessDeclaration(ScriptLine AS STRING)

    DIM VariableName AS STRING
    DIM VariableType AS STRING

    VariableCount = VariableCount + 1
    REDIM PRESERVE Variables(VariableCount) AS VariableRecord
    VariableName  = GetWordNumber(ScriptLine, 2)
    VariableType  = GetWordNumber(ScriptLine, 3)
    Variables(VariableCount).VariableNumber = VariableCount
    Variables(VariableCount).VariableName   = VariableName
    SELECT CASE VariableType
           CASE "INTEGER"
                Variables(VariableCount).VariableType = IntegerType
           CASE "NUMERIC"
                Variables(VariableCount).VariableType = NumericType
           CASE "CHARACTER"
                Variables(VariableCount).VariableType = CharacterType
           CASE "STRING"
                Variables(VariableCount).VariableType = StringType
           CASE "DATE"
                Variables(VariableCount).VariableType = DateType
           CASE "TIME"
                Variables(VariableCount).VariableType = TimeType
           CASE "STATE"
                Variables(VariableCount).VariableType = StateType
    END SELECT

END SUB

' ============================================================
'  NAME........: ProcessAssignment()
'  PARAMETERS..: ScriptLine AS STRING
'  RETURN......: No Value
'  ASSUMES.....: ScriptLine is valid GenesisBasic Script Line
'  CALLED FROM.: Anywhere
' ------------------------------------------------------------
'  DESCRIPTION.: This subroutine takes a script line and
'                performs appropriate code to assign the
'                value given to the selected variable
'                index.
' ------------------------------------------------------------
'  SCRIPT LABEL: @SET
' ============================================================
SUB ProcessAssignment(ScriptLine AS STRING)

    DIM VariableName  AS STRING
    DIM VariableValue AS STRING
    DIM Counter       AS INTEGER

    VariableName  = GetWordNumber(ScriptLine, 2)
    VariableValue = GetWordNumber(ScriptLine, 4)  ' 3 should be the = sign
    FOR Counter = 1 TO UBOUND(Variables)
        IF Variables(Counter).VariableName = VariableName THEN
           Variables(Counter).VariableValue = VariableValue
           EXIT FOR
        END IF
    NEXT Counter

END SUB

' ============================================================
'  NAME........: ProcessVariableRead()
'  PARAMETERS..: ScriptLine AS STRING
'  RETURN......: No Value
'  ASSUMES.....: ScriptLine is valid GenesisBasic Script Line
'  CALLED FROM.: Anywhere
' ------------------------------------------------------------
'  DESCRIPTION.: This subroutine takes a script line and
'                performs appropriate code to read the value
'                of a variable and return it to the game
'                for future reading or processing.
' ------------------------------------------------------------
'  SCRIPT LABEL: @GET
' ============================================================
FUNCTION ProcessVariableRead(ScriptLine AS STRING) AS STRING

    DIM VariableName  AS STRING
    DIM VariableValue AS STRING
    DIM Counter       AS INTEGER

    VariableName  = GetWordNumber(ScriptLine, 1)
    FOR Counter = 1 TO UBOUND(Variables)
        IF Variables(Counter).VariableName = VariableName THEN
           VariableValue = Variables(Counter).VariableValue
           EXIT FOR
        END IF
    NEXT Counter
    ProcessVariableRead = TRIM$(VariableValue)

END FUNCTION
Variables(VariableCount).VariableType is an integer that can have the following values:

Code: Select all

' --------------------
'  Allowed Data Types
' --------------------
CONST IntegerType   = 1  ' INTEGER    An Integer only numeric variable
CONST NumericType   = 2  ' NUMERIC    A Number that can have decimals
CONST CharacterType = 3  ' CHARACTER  1 space character
CONST StringType    = 4  ' STRING     A regular string variable
CONST DateType      = 5  ' DATE
CONST TimeType      = 6  ' TIME
CONST StateType     = 7  ' STATE      Can only be 0 or 1
When God created light, so too was born, the first Shadow!

MystikShadows

Need hosting? http://www.jc-hosting.net

Interested in Text & ASCII development? Look no further!
http://www.ascii-world.com
Post Reply