Skip to content

Commit

Permalink
Switched to lexical scope
Browse files Browse the repository at this point in the history
  • Loading branch information
TheUnlocked committed Jun 15, 2018
1 parent 323c7fb commit 1e8fe1d
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 7 deletions.
5 changes: 5 additions & 0 deletions FAILang/FAILang.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<NoWarn>1701;1702;1705;3021</NoWarn>
<LangVersion>7.1</LangVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<LangVersion>7.1</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions FAILang/FAILangVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public override IType VisitDef([NotNull] FAILangParser.DefContext context)
else if (context.fparams() != null)
{
IType expr = VisitExpression(exp);
Function f = new Function(context.fparams().param().Select(x => x.GetText()).ToArray(), expr, memoize: memoize, elipsis: context.fparams().elipsis != null);
Function f = new Function(context.fparams().param().Select(x => x.GetText()).ToArray(), expr, Global.Instance.globalVariables, memoize: memoize, elipsis: context.fparams().elipsis != null);

Global.Instance.globalVariables[name] = f;
}
Expand Down Expand Up @@ -333,9 +333,9 @@ public override IType VisitPiecewise([NotNull] FAILangParser.PiecewiseContext co
public override IType VisitLambda([NotNull] FAILangParser.LambdaContext context)
{
if (context.fparams() != null)
return new Function(context.fparams().param().Select(x => x.GetText()).ToArray(), VisitExpression(context.expression()), memoize: context.memoize != null, elipsis: context.fparams().elipsis != null);
return new UnevaluatedFunction(context.fparams().param().Select(x => x.GetText()).ToArray(), VisitExpression(context.expression()), memoize: context.memoize != null, elipsis: context.fparams().elipsis != null);
else
return new Function(new string[] { context.param().GetText() }, VisitExpression(context.expression()), memoize: false, elipsis: context.elipsis != null);
return new UnevaluatedFunction(new string[] { context.param().GetText() }, VisitExpression(context.expression()), memoize: false, elipsis: context.elipsis != null);
}

public override IType VisitUnion([NotNull] FAILangParser.UnionContext context)
Expand Down
2 changes: 1 addition & 1 deletion FAILang/Types/ExternFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class ExternFunction : Function
{
ExternalFunction func;

public ExternFunction(ExternalFunction func, params string[] fparams) : base(fparams, null)
public ExternFunction(ExternalFunction func, params string[] fparams) : base(fparams, null, null, false, false)
{
this.func = func;
}
Expand Down
6 changes: 4 additions & 2 deletions FAILang/Types/Function.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ public class Function : IType

public bool memoize;
public Dictionary<int, IType> memos = new Dictionary<int, IType>();
public Dictionary<string, IType> lookups;

public Function(string[] fparams, IType expression, bool memoize = false, bool elipsis = false)
public Function(string[] fparams, IType expression, Dictionary<string, IType> lookups, bool memoize, bool elipsis)
{
this.fparams = fparams.ToArray();
this.expression = expression;
this.lookups = lookups;
this.memoize = memoize;
this.elipsis = elipsis;
}
Expand All @@ -32,7 +34,7 @@ public virtual IType Evaluate(IType[] args)
{
if (memoize && memos.TryGetValue(GetArgListHashCode(args), out IType v))
return v;
Dictionary<string, IType> lookup = new Dictionary<string, IType>();
Dictionary<string, IType> lookup = new Dictionary<string, IType>(lookups);
IType[] extra = new IType[args.Length - fparams.Length + 1];
for (int i = 0; i < args.Length; i++)
{
Expand Down
2 changes: 1 addition & 1 deletion FAILang/Types/Unevaluated/FunctionExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public IType Evaluate(Dictionary<string, IType> lookups)
if (func is Error)
return func;

if (func is Function f)
if (func is Function f && !(func is UnevaluatedFunction))
{
List<IType> args = new List<IType>();
for (int i = 0; i < this.args.Length; i++)
Expand Down
18 changes: 18 additions & 0 deletions FAILang/Types/Unevaluated/UnevaluatedFunction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace FAILang.Types.Unevaluated
{
class UnevaluatedFunction : Function, IUnevaluated
{
public UnevaluatedFunction(string[] fparams, IType expression, bool memoize = false, bool elipsis = false) :
base(fparams, expression, null, memoize, elipsis)
{

}

public IType Evaluate(Dictionary<string, IType> lookups) =>
new Function(fparams, expression, lookups, memoize, elipsis);
}
}

0 comments on commit 1e8fe1d

Please sign in to comment.