1 | using System;
|
---|
2 | using System.Dynamic;
|
---|
3 | using System.IO;
|
---|
4 | using System.Text;
|
---|
5 | using System.Threading;
|
---|
6 | using HeuristicLab.Common;
|
---|
7 |
|
---|
8 | namespace HeuristicLab.HLScript {
|
---|
9 | public abstract class HLScriptGeneration {
|
---|
10 | protected dynamic vars;
|
---|
11 |
|
---|
12 | private readonly EventWriter console;
|
---|
13 | protected EventWriter Console {
|
---|
14 | get { return console; }
|
---|
15 | }
|
---|
16 |
|
---|
17 | protected HLScriptGeneration() {
|
---|
18 | console = new EventWriter(this);
|
---|
19 | }
|
---|
20 |
|
---|
21 | public abstract void Main();
|
---|
22 |
|
---|
23 | private void Execute(VariableStore variableStore) {
|
---|
24 | vars = new Variables(variableStore);
|
---|
25 | try {
|
---|
26 | Main();
|
---|
27 | } catch (ThreadAbortException) {
|
---|
28 | } catch (Exception e) {
|
---|
29 | Console.WriteLine("---");
|
---|
30 | Console.WriteLine(e);
|
---|
31 | }
|
---|
32 | }
|
---|
33 |
|
---|
34 | protected internal event EventHandler<EventArgs<string>> ConsoleOutputChanged;
|
---|
35 | private void OnConsoleOutputChanged(string args) {
|
---|
36 | var handler = ConsoleOutputChanged;
|
---|
37 | if (handler != null) handler(null, new EventArgs<string>(args));
|
---|
38 | }
|
---|
39 |
|
---|
40 | private class Variables : DynamicObject {
|
---|
41 | private readonly VariableStore variableStore;
|
---|
42 |
|
---|
43 | public Variables(VariableStore variableStore) {
|
---|
44 | this.variableStore = variableStore;
|
---|
45 | }
|
---|
46 |
|
---|
47 | public override bool TryGetMember(GetMemberBinder binder, out object result) {
|
---|
48 | return variableStore.TryGetValue(binder.Name, out result);
|
---|
49 | }
|
---|
50 |
|
---|
51 | public override bool TrySetMember(SetMemberBinder binder, object value) {
|
---|
52 | variableStore[binder.Name] = value;
|
---|
53 | return true;
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | protected class EventWriter : TextWriter {
|
---|
58 | private readonly HLScriptGeneration hlsg;
|
---|
59 |
|
---|
60 | public EventWriter(HLScriptGeneration hlsg) {
|
---|
61 | this.hlsg = hlsg;
|
---|
62 | }
|
---|
63 |
|
---|
64 | public override Encoding Encoding {
|
---|
65 | get { return Encoding.UTF8; }
|
---|
66 | }
|
---|
67 |
|
---|
68 | #region Write/WriteLine Overrides
|
---|
69 | #region Write
|
---|
70 | public override void Write(bool value) { hlsg.OnConsoleOutputChanged(value.ToString()); }
|
---|
71 | public override void Write(char value) { hlsg.OnConsoleOutputChanged(value.ToString()); }
|
---|
72 | public override void Write(char[] buffer) { hlsg.OnConsoleOutputChanged(new string(buffer)); }
|
---|
73 | public override void Write(char[] buffer, int index, int count) { hlsg.OnConsoleOutputChanged(new string(buffer, index, count)); }
|
---|
74 | public override void Write(decimal value) { hlsg.OnConsoleOutputChanged(value.ToString()); }
|
---|
75 | public override void Write(double value) { hlsg.OnConsoleOutputChanged(value.ToString()); }
|
---|
76 | public override void Write(float value) { hlsg.OnConsoleOutputChanged(value.ToString()); }
|
---|
77 | public override void Write(int value) { hlsg.OnConsoleOutputChanged(value.ToString()); }
|
---|
78 | public override void Write(long value) { hlsg.OnConsoleOutputChanged(value.ToString()); }
|
---|
79 | public override void Write(object value) { hlsg.OnConsoleOutputChanged(value.ToString()); }
|
---|
80 | public override void Write(string value) { hlsg.OnConsoleOutputChanged(value); }
|
---|
81 | public override void Write(string format, object arg0) { hlsg.OnConsoleOutputChanged(string.Format(format, arg0)); }
|
---|
82 | public override void Write(string format, object arg0, object arg1) { hlsg.OnConsoleOutputChanged(string.Format(format, arg0, arg0)); }
|
---|
83 | public override void Write(string format, object arg0, object arg1, object arg2) { hlsg.OnConsoleOutputChanged(string.Format(format, arg0, arg1, arg2)); }
|
---|
84 | public override void Write(string format, params object[] arg) { hlsg.OnConsoleOutputChanged(string.Format(format, arg)); }
|
---|
85 | public override void Write(uint value) { hlsg.OnConsoleOutputChanged(value.ToString()); }
|
---|
86 | public override void Write(ulong value) { hlsg.OnConsoleOutputChanged(value.ToString()); }
|
---|
87 | #endregion
|
---|
88 |
|
---|
89 | #region WriteLine
|
---|
90 | public override void WriteLine() { hlsg.OnConsoleOutputChanged(Environment.NewLine); }
|
---|
91 | public override void WriteLine(bool value) { hlsg.OnConsoleOutputChanged(value + Environment.NewLine); }
|
---|
92 | public override void WriteLine(char value) { hlsg.OnConsoleOutputChanged(value + Environment.NewLine); }
|
---|
93 | public override void WriteLine(char[] buffer) { hlsg.OnConsoleOutputChanged(new string(buffer) + Environment.NewLine); }
|
---|
94 | public override void WriteLine(char[] buffer, int index, int count) { hlsg.OnConsoleOutputChanged(new string(buffer, index, count) + Environment.NewLine); }
|
---|
95 | public override void WriteLine(decimal value) { hlsg.OnConsoleOutputChanged(value + Environment.NewLine); }
|
---|
96 | public override void WriteLine(double value) { hlsg.OnConsoleOutputChanged(value + Environment.NewLine); }
|
---|
97 | public override void WriteLine(float value) { hlsg.OnConsoleOutputChanged(value + Environment.NewLine); }
|
---|
98 | public override void WriteLine(int value) { hlsg.OnConsoleOutputChanged(value + Environment.NewLine); }
|
---|
99 | public override void WriteLine(long value) { hlsg.OnConsoleOutputChanged(value + Environment.NewLine); }
|
---|
100 | public override void WriteLine(object value) { hlsg.OnConsoleOutputChanged(value + Environment.NewLine); }
|
---|
101 | public override void WriteLine(string value) { hlsg.OnConsoleOutputChanged(value + Environment.NewLine); }
|
---|
102 | public override void WriteLine(string format, object arg0) { hlsg.OnConsoleOutputChanged(string.Format(format, arg0) + Environment.NewLine); }
|
---|
103 | public override void WriteLine(string format, object arg0, object arg1) { hlsg.OnConsoleOutputChanged(string.Format(format, arg0, arg1) + Environment.NewLine); }
|
---|
104 | public override void WriteLine(string format, object arg0, object arg1, object arg2) { hlsg.OnConsoleOutputChanged(string.Format(format, arg0, arg1, arg2) + Environment.NewLine); }
|
---|
105 | public override void WriteLine(string format, params object[] arg) { hlsg.OnConsoleOutputChanged(string.Format(format, arg) + Environment.NewLine); }
|
---|
106 | public override void WriteLine(uint value) { hlsg.OnConsoleOutputChanged(value + Environment.NewLine); }
|
---|
107 | public override void WriteLine(ulong value) { hlsg.OnConsoleOutputChanged(value + Environment.NewLine); }
|
---|
108 | #endregion
|
---|
109 | #endregion
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|