B4J Question [B4X Eval] Adding a "power" operator?

MegatenFreak

Active Member
Licensed User
Hi.
I've tried to add a power operator to the great B4XEval class, so that, for example, 2^3 would evaluate to 8. That's the operator the users of my app use.
Since the source was available, I added two lines to the EvalNode Sub:
B4X:
Private Sub EvalNode (pn As ParsedNode) As Double
    If pn.NodeType = NUMBER_TYPE Then Return pn.Value
    Dim left As Double = EvalNode(pn.Left)
    Dim right As Double = EvalNode(pn.Right)
    Select pn.Operator
        Case "+"
            Return left + right
        Case "-"
            Return left - right
        Case "*"
            Return left * right
        Case "/"
            Return left / right
        Case "^"                       'I added this line and the next
            Return Power(left, right)
        Case Else
            Log("Syntax error: " & pn.Operator)
            Return "error"
    End Select
End Sub
But when I test it I get an exception error: java.lang.NumberFormatException: For input string: "null"
Is there any part of the code that needs changing? (or is there a way I could use the 'functions' feature supported by the library to make this work?)
Thanks a lot in advance.
 

imbault

Well-Known Member
Licensed User
Longtime User
Hi you forgot to add your Power function : ^ in Initialize of the B4XEval class :

B4X:
Public Sub Initialize (Callback As Object, EventName As String)
    mCallback = Callback
    mEventName = EventName
    OperatorLevel = CreateMap("+": 1, "-": 1, "*":2, "/": 2, "^": 2)
End Sub

Patrick
 
Upvote 0
Top