mirror of
https://github.com/aNNiMON/Own-Programming-Language-Tutorial.git
synced 2024-09-20 00:34:20 +03:00
Урок 8. break, continue, цикл do/while
This commit is contained in:
parent
40949f3e29
commit
3a4ea7b3b1
@ -13,10 +13,18 @@ if (40 < 50 || 50 > 60) {
|
|||||||
print "true1\n"
|
print "true1\n"
|
||||||
print "true2\n"
|
print "true2\n"
|
||||||
i = 0
|
i = 0
|
||||||
|
print "do while"
|
||||||
|
do {
|
||||||
|
print "i = " + i + "\n"
|
||||||
|
i = i + 1
|
||||||
|
} while (i < 10)
|
||||||
|
i = 0
|
||||||
|
print "while"
|
||||||
while (i < 10) {
|
while (i < 10) {
|
||||||
print "i = " + i + "\n"
|
print "i = " + i + "\n"
|
||||||
i = i + 1
|
i = i + 1
|
||||||
}
|
}
|
||||||
|
print "for"
|
||||||
for i = 0, i < 10, i = i + 1 {
|
for i = 0, i < 10, i = i + 1 {
|
||||||
print "i = " + i + "\n"
|
print "i = " + i + "\n"
|
||||||
}
|
}
|
||||||
|
@ -150,6 +150,9 @@ public final class Lexer {
|
|||||||
case "else": addToken(TokenType.ELSE); break;
|
case "else": addToken(TokenType.ELSE); break;
|
||||||
case "while": addToken(TokenType.WHILE); break;
|
case "while": addToken(TokenType.WHILE); break;
|
||||||
case "for": addToken(TokenType.FOR); break;
|
case "for": addToken(TokenType.FOR); break;
|
||||||
|
case "do": addToken(TokenType.DO); break;
|
||||||
|
case "break": addToken(TokenType.BREAK); break;
|
||||||
|
case "continue": addToken(TokenType.CONTINUE); break;
|
||||||
default:
|
default:
|
||||||
addToken(TokenType.WORD, word);
|
addToken(TokenType.WORD, word);
|
||||||
break;
|
break;
|
||||||
|
@ -1,19 +1,6 @@
|
|||||||
package com.annimon.ownlang.parser;
|
package com.annimon.ownlang.parser;
|
||||||
|
|
||||||
import com.annimon.ownlang.parser.ast.PrintStatement;
|
import com.annimon.ownlang.parser.ast.*;
|
||||||
import com.annimon.ownlang.parser.ast.AssignmentStatement;
|
|
||||||
import com.annimon.ownlang.parser.ast.BinaryExpression;
|
|
||||||
import com.annimon.ownlang.parser.ast.BlockStatement;
|
|
||||||
import com.annimon.ownlang.parser.ast.ConditionalExpression;
|
|
||||||
import com.annimon.ownlang.parser.ast.VariableExpression;
|
|
||||||
import com.annimon.ownlang.parser.ast.Expression;
|
|
||||||
import com.annimon.ownlang.parser.ast.ForStatement;
|
|
||||||
import com.annimon.ownlang.parser.ast.IfStatement;
|
|
||||||
import com.annimon.ownlang.parser.ast.ValueExpression;
|
|
||||||
import com.annimon.ownlang.parser.ast.Statement;
|
|
||||||
import com.annimon.ownlang.parser.ast.UnaryExpression;
|
|
||||||
import com.annimon.ownlang.parser.ast.WhileStatement;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -66,6 +53,15 @@ public final class Parser {
|
|||||||
if (match(TokenType.WHILE)) {
|
if (match(TokenType.WHILE)) {
|
||||||
return whileStatement();
|
return whileStatement();
|
||||||
}
|
}
|
||||||
|
if (match(TokenType.DO)) {
|
||||||
|
return doWhileStatement();
|
||||||
|
}
|
||||||
|
if (match(TokenType.BREAK)) {
|
||||||
|
return new BreakStatement();
|
||||||
|
}
|
||||||
|
if (match(TokenType.CONTINUE)) {
|
||||||
|
return new ContinueStatement();
|
||||||
|
}
|
||||||
if (match(TokenType.FOR)) {
|
if (match(TokenType.FOR)) {
|
||||||
return forStatement();
|
return forStatement();
|
||||||
}
|
}
|
||||||
@ -101,6 +97,13 @@ public final class Parser {
|
|||||||
return new WhileStatement(condition, statement);
|
return new WhileStatement(condition, statement);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Statement doWhileStatement() {
|
||||||
|
final Statement statement = statementOrBlock();
|
||||||
|
consume(TokenType.WHILE);
|
||||||
|
final Expression condition = expression();
|
||||||
|
return new DoWhileStatement(condition, statement);
|
||||||
|
}
|
||||||
|
|
||||||
private Statement forStatement() {
|
private Statement forStatement() {
|
||||||
final Statement initialization = assignmentStatement();
|
final Statement initialization = assignmentStatement();
|
||||||
consume(TokenType.COMMA);
|
consume(TokenType.COMMA);
|
||||||
|
@ -17,6 +17,9 @@ public enum TokenType {
|
|||||||
ELSE,
|
ELSE,
|
||||||
WHILE,
|
WHILE,
|
||||||
FOR,
|
FOR,
|
||||||
|
DO,
|
||||||
|
BREAK,
|
||||||
|
CONTINUE,
|
||||||
|
|
||||||
PLUS,
|
PLUS,
|
||||||
MINUS,
|
MINUS,
|
||||||
|
18
src/com/annimon/ownlang/parser/ast/BreakStatement.java
Normal file
18
src/com/annimon/ownlang/parser/ast/BreakStatement.java
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package com.annimon.ownlang.parser.ast;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author aNNiMON
|
||||||
|
*/
|
||||||
|
public final class BreakStatement extends RuntimeException implements Statement {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute() {
|
||||||
|
throw this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "break";
|
||||||
|
}
|
||||||
|
}
|
18
src/com/annimon/ownlang/parser/ast/ContinueStatement.java
Normal file
18
src/com/annimon/ownlang/parser/ast/ContinueStatement.java
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package com.annimon.ownlang.parser.ast;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author aNNiMON
|
||||||
|
*/
|
||||||
|
public final class ContinueStatement extends RuntimeException implements Statement {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute() {
|
||||||
|
throw this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "continue";
|
||||||
|
}
|
||||||
|
}
|
35
src/com/annimon/ownlang/parser/ast/DoWhileStatement.java
Normal file
35
src/com/annimon/ownlang/parser/ast/DoWhileStatement.java
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
package com.annimon.ownlang.parser.ast;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author aNNiMON
|
||||||
|
*/
|
||||||
|
public final class DoWhileStatement implements Statement {
|
||||||
|
|
||||||
|
private final Expression condition;
|
||||||
|
private final Statement statement;
|
||||||
|
|
||||||
|
public DoWhileStatement(Expression condition, Statement statement) {
|
||||||
|
this.condition = condition;
|
||||||
|
this.statement = statement;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute() {
|
||||||
|
do {
|
||||||
|
try {
|
||||||
|
statement.execute();
|
||||||
|
} catch (BreakStatement bs) {
|
||||||
|
break;
|
||||||
|
} catch (ContinueStatement cs) {
|
||||||
|
// continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while (condition.eval().asNumber() != 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "do " + statement + " while " + condition;
|
||||||
|
}
|
||||||
|
}
|
@ -21,7 +21,13 @@ public final class ForStatement implements Statement {
|
|||||||
@Override
|
@Override
|
||||||
public void execute() {
|
public void execute() {
|
||||||
for (initialization.execute(); termination.eval().asNumber() != 0; increment.execute()) {
|
for (initialization.execute(); termination.eval().asNumber() != 0; increment.execute()) {
|
||||||
|
try {
|
||||||
statement.execute();
|
statement.execute();
|
||||||
|
} catch (BreakStatement bs) {
|
||||||
|
break;
|
||||||
|
} catch (ContinueStatement cs) {
|
||||||
|
// continue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,7 +17,13 @@ public final class WhileStatement implements Statement {
|
|||||||
@Override
|
@Override
|
||||||
public void execute() {
|
public void execute() {
|
||||||
while (condition.eval().asNumber() != 0) {
|
while (condition.eval().asNumber() != 0) {
|
||||||
|
try {
|
||||||
statement.execute();
|
statement.execute();
|
||||||
|
} catch (BreakStatement bs) {
|
||||||
|
break;
|
||||||
|
} catch (ContinueStatement cs) {
|
||||||
|
// continue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user