main.lua

Sat, 19 Sep 2015 21:44:59 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Sat, 19 Sep 2015 21:44:59 +0100
changeset 21
8ac80da35408
parent 14
ad0dd3d45edc
child 30
3c2489e79074
permissions
-rwxr-xr-x

main: Refactor into functions

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.."'");
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
58 print(object.name, action.action.."...");
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
59 local ok, err = pcall(handler[action.action], object, action.extra);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
60 if not ok then
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
61 error(err);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
62 end
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
63 end
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
64 verse.log("info", "Completed script!");
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
65 verse.add_task(5, function ()
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
66 verse.quit();
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
67 end);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
68 end, {
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
69 error = function (runner, _err)
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
70 verse.log("error", "Runner caught error: %s", _err);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
71 ok, err = false, _err;
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
72 verse.quit();
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
73 end;
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
74 });
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 runner:run(true);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
77 verse.log("debug", "runner paused")
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
78
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
79 verse.loop();
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
80 return ok, err;
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
81 end
7
ecac723bb6e1 main: Run actions in async runner
Matthew Wild <mwild1@gmail.com>
parents: 3
diff changeset
82
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
83 local ok, result, err = pcall(main);
3
3cc860a893d2 main: verse.loop()
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
84
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
85 if not ok then
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
86 print("TEST ERROR:", result);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
87 elseif not result then
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
88 print("TEST FAILED", err);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
89 else
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
90 print("TEST PASSED");
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
91 end

mercurial