diff --git a/FAILang/FAILang.csproj b/FAILang/FAILang.csproj index 726d4f2..3bdfc34 100644 --- a/FAILang/FAILang.csproj +++ b/FAILang/FAILang.csproj @@ -17,6 +17,11 @@ 1701;1702;1705;3021 + 7.1 + + + + 7.1 diff --git a/FAILang/FAILangVisitor.cs b/FAILang/FAILangVisitor.cs index 1e58fa5..dbb50ca 100644 --- a/FAILang/FAILangVisitor.cs +++ b/FAILang/FAILangVisitor.cs @@ -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; } @@ -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) diff --git a/FAILang/Types/ExternFunction.cs b/FAILang/Types/ExternFunction.cs index cdc3e05..6cab0c6 100644 --- a/FAILang/Types/ExternFunction.cs +++ b/FAILang/Types/ExternFunction.cs @@ -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; } diff --git a/FAILang/Types/Function.cs b/FAILang/Types/Function.cs index b076a46..b4bf3bb 100644 --- a/FAILang/Types/Function.cs +++ b/FAILang/Types/Function.cs @@ -17,11 +17,13 @@ public class Function : IType public bool memoize; public Dictionary memos = new Dictionary(); + public Dictionary lookups; - public Function(string[] fparams, IType expression, bool memoize = false, bool elipsis = false) + public Function(string[] fparams, IType expression, Dictionary lookups, bool memoize, bool elipsis) { this.fparams = fparams.ToArray(); this.expression = expression; + this.lookups = lookups; this.memoize = memoize; this.elipsis = elipsis; } @@ -32,7 +34,7 @@ public virtual IType Evaluate(IType[] args) { if (memoize && memos.TryGetValue(GetArgListHashCode(args), out IType v)) return v; - Dictionary lookup = new Dictionary(); + Dictionary lookup = new Dictionary(lookups); IType[] extra = new IType[args.Length - fparams.Length + 1]; for (int i = 0; i < args.Length; i++) { diff --git a/FAILang/Types/Unevaluated/FunctionExpression.cs b/FAILang/Types/Unevaluated/FunctionExpression.cs index dc42552..a43e487 100644 --- a/FAILang/Types/Unevaluated/FunctionExpression.cs +++ b/FAILang/Types/Unevaluated/FunctionExpression.cs @@ -26,7 +26,7 @@ public IType Evaluate(Dictionary lookups) if (func is Error) return func; - if (func is Function f) + if (func is Function f && !(func is UnevaluatedFunction)) { List args = new List(); for (int i = 0; i < this.args.Length; i++) diff --git a/FAILang/Types/Unevaluated/UnevaluatedFunction.cs b/FAILang/Types/Unevaluated/UnevaluatedFunction.cs new file mode 100644 index 0000000..f629873 --- /dev/null +++ b/FAILang/Types/Unevaluated/UnevaluatedFunction.cs @@ -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 lookups) => + new Function(fparams, expression, lookups, memoize, elipsis); + } +}