Skip to content

Commit

Permalink
Added is operator and patched relational ops
Browse files Browse the repository at this point in the history
  • Loading branch information
TheUnlocked committed Apr 21, 2018
1 parent 411f808 commit 9639f28
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 13 deletions.
9 changes: 6 additions & 3 deletions FAILang/FAILangVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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++)
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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]));
}
Expand Down
5 changes: 3 additions & 2 deletions FAILang/Global.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 15 additions & 1 deletion FAILang/Grammar/FAILang.g4
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,15 @@ expression
;

relational
: binary ( op=(EQ | NE | R_ARR | L_ARR | GE | LE) binary)*
: binary ( relational_op binary)*
;

binary
: prefix
| <assoc=right> binary op=EXPONENT binary
| binary op=( MULTIPLY | DIVIDE | MODULO ) binary
| binary op=( PLUS | SUBTRACT ) binary
| binary op=IS binary
;

prefix
Expand Down Expand Up @@ -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
|
Expand Down Expand Up @@ -268,6 +278,10 @@ OTHERWISE
: 'otherwise'
;

IS
: 'is'
;

NAME
:
( UPPERCASE
Expand Down
13 changes: 8 additions & 5 deletions FAILang/Operator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ public enum BinaryOperator
MULTIPLY,
DIVIDE,
MODULO,
EXPONENT
EXPONENT,
IS
}
public enum RelationalOperator
{
EQUALS,
NOT_EQUALS,
GREATER,
LESS,
GR_EQUAL,
LE_EQUAL
GREATER_EQUAL,
LESS_EQUAL
}
public enum UnaryOperator
{
Expand All @@ -51,6 +52,8 @@ public static string ToDisplayString(this BinaryOperator op)
return "%";
case BinaryOperator.EXPONENT:
return "^";
case BinaryOperator.IS:
return "is";
default:
return "";
}
Expand All @@ -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 "";
Expand Down
4 changes: 2 additions & 2 deletions FAILang/Types/Number.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<UnaryOperator, Func<IType>> UnaryOperators => new Dictionary<UnaryOperator, Func<IType>>()
Expand Down
5 changes: 5 additions & 0 deletions FAILang/Types/Unevaluated/BinaryOperatorExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public IType Evaluate(Dictionary<string, IType> 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;

Expand Down

1 comment on commit 9639f28

@TheUnlocked
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolves #27

Please sign in to comment.