main.lua

Tue, 27 Oct 2015 23:07:46 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Tue, 27 Oct 2015 23:07:46 +0000
changeset 43
b37504fa3031
parent 30
3c2489e79074
child 46
d711bf1e9979
permissions
-rwxr-xr-x

Add annotations to actions (by grabbing the preceding comment)

0
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env lua5.1
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
3
0
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
5 function read_script()
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
6 io.input(arg[1]);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
7 return io.read("*a");
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
8
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
9 end
0
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
11 function parse_script(data)
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
12 local parser = require "scansion.parser";
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
13 return assert(parser.parse(data));
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
14 end
0
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
16 function initialize_script(script)
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
17 local c = 0;
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
18 for name, object in pairs(script.objects) do
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
19 local o = require("scansion.objects."..object.type);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
20 object.handler = o;
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
21 object.script = script;
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
22 o._validate(object);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
23 c = c + 1;
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
24 end
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
25
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
26 --print("Script defines "..c.." objects, and "..#script.actions.." actions");
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
27 return script;
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
28 end
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
29
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
30 function initialize_verse(errcb)
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
31 local verse = require "verse";
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
32
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
33 verse.set_log_handler(verse._default_log_handler, { "debug", "info", "warn", "error" });
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
34
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
35 local function error_handler(err)
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
36 verse.log("error", "Error: %s", err);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
37 verse.log("error", "Traceback: %s", debug.traceback());
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
38 errcb(err);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
39 end
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
40 verse.set_error_handler(error_handler);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
41 return verse;
0
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 end
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
44 function main()
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
45 local ok, err = true;
0
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
47 local script = initialize_script(parse_script(read_script()));
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
48
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
49 local verse = initialize_verse(function (_err) ok, err = false, _err end);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
50 local async = require "scansion.async";
7
ecac723bb6e1 main: Run actions in async runner
Matthew Wild <mwild1@gmail.com>
parents: 3
diff changeset
51
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
52
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
53 local runner = async.runner(function (d)
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
54 for _, action in pairs(script.actions) do
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
55 local object = script.objects[action.object_name];
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
56 local handler = object.handler;
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
57 assert(handler[action.action], "Objects of type '"..object.type.."' do not support action '"..action.action.."'");
43
b37504fa3031 Add annotations to actions (by grabbing the preceding comment)
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
58 if action.annotation then
b37504fa3031 Add annotations to actions (by grabbing the preceding comment)
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
59 print(action.annotation);
b37504fa3031 Add annotations to actions (by grabbing the preceding comment)
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
60 end
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
61 print(object.name, action.action.."...");
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
62 local ok, err = pcall(handler[action.action], object, action.extra);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
63 if not ok then
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
64 error(err);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
65 end
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
66 end
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
67 verse.log("info", "Completed script!");
30
3c2489e79074 main: Remove 5s delay on quitting (was a hack for debug purposes)
Matthew Wild <mwild1@gmail.com>
parents: 21
diff changeset
68 verse.quit();
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
69 end, {
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
70 error = function (runner, _err)
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
71 verse.log("error", "Runner caught error: %s", _err);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
72 ok, err = false, _err;
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
73 verse.quit();
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
74 end;
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
75 });
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
76
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
77 runner:run(true);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
78 verse.log("debug", "runner paused")
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
79
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
80 verse.loop();
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
81 return ok, err;
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
82 end
7
ecac723bb6e1 main: Run actions in async runner
Matthew Wild <mwild1@gmail.com>
parents: 3
diff changeset
83
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
84 local ok, result, err = pcall(main);
3
3cc860a893d2 main: verse.loop()
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
85
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
86 if not ok then
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
87 print("TEST ERROR:", result);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
88 elseif not result then
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
89 print("TEST FAILED", err);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
90 else
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
91 print("TEST PASSED");
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
92 end

mercurial