Пример #1
0
            public override Ast.Node Transform(ParseScope Scope)
            {
                List = List.Transform(Scope);
                ResultType = List.ResultType;

                //Try to find an access macro for this type.
                var getterArguments = DummyArguments(Keyword("GET"), Keyword("AT"), Term(Scope.FindType("NUMBER")),
                    Keyword("FROM"), Term(List.ResultType));
                var indexerMacro = Scope.FindAllPossibleMacroMatches(getterArguments).Where(d =>
                    ExactDummyMatch(d.Terms, getterArguments)).FirstOrDefault();
                if (indexerMacro == null)
                    throw new CompileError("No macro of the form GET AT NUMBER FROM " +
                        List.ResultType.Name + " found.", Source);

                var lengthArguments = DummyArguments(Keyword("LENGTH"), Keyword("OF"), Term(List.ResultType));
                var lengthMacro = Scope.FindAllPossibleMacroMatches(lengthArguments).Where(d =>
                    ExactDummyMatch(d.Terms, lengthArguments)).FirstOrDefault();
                if (lengthMacro == null)
                    throw new CompileError("No macro of the form LENGTH OF " + List.ResultType.Name + " found.", Source);

                var nestedScope = Scope.Push(ScopeType.Block);

                ResultVariable = nestedScope.NewLocal("__result@" + VariableName, Scope.FindType("LIST"));
                ListVariable = nestedScope.NewLocal("__list@" + VariableName, Scope.FindType("LIST"));
                TotalVariable = nestedScope.NewLocal("__total@" + VariableName, Scope.FindType("NUMBER"));
                CounterVariable = nestedScope.NewLocal("__counter@" + VariableName, Scope.FindType("NUMBER"));
                ValueVariable = nestedScope.NewLocal(VariableName, indexerMacro.ReturnType);

                Indexer = Ast.StaticInvokation.CreateCorrectInvokationNode(Source, nestedScope, indexerMacro,
                    new List<Ast.Node>(new Ast.Node[] {
                        new Ast.Identifier(new Token { Type = TokenType.Identifier, Value = "__counter@"+VariableName }),
                        new Ast.Identifier(new Token { Type = TokenType.Identifier, Value = "__list@"+VariableName })
                    })).Transform(nestedScope);

                LengthFunc = Ast.StaticInvokation.CreateCorrectInvokationNode(Source, nestedScope, lengthMacro,
                    new List<Ast.Node>(new Ast.Node[] {
                        new Ast.Identifier(new Token { Type = TokenType.Identifier, Value = "__list@"+VariableName })
                    })).Transform(nestedScope);

                Condition = Condition.Transform(nestedScope);
                if (Condition.ResultType.Name != "BOOLEAN")
                    throw new CompileError("Condition to where clause must return boolean", Source);
                return this;
            }
Пример #2
0
        public void ResolveTypes(ParseScope Scope)
        {
            try
            {
                DeclarationScope = Scope.Push(ScopeType.Function);
                DeclarationScope.Owner = this;

                if (!String.IsNullOrEmpty(ReturnTypeName))
                {
                    ReturnType = Scope.FindType(ReturnTypeName);
                    if (ReturnType == null)
                    {
                        if (Body != null && Body.Body != null)
                            throw new CompileError("Could not find type '" +
                                ReturnTypeName + "'.", Body.Body.Source);
                        else
                            throw new CompileError("Could not find type '" +
                                ReturnTypeName + "'.");
                    }
                }
                else
                    ReturnType = EtcScriptLib.Type.Void;

                CreateParameterDescriptors();

                var header = DescriptiveHeader;
                if (Body != null) Body.Name = header;
                if (WhenClause != null) WhenClause.Name = header + " : when ...";
            }
            catch (Exception e)
            {
                throw new CompileError(e.Message + " while resolving types on " + DescriptiveHeader + "\n" + e.StackTrace);
            }
        }
Пример #3
0
 public override Ast.Node Transform(ParseScope Scope)
 {
     ResultType = Type.Void;
     Max = Max.Transform(Scope);
     var nestedScope = Scope.Push(ScopeType.Block);
     TotalVariable = nestedScope.NewLocal("__total@" + VariableName, Scope.FindType("NUMBER"));
     Min = Min.Transform(nestedScope);
     CounterVariable = nestedScope.NewLocal("__counter@" + VariableName, Scope.FindType("NUMBER"));
     ValueVariable = nestedScope.NewLocal(VariableName, Scope.FindType("NUMBER"));
     Body = Body.Transform(nestedScope);
     return this;
 }