Skip to content

Commit 794d6f2

Browse files
committed
Синтаксический сахар - доступ к элементам map через точку
1 parent d0c8c75 commit 794d6f2

File tree

4 files changed

+33
-15
lines changed

4 files changed

+33
-15
lines changed

examples/network/github_timeline.own

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,42 +19,42 @@ thread(::http, "https://api.github.com/events", def(r) {
1919
})
2020

2121
def show_github_events(event) {
22-
println event["created_at"]
23-
actor = event["actor"]
24-
println "User: https://github.com/" + actor["login"]
22+
println event.created_at
23+
actor = event.actor
24+
println "User: https://github.com/" + actor.login
2525
println github_event_type(event)
2626
println "-" * 50
2727
}
2828

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

3434
if (type == "CommitCommentEvent") {
35-
return "commented commit in " + repo + "\n" + payload["comment"]["body"]
35+
return "commented commit in " + repo + "\n" + payload.comment.body
3636
}
3737
if (type == "CreateEvent") {
38-
return "created " + payload["ref_type"] + " on " + repo
38+
return "created " + payload.ref_type + " on " + repo
3939
}
4040
if (type == "DeleteEvent") {
41-
return "deleted " + payload["ref_type"] + " on " + repo
41+
return "deleted " + payload.ref_type + " on " + repo
4242
}
4343
if (type == "ForkEvent") {
4444
return "forked repository " + repo
4545
}
4646
if (type == "IssueCommentEvent") {
47-
return "commented issue " + payload["issue"]["title"] + " on " + repo + "\n" + payload["comment"]["body"]
47+
return "commented issue " + payload.issue.title + " on " + repo + "\n" + payload.comment.body
4848
}
4949
if (type == "IssuesEvent") {
50-
return payload["action"] + " issue '" + payload["issue"]["title"] + "' on " + repo
50+
return payload.action + " issue '" + payload.issue.title + "' on " + repo
5151
}
5252
if (type == "PullRequestEvent") {
53-
pr = payload["pull_request"]
54-
return payload["action"] + " pull request #" + payload["number"] + " '" + pr["title"] + "' on " + repo
53+
pr = payload.pull_request
54+
return payload.action + " pull request #" + payload.number + " '" + pr.title + "' on " + repo
5555
}
5656
if (type == "PushEvent") {
57-
return "pushed " + length(payload["commits"]) + " commits to " + repo
57+
return "pushed " + length(payload.commits) + " commits to " + repo
5858
}
5959
if (type == "WatchEvent") {
6060
return "start watching repository " + repo

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212
public final class Lexer {
1313

14-
private static final String OPERATOR_CHARS = "+-*/%()[]{}=<>!&|,^~?:";
14+
private static final String OPERATOR_CHARS = "+-*/%()[]{}=<>!&|.,^~?:";
1515

1616
private static final Map<String, TokenType> OPERATORS;
1717
static {
@@ -30,6 +30,7 @@ public final class Lexer {
3030
OPERATORS.put("=", TokenType.EQ);
3131
OPERATORS.put("<", TokenType.LT);
3232
OPERATORS.put(">", TokenType.GT);
33+
OPERATORS.put(".", TokenType.DOT);
3334
OPERATORS.put(",", TokenType.COMMA);
3435
OPERATORS.put("^", TokenType.CARET);
3536
OPERATORS.put("~", TokenType.TILDE);

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,19 @@ private ArrayAccessExpression element() {
237237
return new ArrayAccessExpression(variable, indices);
238238
}
239239

240+
private ArrayAccessExpression object() {
241+
// object.field1.field2
242+
// Syntaxic sugar for object["field1"]["field2"]
243+
final String variable = consume(TokenType.WORD).getText();
244+
final List<Expression> indices = new ArrayList<>();
245+
while (match(TokenType.DOT)) {
246+
final String fieldName = consume(TokenType.WORD).getText();
247+
final Expression key = new ValueExpression(fieldName);
248+
indices.add(key);
249+
}
250+
return new ArrayAccessExpression(variable, indices);
251+
}
252+
240253
private Expression expression() {
241254
return ternary();
242255
}
@@ -459,6 +472,9 @@ private Expression primary() {
459472
if (lookMatch(0, TokenType.WORD) && lookMatch(1, TokenType.LPAREN)) {
460473
return function();
461474
}
475+
if (lookMatch(0, TokenType.WORD) && lookMatch(1, TokenType.DOT)) {
476+
return object();
477+
}
462478
if (lookMatch(0, TokenType.LBRACKET)) {
463479
return array();
464480
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public enum TokenType {
6161
LBRACE, // {
6262
RBRACE, // }
6363
COMMA, // ,
64+
DOT, // .
6465

6566
EOF
6667
}

0 commit comments

Comments
 (0)