Skip to content

Commit d0c8c75

Browse files
committed
Исправлен последовательный доступ к элементам map
1 parent f885475 commit d0c8c75

File tree

4 files changed

+30
-22
lines changed

4 files changed

+30
-22
lines changed

examples/network/github_timeline.own

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,11 @@ def show_github_events(event) {
2828

2929
def github_event_type(event) {
3030
type = event["type"]
31-
repo = event["repo"]
32-
repo = "https://github.com/" + repo["name"]
31+
repo = "https://github.com/" + event["repo"]["name"]
3332
payload = event["payload"]
3433

3534
if (type == "CommitCommentEvent") {
36-
comment = payload["comment"]
37-
return "commented commit in " + repo + "\n" + comment["body"]
35+
return "commented commit in " + repo + "\n" + payload["comment"]["body"]
3836
}
3937
if (type == "CreateEvent") {
4038
return "created " + payload["ref_type"] + " on " + repo
@@ -46,13 +44,10 @@ def github_event_type(event) {
4644
return "forked repository " + repo
4745
}
4846
if (type == "IssueCommentEvent") {
49-
comment = payload["comment"]
50-
issue = payload["issue"]
51-
return "commented issue " + issue["title"] + " on " + repo + "\n" + comment["body"]
47+
return "commented issue " + payload["issue"]["title"] + " on " + repo + "\n" + payload["comment"]["body"]
5248
}
5349
if (type == "IssuesEvent") {
54-
issue = payload["issue"]
55-
return payload["action"] + " issue '" + issue["title"] + "' on " + repo
50+
return payload["action"] + " issue '" + payload["issue"]["title"] + "' on " + repo
5651
}
5752
if (type == "PullRequestEvent") {
5853
pr = payload["pull_request"]

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ private ForeachMapStatement foreachMapStatement() {
172172
}
173173

174174
private FunctionDefineStatement functionDefine() {
175+
// def name(arg1, arg2) { ... } || def name(args) = expr
175176
final String name = consume(TokenType.WORD).getText();
176177
consume(TokenType.LPAREN);
177178
final List<String> argNames = new ArrayList<>();
@@ -188,6 +189,7 @@ private FunctionDefineStatement functionDefine() {
188189
}
189190

190191
private FunctionalExpression function() {
192+
// function(arg1, arg2, ...)
191193
final String name = consume(TokenType.WORD).getText();
192194
consume(TokenType.LPAREN);
193195
final FunctionalExpression function = new FunctionalExpression(name);
@@ -199,6 +201,7 @@ private FunctionalExpression function() {
199201
}
200202

201203
private Expression array() {
204+
// [value1, value2, ...]
202205
consume(TokenType.LBRACKET);
203206
final List<Expression> elements = new ArrayList<>();
204207
while (!match(TokenType.RBRACKET)) {
@@ -209,6 +212,7 @@ private Expression array() {
209212
}
210213

211214
private Expression map() {
215+
// {key1 : value1, key2 : value2, ...}
212216
consume(TokenType.LBRACE);
213217
final Map<Expression, Expression> elements = new HashMap<>();
214218
while (!match(TokenType.RBRACE)) {
@@ -222,6 +226,7 @@ private Expression map() {
222226
}
223227

224228
private ArrayAccessExpression element() {
229+
// array[e1][e2]...[eN]
225230
final String variable = consume(TokenType.WORD).getText();
226231
final List<Expression> indices = new ArrayList<>();
227232
do {

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

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
package com.annimon.ownlang.parser.ast;
22

3-
import com.annimon.ownlang.lib.ArrayValue;
4-
import com.annimon.ownlang.lib.MapValue;
5-
import com.annimon.ownlang.lib.Types;
6-
import com.annimon.ownlang.lib.Value;
7-
import com.annimon.ownlang.lib.Variables;
3+
import com.annimon.ownlang.lib.*;
84
import java.util.List;
95

106
/**
@@ -25,26 +21,37 @@ public ArrayAccessExpression(String variable, List<Expression> indices) {
2521
public Value eval() {
2622
Value container = Variables.get(variable);
2723
if (container.type() == Types.ARRAY) {
28-
return getArray().get(lastIndex());
24+
final int lastIndex = (int) lastIndex().asNumber();
25+
return getArray().get(lastIndex);
2926
}
30-
return consumeMap(container).get(indices.get(0).eval());
27+
return getMap().get(lastIndex());
3128
}
3229

3330
public ArrayValue getArray() {
3431
ArrayValue array = consumeArray(Variables.get(variable));
3532
final int last = indices.size() - 1;
3633
for (int i = 0; i < last; i++) {
37-
array = consumeArray( array.get(index(i)) );
34+
final int index = (int) index(i).asNumber();
35+
array = consumeArray( array.get(index) );
3836
}
3937
return array;
4038
}
4139

42-
public int lastIndex() {
40+
public MapValue getMap() {
41+
MapValue map = consumeMap(Variables.get(variable));
42+
final int last = indices.size() - 1;
43+
for (int i = 0; i < last; i++) {
44+
map = consumeMap( map.get(index(i)) );
45+
}
46+
return map;
47+
}
48+
49+
public Value lastIndex() {
4350
return index(indices.size() - 1);
4451
}
4552

46-
private int index(int index) {
47-
return (int) indices.get(index).eval().asNumber();
53+
private Value index(int index) {
54+
return indices.get(index).eval();
4855
}
4956

5057
private ArrayValue consumeArray(Value value) {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ public ArrayAssignmentStatement(ArrayAccessExpression array, Expression expressi
2222
public void execute() {
2323
final Value container = Variables.get(array.variable);
2424
if (container.type() == Types.ARRAY) {
25-
array.getArray().set(array.lastIndex(), expression.eval());
25+
final int lastIndex = (int) array.lastIndex().asNumber();
26+
array.getArray().set(lastIndex, expression.eval());
2627
return;
2728
}
28-
array.consumeMap(container).set(array.indices.get(0).eval(), expression.eval());
29+
array.getMap().set(array.lastIndex(), expression.eval());
2930
}
3031

3132
@Override

0 commit comments

Comments
 (0)