diff --git a/FAILang/FAILangVisitor.cs b/FAILang/FAILangVisitor.cs index d257971..f392ee4 100644 --- a/FAILang/FAILangVisitor.cs +++ b/FAILang/FAILangVisitor.cs @@ -87,7 +87,7 @@ public override IType VisitRelational([NotNull] FAILangParser.RelationalContext return VisitBinary(context.binary(0)); } var binaryNodes = context.binary(); - var ops = context.GetTokens(context.op.Type); + var ops = context.relational_op(); RelationalOperator[] opers = new RelationalOperator[ops.Length]; for (int i = 0; i < opers.Length; i++) @@ -107,10 +107,10 @@ public override IType VisitRelational([NotNull] FAILangParser.RelationalContext opers[i] = RelationalOperator.LESS; break; case ">=": - opers[i] = RelationalOperator.GR_EQUAL; + opers[i] = RelationalOperator.GREATER_EQUAL; break; case "<=": - opers[i] = RelationalOperator.LE_EQUAL; + opers[i] = RelationalOperator.LESS_EQUAL; break; } } @@ -149,6 +149,9 @@ public override IType VisitBinary([NotNull] FAILangParser.BinaryContext context) case "^": oper = BinaryOperator.EXPONENT; break; + case "is": + oper = BinaryOperator.IS; + break; } return new BinaryOperatorExpression(oper, VisitBinary(binaryNodes[0]), VisitBinary(binaryNodes[1])); } diff --git a/FAILang/Global.cs b/FAILang/Global.cs index 951eaa7..bf4e25f 100644 --- a/FAILang/Global.cs +++ b/FAILang/Global.cs @@ -35,13 +35,14 @@ public static void ResetGlobals() "i", "true", "false", - "void", + "undefined", "lambda", "update", "memo", "self", "if", - "otherwise" + "otherwise", + "is" }; public void LoadBuiltins(params IBuiltinProvider[] builtinProviders) diff --git a/FAILang/Grammar/FAILang.g4 b/FAILang/Grammar/FAILang.g4 index 3d8ad1b..3a4ae1a 100644 --- a/FAILang/Grammar/FAILang.g4 +++ b/FAILang/Grammar/FAILang.g4 @@ -48,7 +48,7 @@ expression ; relational - : binary ( op=(EQ | NE | R_ARR | L_ARR | GE | LE) binary)* + : binary ( relational_op binary)* ; binary @@ -56,6 +56,7 @@ binary | binary op=EXPONENT binary | binary op=( MULTIPLY | DIVIDE | MODULO ) binary | binary op=( PLUS | SUBTRACT ) binary + | binary op=IS binary ; prefix @@ -114,6 +115,15 @@ union : L_PAREN (expression VERT_LINE)+ expression R_PAREN ; +relational_op + : EQ + | NE + | R_ARR + | L_ARR + | GE + | LE + ; + end : SEMI_COLON | @@ -268,6 +278,10 @@ OTHERWISE : 'otherwise' ; +IS + : 'is' + ; + NAME : ( UPPERCASE diff --git a/FAILang/Operator.cs b/FAILang/Operator.cs index 457fba8..4a1651e 100644 --- a/FAILang/Operator.cs +++ b/FAILang/Operator.cs @@ -17,7 +17,8 @@ public enum BinaryOperator MULTIPLY, DIVIDE, MODULO, - EXPONENT + EXPONENT, + IS } public enum RelationalOperator { @@ -25,8 +26,8 @@ public enum RelationalOperator NOT_EQUALS, GREATER, LESS, - GR_EQUAL, - LE_EQUAL + GREATER_EQUAL, + LESS_EQUAL } public enum UnaryOperator { @@ -51,6 +52,8 @@ public static string ToDisplayString(this BinaryOperator op) return "%"; case BinaryOperator.EXPONENT: return "^"; + case BinaryOperator.IS: + return "is"; default: return ""; } @@ -67,9 +70,9 @@ public static string ToDisplayString(this RelationalOperator op) return ">"; case RelationalOperator.LESS: return "<"; - case RelationalOperator.GR_EQUAL: + case RelationalOperator.GREATER_EQUAL: return ">="; - case RelationalOperator.LE_EQUAL: + case RelationalOperator.LESS_EQUAL: return "<="; default: return ""; diff --git a/FAILang/Types/Number.cs b/FAILang/Types/Number.cs index 70ad3e6..4eea8a8 100644 --- a/FAILang/Types/Number.cs +++ b/FAILang/Types/Number.cs @@ -32,9 +32,9 @@ public Number(Complex value) {RelationalOperator.EQUALS, OpEquals}, {RelationalOperator.NOT_EQUALS, OpNotEquals}, {RelationalOperator.GREATER, OpGreaterThan}, - {RelationalOperator.GR_EQUAL, OpGreaterEqual}, + {RelationalOperator.GREATER_EQUAL, OpGreaterEqual}, {RelationalOperator.LESS, OpLessThan}, - {RelationalOperator.LE_EQUAL, OpLessEqual} + {RelationalOperator.LESS_EQUAL, OpLessEqual} }; public Dictionary> UnaryOperators => new Dictionary>() diff --git a/FAILang/Types/Unevaluated/BinaryOperatorExpression.cs b/FAILang/Types/Unevaluated/BinaryOperatorExpression.cs index 669326c..a1345ac 100644 --- a/FAILang/Types/Unevaluated/BinaryOperatorExpression.cs +++ b/FAILang/Types/Unevaluated/BinaryOperatorExpression.cs @@ -56,6 +56,11 @@ public IType Evaluate(Dictionary lookups) return eRight; // Operate + if (op == BinaryOperator.IS) + { + return left.Equals(right) ? MathBool.TRUE : MathBool.FALSE; + } + if (left == Undefined.instance || right == Undefined.instance) return Undefined.instance;