Module:Polola

From Linguifex
Revision as of 08:34, 3 July 2026 by Nyima (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search


local p = {}

-- Helper function to trim whitespaces
local function trim(s)
    return s:match("^%s*(.-)%s*$")
end

-- Parse function for Polola language grammar and structures
function p.parse(frame)
    -- Read 'expr' parameter from template or direct call
    local input = frame.args.expr or frame:getParent().args.expr or ""
    if trim(input) == "" then
        return "<span style='color:red;'>Error: No expression provided!</span>"
    end

    local result = {}
    
    -- Safe tokenization using escaped pattern matching
    -- % character escapes operators like +, *, /, and brackets () so they don't cause capture errors
    for token in string.gmatch(input, "([^%s%+%*%/]+)") do
        -- Clean brackets from the token safely
        local clean_token = token:gsub("%(", ""):gsub("%)", "")
        
        -- Separate the base word from numeric quantifiers (e.g., sfg3 -> base: sfg, quantifier: 3)
        local base_word, quantifier = clean_token:match("^([a-z]+)(%d*)$")
        
        if base_word then
            local meaning = ""
            -- Basic Polola vocabulary translation logic
            if string.sub(base_word, 1, 1) == "l" then
                meaning = "distant/hidden concept (" .. base_word .. ")"
            elseif string.sub(base_word, 1, 1) == "s" then
                meaning = "present/near concept (" .. base_word .. ")"
            else
                meaning = "general concept (" .. base_word .. ")"
            end
            
            -- Append quantifier info if present
            if quantifier and quantifier ~= "" then
                meaning = meaning .. " [quantifier: " .. quantifier .. "x]"
            end
            table.insert(result, meaning)
        end
    end

    -- Check for complex structural operators
    local structure_info = ""
    if input:match("%(") or input:match("%+") or input:match("%*") or input:match("%/") then
        structure_info = "<br/><small><i>Structure: Complex logical/associative relation detected.</i></small>"
    end

    -- Return final formatted output
    if #result == 0 then
        return "Polola syntax: " .. input .. " → Uninterpretable expression."
    else
        return "Polola syntax: <b>" .. input .. "</b><br/>Interpretation: " .. table.concat(result, ", ") .. structure_info
    end
end

return p