new test + testprograms
This commit is contained in:
86
src/test/java/parser/typechecker/SymbolTableTest.java
Normal file
86
src/test/java/parser/typechecker/SymbolTableTest.java
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
package parser.typechecker;
|
||||||
|
|
||||||
|
import lexer.StupsLexer;
|
||||||
|
import org.antlr.v4.runtime.CharStreams;
|
||||||
|
import org.antlr.v4.runtime.Lexer;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import parser.LL1Parser;
|
||||||
|
import parser.ast.AST;
|
||||||
|
import parser.grammar.Grammar;
|
||||||
|
import typechecker.SymbolAlreadyDefinedException;
|
||||||
|
import typechecker.SymbolTable;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||||
|
|
||||||
|
class SymbolTableTest {
|
||||||
|
|
||||||
|
private Lexer initLexer(String program) {
|
||||||
|
try {
|
||||||
|
Path path = Paths.get(this.getClass().getClassLoader().getResource("examplePrograms/" + program).toURI());
|
||||||
|
String programCode = Files.readString(path, StandardCharsets.US_ASCII);
|
||||||
|
return new StupsLexer(CharStreams.fromString(programCode));
|
||||||
|
} catch (Exception ignore) {
|
||||||
|
ignore.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSingleSymbol() 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("SingleSymbol.stups");
|
||||||
|
AST tree = parser.parse(lex.getAllTokens(), lex.getVocabulary());
|
||||||
|
tree.preprocess(grammar);
|
||||||
|
|
||||||
|
SymbolTable table = SymbolTable.fromAST(tree);
|
||||||
|
|
||||||
|
assertThat(table.getSymbolType("i")).isEqualTo("INTEGER_TYPE");
|
||||||
|
assertThat(table.getSymbolCount()).isEqualTo(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testMultipleSymbol() 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("MultipleSymbol.stups");
|
||||||
|
AST tree = parser.parse(lex.getAllTokens(), lex.getVocabulary());
|
||||||
|
tree.preprocess(grammar);
|
||||||
|
|
||||||
|
SymbolTable table = SymbolTable.fromAST(tree);
|
||||||
|
|
||||||
|
assertThat(table.getSymbolType("i")).isEqualTo("INTEGER_TYPE");
|
||||||
|
assertThat(table.getSymbolType("ii")).isEqualTo("INTEGER_TYPE");
|
||||||
|
assertThat(table.getSymbolType("b")).isEqualTo("BOOLEAN_TYPE");
|
||||||
|
assertThat(table.getSymbolType("bb")).isEqualTo("BOOLEAN_TYPE");
|
||||||
|
assertThat(table.getSymbolType("s")).isEqualTo("STRING_TYPE");
|
||||||
|
assertThat(table.getSymbolType("ss")).isEqualTo("STRING_TYPE");
|
||||||
|
assertThat(table.getSymbolCount()).isEqualTo(6);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testExistingSymbol() 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("ExistingSymbol.stups");
|
||||||
|
AST tree = parser.parse(lex.getAllTokens(), lex.getVocabulary());
|
||||||
|
tree.preprocess(grammar);
|
||||||
|
|
||||||
|
assertThatThrownBy(() -> SymbolTable.fromAST(tree)).isInstanceOf(SymbolAlreadyDefinedException.class);
|
||||||
|
}
|
||||||
|
}
|
||||||
6
src/test/resources/examplePrograms/ExistingSymbol.stups
Normal file
6
src/test/resources/examplePrograms/ExistingSymbol.stups
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
class SingleSymbol {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int i = 0;
|
||||||
|
String i = "Hi";
|
||||||
|
}
|
||||||
|
}
|
||||||
10
src/test/resources/examplePrograms/MultipleSymbol.stups
Normal file
10
src/test/resources/examplePrograms/MultipleSymbol.stups
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
class SingleSymbol {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int i = 0;
|
||||||
|
int ii = 0;
|
||||||
|
boolean b = true;
|
||||||
|
boolean bb = true;
|
||||||
|
String s = "Hi";
|
||||||
|
String ss = "Hi";
|
||||||
|
}
|
||||||
|
}
|
||||||
5
src/test/resources/examplePrograms/SingleSymbol.stups
Normal file
5
src/test/resources/examplePrograms/SingleSymbol.stups
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
class SingleSymbol {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int i = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user