mirror of
https://github.com/aNNiMON/Own-Programming-Language-Tutorial.git
synced 2024-09-20 08:44:20 +03:00
Небольшие исправления
This commit is contained in:
parent
bdcb54e980
commit
207f91a228
@ -1,6 +1,5 @@
|
||||
package com.annimon.ownlang;
|
||||
|
||||
import com.annimon.ownlang.lib.Variables;
|
||||
import com.annimon.ownlang.parser.Lexer;
|
||||
import com.annimon.ownlang.parser.Parser;
|
||||
import com.annimon.ownlang.parser.Token;
|
||||
@ -16,7 +15,7 @@ import java.util.List;
|
||||
public final class Main {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
final String input = new String( Files.readAllBytes(Paths.get("program.txt")), "UTF-8");
|
||||
final String input = new String( Files.readAllBytes(Paths.get("program.own")), "UTF-8");
|
||||
final List<Token> tokens = new Lexer(input).tokenize();
|
||||
for (Token token : tokens) {
|
||||
System.out.println(token);
|
||||
|
@ -40,7 +40,7 @@ public final class Parser {
|
||||
}
|
||||
|
||||
private Statement statementOrBlock() {
|
||||
if (get(0).getType() == TokenType.LBRACE) return block();
|
||||
if (lookMatch(0, TokenType.LBRACE)) return block();
|
||||
return statement();
|
||||
}
|
||||
|
||||
@ -72,7 +72,7 @@ public final class Parser {
|
||||
if (match(TokenType.DEF)) {
|
||||
return functionDefine();
|
||||
}
|
||||
if (get(0).getType() == TokenType.WORD && get(1).getType() == TokenType.LPAREN) {
|
||||
if (lookMatch(0, TokenType.WORD) && lookMatch(1, TokenType.LPAREN)) {
|
||||
return new FunctionStatement(function());
|
||||
}
|
||||
return assignmentStatement();
|
||||
@ -81,7 +81,7 @@ public final class Parser {
|
||||
private Statement assignmentStatement() {
|
||||
// WORD EQ
|
||||
final Token current = get(0);
|
||||
if (match(TokenType.WORD) && get(0).getType() == TokenType.EQ) {
|
||||
if (match(TokenType.WORD) && lookMatch(0, TokenType.EQ)) {
|
||||
final String variable = current.getText();
|
||||
consume(TokenType.EQ);
|
||||
return new AssignmentStatement(variable, expression());
|
||||
@ -240,7 +240,6 @@ public final class Parser {
|
||||
Expression result = unary();
|
||||
|
||||
while (true) {
|
||||
// 2 * 6 / 3
|
||||
if (match(TokenType.STAR)) {
|
||||
result = new BinaryExpression('*', result, unary());
|
||||
continue;
|
||||
@ -273,7 +272,7 @@ public final class Parser {
|
||||
if (match(TokenType.HEX_NUMBER)) {
|
||||
return new ValueExpression(Long.parseLong(current.getText(), 16));
|
||||
}
|
||||
if (get(0).getType() == TokenType.WORD && get(1).getType() == TokenType.LPAREN) {
|
||||
if (lookMatch(0, TokenType.WORD) && lookMatch(1, TokenType.LPAREN)) {
|
||||
return function();
|
||||
}
|
||||
if (match(TokenType.WORD)) {
|
||||
@ -304,6 +303,10 @@ public final class Parser {
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean lookMatch(int pos, TokenType type) {
|
||||
return get(pos).getType() == type;
|
||||
}
|
||||
|
||||
private Token get(int relativePosition) {
|
||||
final int position = pos + relativePosition;
|
||||
if (position >= size) return EOF;
|
||||
|
@ -17,13 +17,12 @@ public final class VariableExpression implements Expression {
|
||||
|
||||
@Override
|
||||
public Value eval() {
|
||||
if (!Variables.isExists(name)) throw new RuntimeException("Constant does not exists");
|
||||
if (!Variables.isExists(name)) throw new RuntimeException("Variable does not exists");
|
||||
return Variables.get(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
// return String.format("%s [%f]", name, Constants.get(name));
|
||||
return String.format("%s", name);
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
@ -1,29 +0,0 @@
|
||||
package com.annimon.ownlang.parser.ast;
|
||||
|
||||
import com.annimon.ownlang.lib.Value;
|
||||
import com.annimon.ownlang.lib.Variables;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author aNNiMON
|
||||
*/
|
||||
public final class VariabletExpression implements Expression {
|
||||
|
||||
private final String name;
|
||||
|
||||
public VariabletExpression(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Value eval() {
|
||||
if (!Variables.isExists(name)) throw new RuntimeException("Constant does not exists");
|
||||
return Variables.get(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
// return String.format("%s [%f]", name, Constants.get(name));
|
||||
return String.format("%s", name);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user