Skip to content

Commit

Permalink
Fixed some stuff that didn't work correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
TheUnlocked committed Mar 17, 2018
1 parent 2e9a17b commit d3cc492
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 9 deletions.
33 changes: 33 additions & 0 deletions FAILang/Builtins/CollectionBuiltinProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Text;
using FAILang.Types;

namespace FAILang.Builtins
{
class CollectionBuiltinProvider : IBuiltinProvider
{
private static ExternalFunction ValidateType<T>(Func<T, IType> f, Func<IType, IType> fail) where T : IType
{
return x =>
{
if (x[0] is T t)
return f.Invoke(t);
return fail.Invoke(x[0]);
};
}

private static ExternFunction LENGTH = new ExternFunction(ValidateType<IIndexable>(
x => new Number(x.Length),
x => new Error("WrongType", $"{x} has no length")),
"c");

public (string, ExternFunction)[] GetBuiltins() => new(string, ExternFunction)[] {
("length", LENGTH)
};

public string[] GetReservedNames() => new string[] {
"length"
};
}
}
2 changes: 1 addition & 1 deletion FAILang/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Program
{
static void Main(string[] args)
{
Global.LoadBuiltins(new NumberBuiltinProvider());
Global.LoadBuiltins(new NumberBuiltinProvider(), new CollectionBuiltinProvider());
Console.InputEncoding = Encoding.Unicode;
Console.OutputEncoding = Encoding.Unicode;

Expand Down
2 changes: 1 addition & 1 deletion FAILang/Types/IIndexable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace FAILang.Types
{
interface IIndexable
interface IIndexable : IType
{
int Length { get; }
IType IndexRange(int left_b, int right_b);
Expand Down
19 changes: 19 additions & 0 deletions FAILang/Types/Unevaluated/BinaryOperatorExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,26 @@ public IType Evaluate(Dictionary<string, IType> lookups)
if (left is IUnevaluated || right is IUnevaluated)
return new BakedExpression(new BinaryOperatorExpression(op, left, right), lookups);

if (left is Error eLeft)
return eLeft;
if (right is Error eRight)
return eRight;

// Operate
if (op == BinaryOperator.EQUALS)
{
return left.Equals(right) ? MathBool.TRUE : MathBool.FALSE;
}
else if (op == BinaryOperator.NOT_EQUALS)
{
return left.Equals(right) ? MathBool.FALSE : MathBool.TRUE;
}
else
{
if (left == Void.instance || right == Void.instance)
return Void.instance;
}

if (left is IOperable lop && right is IOperable rop)
{
if (lop.BinaryOperators != null && rop.BinaryOperators != null && lop.BinaryOperators.TryGetValue(op, out var lac) && rop.BinaryOperators.ContainsKey(op))
Expand Down
16 changes: 9 additions & 7 deletions FAILang/Types/Unevaluated/CondExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ public IType Evaluate(Dictionary<string, IType> lookups)
{
for (int i = 0; i < conds.Length; i++)
{
IType t = conds[i];
if (t is IUnevaluated u)
t = u.Evaluate(lookups);
if (t is Union tu)
IType tCond = conds[i];
if (tCond is IUnevaluated u)
tCond = u.Evaluate(lookups);
if (tCond is Union tu)
{
IType[] result = new IType[tu.values.Length];
for (int j = 0; j < result.Length; j++)
Expand All @@ -39,13 +39,13 @@ public IType Evaluate(Dictionary<string, IType> lookups)
}
return new Union(result, lookups);
}
if (t is IUnevaluated)
if (tCond is IUnevaluated)
{
var nexpr = new CondExpression(conds.Skip(i).ToArray(), exprs.Skip(i).ToArray(), default_expr);
nexpr.conds[0] = t;
nexpr.conds[0] = tCond;
return new BakedExpression(nexpr, lookups);
}
if (t == MathBool.TRUE)
if (tCond == MathBool.TRUE)
{
IType ret = exprs[i];
if (ret is IUnevaluated uexpr)
Expand All @@ -58,6 +58,8 @@ public IType Evaluate(Dictionary<string, IType> lookups)
}
return ret;
}
if (tCond is Error)
return tCond;
}
return default_expr is IUnevaluated retd ? retd.Evaluate(lookups) : default_expr;
}
Expand Down
3 changes: 3 additions & 0 deletions FAILang/Types/Unevaluated/FunctionExpression.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Linq;

namespace FAILang.Types.Unevaluated
{
Expand Down Expand Up @@ -53,6 +54,8 @@ public IType Evaluate(Dictionary<string, IType> lookups)
args.Add(arg);
}
}
if (args.Any(x => x is IUnevaluated))
return new BakedExpression(new FunctionExpression(func_expr, args.Select(x => (x, false)).ToArray()), lookups);
return f.Evaluate(args.ToArray());
}
else if (func is Number n1 && args.Length == 1)
Expand Down
2 changes: 2 additions & 0 deletions FAILang/Types/Unevaluated/IndexerExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public IType Evaluate(Dictionary<string, IType> lookups)
}
else
{
if (expr is Error)
return expr;
return new Error("TypeError", $"The type {expr.TypeName} cannot be indexed");
}
}
Expand Down

0 comments on commit d3cc492

Please sign in to comment.