Commit 679d8b10 authored by Alexander Weigl's avatar Alexander Weigl

fixes on getType

parent 9f439f6c
...@@ -32,8 +32,8 @@ assignment ...@@ -32,8 +32,8 @@ assignment
expression expression
: :
MINUS expression #exprMinus MINUS expression #exprNegate
| NEGATE expression #exprNegate | NOT expression #exprNot
| expression MUL expression #exprMultiplication | expression MUL expression #exprMultiplication
| <assoc=right> expression DIV expression #exprDivision | <assoc=right> expression DIV expression #exprDivision
| expression op=(PLUS|MINUS) expression #exprLineOperators | expression op=(PLUS|MINUS) expression #exprLineOperators
...@@ -159,7 +159,7 @@ AND : '&' ; ...@@ -159,7 +159,7 @@ AND : '&' ;
OR: '|' ; OR: '|' ;
IMP : '==>' ; IMP : '==>' ;
EQUIV : '<=>' ; EQUIV : '<=>' ;
NEGATE: 'not'; NOT: 'not';
//options {...} //options {...}
//import ... ; //import ... ;
......
...@@ -103,12 +103,12 @@ public class TransformAst implements ScriptLanguageVisitor<Object> { ...@@ -103,12 +103,12 @@ public class TransformAst implements ScriptLanguageVisitor<Object> {
} }
@Override @Override
public Object visitExprMinus(ScriptLanguageParser.ExprMinusContext ctx) { public Object visitExprNegate(ScriptLanguageParser.ExprNegateContext ctx) {
return createUnaryExpression(ctx, ctx.expression(), Operator.NEGATE); return createUnaryExpression(ctx, ctx.expression(), Operator.NEGATE);
} }
@Override @Override
public Object visitExprNegate(ScriptLanguageParser.ExprNegateContext ctx) { public Object visitExprNot(ScriptLanguageParser.ExprNotContext ctx) {
return createUnaryExpression(ctx, ctx.expression(), Operator.NOT); return createUnaryExpression(ctx, ctx.expression(), Operator.NOT);
} }
...@@ -118,8 +118,12 @@ public class TransformAst implements ScriptLanguageVisitor<Object> { ...@@ -118,8 +118,12 @@ public class TransformAst implements ScriptLanguageVisitor<Object> {
} }
private Operator findOperator(String n) { private Operator findOperator(String n) {
return findOperator(n, 2);
}
private Operator findOperator(String n, int arity) {
for (Operator op : Operator.values()) { for (Operator op : Operator.values()) {
if (op.symbol().equals(n)) if (op.symbol().equals(n) && op.arity() == arity)
return op; return op;
} }
throw new IllegalStateException("Operator " + n + " not defined"); throw new IllegalStateException("Operator " + n + " not defined");
......
...@@ -34,13 +34,14 @@ public class BinaryExpression extends Expression<ParserRuleContext> { ...@@ -34,13 +34,14 @@ public class BinaryExpression extends Expression<ParserRuleContext> {
return be; return be;
} }
@Override @Override
public Type getType(Signature signature) throws NotWelldefinedException { public Type getType(Signature signature) throws NotWelldefinedException {
if (operator.arity() != 2) if (operator.arity() != 2)
throw new NotWelldefinedException("Arity mismatch", this); throw new NotWelldefinedException("Arity mismatch", this);
operator.type()[0].equals(left.getType(signature)); checkType(operator.type()[0],left,signature);
operator.type()[1].equals(right.getType(signature)); checkType(operator.type()[1],right,signature);
return operator.returnType(); return operator.returnType();
} }
......
...@@ -10,9 +10,16 @@ import org.antlr.v4.runtime.ParserRuleContext; ...@@ -10,9 +10,16 @@ import org.antlr.v4.runtime.ParserRuleContext;
public abstract class Expression<T extends ParserRuleContext> extends ASTNode<T> { public abstract class Expression<T extends ParserRuleContext> extends ASTNode<T> {
public abstract int getPrecedence(); public abstract int getPrecedence();
@Override public abstract Expression clone(); @Override
public abstract Expression clone();
public abstract Type getType(Signature signature) public abstract Type getType(Signature signature)
throws NotWelldefinedException; throws NotWelldefinedException;
public static final void checkType(Type type, Expression e, Signature signature) throws NotWelldefinedException {
Type got = e.getType(signature);
if (!type.equals(got)) {
throw new NotWelldefinedException("Typemismatch in expected " + type + ", got" + got, e);
}
}
} }
...@@ -35,9 +35,9 @@ public class UnaryExpression extends Expression<ParserRuleContext> { ...@@ -35,9 +35,9 @@ public class UnaryExpression extends Expression<ParserRuleContext> {
@Override @Override
public Type getType(Signature signature) throws NotWelldefinedException { public Type getType(Signature signature) throws NotWelldefinedException {
if(operator.arity()!=1) if (operator.arity() != 1)
throw new NotWelldefinedException("Arity mismatch!", this); throw new NotWelldefinedException("Arity mismatch!", this);
operator.type()[0].equals(expression.getType(signature)); checkType(operator.type()[0], expression, signature);
return operator.returnType(); return operator.returnType();
} }
......
...@@ -22,11 +22,13 @@ public class Variable extends Literal { ...@@ -22,11 +22,13 @@ public class Variable extends Literal {
setToken(variable); setToken(variable);
} }
@Override public <T> T accept(Visitor<T> visitor) { @Override
public <T> T accept(Visitor<T> visitor) {
return visitor.visit(this); return visitor.visit(this);
} }
@Override public Variable clone() { @Override
public Variable clone() {
Variable v = new Variable(identifier); Variable v = new Variable(identifier);
v.token = token; v.token = token;
return v; return v;
...@@ -37,11 +39,15 @@ public class Variable extends Literal { ...@@ -37,11 +39,15 @@ public class Variable extends Literal {
* @return * @return
* @throws NotWelldefinedException * @throws NotWelldefinedException
*/ */
@Override public Type getType(Signature signature) throws NotWelldefinedException { @Override
return null; public Type getType(Signature signature) throws NotWelldefinedException {
if (signature.containsKey(this))
return signature.get(this);
throw new NotWelldefinedException(toString() + "not defined in signature.", this);
} }
@Override public String toString() { @Override
public String toString() {
return "Variable{" + "identifier='" + identifier + '\'' + '}'; return "Variable{" + "identifier='" + identifier + '\'' + '}';
} }
......
package edu.kit.formal.proofscriptparser; package edu.kit.formal.proofscriptparser;
import edu.kit.formal.proofscriptparser.ast.Expression;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
...@@ -18,11 +19,13 @@ public class BadExpressionTest { ...@@ -18,11 +19,13 @@ public class BadExpressionTest {
public String testExpression; public String testExpression;
@Test @Test(expected = NotWelldefinedException.class)
public void test() throws IOException { public void test() throws IOException, NotWelldefinedException {
ScriptLanguageParser slp = TestHelper.getParser(testExpression); ScriptLanguageParser slp = TestHelper.getParser(testExpression);
slp.expression(); ScriptLanguageParser.ExpressionContext e = slp.expression();
Assert.assertNotEquals(0, slp.getNumberOfSyntaxErrors()); //Assert.assertNotEquals(0, slp.getNumberOfSyntaxErrors());
Expression expr = (Expression) e.accept(new TransformAst());
expr.getType(GoodExpressionTest.createSignature());
} }
} }
\ No newline at end of file
...@@ -28,10 +28,20 @@ public class GoodExpressionTest { ...@@ -28,10 +28,20 @@ public class GoodExpressionTest {
ScriptLanguageParser.ExpressionContext e = slp.expression(); ScriptLanguageParser.ExpressionContext e = slp.expression();
Assert.assertEquals(0, slp.getNumberOfSyntaxErrors()); Assert.assertEquals(0, slp.getNumberOfSyntaxErrors());
Expression expr = (Expression) e.accept(new TransformAst()); Expression expr = (Expression) e.accept(new TransformAst());
Signature s = new Signature(); Signature s = createSignature();
s.put(new Variable("a"), Type.integer);
System.out.println(expr.getType(s)); System.out.println(expr.getType(s));
} }
public static Signature createSignature() {
Signature s = new Signature();
s.put(new Variable("a"), Type.BOOL);
s.put(new Variable("b"), Type.BOOL);
s.put(new Variable("c"), Type.BOOL);
s.put(new Variable("i"), Type.INT);
s.put(new Variable("j"), Type.INT);
s.put(new Variable("k"), Type.INT);
return s;
}
} }
\ No newline at end of file
...@@ -3,5 +3,7 @@ true ...@@ -3,5 +3,7 @@ true
false false
1+1 1+1
2+2*(3) 2+2*(3)
a+2 i+2
a ==> b a ==> b
i+2=j-1
a<=>b<=>c
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment