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