Ejemplo n.º 1
0
        public override Node Transform(ParseScope Scope)
        {
            Object = Object.Transform(Scope);
            Index = Index.Transform(Scope);

            if (IsAssignmentTarget)
            {
                //Try to find an indexer macro for this type.
                var setterArguments = DummyArguments(Keyword("SET"), Keyword("AT"), Term(Index.ResultType),
                    Keyword("IN"), Term(Object.ResultType), Keyword("TO"), TermOfAnyType());
                var matchingSetter = Scope.FindAllPossibleMacroMatches(setterArguments).Where(d =>
                    ExactDummyMatch(d.Terms, setterArguments)).FirstOrDefault();
                if (matchingSetter != null)
                    return new ExplicitIndexSetter(Source, matchingSetter, Object, Index).Transform(Scope);
                else
                    throw new CompileError("No macro of the form SET AT " + Index.ResultType.Name + " IN " +
                            Object.ResultType.Name + " TO VALUE found.", Source);
            }
            else
            {
                //Try to find an access macro for this type.
                var getterArguments = DummyArguments(Keyword("GET"), Keyword("AT"), Term(Index.ResultType),
                    Keyword("FROM"), Term(Object.ResultType));
                var matchingGetter = Scope.FindAllPossibleMacroMatches(getterArguments).Where(d =>
                    ExactDummyMatch(d.Terms, getterArguments)).FirstOrDefault();
                if (matchingGetter != null)
                    return StaticInvokation.CreateCorrectInvokationNode(Source, Scope, matchingGetter,
                        new List<Node>(new Node[] { Index, Object })).Transform(Scope);
                else
                    throw new CompileError("No macro of the form GET AT " + Index.ResultType.Name + " FROM " +
                        Object.ResultType.Name + " found.", Source);
            }
        }
Ejemplo n.º 2
0
 public override Node Transform(ParseScope Scope)
 {
     ResultType = Scope.FindType(ResultTypename);
     if (ResultType == null) throw new CompileError("Could not find type '" + ResultTypename + "'.", Source);
     Parameters = new List<Node>(Parameters.Select(n => n.Transform(Scope)));
     return this;
 }
Ejemplo n.º 3
0
        public override Ast.Node Transform(ParseScope Scope)
        {
            //Look for a control macro.
            var control = Scope.EnvironmentContext.FindControl(Arguments);
            if (control != null && control.BlockType == ControlBlockType.NoBlock)
                return control.TransformationFunction(
                    Declaration.GenerateParameterListSyntaxTree(Arguments, control.DeclarationTerms).Members,
                    null).Transform(Scope);

            var possibleMatches = Scope.FindAllPossibleMacroMatches(Arguments);
            var match = FindTypeMatch(possibleMatches, Arguments, Scope);
            if (match == null)
            {
                var errorMessage = "Could not find match for static invokation. Arguments: ";
                foreach (var argument in Arguments)
                {
                    errorMessage += "(";
                    if (argument is Identifier) errorMessage += (argument as Identifier).Name.Value;
                    if (argument.ResultType != null) errorMessage += argument.ResultType.Name + ") ";
                    else errorMessage += "NULL)";
                }
                throw new CompileError(errorMessage, Source);
            }

            return CreateCorrectInvokationNode(Source, Scope, match.Item1, match.Item2).Transform(Scope);
        }
Ejemplo n.º 4
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;
        }
Ejemplo n.º 5
0
 public override Node Transform(ParseScope Scope)
 {
     ResultType = Scope.FindType(Typename);
     if (ResultType == null) throw new CompileError("Could not find type '" + Typename + "'.", Source);
     Value = Value.Transform(Scope);
     return this;
 }
Ejemplo n.º 6
0
        public override Node Transform(ParseScope Scope)
        {
            ResultType = Type.Void;
            if (Value != null)
            {
                Value = Value.Transform(Scope);

                if (Scope.OwnerFunction.Type == DeclarationType.Rule
                    && Scope.OwnerFunctionReturnType.Name != "RULE-RESULT")
                {
                    Value = new RuleResultNode(Source, Value).Transform(Scope);
                }
                else
                {
                    var conversionInfo = Type.AreTypesCompatible(Value.ResultType, Scope.OwnerFunctionReturnType, Scope);
                    if (!conversionInfo.Compatible)
                        Type.ThrowConversionError(Value.ResultType, Scope.OwnerFunctionReturnType, Source);

                    if (conversionInfo.ConversionRequired)
                        Value = Type.CreateConversionInvokation(Scope, conversionInfo.ConversionMacro, Value).Transform(Scope);
                }
            }
            else
            {
                if (!Object.ReferenceEquals(Scope.OwnerFunctionReturnType, Type.Void))
                    throw new CompileError("This function must return a value", Source);
            }
            DeclarationScope = Scope;
            return this;
        }
Ejemplo n.º 7
0
 public override Node Transform(ParseScope Scope)
 {
     ResultType = Type.Void;
     LocalScope = Scope.Push(ScopeType.Block);
     Statements = new List<Node>(Statements.Select(s => s.Transform(LocalScope)).Where(n => n != null));
     return this;
 }
Ejemplo n.º 8
0
 public override Ast.Node Transform(ParseScope Scope)
 {
     ResultType = Type.Void;
     var r = Control.TransformationFunction(
         Declaration.GenerateParameterListSyntaxTree(Arguments, Control.DeclarationTerms).Members,
         Body);
     return r.Transform(Scope);
 }
Ejemplo n.º 9
0
 public override Node Transform(ParseScope Scope)
 {
     ResultType = Type.Void;
     Header = Header.Transform(Scope);
     ThenBlock = ThenBlock.Transform(Scope);
     if (ElseBlock != null) ElseBlock = ElseBlock.Transform(Scope);
     return this;
 }
Ejemplo n.º 10
0
        public Node TransformAssignment(ParseScope Scope, Let Let, Node Value)
        {
            var arguments = new List<Node>();
            arguments.Add(Object);
            arguments.Add(Value);

            return StaticInvokation.CreateCorrectInvokationNode(Source, Scope, Function, arguments);
        }
Ejemplo n.º 11
0
 public override Node Transform(ParseScope Scope)
 {
     if (String.IsNullOrEmpty(Typename)) ResultType = Type.Generic;
     else
     {
         ResultType = Scope.FindType(Typename);
         if (ResultType == null) throw new CompileError("Could not find type '" + Typename + "'.", Source);
     }
     return this;
 }
Ejemplo n.º 12
0
 public override Node Transform(ParseScope Scope)
 {
     Object = Object.Transform(Scope);
     if (Object.ResultType.Origin == TypeOrigin.System) throw new InvalidOperationException();
     MemberVariable = Object.ResultType.FindMember(MemberName);
     if (MemberVariable == null) throw new CompileError("Could not find member '" + MemberName + "' on type '" +
         Object.ResultType.Name + "'.", Source);
     this.ResultType = MemberVariable.DeclaredType;
     return this;
 }
 /// <summary>
 /// 写入设置
 /// </summary>
 /// <param name="parseScope"></param>
 private void SetParseScopeSetting(ParseScope parseScope)
 {
     if (IsParseDefault)
     {
         SettingsManager.GetInstance().SetParseScope(parseScope);
     }
     else
     {
         SettingsManager.GetInstance().SetParseScope(ParseScope.NONE);
     }
 }
Ejemplo n.º 14
0
        private Tuple<Declaration, List<Node>> FindTypeMatch(List<Declaration> PossibleMatches, List<Node> Arguments, ParseScope Scope)
        {
            foreach (var possibleMatch in PossibleMatches)
            {
                var matches = TryTypeMatch(possibleMatch, Arguments, Scope);
                if (matches.Item1) return new Tuple<Declaration, List<Node>>(possibleMatch, matches.Item2);
            }

            //No macros matches
            return null;
        }
        public ViewParsingSelectorViewModel()
        {
            #region 属性初始化

            Title = DictionaryResource.GetString("ParsingSelector");

            // 解析范围
            ParseScope parseScope = SettingsManager.GetInstance().GetParseScope();
            IsParseDefault = parseScope != ParseScope.NONE;

            #endregion
        }
Ejemplo n.º 16
0
        public override Node Transform(ParseScope Scope)
        {
            ResultType = Type.Void;

            Value = Value.Transform(Scope);
            Member = ObjectType.FindMember(MemberName);
            if (Member == null) throw new CompileError("Unable to find member '" + MemberName + "' of " + ObjectType.Name);

            var compatibilityResult = Type.AreTypesCompatible(Value.ResultType, Member.DeclaredType, Scope);
            if (!compatibilityResult.Compatible)
                Type.ThrowConversionError(Value.ResultType, Member.DeclaredType, Source);

            if (compatibilityResult.ConversionRequired)
                Value = Type.CreateConversionInvokation(Scope, compatibilityResult.ConversionMacro, Value).Transform(Scope);

            return this;
        }
Ejemplo n.º 17
0
        public override Node Transform(ParseScope Scope)
        {
            ResultType = Type.Void;
            LHS.IsAssignmentTarget = true;
            LHS = LHS.Transform(Scope);
            if (!(LHS is IAssignable)) throw new CompileError("Assignment target is not an lvalue", Source);
            Value = Value.Transform(Scope);

            var compatibilityResult = Type.AreTypesCompatible(Value.ResultType, (LHS as IAssignable).DestinationType, Scope);
            if (!compatibilityResult.Compatible)
                Type.ThrowConversionError(Value.ResultType, (LHS as IAssignable).DestinationType, Source);

            if (compatibilityResult.ConversionRequired)
                Value = Type.CreateConversionInvokation(Scope, compatibilityResult.ConversionMacro, Value)
                    .Transform(Scope);

            return (LHS as IAssignable).TransformAssignment(Scope, this, Value);
        }
Ejemplo n.º 18
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;
        }
Ejemplo n.º 19
0
        public static Ast.Node CreateCorrectInvokationNode(
			Token Source, 
			ParseScope Scope, 
			Declaration Declaration, 
			List<Node> Arguments)
        {
            if (Declaration.OwnerContextID == Scope.EnvironmentContext.ID && Declaration.OwnerContextID != 0)
                return new Ast.JumpCall(Source, Declaration, Arguments);
            else
            {
                var implementation = Declaration.MakeInvokableFunction();
                if (implementation.IsStackInvokable)
                    return new Ast.StackCall(Source, Declaration, Arguments);
                else
                {
                    throw new CompileError("This should be impossible", Source);
                    //Arguments.Insert(0, new Ast.Literal(Source, Declaration.MakeInvokableFunction(), "GENERIC"));
                    //return new Ast.CompatibleCall(Source, Arguments, Declaration.ReturnTypeName);
                }
            }
        }
Ejemplo n.º 20
0
        public override Node Transform(ParseScope Scope)
        {
            //var existingVariable = Scope.FindVariable(Name);
            //if (existingVariable != null)
            //    throw new CompileError("A variable called '" + Name +
            //        "' can't be defined here because it would hide a variable already defined with that name.", Source);

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

            if (Value != null)
            {
                Value = Value.Transform(Scope);

                if (!String.IsNullOrEmpty(Typename))
                {
                    var compatibilityResult = Type.AreTypesCompatible(Value.ResultType, ResultType, Scope);
                    if (!compatibilityResult.Compatible)
                        Type.ThrowConversionError(Value.ResultType, ResultType, Source);

                    if (compatibilityResult.ConversionRequired)
                        Value = Type.CreateConversionInvokation(Scope, compatibilityResult.ConversionMacro, Value)
                            .Transform(Scope);
                }
                else //Infer the type of the variable from the expression assigned to it.
                    ResultType = Value.ResultType;
            }

            Variable = Scope.NewLocal(Name.ToUpper(), ResultType);
            Variable.DeclaredTypeName = Typename;
            Variable.DeclaredType = ResultType;

            return this;
        }
Ejemplo n.º 21
0
        public override Node Transform(ParseScope Scope)
        {
            LHS = LHS.Transform(Scope);
            RHS = RHS.Transform(Scope);

            //Generics behave dynamically with operators.
            if (Object.ReferenceEquals(LHS.ResultType, Type.Generic) || Object.ReferenceEquals(RHS.ResultType, Type.Generic))
                return new RawBinaryOperator(Source, Operator.instruction, LHS, RHS, Type.Generic);

            //Equality works with everything.
            if (Operator.token == "==" || Operator.token == "!=")
                return new RawBinaryOperator(Source, Operator.instruction, LHS, RHS, Scope.FindType("BOOLEAN"));

            if (Object.ReferenceEquals(LHS.ResultType, RHS.ResultType))
            {
                PopulateRawOperators();
                if (RawOperators.ContainsKey(LHS.ResultType.Name))
                    if (RawOperators[LHS.ResultType.Name].Contains(Operator.token))
                    {
                        //Return type is always boolean for raw comparisons.
                        if (Operator.token == ">" || Operator.token == ">=" || Operator.token == "<" || Operator.token == "<=")
                            return new RawBinaryOperator(Source, Operator.instruction, LHS, RHS, Scope.FindType("BOOLEAN"));
                        return new RawBinaryOperator(Source, Operator.instruction, LHS, RHS, LHS.ResultType);
                    }
            }

            //Try to find an operator macro for these types.
            var operatorArguments = DummyArguments(Term(LHS.ResultType), Keyword(Operator.token), Term(RHS.ResultType));
            var matchingOperator = Scope.FindAllPossibleMacroMatches(operatorArguments).Where(d =>
                    ExactDummyMatch(d.Terms, operatorArguments)).FirstOrDefault();
            if (matchingOperator != null)
            {
                return StaticInvokation.CreateCorrectInvokationNode(Source, Scope, matchingOperator,
                    new List<Node>(new Node[] { LHS, RHS })).Transform(Scope);
            }
            else
                throw new CompileError("No operator macro of the form " + LHS.ResultType.Name + " " +
                    Operator.token + " " + RHS.ResultType.Name + " found.", Source);
        }
Ejemplo n.º 22
0
        public override Node Transform(ParseScope Scope)
        {
            this.Scope = Scope;
            ResultType = Scope.FindType(Typename);
            if (ResultType == null) throw new CompileError("Unable to find type with name '" + Typename + "'", Source);
            if (ResultType.Origin != TypeOrigin.Script)
                throw new CompileError("New cannot be used with primitive or system types", Source);

            var constructorArguments = DummyArguments(Keyword("CONSTRUCT"), Term(ResultType));
            Constructor = Scope.FindAllPossibleMacroMatches(constructorArguments).Where(d =>
                ExactDummyMatch(d.Terms, constructorArguments)).FirstOrDefault();

            if (Initializers != null)
            {
                foreach (var initializer in Initializers)
                {
                    initializer.ObjectType = ResultType;
                    initializer.Transform(Scope);
                }
            }

            return this;
        }
Ejemplo n.º 23
0
        public virtual void Load()
        {
            this.m_errors = new List<string>();

            // empty file: just return
            if (this.m_stream.Length == 0)
                return;

            this.m_instances.Clear();

            // read header and validate
            m_parsescope = ParseScope.Header;
            ReadFile();

            // read instances
            m_parsescope = ParseScope.DataInstances;
            ReadFile();

            // read fields
            m_parsescope = ParseScope.DataFields;
            ReadFile();
        }
Ejemplo n.º 24
0
 public Node TransformAssignment(ParseScope Scope, Let Let, Node Value)
 {
     return Let;
 }
Ejemplo n.º 25
0
 /// <summary>
 /// 设置视频解析项
 /// </summary>
 /// <param name="parseScope"></param>
 /// <returns></returns>
 public bool SetParseScope(ParseScope parseScope)
 {
     appSettings.Basic.ParseScope = parseScope;
     return(SetSettings());
 }
Ejemplo n.º 26
0
        public override Node Transform(ParseScope Scope)
        {
            if (Name.Type == TokenType.Identifier)
            {
                if (Name.Value.ToUpper() == "TRUE") return new Literal(Source, true, "BOOLEAN").Transform(Scope);
                else if (Name.Value.ToUpper() == "FALSE") return new Literal(Source, false, "BOOLEAN").Transform(Scope);
                else MatchedVariable = Scope.FindVariable(Name.Value.ToUpper());
                if (MatchedVariable == null) throw new CompileError("Could not find variable named '" + Name.Value + "'.", Source);
                ResultType = MatchedVariable.DeclaredType;
            }
            else if (Name.Type == TokenType.String)
                return new StringLiteral(Source, Name.Value).Transform(Scope);
            else if (Name.Type == TokenType.Number)
            {
                ResultType = Scope.FindType("NUMBER");
            }

            return this;
        }
Ejemplo n.º 27
0
 public override Node Transform(ParseScope Scope)
 {
     ResultType = Scope.FindType("BOXED");
     Value = Value.Transform(Scope);
     return this;
 }
Ejemplo n.º 28
0
 public override Node Transform(ParseScope Scope)
 {
     ResultType = Scope.FindType("LIST");
     Members = new List<Node>(Members.Select(s => s.Transform(Scope)));
     return this;
 }
Ejemplo n.º 29
0
 public override Node Transform(ParseScope Scope)
 {
     ResultType = Function.ReturnType;
     Arguments = new List<Node>(Arguments.Select(n => n.Transform(Scope)));
     return this;
 }
Ejemplo n.º 30
0
 public override Node Transform(ParseScope Scope)
 {
     return this;
 }
Ejemplo n.º 31
0
        private Node Convert(Node Node, Type StringType, ParseScope Scope)
        {
            var conversionInfo = Type.AreTypesCompatible(Node.ResultType, StringType, Scope);
            if (!conversionInfo.Compatible)
                Type.ThrowConversionError(Node.ResultType, StringType, Source);

            if (conversionInfo.ConversionRequired)
                return Type.CreateConversionInvokation(Scope, conversionInfo.ConversionMacro, Node).Transform(Scope);
            return Node;
        }
Ejemplo n.º 32
0
 public override Node Transform(ParseScope Scope)
 {
     ResultType = Type.Generic;
     return this;
 }
Ejemplo n.º 33
0
 public override Node Transform(ParseScope Scope)
 {
     ResultType = Function.ReturnType;
     return this;
 }