mirror of
https://github.com/aNNiMON/Own-Programming-Language-Tutorial.git
synced 2024-09-20 00:34:20 +03:00
Исправлен последовательный доступ к элементам map
This commit is contained in:
parent
f8854759ff
commit
d0c8c75734
@ -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"]
|
||||
|
@ -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<String> 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<Expression> 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<Expression, Expression> 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<Expression> indices = new ArrayList<>();
|
||||
do {
|
||||
|
@ -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) {
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user