Exemple #1
0
        public override IFunction CallInternal(IFunction[] arguments, string output, CompilationContext context)
        {
            if (IsNamespace && arguments.Length == 0)
            {
                return(CompileIntermediate(arguments, output, context));
            }
            else if (IsNamespace && !IsClass)
            {
                return(context.LogError(9999, $"This is a Namespace, so it has no constructor"));
            }

            if (IsClass &&
                arguments.Length == Inputs.Length &&
                Outputs.All(p => p.Name != output) &&
                NamespaceMembers.Contains(output))
            {
                var memberFunction = CompileIntermediate(Array.Empty <IFunction>(), output, context);
                if (memberFunction.Inputs.Length > 0 && memberFunction.Inputs[0].Name == "this")
                {
                    var classInstance = this.Call(arguments, context);
                    return(classInstance.AsMethod(memberFunction, MakeCallSite(Ast), context));
                }
            }

            context.PushTrace(MakeCallSite(Ast));
            if (this.CheckArguments(arguments, output, context) != null)
            {
                context.PopTrace();
                return(CompilationError.Instance);
            }

            context.PopTrace();

            if (IsClass)
            {
                return(arguments[Array.FindIndex(Outputs, p => p.Name == output)]);
            }

            var outputPort = Outputs.First(p => p.Name == output);
            var outValue   = CompileIntermediate(arguments, output, context);
            var success    = outputPort.Type.SatisfiedBy(outValue, context);

            return(success switch
            {
                false => context.LogError(8, $"Output `{outputPort}` was not satisfied by its value `{outValue}` (See previous errors)"),
                null => CompilationError.Instance,
                _ => outValue
            });
Exemple #2
0
        /// <summary>
        /// Compiles a single expression (e.g. part of an expression list)
        /// </summary>
        private IFunction CompileExpression(IFunction?previous, Match exprAst, CompilerStackFrame compilerStackFrame, CompilationContext context)
        {
            switch (exprAst.Name)
            {
            case ElementAST.LiteralExpression:
                return(new Constant((float)exprAst.Value));

            case ElementAST.VariableExpression:
                context.PushTrace(MakeCallSite(exprAst));
                var variable = CompileFunction(exprAst.Text, compilerStackFrame, context);
                context.PopTrace();
                return(variable);

            case ElementAST.SubExpression:
                return(previous.Call(exprAst[ElementAST.SubExpressionName].Text, context, MakeCallSite(exprAst)));

            case ElementAST.CallExpression:
                // This is called a *lot*, so try to make it high performance:
                var args    = exprAst[ElementAST.CallArguments].Matches;
                var argList = new IFunction[args.Count];
                for (var i = 0; i < argList.Length; i++)
                {
                    argList[i] = CompileExpressionList(args[i], compilerStackFrame, context);
                }

                return(previous.Call(argList, context, MakeCallSite(exprAst)));

            default:
                return(context.LogError(9999, $"Unknown expression {exprAst}"));
            }
        }
Exemple #3
0
        public static IFunction Call(this IFunction function, IFunction[] arguments, string output,
                                     CompilationContext info, TraceSite?callSite = null)
        {
            if (function == null)
            {
                throw new ArgumentNullException(nameof(function));
            }
            if (callSite.HasValue)
            {
                info.PushTrace(callSite.Value);
            }
            var retval = function.CallInternal(arguments, output, info);

            if (callSite.HasValue)
            {
                info.PopTrace();
            }
            return(retval);
        }