1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2024-11-25 20:32:25 +00:00

Added support for

This commit is contained in:
jespergravgaard 2021-08-02 22:47:39 +02:00
parent bc0fcc50fd
commit 22abed306c
6 changed files with 23 additions and 17 deletions

View File

@ -1,5 +1,11 @@
package dk.camelot64.kickc.model.operators; package dk.camelot64.kickc.model.operators;
import dk.camelot64.kickc.model.CompileError;
import dk.camelot64.kickc.model.values.ConstantBool;
import dk.camelot64.kickc.model.values.ConstantEnumerable;
import dk.camelot64.kickc.model.values.ConstantInteger;
import dk.camelot64.kickc.model.values.ConstantLiteral;
import java.io.Serializable; import java.io.Serializable;
/** /**
@ -64,4 +70,12 @@ public class Operator implements Serializable {
return result; return result;
} }
static Boolean getBool(ConstantLiteral literal) {
if(literal instanceof ConstantBool)
return ((ConstantBool) literal).getBool();
if(literal instanceof ConstantEnumerable)
return !((ConstantEnumerable) literal).getInteger().equals(0L);
throw new CompileError("Not a boolean" + literal);
}
} }

View File

@ -33,4 +33,7 @@ public abstract class OperatorBinary extends Operator {
public boolean isAssociative() { public boolean isAssociative() {
return associative; return associative;
} }
} }

View File

@ -3,6 +3,7 @@ package dk.camelot64.kickc.model.operators;
import dk.camelot64.kickc.model.CompileError; import dk.camelot64.kickc.model.CompileError;
import dk.camelot64.kickc.model.types.SymbolType; import dk.camelot64.kickc.model.types.SymbolType;
import dk.camelot64.kickc.model.values.ConstantBool; import dk.camelot64.kickc.model.values.ConstantBool;
import dk.camelot64.kickc.model.values.ConstantInteger;
import dk.camelot64.kickc.model.values.ConstantLiteral; import dk.camelot64.kickc.model.values.ConstantLiteral;
/** Binary logic and Operator ( x && y ) */ /** Binary logic and Operator ( x && y ) */
@ -14,10 +15,7 @@ public class OperatorLogicAnd extends OperatorBinary {
@Override @Override
public ConstantLiteral calculateLiteral(ConstantLiteral left, ConstantLiteral right) { public ConstantLiteral calculateLiteral(ConstantLiteral left, ConstantLiteral right) {
if(left instanceof ConstantBool && right instanceof ConstantBool) { return new ConstantBool(getBool(left) && getBool(right));
return new ConstantBool(((ConstantBool) left).getBool() && ((ConstantBool) right).getBool());
}
throw new CompileError("Calculation not implemented " + left + " " + getOperator() + " " + right);
} }
@Override @Override

View File

@ -16,13 +16,7 @@ public class OperatorLogicNot extends OperatorUnary {
@Override @Override
public ConstantLiteral calculateLiteral(ConstantLiteral left, ProgramScope scope) { public ConstantLiteral calculateLiteral(ConstantLiteral left, ProgramScope scope) {
if(left instanceof ConstantEnumerable) { return new ConstantBool(!getBool(left));
left = new ConstantBool(((ConstantEnumerable) left).getInteger()!=0);
}
if(left instanceof ConstantBool) {
return new ConstantBool(!((ConstantBool) left).getBool() );
}
throw new CompileError("Calculation not implemented " + getOperator() + " " + left );
} }
@Override @Override

View File

@ -1,6 +1,5 @@
package dk.camelot64.kickc.model.operators; package dk.camelot64.kickc.model.operators;
import dk.camelot64.kickc.model.CompileError;
import dk.camelot64.kickc.model.types.SymbolType; import dk.camelot64.kickc.model.types.SymbolType;
import dk.camelot64.kickc.model.values.ConstantBool; import dk.camelot64.kickc.model.values.ConstantBool;
import dk.camelot64.kickc.model.values.ConstantLiteral; import dk.camelot64.kickc.model.values.ConstantLiteral;
@ -14,10 +13,7 @@ public class OperatorLogicOr extends OperatorBinary {
@Override @Override
public ConstantLiteral calculateLiteral(ConstantLiteral left, ConstantLiteral right) { public ConstantLiteral calculateLiteral(ConstantLiteral left, ConstantLiteral right) {
if(left instanceof ConstantBool && right instanceof ConstantBool) { return new ConstantBool(getBool(left) || getBool(right));
return new ConstantBool(((ConstantBool) left).getBool() || ((ConstantBool) right).getBool());
}
throw new CompileError("Calculation not implemented " + left + " " + getOperator() + " " + right);
} }
@Override @Override
@ -25,5 +21,4 @@ public class OperatorLogicOr extends OperatorBinary {
return SymbolType.BOOLEAN; return SymbolType.BOOLEAN;
} }
} }

View File

@ -162,6 +162,8 @@ public class TestPreprocessor {
assertEquals("name:y;", parse("#if defined A\nx;\n#endif\ny;")); assertEquals("name:y;", parse("#if defined A\nx;\n#endif\ny;"));
// Output generated by #elif // Output generated by #elif
assertEquals("name:y;name:w;", parse("#if 0\nx;\n#elif 1\ny;\n#elif 1\nz;\n#else\nq;\n#endif\nw;")); assertEquals("name:y;name:w;", parse("#if 0\nx;\n#elif 1\ny;\n#elif 1\nz;\n#else\nq;\n#endif\nw;"));
// Output generated by #if using defined and || operator
assertEquals("name:x;name:y;", parse("#define A X\n#if defined(A) || defined(B)\nx;\n#endif\ny;"));
} }
/** /**