mirror of
https://github.com/aNNiMON/Own-Programming-Language-Tutorial.git
synced 2024-09-20 08:44: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) {
|
def github_event_type(event) {
|
||||||
type = event["type"]
|
type = event["type"]
|
||||||
repo = event["repo"]
|
repo = "https://github.com/" + event["repo"]["name"]
|
||||||
repo = "https://github.com/" + repo["name"]
|
|
||||||
payload = event["payload"]
|
payload = event["payload"]
|
||||||
|
|
||||||
if (type == "CommitCommentEvent") {
|
if (type == "CommitCommentEvent") {
|
||||||
comment = payload["comment"]
|
return "commented commit in " + repo + "\n" + payload["comment"]["body"]
|
||||||
return "commented commit in " + repo + "\n" + comment["body"]
|
|
||||||
}
|
}
|
||||||
if (type == "CreateEvent") {
|
if (type == "CreateEvent") {
|
||||||
return "created " + payload["ref_type"] + " on " + repo
|
return "created " + payload["ref_type"] + " on " + repo
|
||||||
@ -46,13 +44,10 @@ def github_event_type(event) {
|
|||||||
return "forked repository " + repo
|
return "forked repository " + repo
|
||||||
}
|
}
|
||||||
if (type == "IssueCommentEvent") {
|
if (type == "IssueCommentEvent") {
|
||||||
comment = payload["comment"]
|
return "commented issue " + payload["issue"]["title"] + " on " + repo + "\n" + payload["comment"]["body"]
|
||||||
issue = payload["issue"]
|
|
||||||
return "commented issue " + issue["title"] + " on " + repo + "\n" + comment["body"]
|
|
||||||
}
|
}
|
||||||
if (type == "IssuesEvent") {
|
if (type == "IssuesEvent") {
|
||||||
issue = payload["issue"]
|
return payload["action"] + " issue '" + payload["issue"]["title"] + "' on " + repo
|
||||||
return payload["action"] + " issue '" + issue["title"] + "' on " + repo
|
|
||||||
}
|
}
|
||||||
if (type == "PullRequestEvent") {
|
if (type == "PullRequestEvent") {
|
||||||
pr = payload["pull_request"]
|
pr = payload["pull_request"]
|
||||||
|
@ -172,6 +172,7 @@ public final class Parser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private FunctionDefineStatement functionDefine() {
|
private FunctionDefineStatement functionDefine() {
|
||||||
|
// def name(arg1, arg2) { ... } || def name(args) = expr
|
||||||
final String name = consume(TokenType.WORD).getText();
|
final String name = consume(TokenType.WORD).getText();
|
||||||
consume(TokenType.LPAREN);
|
consume(TokenType.LPAREN);
|
||||||
final List<String> argNames = new ArrayList<>();
|
final List<String> argNames = new ArrayList<>();
|
||||||
@ -188,6 +189,7 @@ public final class Parser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private FunctionalExpression function() {
|
private FunctionalExpression function() {
|
||||||
|
// function(arg1, arg2, ...)
|
||||||
final String name = consume(TokenType.WORD).getText();
|
final String name = consume(TokenType.WORD).getText();
|
||||||
consume(TokenType.LPAREN);
|
consume(TokenType.LPAREN);
|
||||||
final FunctionalExpression function = new FunctionalExpression(name);
|
final FunctionalExpression function = new FunctionalExpression(name);
|
||||||
@ -199,6 +201,7 @@ public final class Parser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Expression array() {
|
private Expression array() {
|
||||||
|
// [value1, value2, ...]
|
||||||
consume(TokenType.LBRACKET);
|
consume(TokenType.LBRACKET);
|
||||||
final List<Expression> elements = new ArrayList<>();
|
final List<Expression> elements = new ArrayList<>();
|
||||||
while (!match(TokenType.RBRACKET)) {
|
while (!match(TokenType.RBRACKET)) {
|
||||||
@ -209,6 +212,7 @@ public final class Parser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Expression map() {
|
private Expression map() {
|
||||||
|
// {key1 : value1, key2 : value2, ...}
|
||||||
consume(TokenType.LBRACE);
|
consume(TokenType.LBRACE);
|
||||||
final Map<Expression, Expression> elements = new HashMap<>();
|
final Map<Expression, Expression> elements = new HashMap<>();
|
||||||
while (!match(TokenType.RBRACE)) {
|
while (!match(TokenType.RBRACE)) {
|
||||||
@ -222,6 +226,7 @@ public final class Parser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private ArrayAccessExpression element() {
|
private ArrayAccessExpression element() {
|
||||||
|
// array[e1][e2]...[eN]
|
||||||
final String variable = consume(TokenType.WORD).getText();
|
final String variable = consume(TokenType.WORD).getText();
|
||||||
final List<Expression> indices = new ArrayList<>();
|
final List<Expression> indices = new ArrayList<>();
|
||||||
do {
|
do {
|
||||||
|
@ -1,10 +1,6 @@
|
|||||||
package com.annimon.ownlang.parser.ast;
|
package com.annimon.ownlang.parser.ast;
|
||||||
|
|
||||||
import com.annimon.ownlang.lib.ArrayValue;
|
import com.annimon.ownlang.lib.*;
|
||||||
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 java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -25,26 +21,37 @@ public final class ArrayAccessExpression implements Expression {
|
|||||||
public Value eval() {
|
public Value eval() {
|
||||||
Value container = Variables.get(variable);
|
Value container = Variables.get(variable);
|
||||||
if (container.type() == Types.ARRAY) {
|
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() {
|
public ArrayValue getArray() {
|
||||||
ArrayValue array = consumeArray(Variables.get(variable));
|
ArrayValue array = consumeArray(Variables.get(variable));
|
||||||
final int last = indices.size() - 1;
|
final int last = indices.size() - 1;
|
||||||
for (int i = 0; i < last; i++) {
|
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;
|
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);
|
return index(indices.size() - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
private int index(int index) {
|
private Value index(int index) {
|
||||||
return (int) indices.get(index).eval().asNumber();
|
return indices.get(index).eval();
|
||||||
}
|
}
|
||||||
|
|
||||||
private ArrayValue consumeArray(Value value) {
|
private ArrayValue consumeArray(Value value) {
|
||||||
|
@ -22,10 +22,11 @@ public final class ArrayAssignmentStatement implements Statement {
|
|||||||
public void execute() {
|
public void execute() {
|
||||||
final Value container = Variables.get(array.variable);
|
final Value container = Variables.get(array.variable);
|
||||||
if (container.type() == Types.ARRAY) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
array.consumeMap(container).set(array.indices.get(0).eval(), expression.eval());
|
array.getMap().set(array.lastIndex(), expression.eval());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
Reference in New Issue
Block a user