Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Parser/PushParser.cs @ 14745

Last change on this file since 14745 was 14745, checked in by pkimmesw, 7 years ago

#2665 Merged ExecExpandExpression and PushProgram due to performance reasons, Tested managed object pooling

File size: 2.7 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Push.Parser {
2  using System.Collections.Generic;
3  using System.Globalization;
4
5  using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
6
7  public static class PushParser {
8    private const string openBrace = "(";
9
10    private const string closeBrace = ")";
11
12    private const char delimiter = ' ';
13
14    private static readonly CultureInfo cultureInfo = CultureInfo.CreateSpecificCulture("en-US");
15
16    private static readonly char[] symbolTrim = { '\r', '\n' };
17
18    public static PushProgram Parse(string source, int startIndex = 0) {
19      var symbols = source.Split(delimiter);
20
21      int endIndex;
22      return Parse(symbols, 0, out endIndex);
23    }
24
25    private static PushProgram Parse(string[] symbols, int startIndex, out int endIndex) {
26      var expressions = new List<Expression>();
27
28      for (var i = startIndex; i < symbols.Length; i++) {
29        var symbol = symbols[i].TrimEnd(symbolTrim);
30
31        if (string.IsNullOrWhiteSpace(symbol)) continue;
32
33        switch (symbol) {
34          case openBrace:
35            var subExpression = Parse(symbols, i + 1, out endIndex);
36            expressions.Insert(0, subExpression);
37            i = endIndex;
38            continue;
39          case closeBrace:
40            endIndex = i;
41            return new PushProgram(expressions.ToArray());
42        }
43
44        // literal
45        Expression expression;
46        if (TryParseLiteral(symbol, out expression)) {
47          expressions.Insert(0, expression);
48          continue;
49        }
50
51        // expression
52        if (ExpressionTable.TryGetStatelessExpression(symbol, out expression)
53            || ExpressionTable.TryGetStatefullExpression(symbol, out expression)) {
54          expressions.Insert(0, expression);
55          continue;
56        }
57
58        // identifier - custom expression or named literals
59        expressions.Insert(0, new NameDefineXExecExpression(symbol));
60      }
61
62      endIndex = symbols.Length - 1;
63
64      return new PushProgram(expressions.ToArray());
65    }
66
67    private static bool TryParseLiteral(string word, out Expression expression) {
68      long longValue;
69      if (long.TryParse(word, out longValue)) {
70        expression = new IntegerPushExpression(longValue);
71        return true;
72      }
73
74      double floatValue;
75      if (double.TryParse(word, NumberStyles.Float, cultureInfo, out floatValue)) {
76        expression = new FloatPushExpression(floatValue);
77        return true;
78      }
79
80      bool booleanValue;
81      if (bool.TryParse(word, out booleanValue)) {
82        expression = new BooleanPushExpression(booleanValue);
83        return true;
84      }
85
86      expression = null;
87      return false;
88    }
89  }
90}
Note: See TracBrowser for help on using the repository browser.