Changeset 14952 for branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Parser
- Timestamp:
- 05/10/17 11:23:05 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Parser/PushParser.cs
r14777 r14952 2 2 using System.Collections.Generic; 3 3 using System.Globalization; 4 4 using System.Linq; 5 5 using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions; 6 6 … … 57 57 // expression 58 58 if (ExpressionTable.TryGetStatelessExpression(symbol, out expression) 59 59 || ExpressionTable.TryGetStatefulExpression(symbol, out expression)) { 60 60 expressions.Insert(0, expression); 61 61 continue; … … 79 79 80 80 private static bool TryParseLiteral(string word, out Expression expression) { 81 if (word.StartsWith("'") && word.EndsWith("'") && word.Length == 3) { 82 expression = new CharPushExpression(word[1]); 83 return true; 84 } 85 86 if (word.StartsWith("\"") && word.EndsWith("\"")) { 87 expression = new StringPushExpression(word); 88 return true; 89 } 90 91 // "[]" has no values and can therefer not parsed correctly as vector of a specific type 92 if (word.StartsWith("[") && word.EndsWith("]") && word.Length > 3) { 93 var vectorEntries = word 94 .Substring(1, word.Length - 2) 95 .Split(',') 96 .Select(x => { 97 Expression e; 98 var success = TryParseLiteral(x.Trim(), out e); 99 100 return new { success, e, type = e.GetType() }; 101 }) 102 .ToArray(); 103 104 if (vectorEntries.Length == 0) { 105 expression = null; 106 return false; 107 } 108 109 var vectorEntryType = vectorEntries.First().type; 110 if (vectorEntries.Any(x => !x.success || x.type != vectorEntryType)) { 111 expression = null; 112 return false; 113 } 114 115 var result = vectorEntries 116 .Select(x => x.e) 117 .ToArray(); 118 119 // TODO 120 //switch(vectorEntryType.GetHashCode()) 121 //{ 122 // case typeof(IntegerPushExpression).GetHashCode(): return new IntegerVectorPushExpression(result) 123 //} 124 125 //expression = new StringPushExpression(word); 126 //return true; 127 } 128 81 129 long longValue; 82 130 if (long.TryParse(word, out longValue)) {
Note: See TracChangeset
for help on using the changeset viewer.