Beispiel #1
0
        public override Node Transform(ParseScope Scope)
        {
            if (HasBeenTransformed) return this;

            Function.ResolveTypes(Scope);
            Function.Transform(Scope.EnvironmentContext.ID);

            //Shift all parameters down by one index to accomodate the RSO used to store captured variables.
            foreach (var variable in Function.DeclarationScope.Variables)
                if (variable.StorageMethod == VariableStorageMethod.Local &&
                    variable.Offset < 0)
                    variable.Offset -= 1;

            Scope.AddChildLambda(Function);

            if (String.IsNullOrEmpty(ResultTypeName))
                ResultType = Type.Generic;
            else
            {
                ResultType = Scope.FindType(ResultTypeName);
                if (ResultType == null) throw new CompileError("Could not find type '" + ResultTypeName + "'.", Source);
            }

            HasBeenTransformed = true;

            return this;
        }
Beispiel #2
0
        public override Node Transform(ParseScope Scope)
        {
            if (HasBeenTransformed) return this;

            ResultType = Scope.FindType("COMPLEXSTRING");

            Function = new Declaration();
            Function.Type = DeclarationType.Lambda;
            Function.Terms = new List<DeclarationTerm>();
            Function.ReturnTypeName = "STRING";
            Function.ReturnType = Scope.FindType("STRING");
            Function.DeclarationScope = Scope.Push(ScopeType.Function);
            Function.DeclarationScope.Owner = Function;

            Pieces = new List<Node>(Pieces.Select(s => s.Transform(Function.DeclarationScope)).Where(n => n != null));

            if (Pieces.Count < 1)
                Pieces.Insert(0, new StringLiteral(Source, "").Transform(Function.DeclarationScope));

            if (Pieces.Count == 1)
            {
                Function.Body = new LambdaBlock(new Ast.Return(Source) { Value = Pieces[0] });
                Function.Body.Transform(Function.DeclarationScope);
            }
            else
            {
                var stringType = Scope.FindType("STRING");

                var binOp = Convert(Pieces[0], stringType, Scope);
                for (int i = 1; i < Pieces.Count; ++i)
                    binOp = new RawBinaryOperator(Source, VirtualMachine.InstructionSet.ADD, binOp,
                        Convert(Pieces[i], stringType, Function.DeclarationScope), stringType);

                Function.Body = new LambdaBlock(new Ast.Return(Source) { Value = binOp });
                Function.Body.Transform(Function.DeclarationScope);
            }

            Scope.AddChildLambda(Function);

            HasBeenTransformed = true;
            return this;
        }