Небольшие исправления

This commit is contained in:
Victor 2015-06-23 10:37:29 +03:00
parent bdcb54e980
commit 207f91a228
4 changed files with 11 additions and 39 deletions

View File

@ -1,6 +1,5 @@
package com.annimon.ownlang; package com.annimon.ownlang;
import com.annimon.ownlang.lib.Variables;
import com.annimon.ownlang.parser.Lexer; import com.annimon.ownlang.parser.Lexer;
import com.annimon.ownlang.parser.Parser; import com.annimon.ownlang.parser.Parser;
import com.annimon.ownlang.parser.Token; import com.annimon.ownlang.parser.Token;
@ -16,7 +15,7 @@ import java.util.List;
public final class Main { public final class Main {
public static void main(String[] args) throws IOException { 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(); final List<Token> tokens = new Lexer(input).tokenize();
for (Token token : tokens) { for (Token token : tokens) {
System.out.println(token); System.out.println(token);

View File

@ -40,7 +40,7 @@ public final class Parser {
} }
private Statement statementOrBlock() { private Statement statementOrBlock() {
if (get(0).getType() == TokenType.LBRACE) return block(); if (lookMatch(0, TokenType.LBRACE)) return block();
return statement(); return statement();
} }
@ -72,7 +72,7 @@ public final class Parser {
if (match(TokenType.DEF)) { if (match(TokenType.DEF)) {
return functionDefine(); 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 new FunctionStatement(function());
} }
return assignmentStatement(); return assignmentStatement();
@ -81,7 +81,7 @@ public final class Parser {
private Statement assignmentStatement() { private Statement assignmentStatement() {
// WORD EQ // WORD EQ
final Token current = get(0); 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(); final String variable = current.getText();
consume(TokenType.EQ); consume(TokenType.EQ);
return new AssignmentStatement(variable, expression()); return new AssignmentStatement(variable, expression());
@ -240,7 +240,6 @@ public final class Parser {
Expression result = unary(); Expression result = unary();
while (true) { while (true) {
// 2 * 6 / 3
if (match(TokenType.STAR)) { if (match(TokenType.STAR)) {
result = new BinaryExpression('*', result, unary()); result = new BinaryExpression('*', result, unary());
continue; continue;
@ -273,7 +272,7 @@ public final class Parser {
if (match(TokenType.HEX_NUMBER)) { if (match(TokenType.HEX_NUMBER)) {
return new ValueExpression(Long.parseLong(current.getText(), 16)); 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(); return function();
} }
if (match(TokenType.WORD)) { if (match(TokenType.WORD)) {
@ -304,6 +303,10 @@ public final class Parser {
return true; return true;
} }
private boolean lookMatch(int pos, TokenType type) {
return get(pos).getType() == type;
}
private Token get(int relativePosition) { private Token get(int relativePosition) {
final int position = pos + relativePosition; final int position = pos + relativePosition;
if (position >= size) return EOF; if (position >= size) return EOF;

View File

@ -17,13 +17,12 @@ public final class VariableExpression implements Expression {
@Override @Override
public Value eval() { 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); return Variables.get(name);
} }
@Override @Override
public String toString() { public String toString() {
// return String.format("%s [%f]", name, Constants.get(name)); return name;
return String.format("%s", name);
} }
} }

View File

@ -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);
}
}