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
It has four rich text boxes.
When user enters expressions containing straightforward characters like -
, +
, /
, *
, ^
then the program works just fine:
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
.
By tempalte, I mean buttons like these on a TI-Nspire calculator:
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.
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.
Lua Scripting API Reference Guide provides a table with more information about annotations in proprietary markup language:
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:
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)
-- ...
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