feat(demo): add a spreadsheet engine written in Lua
The demo folder only had a toy todo list, which did not show that a non-trivial application can be written in Lua and shipped as a single compiled binary. This adds LuaSheet: a tokenizer, a formula parser, a lazy evaluator with cycle detection, range-aware builtin functions, ASCII/CSV rendering and a benchmark, all driven by a CLI entry point. Everything stays plain Lua under Demo/: no C++ change is needed to build or run it.
This commit is contained in:
9 files changed
+1950
No files matched your search
@@ -0,0 +1,260 @@
|
||||
-- LuaSheet : un tableur en console, 100% Lua, compilé en un binaire unique.
|
||||
--
|
||||
-- ./build/luacpp run Demo/luasheet.lua classeur Budget (défaut)
|
||||
-- ./build/luacpp run Demo/luasheet.lua ventes second classeur
|
||||
-- ./build/luacpp run Demo/luasheet.lua errors feuille d'erreurs
|
||||
-- ./build/luacpp run Demo/luasheet.lua formula "=SUM(1,2,3)*2"
|
||||
-- ./build/luacpp run Demo/luasheet.lua csv
|
||||
-- ./build/luacpp run Demo/luasheet.lua bench 200
|
||||
-- ./build/luacpp run Demo/luasheet.lua test
|
||||
local sheet_module = require("sheet.sheet")
|
||||
local render = require("sheet.render")
|
||||
local workbooks = require("sheet.workbooks")
|
||||
local tests = require("sheet.tests")
|
||||
local builtins = require("sheet.builtins")
|
||||
local format = require("sheet.format")
|
||||
|
||||
local M = {}
|
||||
|
||||
local argv = arg or {}
|
||||
local command = argv[1] or "demo"
|
||||
|
||||
local function echo(line)
|
||||
print(line or "")
|
||||
end
|
||||
|
||||
local function banner(title)
|
||||
echo(string.rep("=", 64))
|
||||
echo(title)
|
||||
echo(string.rep("=", 64))
|
||||
end
|
||||
|
||||
local function demo()
|
||||
local sheet = sheet_module.from_text(workbooks.budget, "Budget 2026")
|
||||
banner("LuaSheet - classeur Budget 2026")
|
||||
echo("")
|
||||
|
||||
local rendered = render.render(sheet)
|
||||
echo(rendered)
|
||||
echo("")
|
||||
echo(string.format(
|
||||
"%d cellules, %d evaluations, %d erreurs",
|
||||
sheet:count(), sheet.stats.eval, sheet.stats.errors
|
||||
))
|
||||
|
||||
echo("")
|
||||
echo("--- inspecteur de formules ---")
|
||||
local inspected = { "D4", "C11", "D11", "B13", "C16", "B18" }
|
||||
for i = 1, #inspected do
|
||||
local key = inspected[i]
|
||||
echo(string.format(" %-4s %-22s -> %s", key, sheet:raw(key) or "", sheet:display(key)))
|
||||
end
|
||||
|
||||
echo("")
|
||||
echo("--- repartition des charges (B4:B9) ---")
|
||||
local posts = {
|
||||
{ "Loyer", "B4" }, { "Courses", "B5" }, { "Transport", "B6" },
|
||||
{ "Energie", "B7" }, { "Loisirs", "B8" }, { "Epargne", "B9" },
|
||||
}
|
||||
local total = 0
|
||||
for i = 1, #posts do
|
||||
local value = sheet:value(posts[i][2])
|
||||
if type(value) == "number" then
|
||||
total = total + value
|
||||
end
|
||||
end
|
||||
for i = 1, #posts do
|
||||
local value = sheet:value(posts[i][2])
|
||||
local label = posts[i][1]
|
||||
local ratio = 0
|
||||
if total > 0 then
|
||||
ratio = value / total
|
||||
end
|
||||
echo(string.format(
|
||||
" %-10s %6s %s %5s",
|
||||
label, format.number(value), render.bar(value, total, 24), format.percent(ratio, 1)
|
||||
))
|
||||
end
|
||||
|
||||
echo("")
|
||||
echo("--- indicateurs ---")
|
||||
local revenue = sheet:value("B12") or 0
|
||||
local remaining = sheet:value("B13") or 0
|
||||
local remaining_ratio = 0
|
||||
if revenue ~= 0 then
|
||||
remaining_ratio = remaining / revenue
|
||||
end
|
||||
local savings = sheet:value("B21") or 0
|
||||
echo(" charges mensuelles : " .. sheet:display("B11"))
|
||||
echo(" reste a vivre : " .. sheet:display("B13") .. " (" .. format.percent(remaining_ratio, 1) .. " des revenus)")
|
||||
echo(" epargne annuelle : " .. sheet:display("B15"))
|
||||
echo(" objectif epargne : " .. sheet:display("B16") .. " -> " .. sheet:display("C16"))
|
||||
echo(" part de l'epargne : " .. format.percent(savings, 1))
|
||||
|
||||
local found = sheet:errors()
|
||||
if #found > 0 then
|
||||
echo("")
|
||||
echo("--- erreurs ---")
|
||||
for i = 1, #found do
|
||||
echo(string.format(" %-4s %-16s %s", found[i].key, found[i].text, found[i].err))
|
||||
end
|
||||
end
|
||||
return 0
|
||||
end
|
||||
|
||||
local function ventes()
|
||||
local sheet = sheet_module.from_text(workbooks.ventes, "Ventes")
|
||||
banner("LuaSheet - classeur Ventes")
|
||||
echo("")
|
||||
echo(render.render(sheet))
|
||||
echo("")
|
||||
echo("vue formules :")
|
||||
echo(render.render(sheet, { show_formulas = true }))
|
||||
return 0
|
||||
end
|
||||
|
||||
local function show_errors()
|
||||
local sheet = sheet_module.from_text(workbooks.errors, "Erreurs")
|
||||
banner("LuaSheet - feuille d'erreurs (volontaires)")
|
||||
echo("")
|
||||
echo(render.render(sheet))
|
||||
echo("")
|
||||
local found = sheet:errors()
|
||||
echo(string.format("%d cellules en erreur :", #found))
|
||||
for i = 1, #found do
|
||||
local entry = found[i]
|
||||
echo(string.format(" %-4s %-14s %-10s %s", entry.key, entry.err, entry.text, entry.detail or ""))
|
||||
end
|
||||
return 0
|
||||
end
|
||||
|
||||
local function formula(text)
|
||||
if text == nil or text == "" then
|
||||
echo('usage : luasheet formula "=SUM(1,2,3)*2"')
|
||||
return 2
|
||||
end
|
||||
local sheet = sheet_module.new("formula")
|
||||
sheet:set("A1", "120")
|
||||
sheet:set("A2", "80")
|
||||
sheet:set("A3", "Lua")
|
||||
sheet:set("A4", "=A1+A2")
|
||||
if string.sub(text, 1, 1) == "=" then
|
||||
text = string.sub(text, 2)
|
||||
end
|
||||
sheet:set("B1", "=" .. text)
|
||||
echo(string.format("A1=120 A2=80 A3=\"Lua\" A4=A1+A2=%s", sheet:display("A4")))
|
||||
echo(string.format("%s -> %s", text, sheet:display("B1")))
|
||||
local found = sheet:errors()
|
||||
for i = 1, #found do
|
||||
if found[i].key == "B1" and found[i].detail ~= nil then
|
||||
echo("detail : " .. found[i].detail)
|
||||
end
|
||||
end
|
||||
return 0
|
||||
end
|
||||
|
||||
local function csv()
|
||||
local sheet = sheet_module.from_text(workbooks.budget, "Budget 2026")
|
||||
echo(render.csv(sheet))
|
||||
return 0
|
||||
end
|
||||
|
||||
local function bench(argument)
|
||||
local rows = tonumber(argument) or 300
|
||||
if rows < 1 then
|
||||
rows = 1
|
||||
end
|
||||
local sheet = sheet_module.new("bench")
|
||||
for row = 1, rows do
|
||||
for column = 1, 7 do
|
||||
sheet:set(format.column_label(column) .. row, tostring((row * column) % 97))
|
||||
end
|
||||
sheet:set("H" .. row, string.format("=SUM(A%d:G%d)", row, row))
|
||||
end
|
||||
sheet:set("I1", string.format("=SUM(H1:H%d)", rows))
|
||||
|
||||
local best = 0
|
||||
local last = 0
|
||||
for pass = 1, 3 do
|
||||
sheet:invalidate()
|
||||
local started = os.clock()
|
||||
local total = sheet:value("I1")
|
||||
local elapsed = os.clock() - started
|
||||
last = total
|
||||
if pass == 1 or elapsed < best then
|
||||
best = elapsed
|
||||
end
|
||||
echo(string.format(
|
||||
" passe %d : %6.1f ms (%d evaluations, total I1 = %s)",
|
||||
pass, elapsed * 1000, sheet.stats.eval, format.number(total)
|
||||
))
|
||||
end
|
||||
|
||||
local cells = rows * 8 + 1
|
||||
local rate = 0
|
||||
if best > 0 then
|
||||
rate = cells / best
|
||||
end
|
||||
echo("")
|
||||
echo(string.format(" %d lignes x 8 colonnes = %d cellules", rows, cells))
|
||||
echo(string.format(" meilleure passe : %.1f ms -> environ %d cellules/s", best * 1000, math.floor(rate)))
|
||||
echo(string.format(" total recalcule : %s", format.number(last)))
|
||||
return 0
|
||||
end
|
||||
|
||||
local function run_tests()
|
||||
banner("LuaSheet - tests internes")
|
||||
local summary = tests.run()
|
||||
echo("")
|
||||
echo(string.format(" %d tests OK, %d echec(s)", summary.passed, summary.failed))
|
||||
if summary.failed > 0 then
|
||||
echo("")
|
||||
for i = 1, #summary.failures do
|
||||
echo(" FAIL " .. summary.failures[i])
|
||||
end
|
||||
return 1
|
||||
end
|
||||
return 0
|
||||
end
|
||||
|
||||
local function usage()
|
||||
banner("LuaSheet - tableur en Lua compilé par luacpp")
|
||||
echo("")
|
||||
echo(" luasheet demo classeur Budget 2026 (défaut)")
|
||||
echo(" luasheet ventes second classeur (texte, IF, &)")
|
||||
echo(" luasheet errors feuille d'erreurs (#DIV/0!, #CYCLE!, ...)")
|
||||
echo(' luasheet formula "=..." évalue une formule')
|
||||
echo(" luasheet csv exporte le classeur Budget en CSV")
|
||||
echo(" luasheet bench [lignes] mesure le recalcul (défaut : 300 lignes)")
|
||||
echo(" luasheet test lance les tests internes")
|
||||
echo(" luasheet help cette aide")
|
||||
echo("")
|
||||
echo("Opérateurs : + - * / % ^ & = <> < > <= >=")
|
||||
echo("Références : A1, $A$1, plages A1:B4")
|
||||
echo("Fonctions : " .. table.concat(builtins.names(), " "))
|
||||
echo("")
|
||||
echo("Exemple : luacpp run Demo/luasheet.lua formula \"=SUM(1,2,3)*2\"")
|
||||
return 0
|
||||
end
|
||||
|
||||
local commands = {
|
||||
demo = demo,
|
||||
ventes = ventes,
|
||||
errors = show_errors,
|
||||
formula = formula,
|
||||
csv = csv,
|
||||
bench = bench,
|
||||
test = run_tests,
|
||||
help = usage,
|
||||
["--help"] = usage,
|
||||
["-h"] = usage,
|
||||
}
|
||||
|
||||
local handler = commands[command]
|
||||
if handler == nil then
|
||||
echo("commande inconnue : " .. command)
|
||||
usage()
|
||||
os.exit(2)
|
||||
end
|
||||
|
||||
os.exit(handler(argv[2]) or 0)
|
||||
Reference in new issue
Block a user