Skip to content

Commit

Permalink
Added and/or operators
Browse files Browse the repository at this point in the history
  • Loading branch information
TheUnlocked committed May 1, 2018
1 parent e98e218 commit 8919c51
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
22 changes: 21 additions & 1 deletion FAILang/FAILangVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,27 @@ public override IType VisitDef([NotNull] FAILangParser.DefContext context)

public override IType VisitExpression([NotNull] FAILangParser.ExpressionContext context)
{
return VisitRelational(context.relational());
return VisitBoolean(context.boolean());
}

public override IType VisitBoolean([NotNull] FAILangParser.BooleanContext context)
{
if (context.op != null)
{
var relationalNodes = context.relational();
BinaryOperator oper = BinaryOperator.MULTIPLY;
switch (context.op.Text)
{
case "and":
oper = BinaryOperator.AND;
break;
case "or":
oper = BinaryOperator.OR;
break;
}
return new BinaryOperatorExpression(oper, VisitRelational(relationalNodes[0]), VisitRelational(relationalNodes[1]));
}
return VisitRelational(context.relational(0));
}

public override IType VisitRelational([NotNull] FAILangParser.RelationalContext context)
Expand Down
14 changes: 12 additions & 2 deletions FAILang/Grammar/FAILang.g4
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,15 @@ param
;

expression
: relational
: boolean
;

boolean
: relational ( op=( AND | OR ) relational)?
;

relational
: binary ( relational_op binary)*
: binary ( relational_op binary )*
;

binary
Expand Down Expand Up @@ -210,6 +214,12 @@ LE
NOT
: '~'
;
AND
: 'and'
;
OR
: 'or'
;

L_PAREN
: '('
Expand Down
8 changes: 7 additions & 1 deletion FAILang/Operator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public enum BinaryOperator
DIVIDE,
EXPONENT,
CONCAT,
IS
IS,
AND,
OR
}
public enum RelationalOperator
{
Expand Down Expand Up @@ -59,6 +61,10 @@ public static string ToDisplayString(this BinaryOperator op)
return "||";
case BinaryOperator.IS:
return "is";
case BinaryOperator.AND:
return "and";
case BinaryOperator.OR:
return "or";
default:
return "";
}
Expand Down
4 changes: 2 additions & 2 deletions FAILang/Types/MathBool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ private MathBool(bool value)
}

public Dictionary<BinaryOperator, Func<IOperable, IType>> BinaryOperators => new Dictionary<BinaryOperator, Func<IOperable, IType>>() {
{BinaryOperator.ADD, OpOr},
{BinaryOperator.MULTIPLY, OpAnd},
{BinaryOperator.OR, OpOr},
{BinaryOperator.AND, OpAnd},
{BinaryOperator.EXPONENT, OpXor}
};

Expand Down

0 comments on commit 8919c51

Please sign in to comment.