Skip to content
Snippets Groups Projects
Commit d5cd0dbd authored by qwertzniki6's avatar qwertzniki6
Browse files

Added design by contracts

parent c7175fc5
No related branches found
No related tags found
No related merge requests found
......@@ -27,13 +27,16 @@
<version>5.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.java.contract</groupId>
<artifactId>cofoja</artifactId>
<version>1.1-r150</version>
<groupId>org.valid4j</groupId>
<artifactId>valid4j</artifactId>
<version>0.5.0</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
</project>
import static org.valid4j.Assertive.*;
public class Evaluator {
......@@ -29,7 +30,8 @@ public class Evaluator {
public float visit(AstBinaryOp node) {
visit(node.astExpression1);
//visit(node.astExpression1);
switch(node.astOperator.astOperatorContent) {
......@@ -45,6 +47,7 @@ public class Evaluator {
return (float) Math.pow(visit(node.astExpression1), visit(node.astExpression2));
}
neverGetHere();
return -111;
}
......
import java.util.ArrayList;
import java.util.Objects;
import com.google.java.contract.*;
public class Lexer {
@Requires({"s != null"})
public ArrayList<Token> lex(String s) {
require(s != null);
require(s.length() > 0);
ArrayList<String> separatedChars = separateChars(s);
ArrayList<String> tokensAsStrings = buildSubstringsFromSeparatedChars(separatedChars);
return constructTokenList(tokensAsStrings);
ArrayList<Token> tokenList = constructTokenList(tokensAsStrings);
ensure(tokenList.size() > 0);
return tokenList;
}
private ArrayList<String> separateChars(String stringToTokenize) {
......
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static org.valid4j.Assertive.*;
public class Parser {
public AstExpression parse (ArrayList<Token> tokenList) throws Exception {
public AstExpression parse(ArrayList<Token> tokenList) throws Exception {
require(tokenList.size() > 0);
AstExpression root;
root = parseAstExpression(tokenList);
ensure(root.astBinaryOp != null || root.astExpression != null || root.astValue != null);
return root;
}
......@@ -25,6 +29,7 @@ public class Parser {
if(tokenList.size() == 1 ) {
toReturn.astValue = parseValue(tokenList);
return toReturn;
}
......@@ -283,6 +288,7 @@ public class Parser {
}
private AstDigitWoZ parseDigitWoZ (Token digitWoZ) {
require(!Objects.equals(digitWoZ.tokenString, "0"));
AstDigitWoZ toReturn = new AstDigitWoZ();
......
......@@ -68,24 +68,20 @@ class PlotterPanel extends JPanel {
int[] xCords = valuesToDraw.getXCords();
for(int cord : xCords) {
yCordsWithOffset[i] = (ZERO_POINT_Y + (cord * -1 * 10));
i++;
}
System.out.println(Arrays.toString(yCordsWithOffset));
int j = 0;
int[] yCords = valuesToDraw.getYCords();
for(int cord : yCords) {
System.out.print("cords=" + cord);
xCordsWithOffset[j] = ZERO_POINT_X + (cord * 10);
j++;
}
System.out.println(Arrays.toString(xCordsWithOffset));
PlotterPanel.this.yCords = yCordsWithOffset;
......
import com.google.java.contract.Invariant;
import static org.valid4j.Assertive.*;
public class dbcTest {
public static void main(String[] args) {
print(null);
}
public static void print(String s) {
System.out.println(s);
int i = 5;
ensure(i > 6);
}
}
11111111111111111111111111111111111
x
x^2
4+3+x
......
......@@ -51,7 +51,13 @@ public class main {
for(ArrayList<Token> tokenList : tokenListList) {
rootAstExpressions.add(p.parse(tokenList));
try {
rootAstExpressions.add(p.parse(tokenList));
} catch (Exception e) {
System.out.println("Invalid function received");
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment