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

fixes on getType

parent 9f439f6c
......@@ -32,8 +32,8 @@ assignment
expression
:
MINUS expression #exprMinus
| NEGATE expression #exprNegate
MINUS expression #exprNegate
| NOT expression #exprNot
| expression MUL expression #exprMultiplication
| <assoc=right> expression DIV expression #exprDivision
| expression op=(PLUS|MINUS) expression #exprLineOperators
......@@ -159,7 +159,7 @@ AND : '&' ;
OR: '|' ;
IMP : '==>' ;
EQUIV : '<=>' ;
NEGATE: 'not';
NOT: 'not';
//options {...}
//import ... ;
......
......@@ -103,12 +103,12 @@ public class TransformAst implements ScriptLanguageVisitor<Object> {
}
@Override
public Object visitExprMinus(ScriptLanguageParser.ExprMinusContext ctx) {
public Object visitExprNegate(ScriptLanguageParser.ExprNegateContext ctx) {
return createUnaryExpression(ctx, ctx.expression(), Operator.NEGATE);
}
@Override
public Object visitExprNegate(ScriptLanguageParser.ExprNegateContext ctx) {
public Object visitExprNot(ScriptLanguageParser.ExprNotContext ctx) {
return createUnaryExpression(ctx, ctx.expression(), Operator.NOT);
}
......@@ -118,8 +118,12 @@ public class TransformAst implements ScriptLanguageVisitor<Object> {
}
private Operator findOperator(String n) {
return findOperator(n, 2);
}
private Operator findOperator(String n, int arity) {
for (Operator op : Operator.values()) {
if (op.symbol().equals(n))
if (op.symbol().equals(n) && op.arity() == arity)
return op;
}
throw new IllegalStateException("Operator " + n + " not defined");
......
......@@ -34,13 +34,14 @@ public class BinaryExpression extends Expression<ParserRuleContext> {
return be;
}
@Override
public Type getType(Signature signature) throws NotWelldefinedException {
if (operator.arity() != 2)
throw new NotWelldefinedException("Arity mismatch", this);
operator.type()[0].equals(left.getType(signature));
operator.type()[1].equals(right.getType(signature));
checkType(operator.type()[0],left,signature);
checkType(operator.type()[1],right,signature);
return operator.returnType();
}
......
......@@ -10,9 +10,16 @@ import org.antlr.v4.runtime.ParserRuleContext;
public abstract class Expression<T extends ParserRuleContext> extends ASTNode<T> {
public abstract int getPrecedence();
@Override public abstract Expression clone();
@Override
public abstract Expression clone();
public abstract Type getType(Signature signature)
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> {
@Override
public Type getType(Signature signature) throws NotWelldefinedException {
if(operator.arity()!=1)
if (operator.arity() != 1)
throw new NotWelldefinedException("Arity mismatch!", this);
operator.type()[0].equals(expression.getType(signature));
checkType(operator.type()[0], expression, signature);
return operator.returnType();
}
......
......@@ -22,11 +22,13 @@ public class Variable extends Literal {
setToken(variable);
}
@Override public <T> T accept(Visitor<T> visitor) {
@Override
public <T> T accept(Visitor<T> visitor) {
return visitor.visit(this);
}
@Override public Variable clone() {
@Override
public Variable clone() {
Variable v = new Variable(identifier);
v.token = token;
return v;
......@@ -37,11 +39,15 @@ public class Variable extends Literal {
* @return
* @throws NotWelldefinedException
*/
@Override public Type getType(Signature signature) throws NotWelldefinedException {
return null;
@Override
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 + '\'' + '}';
}
......
package edu.kit.formal.proofscriptparser;
import edu.kit.formal.proofscriptparser.ast.Expression;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
......@@ -18,11 +19,13 @@ public class BadExpressionTest {
public String testExpression;
@Test
public void test() throws IOException {
@Test(expected = NotWelldefinedException.class)
public void test() throws IOException, NotWelldefinedException {
ScriptLanguageParser slp = TestHelper.getParser(testExpression);
slp.expression();
Assert.assertNotEquals(0, slp.getNumberOfSyntaxErrors());
ScriptLanguageParser.ExpressionContext e = slp.expression();
//Assert.assertNotEquals(0, slp.getNumberOfSyntaxErrors());
Expression expr = (Expression) e.accept(new TransformAst());
expr.getType(GoodExpressionTest.createSignature());
}
}
\ No newline at end of file
......@@ -29,9 +29,19 @@ public class GoodExpressionTest {
Assert.assertEquals(0, slp.getNumberOfSyntaxErrors());
Expression expr = (Expression) e.accept(new TransformAst());
Signature s = new Signature();
s.put(new Variable("a"), Type.integer);
Signature s = createSignature();
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
false
1+1
2+2*(3)
a+2
i+2
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