From 8919c51f7d7461b40ab7f14615e90c0bd1d1f91e Mon Sep 17 00:00:00 2001 From: TheUnlocked <> Date: Mon, 30 Apr 2018 18:01:37 -0700 Subject: [PATCH] Added and/or operators --- FAILang/FAILangVisitor.cs | 22 +++++++++++++++++++++- FAILang/Grammar/FAILang.g4 | 14 ++++++++++++-- FAILang/Operator.cs | 8 +++++++- FAILang/Types/MathBool.cs | 4 ++-- 4 files changed, 42 insertions(+), 6 deletions(-) diff --git a/FAILang/FAILangVisitor.cs b/FAILang/FAILangVisitor.cs index 85ba110..1e58fa5 100644 --- a/FAILang/FAILangVisitor.cs +++ b/FAILang/FAILangVisitor.cs @@ -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) diff --git a/FAILang/Grammar/FAILang.g4 b/FAILang/Grammar/FAILang.g4 index f17b974..39d19d5 100644 --- a/FAILang/Grammar/FAILang.g4 +++ b/FAILang/Grammar/FAILang.g4 @@ -44,11 +44,15 @@ param ; expression - : relational + : boolean + ; + +boolean + : relational ( op=( AND | OR ) relational)? ; relational - : binary ( relational_op binary)* + : binary ( relational_op binary )* ; binary @@ -210,6 +214,12 @@ LE NOT : '~' ; +AND + : 'and' + ; +OR + : 'or' + ; L_PAREN : '(' diff --git a/FAILang/Operator.cs b/FAILang/Operator.cs index fc0db52..b4c686c 100644 --- a/FAILang/Operator.cs +++ b/FAILang/Operator.cs @@ -19,7 +19,9 @@ public enum BinaryOperator DIVIDE, EXPONENT, CONCAT, - IS + IS, + AND, + OR } public enum RelationalOperator { @@ -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 ""; } diff --git a/FAILang/Types/MathBool.cs b/FAILang/Types/MathBool.cs index 3551799..6bddff2 100644 --- a/FAILang/Types/MathBool.cs +++ b/FAILang/Types/MathBool.cs @@ -20,8 +20,8 @@ private MathBool(bool value) } public Dictionary> BinaryOperators => new Dictionary>() { - {BinaryOperator.ADD, OpOr}, - {BinaryOperator.MULTIPLY, OpAnd}, + {BinaryOperator.OR, OpOr}, + {BinaryOperator.AND, OpAnd}, {BinaryOperator.EXPONENT, OpXor} };