diff --git a/examples/network/github_timeline.own b/examples/network/github_timeline.own index 2363f3b..b04c2c2 100644 --- a/examples/network/github_timeline.own +++ b/examples/network/github_timeline.own @@ -28,13 +28,11 @@ def show_github_events(event) { def github_event_type(event) { type = event["type"] - repo = event["repo"] - repo = "https://github.com/" + repo["name"] + repo = "https://github.com/" + event["repo"]["name"] payload = event["payload"] if (type == "CommitCommentEvent") { - comment = payload["comment"] - return "commented commit in " + repo + "\n" + comment["body"] + return "commented commit in " + repo + "\n" + payload["comment"]["body"] } if (type == "CreateEvent") { return "created " + payload["ref_type"] + " on " + repo @@ -46,13 +44,10 @@ def github_event_type(event) { return "forked repository " + repo } if (type == "IssueCommentEvent") { - comment = payload["comment"] - issue = payload["issue"] - return "commented issue " + issue["title"] + " on " + repo + "\n" + comment["body"] + return "commented issue " + payload["issue"]["title"] + " on " + repo + "\n" + payload["comment"]["body"] } if (type == "IssuesEvent") { - issue = payload["issue"] - return payload["action"] + " issue '" + issue["title"] + "' on " + repo + return payload["action"] + " issue '" + payload["issue"]["title"] + "' on " + repo } if (type == "PullRequestEvent") { pr = payload["pull_request"] diff --git a/src/com/annimon/ownlang/parser/Parser.java b/src/com/annimon/ownlang/parser/Parser.java index 4d6403a..9608ebf 100644 --- a/src/com/annimon/ownlang/parser/Parser.java +++ b/src/com/annimon/ownlang/parser/Parser.java @@ -172,6 +172,7 @@ public final class Parser { } private FunctionDefineStatement functionDefine() { + // def name(arg1, arg2) { ... } || def name(args) = expr final String name = consume(TokenType.WORD).getText(); consume(TokenType.LPAREN); final List argNames = new ArrayList<>(); @@ -188,6 +189,7 @@ public final class Parser { } private FunctionalExpression function() { + // function(arg1, arg2, ...) final String name = consume(TokenType.WORD).getText(); consume(TokenType.LPAREN); final FunctionalExpression function = new FunctionalExpression(name); @@ -199,6 +201,7 @@ public final class Parser { } private Expression array() { + // [value1, value2, ...] consume(TokenType.LBRACKET); final List elements = new ArrayList<>(); while (!match(TokenType.RBRACKET)) { @@ -209,6 +212,7 @@ public final class Parser { } private Expression map() { + // {key1 : value1, key2 : value2, ...} consume(TokenType.LBRACE); final Map elements = new HashMap<>(); while (!match(TokenType.RBRACE)) { @@ -222,6 +226,7 @@ public final class Parser { } private ArrayAccessExpression element() { + // array[e1][e2]...[eN] final String variable = consume(TokenType.WORD).getText(); final List indices = new ArrayList<>(); do { diff --git a/src/com/annimon/ownlang/parser/ast/ArrayAccessExpression.java b/src/com/annimon/ownlang/parser/ast/ArrayAccessExpression.java index 45f65c4..e5dfff8 100644 --- a/src/com/annimon/ownlang/parser/ast/ArrayAccessExpression.java +++ b/src/com/annimon/ownlang/parser/ast/ArrayAccessExpression.java @@ -1,10 +1,6 @@ package com.annimon.ownlang.parser.ast; -import com.annimon.ownlang.lib.ArrayValue; -import com.annimon.ownlang.lib.MapValue; -import com.annimon.ownlang.lib.Types; -import com.annimon.ownlang.lib.Value; -import com.annimon.ownlang.lib.Variables; +import com.annimon.ownlang.lib.*; import java.util.List; /** @@ -25,26 +21,37 @@ public final class ArrayAccessExpression implements Expression { public Value eval() { Value container = Variables.get(variable); if (container.type() == Types.ARRAY) { - return getArray().get(lastIndex()); + final int lastIndex = (int) lastIndex().asNumber(); + return getArray().get(lastIndex); } - return consumeMap(container).get(indices.get(0).eval()); + return getMap().get(lastIndex()); } public ArrayValue getArray() { ArrayValue array = consumeArray(Variables.get(variable)); final int last = indices.size() - 1; for (int i = 0; i < last; i++) { - array = consumeArray( array.get(index(i)) ); + final int index = (int) index(i).asNumber(); + array = consumeArray( array.get(index) ); } return array; } - public int lastIndex() { + public MapValue getMap() { + MapValue map = consumeMap(Variables.get(variable)); + final int last = indices.size() - 1; + for (int i = 0; i < last; i++) { + map = consumeMap( map.get(index(i)) ); + } + return map; + } + + public Value lastIndex() { return index(indices.size() - 1); } - private int index(int index) { - return (int) indices.get(index).eval().asNumber(); + private Value index(int index) { + return indices.get(index).eval(); } private ArrayValue consumeArray(Value value) { diff --git a/src/com/annimon/ownlang/parser/ast/ArrayAssignmentStatement.java b/src/com/annimon/ownlang/parser/ast/ArrayAssignmentStatement.java index b2222ac..d39b7f6 100644 --- a/src/com/annimon/ownlang/parser/ast/ArrayAssignmentStatement.java +++ b/src/com/annimon/ownlang/parser/ast/ArrayAssignmentStatement.java @@ -22,10 +22,11 @@ public final class ArrayAssignmentStatement implements Statement { public void execute() { final Value container = Variables.get(array.variable); if (container.type() == Types.ARRAY) { - array.getArray().set(array.lastIndex(), expression.eval()); + final int lastIndex = (int) array.lastIndex().asNumber(); + array.getArray().set(lastIndex, expression.eval()); return; } - array.consumeMap(container).set(array.indices.get(0).eval(), expression.eval()); + array.getMap().set(array.lastIndex(), expression.eval()); } @Override