|
1 | 1 | package com.annimon.ownlang.parser.ast;
|
2 | 2 |
|
3 | 3 | import com.annimon.ownlang.exceptions.PatternMatchingException;
|
| 4 | +import com.annimon.ownlang.lib.ArrayValue; |
4 | 5 | import com.annimon.ownlang.lib.NumberValue;
|
| 6 | +import com.annimon.ownlang.lib.Types; |
5 | 7 | import com.annimon.ownlang.lib.Value;
|
6 | 8 | import com.annimon.ownlang.lib.Variables;
|
| 9 | +import java.util.ArrayList; |
7 | 10 | import java.util.List;
|
8 | 11 |
|
9 | 12 | /**
|
@@ -48,10 +51,94 @@ public Value eval() {
|
48 | 51 | Variables.remove(pattern.variable);
|
49 | 52 | }
|
50 | 53 | }
|
| 54 | + if ((value.type() == Types.ARRAY) && (p instanceof ListPattern)) { |
| 55 | + final ListPattern pattern = (ListPattern) p; |
| 56 | + if (matchListPattern((ArrayValue) value, pattern)) { |
| 57 | + // Clean up variables if matched |
| 58 | + final Value result = evalResult(p.result); |
| 59 | + for (String var : pattern.parts) { |
| 60 | + Variables.remove(var); |
| 61 | + } |
| 62 | + return result; |
| 63 | + } |
| 64 | + } |
51 | 65 | }
|
52 | 66 | throw new PatternMatchingException("No pattern were matched");
|
53 | 67 | }
|
54 | 68 |
|
| 69 | + private boolean matchListPattern(ArrayValue array, ListPattern p) { |
| 70 | + final List<String> parts = p.parts; |
| 71 | + final int partsSize = parts.size(); |
| 72 | + final int arraySize = array.size(); |
| 73 | + switch (partsSize) { |
| 74 | + case 0: // match [] { case []: ... } |
| 75 | + if ((arraySize == 0) && optMatches(p)) { |
| 76 | + return true; |
| 77 | + } |
| 78 | + return false; |
| 79 | + |
| 80 | + case 1: // match arr { case [x]: x = arr ... } |
| 81 | + final String variable = parts.get(0); |
| 82 | + Variables.set(variable, array); |
| 83 | + if (optMatches(p)) { |
| 84 | + return true; |
| 85 | + } |
| 86 | + Variables.remove(variable); |
| 87 | + return false; |
| 88 | + |
| 89 | + default: { // match arr { case [...]: .. } |
| 90 | + if (partsSize == arraySize) { |
| 91 | + // match [0, 1, 2] { case [a::b::c]: a=0, b=1, c=2 ... } |
| 92 | + return matchListPatternEqualsSize(p, parts, partsSize, array); |
| 93 | + } else if (partsSize < arraySize) { |
| 94 | + // match [1, 2, 3] { case [head :: tail]: ... } |
| 95 | + return matchListPatternWithTail(p, parts, partsSize, array, arraySize); |
| 96 | + } |
| 97 | + return false; |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private boolean matchListPatternEqualsSize(ListPattern p, List<String> parts, int partsSize, ArrayValue array) { |
| 103 | + // Set variables |
| 104 | + for (int i = 0; i < partsSize; i++) { |
| 105 | + Variables.set(parts.get(i), array.get(i)); |
| 106 | + } |
| 107 | + if (optMatches(p)) { |
| 108 | + // Clean up will be provided after evaluate result |
| 109 | + return true; |
| 110 | + } |
| 111 | + // Clean up variables if no match |
| 112 | + for (String var : parts) { |
| 113 | + Variables.remove(var); |
| 114 | + } |
| 115 | + return false; |
| 116 | + } |
| 117 | + |
| 118 | + private boolean matchListPatternWithTail(ListPattern p, List<String> parts, int partsSize, ArrayValue array, int arraySize) { |
| 119 | + // Set element variables |
| 120 | + final int lastPart = partsSize - 1; |
| 121 | + for (int i = 0; i < lastPart; i++) { |
| 122 | + Variables.set(parts.get(i), array.get(i)); |
| 123 | + } |
| 124 | + // Set tail variable |
| 125 | + final ArrayValue tail = new ArrayValue(arraySize - partsSize + 1); |
| 126 | + for (int i = lastPart; i < arraySize; i++) { |
| 127 | + tail.set(i - lastPart, array.get(i)); |
| 128 | + } |
| 129 | + Variables.set(parts.get(lastPart), tail); |
| 130 | + // Check optional condition |
| 131 | + if (optMatches(p)) { |
| 132 | + // Clean up will be provided after evaluate result |
| 133 | + return true; |
| 134 | + } |
| 135 | + // Clean up variables |
| 136 | + for (String var : parts) { |
| 137 | + Variables.remove(var); |
| 138 | + } |
| 139 | + return false; |
| 140 | + } |
| 141 | + |
55 | 142 | private boolean match(Value value, Value constant) {
|
56 | 143 | if (value.type() != constant.type()) return false;
|
57 | 144 | return value.equals(constant);
|
@@ -117,4 +204,25 @@ public String toString() {
|
117 | 204 | return variable + ": " + result;
|
118 | 205 | }
|
119 | 206 | }
|
| 207 | + |
| 208 | + public static class ListPattern extends Pattern { |
| 209 | + public List<String> parts; |
| 210 | + |
| 211 | + public ListPattern() { |
| 212 | + this(new ArrayList<String>()); |
| 213 | + } |
| 214 | + |
| 215 | + public ListPattern(List<String> parts) { |
| 216 | + this.parts = parts; |
| 217 | + } |
| 218 | + |
| 219 | + public void add(String part) { |
| 220 | + parts.add(part); |
| 221 | + } |
| 222 | + |
| 223 | + @Override |
| 224 | + public String toString() { |
| 225 | + return parts + ": " + result; |
| 226 | + } |
| 227 | + } |
120 | 228 | }
|
0 commit comments