Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
johannes-wolf committed Mar 23, 2024
1 parent a904e9e commit b50aca6
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
60 changes: 60 additions & 0 deletions apps/app_analyze_fn.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
local apps = require 'apps.apps'
local choice = require('dialog.choice').display_sync
local ask_n = require('dialog.input').display_sync_n
local sym = require('ti.sym')
local ui = require('ui.shared')
local lexer = require 'ti.lexer'
local expr = require 'expressiontree'
local sym = require 'ti.sym'

local function each_result(str, fn)
local tokens = lexer.tokenize(eval(str))
local e = tokens and expr.from_infix(tokens)
if e then
if e.text == '{' or e.text == 'or' or e.text == 'and' then
for _, arg in ipairs(e:collect_operands_recursive()) do
fn(arg)
end
else
fn(e)
end
end
end

local function run_analyze(stack)
local fn, low, high = table.unpack(ask_n(3), { title = "Analyze ..." })
local var = "x"

local with_str = nil
if low:len() ~= 0 then
with_str = (with_str or "|") .. low .."<" .. var
end
if high:len() ~= 0 then
with_str = (with_str or "|") .. var .."<" .. high
end

local function get_zeros(fn, bounds)
local tab = {}
each_result(math.evalStr(string.format("zeros(%s,%s)%s", fn, var, bounds or "")),
function(z)
table.insert(tab, z)
end)
return tab
end

local zeros = get_zeros(fn, with_str)
local r = choice({
title = 'Results',
items = (function()
local list = {}
for i, v in ipairs(zeros) do
table.insert(list, {
title=string(v), result=i
})
end
return list
end)()
})
end

apps.add('analyze', 'Analyze function', run_analyze)
1 change: 1 addition & 0 deletions apps/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ require 'apps.app_trassierung'
require 'apps.app_punktsteigung'
require 'apps.app_triangle'
require 'apps.app_constants'
require 'apps.app_analyze_fn'
22 changes: 22 additions & 0 deletions dialog/input.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,28 @@ function t.display_sync(options, on_init)
return res
end

-- Display dialog (sync)
---@param options? table<string, any> Dialog options
---@param on_init? init_callback Initializer
function t.display_sync_n(n, options, on_init)
local co = coroutine.running()
local dlg = t.display_n(n, options, on_init)

local res
dlg.on_cancel = function()
res = nil
coroutine.resume(co)
end
dlg.on_done = function(text)
res = text
coroutine.resume(co)
end

assert(co)
coroutine.yield(co)
return res
end

-- Display dialog
---@param options? table<string, any> Dialog options
---@param on_init? init_callback Initializer
Expand Down

0 comments on commit b50aca6

Please sign in to comment.