Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: General Purpose Eval

  1. #1

    General Purpose Eval

    I need a general purpose eval function that can solve an expression like this:

    Val("1") + 0 Or Int(1.5)

    That would produce the result TRUE (1 or -1 in thinBASIC).

    Notice that the expression has a string, math, and the logical/bitwise operator Or in it. I don't want to have to use Eval_String, or Eval_Math, or Eval() to solve different expressions because I need an evaluator that spits out a variant.

    Do I have any options? I have no idea how to write an expression evaluator, I don't think I'm quite that skilled yet.

  2. #2
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10

    Re: General Purpose Eval

    Sorry, I do not understand why Eval_Math cannot be used. Maybe if I underatand I can help.
    What does it means:
    spits out a variant
    Anyhow, here example using Eval module:
    [code=thinbasic]Uses "Eval"

    MsgBox 0, Eval_Math("Val(""1"") + 0 Or Int(1.5)")[/code]

    Ciao
    Eros
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  3. #3

    Re: General Purpose Eval

    I didn't make it very clear. Let me try again.

    In my code, I'm unaware of what the expression could evaluate to. It's whatever the user made it. What if it was this?

    Str$(Val("1") + 0 Or Int(1.5)) & " hi"

    Eval_Math won't return what you would expect:

    [code=thinbasic]Uses "Eval"

    MsgBox 0, Eval_Math("Str$(Val(""1"") + 0 Or Int(1.5)) & "" hi""")[/code]

    It returns "1" where I need it to correctly return "1 hi". That's understandable, because it as a Math evaluator. I need something that returns a variant, like I mentioned.

    Thanks for the quick response!

  4. #4
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10

    Re: General Purpose Eval

    I do not think it can be a VARIANT the solution because at some point you have to assign the result of your expression to the VARIANT and at that point you need to know what is the main expression main type.

    What about something like the following?
    All expressions are valuated into a string.
    To understand if expression returns a string or a number it checks that FORMAT$ of the VAL of the return string value must be equal to the original returned string. In this case it should/can be a number.

    [code=thinbasic]
    Uses "Console"
    Uses "Eval"

    Dim sResult As String
    Dim eResult As Ext

    PrintL "--- Example 1: -------------------------------------------------"
    sResult = Trim$(Eval_String("Val(""1"") + 0 Or Int(1.5)"))
    eResult = Val(sResult)
    PrintL "String Result:", sResult
    PrintL "Numeric Result:", eResult

    If CheckIfEqual(sResult, eResult) Then
    PrintL "They seems equal so expression seems numeric"
    Else
    PrintL "They seems different so expression seems a string"
    End If


    PrintL
    PrintL
    PrintL "--- Example 2: -------------------------------------------------"
    sResult = Trim$(Eval_String("Str$(Val(""1"") + 0 Or Int(1.5)) & "" hi"""))
    eResult = Val(sResult)
    PrintL "String Result:", sResult
    PrintL "Numeric Result:", eResult
    If CheckIfEqual(sResult, eResult) Then
    PrintL "They seems equal so expression seems numeric"
    Else
    PrintL "They seems different so expression seems a string"
    End If

    WaitKey

    '---------------------------------------------------------------------------
    Function CheckIfEqual(ByVal sBuffer As String, ByVal eNumber As Ext) As Long
    '---------------------------------------------------------------------------
    If Format$(eResult) = sResult Then
    Function = %TRUE
    Else
    Function = %FALSE
    End If

    End Function
    [/code]
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  5. #5
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10

    Re: General Purpose Eval

    Joseph,

    I've done few changes to Eval module adding the following two functions:
    • Eval_Variable_Exists(VariableName)
      Will check if a variable exists after an Eval... execution
    • Eval_Reset
      Will reset internal Eval structure to its base state (all defined variables during a previous Eval... execution will be removed)


    Attached to this post you will find new preliminary version of Eval module. Just download it and substitute the one you will find into \thinBasic\Lib\ directory

    Here an example on how it can be possible to solve your problem (just an idea).
    Ask to your users to define one variable name (string for example) or another (numeric for example) and than use the presence of one of the two variables to determine what is the correct value to retrieve (string or numeric).

    Hope this can help you otherwise let me know your perplexities and I will try to see if I can improve again the module.
    Eros


    [code=thinbasic]
    Uses "Console"
    Uses "Eval"

    Dim nScripts As Long Value 2
    Dim sScripts(nScripts) As String
    Dim Counter As Long
    Dim lReturnCode As Long

    '---Simulate user script using a string
    sScripts(1) = RawText
    Dim Exit_String As String
    Exit_String = Str$(Val("1") + 0 Or Int(1.5)) & " hi"
    End RawText

    '---Simulate user script using a number
    sScripts(2) = RawText
    Dim Exit_Number As Number
    Exit_Number = Val("1") + 0 Or Int(1.5)
    End RawText

    '---Execute all the scripts and check existence of one variable or the other
    For Counter = 1 To nScripts

    lReturnCode = Eval(sScripts(Counter))

    '---If execution was fine, check variable names
    If lReturnCode = 0 Then
    If Eval_Variable_Exists("Exit_String") Then
    PrintL "Exit_String is present and its value is:", Eval_GetString("Exit_String")
    ElseIf Eval_Variable_Exists("Exit_Number") Then
    PrintL "Exit_Number is present and its value is:", Eval_GetNumber("Exit_Number")
    Else
    PrintL "No usable variables defined"
    End If
    Else
    PrintL "An error occurred during script number " & Counter
    Exit For
    End If

    '---Important: reset eval module between each execution
    '---In order to remove all variables defined by previous run
    '---If Eval_Reset function will not be executed, all variables
    '---defined by a previous Eval will be still there
    PrintL
    PrintL "--------------------------------------------------------"
    PrintL "- Resetting Eval ----------------------------------------"
    PrintL "--------------------------------------------------------"
    Eval_Reset
    Next

    WaitKey
    [/code]
    Attached Files Attached Files
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  6. #6

    Re: General Purpose Eval

    Eros, you're awesome!

    While my user does define what expression gets evaluated, it's rather indirect. It's a part of my simple interpreter for a bigger project I've been working on in thinBASIC. thinBASIC and thinAir are holding up really well and I must say it's a pleasure developing with these tools.

    I'll only be evaluating literal expressions, no variables are needed, so your first solution works brilliantly for me. My primitive littler interpreter replaces all the user's variables in the current scope with their appropriate values, so your method of detecting the return type is perfect for me.

    Those new Eval functions might come in handy one of these days though. Thanks!

  7. #7

    Re: General Purpose Eval

    How come Eval_String() puts a blank space in front of some of the results?

    [code=thinbasic]Uses "Console"
    Uses "Eval"

    PrintL Evaluate("""hello, "" & ""world!""")
    PrintL Evaluate("(0 Or 0) = (1 and 0)")
    PrintL Evaluate("Str$(1 And Val(""-1"")) & "".""")
    PrintL Evaluate("""bob"" = ""bob"" And ""willy"" = ""willy""")
    PrintL Evaluate("1 = 1")
    PrintL Evaluate("Val(""9000000"" & ""0"") + (15 + sin(11.211) / 2 & sqr(7))")
    WaitKey

    Function Evaluate(strng As String) As String
    Return Eval_String(strng)
    End Function[/code]

  8. #8
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10

    Re: General Purpose Eval

    Attached a fixed version of Eval module.
    Just download it and substitute the one you will find into \thinBasic\Lib\ directory

    The example using STR$(...) will still retunr a space in front of the returned buffer because in almost all BASIC languages STR$ add a space in front of the converted number (I think for hystorical reasons it leaves a space for sign in case of negative numbers)

    Eros
    Attached Files Attached Files
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  9. #9

    Re: General Purpose Eval

    Thanks Eros, that's wonderful. I didn't know Str$() produces a space, so that makes everything work out just dandy.

  10. #10
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10

    Re: General Purpose Eval

    If you prefer not to have space do not use STR$(number) but use FORMAT$(number)

    Ciao
    Eros
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

Page 1 of 2 12 LastLast

Members who have read this thread: 2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •