From 77666056a821633e151873a342925160ee293011 Mon Sep 17 00:00:00 2001 From: lautaropaske Date: Tue, 25 Jun 2019 23:40:50 -0300 Subject: [PATCH] Executor planned. Interpreted started. Minor fixes in Parser --- .idea/compiler.xml | 14 +++++++++++ .idea/modules.xml | 6 +++++ .idea/modules/executor/tp3.executor.iml | 12 ++++++++++ .idea/modules/executor/tp3.executor.main.iml | 16 +++++++++++++ .idea/modules/executor/tp3.executor.test.iml | 24 +++++++++++++++++++ .idea/modules/interpreter/tp3.interpreter.iml | 12 ++++++++++ .../interpreter/tp3.interpreter.main.iml | 15 ++++++++++++ .../interpreter/tp3.interpreter.test.iml | 23 ++++++++++++++++++ .idea/modules/lexer/tp3.lexer.main.iml | 2 -- .idea/modules/lexer/tp3.lexer.test.iml | 4 +--- .idea/modules/main/tp3.main.iml | 12 ++++++++++ .idea/modules/parser/tp3.parser.main.iml | 2 -- .idea/modules/parser/tp3.parser.test.iml | 2 -- build.gradle | 15 ++++++++++++ executor/src/main/java/Executor.java | 23 ++++++++++++++++++ executor/src/test/java/ExecutorTest.java | 15 ++++++++++++ .../main/java/interpreter/Interpreter.java | 7 ++++++ .../java/interpreter/InterpreterImpl.java | 10 ++++++++ lexer/src/main/java/lexer/Lexer.java | 2 +- lexer/src/main/java/lexer/LexerImpl.java | 2 +- parser/src/main/java/ast/ASTNodeFactory.java | 19 ++++++++------- .../main/java/handlers/ExpressionHandler.java | 4 ++-- .../java/handlers/rules/AssignationRule.java | 5 ++-- .../rules/DeclarationAndAssignationRule.java | 5 ++-- .../java/handlers/rules/DeclarationRule.java | 2 -- settings.gradle | 4 +++- 26 files changed, 229 insertions(+), 28 deletions(-) create mode 100644 .idea/modules/executor/tp3.executor.iml create mode 100644 .idea/modules/executor/tp3.executor.main.iml create mode 100644 .idea/modules/executor/tp3.executor.test.iml create mode 100644 .idea/modules/interpreter/tp3.interpreter.iml create mode 100644 .idea/modules/interpreter/tp3.interpreter.main.iml create mode 100644 .idea/modules/interpreter/tp3.interpreter.test.iml create mode 100644 .idea/modules/main/tp3.main.iml create mode 100644 executor/src/main/java/Executor.java create mode 100644 executor/src/test/java/ExecutorTest.java create mode 100644 interpreter/src/main/java/interpreter/Interpreter.java create mode 100644 interpreter/src/main/java/interpreter/InterpreterImpl.java diff --git a/.idea/compiler.xml b/.idea/compiler.xml index e514ca3..a4c1cf6 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -1,10 +1,24 @@ + + + + + + + + + + + + + + diff --git a/.idea/modules.xml b/.idea/modules.xml index 7547b4a..efe0bd6 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -3,6 +3,12 @@ + + + + + + diff --git a/.idea/modules/executor/tp3.executor.iml b/.idea/modules/executor/tp3.executor.iml new file mode 100644 index 0000000..52135a4 --- /dev/null +++ b/.idea/modules/executor/tp3.executor.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules/executor/tp3.executor.main.iml b/.idea/modules/executor/tp3.executor.main.iml new file mode 100644 index 0000000..01840c9 --- /dev/null +++ b/.idea/modules/executor/tp3.executor.main.iml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules/executor/tp3.executor.test.iml b/.idea/modules/executor/tp3.executor.test.iml new file mode 100644 index 0000000..45b8ea7 --- /dev/null +++ b/.idea/modules/executor/tp3.executor.test.iml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules/interpreter/tp3.interpreter.iml b/.idea/modules/interpreter/tp3.interpreter.iml new file mode 100644 index 0000000..fd1e91f --- /dev/null +++ b/.idea/modules/interpreter/tp3.interpreter.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules/interpreter/tp3.interpreter.main.iml b/.idea/modules/interpreter/tp3.interpreter.main.iml new file mode 100644 index 0000000..d58c532 --- /dev/null +++ b/.idea/modules/interpreter/tp3.interpreter.main.iml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules/interpreter/tp3.interpreter.test.iml b/.idea/modules/interpreter/tp3.interpreter.test.iml new file mode 100644 index 0000000..95906da --- /dev/null +++ b/.idea/modules/interpreter/tp3.interpreter.test.iml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules/lexer/tp3.lexer.main.iml b/.idea/modules/lexer/tp3.lexer.main.iml index 0a4db2d..2936dc2 100644 --- a/.idea/modules/lexer/tp3.lexer.main.iml +++ b/.idea/modules/lexer/tp3.lexer.main.iml @@ -9,7 +9,5 @@ - - \ No newline at end of file diff --git a/.idea/modules/lexer/tp3.lexer.test.iml b/.idea/modules/lexer/tp3.lexer.test.iml index 2427cab..f37c607 100644 --- a/.idea/modules/lexer/tp3.lexer.test.iml +++ b/.idea/modules/lexer/tp3.lexer.test.iml @@ -10,12 +10,10 @@ - - - + diff --git a/.idea/modules/main/tp3.main.iml b/.idea/modules/main/tp3.main.iml new file mode 100644 index 0000000..9b14544 --- /dev/null +++ b/.idea/modules/main/tp3.main.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules/parser/tp3.parser.main.iml b/.idea/modules/parser/tp3.parser.main.iml index a4e5b12..2f0f47d 100644 --- a/.idea/modules/parser/tp3.parser.main.iml +++ b/.idea/modules/parser/tp3.parser.main.iml @@ -10,7 +10,5 @@ - - \ No newline at end of file diff --git a/.idea/modules/parser/tp3.parser.test.iml b/.idea/modules/parser/tp3.parser.test.iml index dd52739..110009e 100644 --- a/.idea/modules/parser/tp3.parser.test.iml +++ b/.idea/modules/parser/tp3.parser.test.iml @@ -12,12 +12,10 @@ - - diff --git a/build.gradle b/build.gradle index 337684f..295b05d 100644 --- a/build.gradle +++ b/build.gradle @@ -25,3 +25,18 @@ project(':parser') { compile project(":lexer") } } + +project(':interpreter') { + dependencies { + compile project(":lexer") + compile project(":parser") + } +} + +project(':executor') { + dependencies { + compile project(":lexer") + compile project(":parser") + compile project(":interpreter") + } +} \ No newline at end of file diff --git a/executor/src/main/java/Executor.java b/executor/src/main/java/Executor.java new file mode 100644 index 0000000..4a5d1dc --- /dev/null +++ b/executor/src/main/java/Executor.java @@ -0,0 +1,23 @@ +import lexer.Lexer; +import lexer.LexerImpl; +import parser.Parser; +import parser.ParserImpl; +import interpreter.Interpreter; +import interpreter.InterpreterImpl; + +public class Executor { + + private Lexer lexer; + private Parser parser; + private Interpreter interpreter; + + public Executor(){ + this.lexer = new LexerImpl(); + this.parser = new ParserImpl(); + this.interpreter = new InterpreterImpl(); + } + + public void execute(String file){ + interpreter.interpret(parser.parse(lexer.lex(file))); + } +} diff --git a/executor/src/test/java/ExecutorTest.java b/executor/src/test/java/ExecutorTest.java new file mode 100644 index 0000000..c66e684 --- /dev/null +++ b/executor/src/test/java/ExecutorTest.java @@ -0,0 +1,15 @@ +import org.junit.jupiter.api.Test; + +public class ExecutorTest { + + @Test + public void executeLines(){ + Executor executor = new Executor(); + + String program = "let foo : number= 5;\n" + + "let bar :number = foo + 4;\n" + + "print(bar + \"is a\" + \'word\');\n"; + + executor.execute(program); + } +} diff --git a/interpreter/src/main/java/interpreter/Interpreter.java b/interpreter/src/main/java/interpreter/Interpreter.java new file mode 100644 index 0000000..620e234 --- /dev/null +++ b/interpreter/src/main/java/interpreter/Interpreter.java @@ -0,0 +1,7 @@ +package interpreter; + +import ast.ASTNode; + +public interface Interpreter { + void interpret(ASTNode program); +} diff --git a/interpreter/src/main/java/interpreter/InterpreterImpl.java b/interpreter/src/main/java/interpreter/InterpreterImpl.java new file mode 100644 index 0000000..0a36c39 --- /dev/null +++ b/interpreter/src/main/java/interpreter/InterpreterImpl.java @@ -0,0 +1,10 @@ +package interpreter; + +import ast.ASTNode; + +public class InterpreterImpl implements Interpreter { + @Override + public void interpret(ASTNode program) { + + } +} diff --git a/lexer/src/main/java/lexer/Lexer.java b/lexer/src/main/java/lexer/Lexer.java index 3e1e83f..6d2fdd3 100644 --- a/lexer/src/main/java/lexer/Lexer.java +++ b/lexer/src/main/java/lexer/Lexer.java @@ -14,5 +14,5 @@ public interface Lexer { * @param file a text file converted to string properly separated by the ' ' character (whitespace) * @return a list of the tokens contained in the file, in lexical order */ - List tokenize(String file); + List lex(String file); } diff --git a/lexer/src/main/java/lexer/LexerImpl.java b/lexer/src/main/java/lexer/LexerImpl.java index 095d272..9f6cfe4 100644 --- a/lexer/src/main/java/lexer/LexerImpl.java +++ b/lexer/src/main/java/lexer/LexerImpl.java @@ -20,7 +20,7 @@ public LexerImpl(){ } @Override - public List tokenize(String file) { + public List lex(String file) { return mapToToken(splitPossibleTokens(file)); } diff --git a/parser/src/main/java/ast/ASTNodeFactory.java b/parser/src/main/java/ast/ASTNodeFactory.java index b170a88..e2d2768 100644 --- a/parser/src/main/java/ast/ASTNodeFactory.java +++ b/parser/src/main/java/ast/ASTNodeFactory.java @@ -1,21 +1,24 @@ package ast; import exceptions.ParserException; -import handlers.AssignationHandler; -import handlers.DeclarationAssignationHandler; -import handlers.DeclarationHandler; -import handlers.PrintHandler; +import handlers.*; import tokens.Token; import tokens.TokenType; import java.util.List; public class ASTNodeFactory { - private DeclarationHandler declarationHandler; - private AssignationHandler assignationHandler; - private DeclarationAssignationHandler declarationAssignationHandler; - private PrintHandler printHandler; + private Handler declarationHandler; + private Handler assignationHandler; + private Handler declarationAssignationHandler; + private Handler printHandler; + public ASTNodeFactory(){ + this.declarationHandler = new DeclarationHandler(); + this.assignationHandler = new AssignationHandler(); + this.declarationAssignationHandler = new DeclarationAssignationHandler(); + this.printHandler = new PrintHandler(); + } public ASTNode getNode(List statement) { switch (statement.get(0).getType()) { diff --git a/parser/src/main/java/handlers/ExpressionHandler.java b/parser/src/main/java/handlers/ExpressionHandler.java index 2d9f266..5b6d94a 100644 --- a/parser/src/main/java/handlers/ExpressionHandler.java +++ b/parser/src/main/java/handlers/ExpressionHandler.java @@ -17,14 +17,14 @@ protected ASTNode parseNode(List expression) { () -> parseBinaryExpression(TokenType.MINUS, expression).orElseGet( () -> parseBinaryExpression(TokenType.MULTIPLY, expression).orElseGet( () -> parseBinaryExpression(TokenType.DIVIDE, expression).orElseGet( - ()-> parseIndentifierOrLiteral(expression) + ()-> parseIdentifierOrLiteral(expression) ) ) ) ); } - private ASTNode parseIndentifierOrLiteral(List expression) { + private ASTNode parseIdentifierOrLiteral(List expression) { Token token = expression.get(0); if(Arrays.asList(TokenType.NUM_LITERAL, TokenType.STR_LITERAL).contains(expression.get(0).getType())) return new Literal(Type.from(token), token.getValue()); if(TokenType.IDENTIFIER == token.getType()) return new Identifier(token.getValue()); diff --git a/parser/src/main/java/handlers/rules/AssignationRule.java b/parser/src/main/java/handlers/rules/AssignationRule.java index 8e456f6..6f2dbfb 100644 --- a/parser/src/main/java/handlers/rules/AssignationRule.java +++ b/parser/src/main/java/handlers/rules/AssignationRule.java @@ -4,6 +4,7 @@ import tokens.Token; import tokens.TokenType; +import java.util.Arrays; import java.util.List; public class AssignationRule implements Rule { @@ -20,9 +21,9 @@ public void validate(List statement) { for (int i = 2; i < statement.size(); i++) { TokenType type = statement.get(i).getType(); if(mustBeAValue) { - if(type == TokenType.NUM_LITERAL || type == TokenType.STR_LITERAL || type == TokenType.IDENTIFIER) { mustBeAValue = false; continue; } + if(Arrays.asList(TokenType.NUM_LITERAL, TokenType.STR_LITERAL, TokenType.IDENTIFIER).contains(type)) { mustBeAValue = false; continue; } } else { // If preceded by a value, then it MUST be an operator or a semicolon - if(type == TokenType.SUM || type == TokenType.DIVIDE || type == TokenType.MULTIPLY || type == TokenType.MINUS || type == TokenType.SEMICOLON) { mustBeAValue = true; continue; } + if(Arrays.asList(TokenType.SUM, TokenType.DIVIDE, TokenType.MULTIPLY, TokenType.MINUS, TokenType.SEMICOLON).contains(type)) { mustBeAValue = true; continue; } } throw new ParserException("Invalid statement", statement.get(i).getCoordinates()[0], statement.get(i).getCoordinates()[1]); } diff --git a/parser/src/main/java/handlers/rules/DeclarationAndAssignationRule.java b/parser/src/main/java/handlers/rules/DeclarationAndAssignationRule.java index 2853565..9de44cb 100644 --- a/parser/src/main/java/handlers/rules/DeclarationAndAssignationRule.java +++ b/parser/src/main/java/handlers/rules/DeclarationAndAssignationRule.java @@ -5,11 +5,12 @@ import java.util.List; public class DeclarationAndAssignationRule implements Rule{ - DeclarationRule declarationRule; - AssignationRule assignationRule; + private DeclarationRule declarationRule = new DeclarationRule(); + private AssignationRule assignationRule = new AssignationRule(); @Override public void validate(List statement) { + // TODO Revisar, está mal porque el assignation no recibe identifier declarationRule.validate(statement.subList(0,4)); assignationRule.validate(statement.subList(4, statement.size())); } diff --git a/parser/src/main/java/handlers/rules/DeclarationRule.java b/parser/src/main/java/handlers/rules/DeclarationRule.java index f59dbf4..cae314e 100644 --- a/parser/src/main/java/handlers/rules/DeclarationRule.java +++ b/parser/src/main/java/handlers/rules/DeclarationRule.java @@ -7,8 +7,6 @@ import java.util.Arrays; import java.util.List; -import java.util.stream.Collectors; -import java.util.stream.Stream; public class DeclarationRule implements Rule { diff --git a/settings.gradle b/settings.gradle index 4601801..f56c2fd 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,2 +1,4 @@ include 'lexer' -include 'parser' \ No newline at end of file +include 'parser' +include 'interpreter' +include 'executor' \ No newline at end of file