bug fix in xrange() that would cause it to yield bogus values
[vox.git] / examples / debug.vx
blob85ac696f034ab320f0e77a61eb8cf73be65e000d
2 local sandbox = import("core/sandbox")
4 local function begin_debug()
6     system.enabledebuginfo(false)
7     system.setdebughook(function(type=null, src=null, line=null, funcname=null)
8     {
9         local action = "unknown"
10         local msg, pre, premsg, isposix = (system.platform_group() == "posix")
11         local mkc = function(light, code)
12             if(isposix)
13                 return "\e[%d;%dm".fmt(light, code)
14             else
15                 return ""
16         local colors =
17         {
18             red =    mkc(0, 31)
19             blue =   mkc(0, 34)
20             yellow = mkc(0, 33)
21             end =    isposix ? "\e[0m" : ""
22         }
23         switch(type)
24         {
25             case 'r':
26                 action = "returned from function %s".fmt(funcname.quote());
27                 break
28             case 'c':
29                 action = "call to function %s".fmt(funcname.quote())
30                 break
31             case 'l':
32                 action = "entering new line"
33                 break
34         }
35         #if(type != '-')
36         #{
37             pre = colors.red + "**debug**" + colors.end
38             premsg = pre + " in %s<%s:%d>%s: ".fmt(colors.blue, src, line, colors.end)
39             msg = premsg + colors.yellow + action + colors.end + "\n"
40             io.stderr.write(msg)
41             io.stderr.flush()
42             system.gc_collect()
43         #}
44     })
47 if(system.argv.len() > 1)
49     begin_debug()
50     local new_argv = system.argv
51     local path = system.argv[1]
52     local sb, env = {}
53     new_argv.remove(1)
54     foreach(k, v in this)
55     {
56         env[k] = v
57     }
58     if(!env.get("system"))
59         env.system = {}
60     env.system.argv = new_argv
61     sb = sandbox(env)
62     sb.callfile(path)
64 else
66     println("Usage: %s <filename> [args]".fmt(system.argv[0]))