initial import: lmcp 0.1.0

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-14 19:58:40 +00:00
parent 6bf0f450dc
commit c6efc8f685
5 changed files with 335 additions and 217 deletions
+11 -10
View File
@@ -89,7 +89,6 @@ end
-- Decode --
local decode_value
local MAX_DEPTH = 64
local ws_chars = { [' '] = true, ['\t'] = true, ['\n'] = true, ['\r'] = true }
local function skip_ws(s, pos)
@@ -144,14 +143,14 @@ local function decode_number(s, pos)
return n, pos
end
local function decode_array(s, pos, depth)
local function decode_array(s, pos)
pos = pos + 1 -- skip [
local arr = {}
pos = skip_ws(s, pos)
if s:sub(pos, pos) == ']' then return arr, pos + 1 end
while true do
local val
val, pos = decode_value(s, pos, (depth or 0) + 1)
val, pos = decode_value(s, pos)
arr[#arr + 1] = val
pos = skip_ws(s, pos)
local c = s:sub(pos, pos)
@@ -161,7 +160,7 @@ local function decode_array(s, pos, depth)
end
end
local function decode_object(s, pos, depth)
local function decode_object(s, pos)
pos = pos + 1 -- skip {
local obj = {}
pos = skip_ws(s, pos)
@@ -175,7 +174,7 @@ local function decode_object(s, pos, depth)
if s:sub(pos, pos) ~= ':' then error('expected : at ' .. pos) end
pos = skip_ws(s, pos + 1)
local val
val, pos = decode_value(s, pos, (depth or 0) + 1)
val, pos = decode_value(s, pos)
obj[key] = val
pos = skip_ws(s, pos)
local c = s:sub(pos, pos)
@@ -185,14 +184,12 @@ local function decode_object(s, pos, depth)
end
end
decode_value = function(s, pos, depth)
depth = depth or 0
if depth > MAX_DEPTH then error("JSON nesting too deep") end
decode_value = function(s, pos)
pos = skip_ws(s, pos)
local c = s:sub(pos, pos)
if c == '"' then return decode_string(s, pos)
elseif c == '{' then return decode_object(s, pos, depth)
elseif c == '[' then return decode_array(s, pos, depth)
elseif c == '{' then return decode_object(s, pos)
elseif c == '[' then return decode_array(s, pos)
elseif c == 't' then
if s:sub(pos, pos + 3) == 'true' then return true, pos + 4 end
elseif c == 'f' then
@@ -213,5 +210,9 @@ end
-- Sentinel for JSON null
json.null = setmetatable({}, { __tostring = function() return 'null' end })
-- Helper: encode a table as a JSON array even if empty
function json.array(t)
return setmetatable(t or {}, { __is_array = true })
end
return json