luati-nspire

Error: 910 i.e. Syntax error is returned by math.evalStr() API of TI-Nspire engine


Code

I have this Lua script for a TI-Nspire CAS calculator.

platform.apilevel = '2.2'

box_prompt, error = D2Editor.newRichText():resize(300, 40)
box_prompt:move(0, 0):setBorder(1):setBorderColor(0x43adee):setFontSize(12):setReadOnly(true):setSelectable(false)
    :setTextColor(0x666666):setVisible(true)
result, error = box_prompt:setText('Please enter an expression in box below')

box_expr, error = D2Editor.newRichText():resize(300, 40)
box_expr:move(0, 50):setBorder(1):setBorderColor(0x43adee):setFontSize(12):setReadOnly(false):setSelectable(true)
    :setTextColor(0x000000):setVisible(true)

box_result, error = D2Editor.newRichText():resize(300, 40)
box_result:move(0, 100):setBorder(1):setBorderColor(0x43adee):setFontSize(12):setReadOnly(false):setSelectable(true)
    :setTextColor(0x000000):setVisible(true)

box_expr_doublecheck, error = D2Editor.newRichText():resize(300, 40)
box_expr_doublecheck:move(0, 150):setBorder(1):setBorderColor(0x43adee):setFontSize(12):setReadOnly(true):setSelectable(
    false):setTextColor(0x666666):setVisible(true)

-- To evaluate the expression
function run()
    local expression = box_expr:getExpression()
    box_expr_doublecheck:setText("expression doublecheck: "..expression)
    local result, err = math.evalStr(expression)
    if err ~= nil then
        show_error(err)
        return
    end
    box_result:setText("Result: " .. result)
end

function on.escapeKey()
    run()
end

function show_error(err)
    if err == nil then
    else
        box_result:setText("Error: " .. err)
    end
end

UI

It has four rich text boxes.

  1. The 1st box is just a read-only help.
  2. The 2nd box is where user enters a mathematical expression.
  3. The 3rd box will display result or error.
  4. The 4th box will display the expression entered by the user to double-check.

Screenshot: before user enters

Good expressions

When user enters expressions containing straightforward characters like -, +, /, *, ^ then the program works just fine:

Screenshot: good expression

Screenshot: good expression result

Bad expression

However, when user employs the TI-Nspire temlates like power and division templates, then an Error: 910 is thrown which according to Error_Codes means Syntax error.

Screenshot: bad expression

Screenshot: bad expression error

Template

By tempalte, I mean buttons like these on a TI-Nspire calculator:

Template buttons

Question

How can I correctly evaluate an expression that contains one or more TI-Nspire template? I played around with math.evalStr(), math.eval() and other APIs, but I couldn't figure it out. Thanks.

UPDATE: createMathBox()

I created a math box inside the rich text editor by this statement: box_expr:createMathBox(). Then, any text entered by the user inside that math box, even ordinaly 2+3, throws the syntax error above.

Screenshot: math box

UPDATE: doc

Lua Scripting API Reference Guide provides a table with more information about annotations in proprietary markup language:

Doc


Solution

  • This is the fixed code:

    platform.apilevel = '2.2'
    
    box_prompt, error = D2Editor.newRichText():resize(300, 40)
    box_prompt:move(0, 0):setBorder(1):setBorderColor(0x43adee):setFontSize(12):setReadOnly(true):setSelectable(false)
        :setTextColor(0x666666):setVisible(true)
    result, error = box_prompt:setText('Please enter an expression in box below')
    
    box_expr, error = D2Editor.newRichText():resize(300, 40)
    box_expr:move(0, 50):setBorder(1):setBorderColor(0x43adee):setFontSize(12):setReadOnly(false):setSelectable(true)
        :setTextColor(0x000000):setVisible(true)
    matbox = box_expr:createMathBox() -- Math Box
    
    box_result, error = D2Editor.newRichText():resize(300, 40)
    box_result:move(0, 100):setBorder(1):setBorderColor(0x43adee):setFontSize(12):setReadOnly(false):setSelectable(true)
        :setTextColor(0x000000):setVisible(true)
    
    box_expr_doublecheck, error = D2Editor.newRichText():resize(300, 40)
    box_expr_doublecheck:move(0, 150):setBorder(1):setBorderColor(0x43adee):setFontSize(12):setReadOnly(true):setSelectable(
        false):setTextColor(0x666666):setVisible(true)
    
    -- To evaluate the expression
    function run()
        local markup = matbox:getExpression()
        -- Math Box markup is "\0el {...}"
        -- Get everything between the curly braces
        local E = markup:match("{(.*)}")
        box_expr_doublecheck:setText("markup:" .. markup .. "   string:" .. E)
        local result, err = math.evalStr(E)
        if err ~= nil then
            show_error(err)
            return
        end
        box_result:setText("Result: " .. result)
    end
    
    function on.escapeKey()
        run()
    end
    
    function show_error(err)
        if err == nil then
        else
            box_result:setText("Error: " .. err)
        end
    end
    

    As can be seen, the annotations in proprietary markup language should be considered:

    Screenshot: fixed

    Solution

    These statements were the key:

    matbox = box_expr:createMathBox() -- Math Box
    
    -- ...
    
        local markup = matbox:getExpression()
        -- Math Box markup is "\0el {...}"
        -- Get everything between the curly braces
        local E = markup:match("{(.*)}")
        
        local result, err = math.evalStr(E)
    
    -- ...
    

    General case

    This is a general function to remove all occurances of markup characters \0el { and } inside an input string:

    
    function delete_markup(strI)
        -- Remove all occurrences of "\0el {" and "}" inside string
        local strO = strI:gsub("\\0el {(.-)}", "%1")
        return strO
    end