Skip to content

Commit 09556f7

Browse files
committed
Операция добавления элемента в массив и слияние массивов
1 parent fd0b8bd commit 09556f7

File tree

4 files changed

+67
-14
lines changed

4 files changed

+67
-14
lines changed

program.own

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,10 @@ print function(map["+"], 4, 5)
9191
print "\n"
9292
foreach(map, def(op, func) = echo (4, op, 5, "=", func(4,5)))
9393
foreach(arr, ::echo)
94+
95+
arr1 = [1,2,3]
96+
arr1 = arr1 :: 4
97+
arr2 = [5,6,7]
98+
print arr1
99+
print "\n"
100+
print arr1 << arr2

src/com/annimon/ownlang/lib/ArrayValue.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,24 @@
99
*/
1010
public final class ArrayValue implements Value, Iterable<Value> {
1111

12+
public static ArrayValue add(ArrayValue array, Value value) {
13+
final int last = array.elements.length;
14+
final ArrayValue result = new ArrayValue(last + 1);
15+
System.arraycopy(array.elements, 0, result.elements, 0, last);
16+
result.elements[last] = value;
17+
return result;
18+
}
19+
20+
public static ArrayValue merge(ArrayValue array1, ArrayValue array2) {
21+
final int length1 = array1.elements.length;
22+
final int length2 = array2.elements.length;
23+
final int length = length1 + length2;
24+
final ArrayValue result = new ArrayValue(length);
25+
System.arraycopy(array1.elements, 0, result.elements, 0, length1);
26+
System.arraycopy(array2.elements, 0, result.elements, length1, length2);
27+
return result;
28+
}
29+
1230
private final Value[] elements;
1331

1432
public ArrayValue(int size) {

src/com/annimon/ownlang/parser/Parser.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,10 @@ private Expression additive() {
356356
result = new BinaryExpression(BinaryExpression.Operator.SUBTRACT, result, multiplicative());
357357
continue;
358358
}
359+
if (match(TokenType.COLONCOLON)) {
360+
result = new BinaryExpression(BinaryExpression.Operator.PUSH, result, multiplicative());
361+
continue;
362+
}
359363
break;
360364
}
361365

src/com/annimon/ownlang/parser/ast/BinaryExpression.java

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public static enum Operator {
1717
MULTIPLY("*"),
1818
DIVIDE("/"),
1919
REMAINDER("%"),
20+
PUSH("::"),
2021
// Bitwise
2122
AND("&"),
2223
OR("|"),
@@ -50,23 +51,46 @@ public BinaryExpression(Operator operation, Expression expr1, Expression expr2)
5051
public Value eval() {
5152
final Value value1 = expr1.eval();
5253
final Value value2 = expr2.eval();
53-
if ( (value1 instanceof StringValue) || (value1 instanceof ArrayValue) ) {
54-
final String string1 = value1.asString();
55-
switch (operation) {
56-
case MULTIPLY: {
57-
final int iterations = (int) value2.asNumber();
58-
final StringBuilder buffer = new StringBuilder();
59-
for (int i = 0; i < iterations; i++) {
60-
buffer.append(string1);
61-
}
62-
return new StringValue(buffer.toString());
54+
55+
if (value1 instanceof StringValue) {
56+
return eval((StringValue) value1, value2);
57+
}
58+
if (value1 instanceof ArrayValue) {
59+
return eval((ArrayValue) value1, value2);
60+
}
61+
return eval(value1, value2);
62+
}
63+
64+
private Value eval(StringValue value1, Value value2) {
65+
final String string1 = value1.asString();
66+
switch (operation) {
67+
case MULTIPLY: {
68+
final int iterations = (int) value2.asNumber();
69+
final StringBuilder buffer = new StringBuilder();
70+
for (int i = 0; i < iterations; i++) {
71+
buffer.append(string1);
6372
}
64-
case ADD:
65-
default:
66-
return new StringValue(string1 + value2.asString());
73+
return new StringValue(buffer.toString());
6774
}
75+
case ADD:
76+
default:
77+
return new StringValue(string1 + value2.asString());
6878
}
69-
79+
}
80+
81+
private Value eval(ArrayValue value1, Value value2) {
82+
switch (operation) {
83+
case LSHIFT:
84+
if (!(value2 instanceof ArrayValue))
85+
throw new RuntimeException("Cannot merge non array value to array");
86+
return ArrayValue.merge(value1, (ArrayValue) value2);
87+
case PUSH:
88+
default:
89+
return ArrayValue.add(value1, value2);
90+
}
91+
}
92+
93+
private Value eval(Value value1, Value value2) {
7094
final double number1 = value1.asNumber();
7195
final double number2 = value2.asNumber();
7296
double result;

0 commit comments

Comments
 (0)