Example #1
0
 private static IExpressionNode BuildExpression(
     ISyntaxNode node,
     IFunctionDictionary functions,
     VariableDictionary variables,
     TypeInferenceResults typeInferenceResults,
     TicTypesConverter typesConverter) =>
 node.Accept(new ExpressionBuilderVisitor(functions, variables, typeInferenceResults, typesConverter));
Example #2
0
        public static GenericUserFunction Create(
            TypeInferenceResults typeInferenceResults,
            UserFunctionDefinitionSyntaxNode syntaxNode,
            IFunctionDictionary dictionary)
        {
            var ticGenerics    = typeInferenceResults.Generics;
            var langConstrains = new GenericConstrains[ticGenerics.Length];

            for (int i = 0; i < ticGenerics.Length; i++)
            {
                var ticConstrains = ticGenerics[i];
                langConstrains[i] = GenericConstrains.FromTicConstrains(ticConstrains);
            }
            var ticFunName         = syntaxNode.Id + "'" + syntaxNode.Args.Count;
            var ticSignature       = (StateFun)typeInferenceResults.GetVariableType(ticFunName);
            var signatureConverter = TicTypesConverter.GenericSignatureConverter(ticGenerics);

            var argTypes = new FunnyType[ticSignature.ArgNodes.Length];

            for (var i = 0; i < ticSignature.ArgNodes.Length; i++)
            {
                argTypes[i] = signatureConverter.Convert(ticSignature.ArgNodes[i].State);
            }
            var retType = signatureConverter.Convert(ticSignature.ReturnType);

#if DEBUG
            TraceLog.WriteLine($"CREATE GENERIC FUN {syntaxNode.Id}({string.Join(",",argTypes)}):{retType}");
            TraceLog.WriteLine($"    ...where {string.Join(", ", langConstrains)}");
#endif
            var function = new GenericUserFunction(typeInferenceResults, syntaxNode, dictionary, langConstrains, retType, argTypes);
            return(function);
        }
Example #3
0
 private ExpressionBuilderVisitor(
     IFunctionDictionary functions,
     VariableDictionary variables,
     TypeInferenceResults typeInferenceResults,
     TicTypesConverter typesConverter)
 {
     _functions            = functions;
     _variables            = variables;
     _typeInferenceResults = typeInferenceResults;
     _typesConverter       = typesConverter;
 }
Example #4
0
 private GenericUserFunction(
     TypeInferenceResults typeInferenceResults,
     UserFunctionDefinitionSyntaxNode syntaxNode,
     IFunctionDictionary dictionary,
     GenericConstrains[] constrains,
     FunnyType returnType,
     FunnyType[] argTypes
     ) : base(syntaxNode.Id, constrains, returnType, argTypes)
 {
     _typeInferenceResults = typeInferenceResults;
     _constrainsMap        = _typeInferenceResults.Generics;
     _syntaxNode           = syntaxNode;
     _dictionary           = dictionary;
 }
Example #5
0
        public static IExpressionNode BuildExpression(
            ISyntaxNode node,
            IFunctionDictionary functions,
            FunnyType outputType,
            VariableDictionary variables,
            TypeInferenceResults typeInferenceResults,
            TicTypesConverter typesConverter)
        {
            var result = node.Accept(
                new ExpressionBuilderVisitor(functions, variables, typeInferenceResults, typesConverter));

            if (result.Type == outputType)
            {
                return(result);
            }
            var converter = VarTypeConverter.GetConverterOrThrow(result.Type, outputType, node.Interval);

            return(new CastExpressionNode(result, outputType, converter, node.Interval));
        }
Example #6
0
        public static ConcreteUserFunction BuildConcrete(
            this UserFunctionDefinitionSyntaxNode functionSyntax,
            FunnyType[] argTypes,
            FunnyType returnType,
            IFunctionDictionary functionsDictionary,
            TypeInferenceResults results,
            TicTypesConverter converter)
        {
            var vars = new VariableDictionary(functionSyntax.Args.Count);

            for (int i = 0; i < functionSyntax.Args.Count; i++)
            {
                var variableSource = RuntimeBuilderHelper.CreateVariableSourceForArgument(
                    argSyntax: functionSyntax.Args[i],
                    actualType: argTypes[i]);

                if (!vars.TryAdd(variableSource))
                {
                    throw ErrorFactory.FunctionArgumentDuplicates(functionSyntax, functionSyntax.Args[i]);
                }
            }

            var bodyExpression = ExpressionBuilderVisitor.BuildExpression(
                node: functionSyntax.Body,
                functions: functionsDictionary,
                outputType: returnType,
                variables: vars,
                typeInferenceResults: results,
                typesConverter: converter);

            vars.ThrowIfSomeVariablesNotExistsInTheList(
                functionSyntax.Args.Select(a => a.Id));

            var function = ConcreteUserFunction.Create(
                isRecursive: functionSyntax.IsRecursive,
                name: functionSyntax.Id,
                variables: vars.GetAllSources().ToArray(),
                isReturnTypeStrictlyTyped: functionSyntax.ReturnType != FunnyType.Empty,
                expression: bodyExpression);

            return(function);
        }
Example #7
0
        private static Equation BuildEquationAndPutItToVariables(
            EquationSyntaxNode equation,
            IFunctionDictionary functionsDictionary,
            VariableDictionary variables,
            TypeInferenceResults typeInferenceResults)
        {
            var expression = ExpressionBuilderVisitor.BuildExpression(
                node:       equation.Expression,
                functions:  functionsDictionary,
                outputType: equation.OutputType,
                variables:  variables,
                typeInferenceResults: typeInferenceResults,
                typesConverter: TicTypesConverter.Concrete);

            VariableSource outputVariableSource;

            if (equation.OutputTypeSpecified)
            {
                outputVariableSource = VariableSource.CreateWithStrictTypeLabel(
                    name: equation.Id,
                    type: equation.OutputType,
                    typeSpecificationIntervalOrNull: equation.TypeSpecificationOrNull.Interval,
                    attributes: equation.Attributes,
                    isOutput: true);
            }
            else
            {
                outputVariableSource = VariableSource.CreateWithoutStrictTypeLabel(
                    name: equation.Id,
                    type: equation.OutputType,
                    isOutput: true,
                    equation.Attributes);
            }

            var itVariable = variables.GetSuperAnonymousVariableOrNull();

            if (itVariable != null)
            {
                throw FunParseException.ErrorStubToDo("Variable cannot starts with it");
            }


            if (!variables.TryAdd(outputVariableSource))
            {
                //some equation referenced the source before
                var usages = variables.GetUsages(equation.Id);
                if (usages.Source.IsOutput)
                {
                    throw ErrorFactory.OutputNameWithDifferentCase(equation.Id, equation.Expression.Interval);
                }
                else
                {
                    throw ErrorFactory.CannotUseOutputValueBeforeItIsDeclared(usages);
                }
            }


            //ReplaceInputType
            if (outputVariableSource.Type != expression.Type)
            {
                throw new ImpossibleException("fitless");
            }
            return(new Equation(equation.Id, expression, outputVariableSource));
        }
Example #8
0
 public ApplyTiResultEnterVisitor(TypeInferenceResults solving, TicTypesConverter tiToLangTypeConverter)
 {
     _solving = solving;
     _tiToLangTypeConverter = tiToLangTypeConverter;
 }