update tests
This commit is contained in:
@ -6,6 +6,7 @@ import org.antlr.v4.runtime.Lexer;
|
|||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import parser.ast.AST;
|
import parser.ast.AST;
|
||||||
import parser.ast.ASTCompacter;
|
import parser.ast.ASTCompacter;
|
||||||
|
import parser.ast.ExpressionBalancer;
|
||||||
import parser.grammar.Grammar;
|
import parser.grammar.Grammar;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -30,7 +31,7 @@ class Demo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void demo() throws URISyntaxException, IOException {
|
void demoClean() throws URISyntaxException, IOException {
|
||||||
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
||||||
Grammar grammar = Grammar.fromFile(path);
|
Grammar grammar = Grammar.fromFile(path);
|
||||||
LL1Parser parser = LL1Parser.fromGrammar(grammar);
|
LL1Parser parser = LL1Parser.fromGrammar(grammar);
|
||||||
@ -40,4 +41,17 @@ class Demo {
|
|||||||
|
|
||||||
ASTCompacter.clean(tree, grammar);
|
ASTCompacter.clean(tree, grammar);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void demoBalance() throws URISyntaxException, IOException {
|
||||||
|
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
||||||
|
Grammar grammar = Grammar.fromFile(path);
|
||||||
|
LL1Parser parser = LL1Parser.fromGrammar(grammar);
|
||||||
|
|
||||||
|
Lexer lex = this.initLexer("General.stups");
|
||||||
|
AST tree = parser.parse(lex.getAllTokens(), lex.getVocabulary());
|
||||||
|
|
||||||
|
ASTCompacter.clean(tree, grammar);
|
||||||
|
ExpressionBalancer.balance(tree);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,11 +1,10 @@
|
|||||||
package parser;
|
package parser.ast;
|
||||||
|
|
||||||
import lexer.StupsLexer;
|
import lexer.StupsLexer;
|
||||||
import org.antlr.v4.runtime.CharStreams;
|
import org.antlr.v4.runtime.CharStreams;
|
||||||
import org.antlr.v4.runtime.Lexer;
|
import org.antlr.v4.runtime.Lexer;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import parser.ast.AST;
|
import parser.LL1Parser;
|
||||||
import parser.ast.ASTCompacter;
|
|
||||||
import parser.grammar.Grammar;
|
import parser.grammar.Grammar;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -82,6 +81,6 @@ class ASTCompacterTest {
|
|||||||
|
|
||||||
ASTCompacter.clean(tree, grammar);
|
ASTCompacter.clean(tree, grammar);
|
||||||
|
|
||||||
assertThat(tree.size()).isEqualTo(33);
|
assertThat(tree.size()).isEqualTo(31);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -66,6 +66,18 @@ class ExpressionBalancerTest {
|
|||||||
assertThat(tree.getRoot().getChildren().get(1).getName()).isEqualTo("EXPR");
|
assertThat(tree.getRoot().getChildren().get(1).getName()).isEqualTo("EXPR");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testTree1Flip2x() {
|
||||||
|
AST tree = tree1();
|
||||||
|
System.out.println("Before:\n" + tree);
|
||||||
|
|
||||||
|
ExpressionBalancer.flip(tree);
|
||||||
|
ExpressionBalancer.flip(tree);
|
||||||
|
System.out.println("After:\n" + tree);
|
||||||
|
|
||||||
|
assertThat(tree).isEqualTo(tree1());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testTree2Flip() {
|
void testTree2Flip() {
|
||||||
AST tree = tree2();
|
AST tree = tree2();
|
||||||
@ -80,6 +92,18 @@ class ExpressionBalancerTest {
|
|||||||
assertThat(tree.getRoot().getChildren().get(1).getChildren().get(1).getName()).isEqualTo("EXPR");
|
assertThat(tree.getRoot().getChildren().get(1).getChildren().get(1).getName()).isEqualTo("EXPR");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testTree2Flip2x() {
|
||||||
|
AST tree = tree2();
|
||||||
|
System.out.println("Before:\n" + tree);
|
||||||
|
|
||||||
|
ExpressionBalancer.flip(tree);
|
||||||
|
ExpressionBalancer.flip(tree);
|
||||||
|
System.out.println("After:\n" + tree);
|
||||||
|
|
||||||
|
assertThat(tree).isEqualTo(tree2());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testTree1Rotate() {
|
void testTree1Rotate() {
|
||||||
AST tree = tree1();
|
AST tree = tree1();
|
||||||
@ -90,6 +114,7 @@ class ExpressionBalancerTest {
|
|||||||
System.out.println("After:\n" + tree);
|
System.out.println("After:\n" + tree);
|
||||||
|
|
||||||
assertThat(tree.size()).isEqualTo(3);
|
assertThat(tree.size()).isEqualTo(3);
|
||||||
|
assertThat(tree.getRoot().getValue()).isEqualTo("SUB");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -102,5 +127,6 @@ class ExpressionBalancerTest {
|
|||||||
System.out.println("After:\n" + tree);
|
System.out.println("After:\n" + tree);
|
||||||
|
|
||||||
assertThat(tree.size()).isEqualTo(5);
|
assertThat(tree.size()).isEqualTo(5);
|
||||||
|
assertThat(tree.getRoot().getValue()).isEqualTo("SUB");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
class MyClass {
|
class MyClass {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
int a = (1 + 2) * 3;
|
while (1 < 2)
|
||||||
|
i = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user