Пример #1
0
        protected override BoundExpression RewriteFunctionCall(BoundFunctionCall expression)
        {
            var  newExpressions = ImmutableArray.CreateBuilder <BoundExpression>();
            bool isSame         = true;

            for (int index = 0; index < expression.Parameters.Length; index++)
            {
                var parameter    = expression.Parameters[index];
                var newParameter = RewriteExpression(parameter);

                if (!newParameter.ValueType.Equals(expression.ParameterTypes[index]) &&
                    TypeConversionSymbol.TryFind(newParameter.ValueType, expression.ParameterTypes[index], out var symbol))
                {
                    newParameter = new BoundInternalTypeConversion(symbol, parameter);
                }

                newExpressions.Add(newParameter);

                if (newParameter != parameter)
                {
                    isSame = false;
                }
            }

            if (isSame)
            {
                return(expression);
            }

            return(new BoundFunctionCall(expression.Function, expression.PointerSymbol, newExpressions.ToImmutable()));
        }
Пример #2
0
        private void OutputFunctionCall(BoundFunctionCall node, string prefix)
        {
            builder.AddFragment(new OutputFragment(prefix, DefaultColour));
            builder.AddFragment(new OutputFragment(node.Name, FunctionNameColour));
            builder.AddFragment(new OutputFragment("(", DefaultColour));

            var paramCount = node.Parameters.Length;

            if (paramCount > 0)
            {
                for (int index = 0; index < paramCount - 1; index++)
                {
                    Output(node.Parameters[index], string.Empty);
                    builder.AddFragment(new OutputFragment(", ", DefaultColour));
                }

                Output(node.Parameters[paramCount - 1], string.Empty);
            }

            builder.AddFragment(new OutputFragment(")", DefaultColour));
        }
Пример #3
0
        protected virtual BoundExpression RewriteFunctionCall(BoundFunctionCall expression)
        {
            var  newExpressions = ImmutableArray.CreateBuilder <BoundExpression>();
            bool isSame         = true;

            foreach (var parameter in expression.Parameters)
            {
                var newParameter = RewriteExpression(parameter);
                newExpressions.Add(newParameter);

                if (newParameter != parameter)
                {
                    isSame = false;
                }
            }

            if (isSame)
            {
                return(expression);
            }

            return(new BoundFunctionCall(expression.Function, expression.PointerSymbol, newExpressions.ToImmutable()));
        }
Пример #4
0
        private object EvaluateFunctionCall(BoundFunctionCall statement)
        {
            FunctionSymbol function = null;

            if (statement.Function != null)
            {
                function = statement.Function;
            }
            else if (statement.PointerSymbol != null)
            {
                object varEval = EvaluateVariable(statement.PointerSymbol.BaseSymbol);

                if (!(varEval is FunctionPointerObject pointer))
                {
                    return(null);
                }

                function = pointer.Function;
            }

            if (function == null)
            {
                return(null);
            }

            if (function == BuiltInFunctions.Print)
            {
                string text = (string)EvaluateExpression(statement.Parameters[0]);
                Console.WriteLine(text);
            }
            else if (function == BuiltInFunctions.Input)
            {
                string input = Console.ReadLine();
                return(input);
            }
            else if (Program.FunctionBodies.TryGetValue(function, out var boundBlock))
            {
                var newScope   = new Dictionary <VariableSymbol, object>();
                var parameters = new VariableSymbol[function.Parameters.Length];
                int index      = 0;

                foreach (var param in function.Parameters)
                {
                    parameters[index] = new VariableSymbol(param.Name, false, param.ValueType);
                    newScope.Add(parameters[index], null);
                    index++;
                }

                for (index = 0; index < parameters.Length; index++)
                {
                    newScope[parameters[index]] = EvaluateExpression(statement.Parameters[index]);
                }

                locals.Push(newScope);

                var value = EvaluateBlock(boundBlock);

                locals.Pop();

                return(value);
            }

            return(null);
        }