turtle/tlang: add numerical dot indexing

[1, 2, 3] now you can do .1 to get the 1st element (Lua style 1-indexed)
wsc-master
cron 2020-12-10 04:45:58 +00:00
parent f54064df07
commit bac0a83110
2 changed files with 17 additions and 4 deletions

View File

@ -228,8 +228,10 @@ local function test()
local paren_test = "('works' print) 'out' print"
local mapdot_test = [[
[1 a:5 b:[a:2 b:3] ] `a =
[1 a:5 b:[a:2 b:3] 3] `a =
4 `a.a =
a.1 print
a.2 print
a.a print
a.b.b print
]]
@ -241,7 +243,7 @@ local function test()
.a print
]]
local test = stackdot_test
local test = mapdot_test
--tlang.print_table(tlang.lex(test))
--tlang.print_table(tlang.parse(tlang.lex(test)))

View File

@ -47,6 +47,18 @@ local function parse_next(state)
return n
end
local function parse_identifier(state)
local lexid = parse_next(state).value
for i, v in ipairs(lexid) do
if v:match("^[0-9]+$") then
lexid[i] = tonumber(v)
end
end
return {type = "identifier", value = lexid}
end
local function parse_map(state)
local map = {}
local mapi = 1
@ -155,8 +167,7 @@ function internal.parse_step(state)
parse_next(state)
return {type = "string", value = n.value}
elseif n.subtype == "identifier" then
parse_next(state)
return {type = "identifier", value = n.value}
return parse_identifier(state)
elseif n.subtype == "quote" then
parse_next(state)
return {type = "quote", value = n.value}