1 | using System.Collections.Generic;
|
---|
2 | using System.Globalization;
|
---|
3 | using HeuristicLab.Algorithms.PushGP.Expressions;
|
---|
4 | using HeuristicLab.Algorithms.PushGP.Expressions.Boolean;
|
---|
5 | using HeuristicLab.Algorithms.PushGP.Expressions.Code;
|
---|
6 | using HeuristicLab.Algorithms.PushGP.Expressions.Exec;
|
---|
7 | using HeuristicLab.Algorithms.PushGP.Expressions.Float;
|
---|
8 | using HeuristicLab.Algorithms.PushGP.Expressions.Integer;
|
---|
9 | using HeuristicLab.Algorithms.PushGP.Expressions.Name;
|
---|
10 |
|
---|
11 | namespace HeuristicLab.Algorithms.PushGP
|
---|
12 | {
|
---|
13 | public static class Parser
|
---|
14 | {
|
---|
15 | private static readonly CultureInfo cultureInfo = CultureInfo.CreateSpecificCulture("en-US");
|
---|
16 |
|
---|
17 | private const string openBrace = "(";
|
---|
18 | private const string closeBrace = ")";
|
---|
19 | private const char delimiter = ' ';
|
---|
20 | private static readonly char[] symbolTrim = new[] { '\r', '\n' };
|
---|
21 |
|
---|
22 | public static Expression Parse(string source, int startIndex = 0)
|
---|
23 | {
|
---|
24 | var symbols = source.Split(delimiter);
|
---|
25 |
|
---|
26 | int endIndex;
|
---|
27 | return Parse(symbols, 0, out endIndex);
|
---|
28 | }
|
---|
29 |
|
---|
30 | private static Expression Parse(string[] symbols, int startIndex, out int endIndex)
|
---|
31 | {
|
---|
32 | var expressions = new List<Expression>();
|
---|
33 |
|
---|
34 | for (var i = startIndex; i < symbols.Length; i++)
|
---|
35 | {
|
---|
36 | var symbol = symbols[i].TrimEnd(symbolTrim);
|
---|
37 |
|
---|
38 | if (string.IsNullOrWhiteSpace(symbol))
|
---|
39 | {
|
---|
40 | continue;
|
---|
41 | }
|
---|
42 |
|
---|
43 | if (symbol == openBrace)
|
---|
44 | {
|
---|
45 | var subExpression = Parse(symbols, i + 1, out endIndex);
|
---|
46 | expressions.Insert(0, subExpression);
|
---|
47 | i = endIndex;
|
---|
48 | continue;
|
---|
49 | }
|
---|
50 | else if (symbol == closeBrace)
|
---|
51 | {
|
---|
52 | endIndex = i;
|
---|
53 | return expressions.Count != 0
|
---|
54 | ? new ExecExpandExpression(expressions.ToArray())
|
---|
55 | : new CodeNoopExpression() as Expression;
|
---|
56 | }
|
---|
57 |
|
---|
58 | // literal
|
---|
59 | Expression expression;
|
---|
60 | if (TryParseLiteral(symbol, out expression))
|
---|
61 | {
|
---|
62 | expressions.Insert(0, expression);
|
---|
63 | continue;
|
---|
64 | }
|
---|
65 |
|
---|
66 | // expression
|
---|
67 | OpCode opCode;
|
---|
68 | if (SymbolTable.TryGetOpCode(symbol, out opCode))
|
---|
69 | {
|
---|
70 | expression = ExpressionFactory.Create(opCode);
|
---|
71 | expressions.Insert(0, expression);
|
---|
72 | continue;
|
---|
73 | }
|
---|
74 |
|
---|
75 | // identifier - custom expression or named literals
|
---|
76 | expressions.Insert(0, new NameDefineXExecExpression(symbol));
|
---|
77 | }
|
---|
78 |
|
---|
79 | endIndex = symbols.Length - 1;
|
---|
80 | return expressions.Count == 1
|
---|
81 | ? expressions[0]
|
---|
82 | : new ExecExpandExpression(expressions.ToArray());
|
---|
83 | }
|
---|
84 |
|
---|
85 | private static bool TryParseLiteral(string word, out Expression expression)
|
---|
86 | {
|
---|
87 | long longValue;
|
---|
88 | if (long.TryParse(word, out longValue))
|
---|
89 | {
|
---|
90 | expression = new IntegerPushExpression(longValue);
|
---|
91 | return true;
|
---|
92 | }
|
---|
93 |
|
---|
94 | double floatValue;
|
---|
95 | if (double.TryParse(word, NumberStyles.Float, cultureInfo, out floatValue))
|
---|
96 | {
|
---|
97 | expression = new FloatPushExpression(floatValue);
|
---|
98 | return true;
|
---|
99 | }
|
---|
100 |
|
---|
101 | bool booleanValue;
|
---|
102 | if (bool.TryParse(word, out booleanValue))
|
---|
103 | {
|
---|
104 | expression = new BooleanPushExpression(booleanValue);
|
---|
105 | return true;
|
---|
106 | }
|
---|
107 |
|
---|
108 | expression = null;
|
---|
109 | return false;
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|