From 3d549a53b74be24e216a4f8bfb524a2a4c066910 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Mon, 26 Mar 2018 16:52:03 -0700 Subject: [PATCH] First draft of running code. --- ldt.lua | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- ldt.moon | 54 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 121 insertions(+), 4 deletions(-) diff --git a/ldt.lua b/ldt.lua index 1546836..cf9f607 100644 --- a/ldt.lua +++ b/ldt.lua @@ -534,6 +534,7 @@ ldb = { end pads.src.filename = filename end + local stack_env local show_vars show_vars = function(stack_index) if pads.vars then @@ -544,13 +545,17 @@ ldb = { end local callstack_min, _ = callstack_range() local var_names, values = { }, { } + stack_env = setmetatable({ }, { + __index = _G + }) for loc = 1, 999 do local name, value = debug.getlocal(callstack_min + stack_index - 1, loc) - if value == nil then + if name == nil then break end table.insert(var_names, tostring(name)) table.insert(values, value) + stack_env[name] = value _ = [[ if type(value) == 'function' info = debug.getinfo(value, 'nS') --values\add_line(("function: %s @ %s:%s")\format(info.name or '???', info.short_src, info.linedefined)) @@ -599,7 +604,11 @@ ldb = { if #value_str >= value_w - 2 then local key_repr key_repr = function(k) - return type(k) == 'string' and k or "[" .. tostring(repr(k, 2)) .. "]" + if type(k) == 'string' and k:match("^[%a_][%w_]*$") then + return k + else + return "[" .. tostring(repr(k, 2)) .. "]" + end end value_str = table.concat((function() local _accum_0 = { } @@ -693,6 +702,64 @@ ldb = { select_pad(pads.vars) elseif ('d'):byte() == _exp_0 then select_pad(pads.values) + elseif (':'):byte() == _exp_0 or ('>'):btye() == _exp_0 or ('?'):byte() == _exp_0 then + C.echo(true) + local code = '' + if c == ('?'):byte() then + stdscr:mvaddstr(SCREEN_H - 1, 0, "? " .. (' '):rep(SCREEN_W - 1)) + stdscr:move(SCREEN_H - 1, 2) + code = 'return ' .. stdscr:getstr() + elseif c == (':'):byte() or c == ('>'):byte() then + local numlines = 1 + stdscr:mvaddstr(SCREEN_H - 1, 0, "> " .. (' '):rep(SCREEN_W - 1)) + stdscr:move(SCREEN_H - 1, 2) + while true do + local line = stdscr:getstr() + if line == '' then + break + end + code = code .. (line .. '\n') + numlines = numlines + 1 + stdscr:mvaddstr(SCREEN_H - numlines, 0, "> " .. ((' '):rep(SCREEN_W) .. '\n'):rep(numlines)) + stdscr:mvaddstr(SCREEN_H - numlines, 2, code) + stdscr:mvaddstr(SCREEN_H - 1, 0, (' '):rep(SCREEN_W)) + stdscr:move(SCREEN_H - 1, 0) + end + end + C.echo(false) + local output = "" + if not stack_env then + stack_env = setmetatable({ }, { + __index = _G + }) + end + stack_env.print = function(...) + for i = 1, select('#', ...) do + if i > 1 then + output = output .. '\t' + end + output = output .. tostring(select(i, ...)) + end + output = output .. "\n" + end + for _, p in pairs(pads) do + p:refresh(true) + end + local run_fn + run_fn, err_msg = load(code, 'user input', 't', stack_env) + if not run_fn then + stdscr:addstr(err_msg) + else + local ret = run_fn() + if ret ~= nil then + output = output .. ('= ' .. repr(ret) .. '\n') + end + local numlines = 0 + for nl in output:gmatch('\n') do + numlines = numlines + 1 + end + stdscr:mvaddstr(SCREEN_H - numlines, 0, output) + end elseif ('o'):byte() == _exp_0 then local file = stack_locations[pads.stack.selected] local filename, line_no = file:match("([^:]*):(.*)") diff --git a/ldt.moon b/ldt.moon index 0817a24..3014315 100644 --- a/ldt.moon +++ b/ldt.moon @@ -362,6 +362,7 @@ ldb = { pads.src = Pad "(S)ource Code", pads.err.height,0,pads.stack.height,pads.stack.x,lines, ->color("red") pads.src.filename = filename + local stack_env show_vars = (stack_index)-> if pads.vars pads.vars\erase! @@ -369,11 +370,13 @@ ldb = { pads.values\erase! callstack_min, _ = callstack_range! var_names, values = {}, {} + stack_env = setmetatable({}, {__index:_G}) for loc=1,999 name, value = debug.getlocal(callstack_min+stack_index-1, loc) - if value == nil then break + if name == nil then break table.insert(var_names, tostring(name)) table.insert(values, value) + stack_env[name] = value [[ if type(value) == 'function' info = debug.getinfo(value, 'nS') @@ -415,7 +418,9 @@ ldb = { value = rawget(value, '__base') value_str = repr(value, 3) if #value_str >= value_w-2 - key_repr = (k)-> type(k) == 'string' and k or "[#{repr(k,2)}]" + key_repr = (k)-> + if type(k) == 'string' and k\match("^[%a_][%w_]*$") then k + else "[#{repr(k,2)}]" value_str = table.concat ["#{key_repr k} = #{repr v,2}" for k,v in pairs(value)], "\n \n" pads.values = Pad "(D)ata [#{type_str}]",var_y,value_x,pads.vars.height,value_w, @@ -501,6 +506,51 @@ ldb = { when ('d')\byte! select_pad(pads.values) -- (D)ata + + when (':')\byte!, ('>')\btye!, ('?')\byte! + C.echo(true) + code = '' + if c == ('?')\byte! + stdscr\mvaddstr(SCREEN_H-1, 0, "? "..(' ')\rep(SCREEN_W-1)) + stdscr\move(SCREEN_H-1, 2) + code = 'return '..stdscr\getstr! + elseif c == (':')\byte! or c == ('>')\byte! + numlines = 1 + stdscr\mvaddstr(SCREEN_H-1, 0, "> "..(' ')\rep(SCREEN_W-1)) + stdscr\move(SCREEN_H-1, 2) + while true + line = stdscr\getstr! + if line == '' then break + code ..= line..'\n' + numlines += 1 + stdscr\mvaddstr(SCREEN_H-numlines, 0, "> "..((' ')\rep(SCREEN_W)..'\n')\rep(numlines)) + stdscr\mvaddstr(SCREEN_H-numlines, 2, code) + stdscr\mvaddstr(SCREEN_H-1, 0, (' ')\rep(SCREEN_W)) + stdscr\move(SCREEN_H-1, 0) + C.echo(false) + output = "" + if not stack_env + stack_env = setmetatable({}, {__index:_G}) + stack_env.print = (...)-> + for i=1,select('#',...) + if i > 1 then output ..= '\t' + output ..= tostring(select(i, ...)) + output ..= "\n" + + for _,p in pairs(pads) + p\refresh(true) + + run_fn, err_msg = load(code, 'user input', 't', stack_env) + if not run_fn + stdscr\addstr(err_msg) + else + ret = run_fn! + if ret != nil + output ..= '= '..repr(ret)..'\n' + numlines = 0 + for nl in output\gmatch('\n') do numlines += 1 + stdscr\mvaddstr(SCREEN_H-numlines, 0, output) + when ('o')\byte! file = stack_locations[pads.stack.selected]