Skip to content

Commit 95ca908

Browse files
committed
match теперь может использоваться в качестве оператора
1 parent 6b95ae5 commit 95ca908

File tree

6 files changed

+45
-39
lines changed

6 files changed

+45
-39
lines changed

examples/canvas/animate_line.own

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@ def sethsbcolor(h1) {
4646
hueindex = floor(qr) % 6
4747
f = qr - floor(qr)
4848

49-
if hueindex == 0 color(255, f*255, 0)
50-
else if hueindex == 1 color(255 - f*255, 255, 0)
51-
else if hueindex == 2 color(0, 255, f*255)
52-
else if hueindex == 3 color(0, 255-f*255, 255)
53-
else if hueindex == 4 color(f*255, 0, 255)
54-
else if hueindex == 5 color(255, 0, 255-f*255)
49+
match hueindex {
50+
case 0: color(255, f*255, 0)
51+
case 1: color(255 - f*255, 255, 0)
52+
case 2: color(0, 255, f*255)
53+
case 3: color(0, 255-f*255, 255)
54+
case 4: color(f*255, 0, 255)
55+
case 5: color(255, 0, 255-f*255)
56+
}
5557
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,11 @@ private Statement statement() {
8484
if (match(TokenType.DEF)) {
8585
return functionDefine();
8686
}
87+
if (match(TokenType.MATCH)) {
88+
return new ExprStatement(match());
89+
}
8790
if (lookMatch(0, TokenType.WORD) && lookMatch(1, TokenType.LPAREN)) {
88-
return new FunctionStatement(function(qualifiedName()));
91+
return new ExprStatement(function(qualifiedName()));
8992
}
9093
return assignmentStatement();
9194
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.annimon.ownlang.parser.ast;
2+
3+
/**
4+
* Wrapper for expressions, which can be used as statements.
5+
*
6+
* @author aNNiMON
7+
*/
8+
public final class ExprStatement implements Statement {
9+
10+
public final Expression expr;
11+
12+
public ExprStatement(Expression function) {
13+
this.expr = function;
14+
}
15+
16+
@Override
17+
public void execute() {
18+
expr.eval();
19+
}
20+
21+
@Override
22+
public void accept(Visitor visitor) {
23+
visitor.visit(this);
24+
}
25+
26+
@Override
27+
public String toString() {
28+
return expr.toString();
29+
}
30+
}

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

Lines changed: 0 additions & 29 deletions
This file was deleted.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public interface Visitor {
2121
void visit(ForeachMapStatement s);
2222
void visit(FunctionDefineStatement s);
2323
void visit(FunctionReferenceExpression e);
24-
void visit(FunctionStatement s);
24+
void visit(ExprStatement s);
2525
void visit(FunctionalExpression s);
2626
void visit(IfStatement s);
2727
void visit(MapExpression s);

src/com/annimon/ownlang/parser/visitors/AbstractVisitor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ public void visit(FunctionReferenceExpression e) {
9898
}
9999

100100
@Override
101-
public void visit(FunctionStatement s) {
102-
s.function.accept(this);
101+
public void visit(ExprStatement s) {
102+
s.expr.accept(this);
103103
}
104104

105105
@Override

0 commit comments

Comments
 (0)