Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Parser/PushGPParser.cs @ 14513

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

#2665 Added Problem.ProgramSynthesis Project, Fixed Expression Issues, Fixed Code Generation

File size: 2.8 KB
Line 
1namespace HeuristicLab.Algorithms.PushGP.Parser {
2  using System.Collections.Generic;
3  using System.Globalization;
4
5  using HeuristicLab.Algorithms.PushGP.Expressions;
6
7  public static class PushGPParser {
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 Expression 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 Expression 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 ExecExpandExpression(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 expressions.Count == 1
65        ? expressions[0]
66        : new ExecExpandExpression(expressions.ToArray());
67    }
68
69    private static bool TryParseLiteral(string word, out Expression expression) {
70      long longValue;
71      if (long.TryParse(word, out longValue)) {
72        expression = new IntegerPushExpression(longValue);
73        return true;
74      }
75
76      double floatValue;
77      if (double.TryParse(word, NumberStyles.Float, cultureInfo, out floatValue)) {
78        expression = new FloatPushExpression(floatValue);
79        return true;
80      }
81
82      bool booleanValue;
83      if (bool.TryParse(word, out booleanValue)) {
84        expression = new BooleanPushExpression(booleanValue);
85        return true;
86      }
87
88      expression = null;
89      return false;
90    }
91  }
92}
Note: See TracBrowser for help on using the repository browser.