Пример #1
0
        public Node(Layer layer, NodeArgs args)
        {
            this.args = args;

            if (args.InputTensors != null)
            {
                kerasInputs.AddRange(args.InputTensors);
            }

            // Wire up Node to Layers.
            layer.InboundNodes.Add(this);
            foreach (var kt in kerasInputs)
            {
                var inbound_layer = kt.KerasHistory.layer;
                if (inbound_layer != null)
                {
                    inbound_layer.OutboundNodes.Add(this);
                }
            }

            // Set metadata on outputs.
            var node_index = layer.InboundNodes.Count - 1;

            foreach (var(i, tensor) in enumerate(Outputs))
            {
                tensor.KerasHistory = new KerasHistory(layer, node_index, i, tensor);
            }
        }
Пример #2
0
 public StatementListNode(NodeArgs args, AstNodeList statements)
     : base(args)
 {
     ChildNodes.Clear();
       foreach (AstNode stmt in statements)
     AddChild(null, stmt);
 }
 public StatementListNode(NodeArgs args, AstNodeList statements) : base(args)
 {
     ChildNodes.Clear();
     foreach (AstNode stmt in statements)
     {
         AddChild(null, stmt);
     }
 }
Пример #4
0
 public BinExprNode(NodeArgs args, AstNode left, string op, AstNode right) : base(args) {
   ChildNodes.Clear();
   Left = left;
   AddChild("Arg", Left);
   Op = op; 
   Right = right;
   AddChild("Arg", Right);
   //Flags |= AstNodeFlags.TypeBasedDispatch;
 }
Пример #5
0
 public UnExprNode(NodeArgs args, string op, AstNode arg) : base(args) {
   ChildNodes.Clear();
   Op = op;
   if (!Op.EndsWith("U"))
     Op += "U"; //Unary operations are marked as "+U", "-U", "!U"
   Arg = arg;
   ChildNodes.Add(arg);
   //Flags |= AstNodeFlags.TypeBasedDispatch;
 }
Пример #6
0
 public FunctionCallNode(NodeArgs args, VarRefNode name, AstNodeList arguments) : base(args) {
   ChildNodes.Clear();
   NameRef = name;
   NameRef.Flags |= AstNodeFlags.SuppressNotDefined;
   AddChild("Name", NameRef);
   Arguments = arguments;
   foreach (AstNode arg in Arguments) 
     AddChild("Arg", arg);
 }//constructor
Пример #7
0
 public AnonFunctionNode(NodeArgs args, AstNode parameters, AstNode body) : base(args) {
   ChildNodes.Clear();
   Parameters = parameters;
   AddChild("Params", Parameters);
   Body = body;
   AddChild("Body", Body);
   foreach (VarRefNode prm in Parameters.ChildNodes)
     prm.Flags |= AstNodeFlags.AllocateSlot;
   BindingInfo = new FunctionBindingInfo(null, Parameters.ChildNodes.Count, this.Body.Evaluate, this, FunctionFlags.IsLocal);
 }
Пример #8
0
 public CondClauseNode(NodeArgs args, AstNode test, StatementListNode expressions) :base(args) {
   ChildNodes.Clear();
   this.Role = "Clause";
   Test = test;
   Test.Role = "Test";
   ChildNodes.Add(Test);
   Expressions = expressions;
   Expressions.Role = "Command";
   ChildNodes.Add(Expressions);
 }
Пример #9
0
 public BinExprNode(NodeArgs args, AstNode left, string op, AstNode right) : base(args)
 {
     ChildNodes.Clear();
     Left = left;
     AddChild("Arg", Left);
     Op    = op;
     Right = right;
     AddChild("Arg", Right);
     //Flags |= AstNodeFlags.TypeBasedDispatch;
 }
Пример #10
0
 public CondClauseNode(NodeArgs args, AstNode test, StatementListNode expressions) : base(args)
 {
     ChildNodes.Clear();
     this.Role = "Clause";
     Test      = test;
     Test.Role = "Test";
     ChildNodes.Add(Test);
     Expressions      = expressions;
     Expressions.Role = "Command";
     ChildNodes.Add(Expressions);
 }
Пример #11
0
 public FunctionCallNode(NodeArgs args, VarRefNode name, AstNodeList arguments) : base(args)
 {
     ChildNodes.Clear();
     NameRef        = name;
     NameRef.Flags |= AstNodeFlags.SuppressNotDefined;
     AddChild("Name", NameRef);
     Arguments = arguments;
     foreach (AstNode arg in Arguments)
     {
         AddChild("Arg", arg);
     }
 }//constructor
Пример #12
0
 public UnExprNode(NodeArgs args, string op, AstNode arg) : base(args)
 {
     ChildNodes.Clear();
     Op = op;
     if (!Op.EndsWith("U"))
     {
         Op += "U"; //Unary operations are marked as "+U", "-U", "!U"
     }
     Arg = arg;
     ChildNodes.Add(arg);
     //Flags |= AstNodeFlags.TypeBasedDispatch;
 }
Пример #13
0
 public AssigmentNode(NodeArgs args, AstNode lvalue, AstNode expression) : base(args) {
   ChildNodes.Clear();
   var Identifier = lvalue as VarRefNode;
   if (Identifier == null) {
     args.Context.ReportError(lvalue.Location, "Expected identifier.");
     return; 
   }
   Identifier.Flags |= AstNodeFlags.AllocateSlot | AstNodeFlags.NotRValue;
   Identifier.Access = AccessType.Write;
   AddChild("Name", Identifier);
   Expression = expression;
   AddChild("Expr", Expression);
 }
Пример #14
0
 public CondFormNode(NodeArgs args, AstNodeList clauses, AstNode elseClause) : base(args) {
   ChildNodes.Clear();
   Clauses = clauses;
   foreach (AstNode clause in clauses) {
     clause.Role = "Arg";
     ChildNodes.Add(clause);
   }
   ElseClause = elseClause;
   if (ElseClause != null) {
     ElseClause.Role = "else";
     ChildNodes.Add(ElseClause);
   }
 }
Пример #15
0
 public AnonFunctionNode(NodeArgs args, AstNode parameters, AstNode body) : base(args)
 {
     ChildNodes.Clear();
     Parameters = parameters;
     AddChild("Params", Parameters);
     Body = body;
     AddChild("Body", Body);
     foreach (VarRefNode prm in Parameters.ChildNodes)
     {
         prm.Flags |= AstNodeFlags.AllocateSlot;
     }
     BindingInfo = new FunctionBindingInfo(null, Parameters.ChildNodes.Count, this.Body.Evaluate, this, FunctionFlags.IsLocal);
 }
Пример #16
0
 public AssigmentNode(NodeArgs args, AstNode id, AstNode expression) : base(args) {
   ChildNodes.Clear();
   Identifier = id as VarRefNode;
   if (Identifier == null) //id might be a simple token
     Identifier = new VarRefNode(args, id);
   if (Identifier == null && id is Token) {
     args.Context.ReportError(id.Location, "Expected identifier.");
   }
   Identifier.Flags |= AstNodeFlags.AllocateSlot | AstNodeFlags.NotRValue;
   Identifier.Access = AccessType.Write;
   AddChild("Name", Identifier);
   Expression = expression;
   AddChild("Expr", Expression);
 }
Пример #17
0
    public IfNode(NodeArgs args, AstNode test, AstNode ifTrue, AstNode ifFalse): base(args) {
      ChildNodes.Clear();

      Test = test;
      AddChild("Test", Test);
      
      IfTrue = ifTrue;
      if (IfTrue.IsEmpty()) 
        IfTrue = null;
      AddChild("IfTrue", IfTrue);

      IfFalse = ifFalse;
      if (IfFalse.IsEmpty()) IfFalse = null;
      AddChild("IfFalse", IfFalse);
    }
Пример #18
0
 public CondFormNode(NodeArgs args, AstNodeList clauses, AstNode elseClause) : base(args)
 {
     ChildNodes.Clear();
     Clauses = clauses;
     foreach (AstNode clause in clauses)
     {
         clause.Role = "Arg";
         ChildNodes.Add(clause);
     }
     ElseClause = elseClause;
     if (ElseClause != null)
     {
         ElseClause.Role = "else";
         ChildNodes.Add(ElseClause);
     }
 }
Пример #19
0
 public AssigmentNode(NodeArgs args, AstNode id, AstNode expression) : base(args)
 {
     ChildNodes.Clear();
     Identifier = id as VarRefNode;
     if (Identifier == null) //id might be a simple token
     {
         Identifier = new VarRefNode(args, id);
     }
     if (Identifier == null && id is Token)
     {
         args.Context.ReportError(id.Location, "Expected identifier.");
     }
     Identifier.Flags |= AstNodeFlags.AllocateSlot | AstNodeFlags.NotRValue;
     Identifier.Access = AccessType.Write;
     AddChild("Name", Identifier);
     Expression = expression;
     AddChild("Expr", Expression);
 }
Пример #20
0
        public Node(Layer layer, NodeArgs args)
        {
            this.args = args;

            kerasInputs = new List <Layer>();

            // Wire up Node to Layers.
            layer.InboundNodes.Add(this);
            foreach (var input in kerasInputs)
            {
                if (input != null)
                {
                    input.OutboundNodes.Add(this);
                }
            }

            // Set metadata on outputs.
        }
Пример #21
0
        public Node(Layer layer, NodeArgs args)
        {
            this.args  = args;
            this.Layer = layer;

            if (args.InputTensors != null)
            {
                KerasInputs.AddRange(args.InputTensors);
            }

            foreach (var(i, ele) in enumerate(KerasInputs))
            {
                _keras_inputs_ids_and_indices[i] = ele.GetHashCode();
            }

            // Wire up Node to Layers.
            layer.InboundNodes.Add(this);
            foreach (var kt in KerasInputs)
            {
                if (kt.KerasHistory == null)
                {
                    continue;
                }
                var(inbound_layer, _, _) = kt.KerasHistory;
                if (inbound_layer != null)
                {
                    inbound_layer.OutboundNodes.Add(this);
                }
            }

            // Set metadata on outputs.
            var node_index = layer.InboundNodes.Count - 1;

            foreach (var(i, tensor) in enumerate(Outputs))
            {
                tensor.KerasHistory = new KerasHistory(layer, node_index, i, tensor);
            }

            // Cached for performance.
            FlatInputIds  = KerasInputs.Select(x => x.GetHashCode()).ToArray();
            FlatOutputIds = Outputs.Select(x => x.GetHashCode()).ToArray();
        }
Пример #22
0
        public IfNode(NodeArgs args, AstNode test, AstNode ifTrue, AstNode ifFalse) : base(args)
        {
            ChildNodes.Clear();

            Test = test;
            AddChild("Test", Test);

            IfTrue = ifTrue;
            if (IfTrue.IsEmpty())
            {
                IfTrue = null;
            }
            AddChild("IfTrue", IfTrue);

            IfFalse = ifFalse;
            if (IfFalse.IsEmpty())
            {
                IfFalse = null;
            }
            AddChild("IfFalse", IfFalse);
        }
Пример #23
0
 public VarRefNode(NodeArgs args, string name)
     : base(args)
 {
     ChildNodes.Clear();
       Name = name;
 }
Пример #24
0
 public VarRefNode(NodeArgs args, AstNode idNode)
     : base(args)
 {
     ChildNodes.Clear();
       Name = idNode.GetContent();
 }
Пример #25
0
 private AstNode CreateDefineVarNode(NodeArgs args)
 {
     //we skip the "define" keyword so the indexes are 1,2
       return new AssigmentNode(args, args.ChildNodes[1] as VarRefNode, args.ChildNodes[2]);
 }
Пример #26
0
 public VarRefNode(NodeArgs args, string name) : base(args)
 {
     ChildNodes.Clear();
     Name = name;
 }
Пример #27
0
 public StatementListNode(NodeArgs args) : base(args)
 {
 }
Пример #28
0
 public AssigmentNode(NodeArgs args)  : this(args, args.ChildNodes[0], args.ChildNodes[2])
 {
 }
Пример #29
0
 private AstNode CreateIfThenElseNode(NodeArgs args)
 {
     AstNode test = args.ChildNodes[1];
       AstNode ifTrue = args.ChildNodes[2];
       AstNode ifFalse = args.ChildNodes[3];
       return new IfNode(args, test, ifTrue, ifFalse);
 }
Пример #30
0
 private AstNode CreateBeginNode(NodeArgs args)
 {
     return new StatementListNode(args, args.ChildNodes[1].ChildNodes);
 }
Пример #31
0
 public VarRefNode(NodeArgs args, AstNode idNode) : base(args)
 {
     ChildNodes.Clear();
     Name = idNode.GetContent();
 }
Пример #32
0
 private AstNode CreateDefineFunNode(NodeArgs args)
 {
     //"define" keyword is at index 0
       VarRefNode funNameNode = args.ChildNodes[1] as VarRefNode;
       funNameNode.Flags |= AstNodeFlags.AllocateSlot;
       AstNode funParams = args.ChildNodes[2];
       AstNode funBody = args.ChildNodes[3];
       AstNode anonFun = new AnonFunctionNode(args, funParams, funBody);
       AssigmentNode function = new AssigmentNode(args, funNameNode, anonFun);
       return function;
 }
Пример #33
0
 public StatementListNode(NodeArgs args)
     : base(args)
 {
 }
Пример #34
0
 public AssigmentNode(NodeArgs args)  : this(args, args.ChildNodes[0], args.ChildNodes[2]) {  }
Пример #35
0
 public VarRefNode(NodeArgs args)
     : this(args, args.ChildNodes[0])
 {
 }
Пример #36
0
 public VarRefNode(NodeArgs args) : this(args, args.ChildNodes[0])
 {
 }
Пример #37
0
 private AstNode CreateCondFormNode(NodeArgs args)
 {
     AstNodeList condClauses = args.ChildNodes[1].ChildNodes;
       AstNode elseNode = args.ChildNodes[2];
       AstNode elseCommands = (elseNode.IsEmpty()? null : elseNode.ChildNodes[1]);
       return new CondFormNode(args, condClauses, elseCommands);
 }
Пример #38
0
 private AstNode CreateLambdaNode(NodeArgs args)
 {
     AstNode funParams = args.ChildNodes[1];
       AstNode funBody = args.ChildNodes[2];
       return new AnonFunctionNode(args, funParams, funBody);
 }
Пример #39
0
 public UnExprNode(NodeArgs args) : this(args, GetContent(args.ChildNodes[0]), args.ChildNodes[1]) {  }
Пример #40
0
 private AstNode CreateFunctionCallNode(NodeArgs args)
 {
     VarRefNode id = args.ChildNodes[0] as VarRefNode;
       AstNodeList funArgs = args.ChildNodes[1].ChildNodes;
       if (IsOperator(id.Name)) {
     return new BinExprNode(args, funArgs[0], id.Name, funArgs[1]);
       } else {
     return new FunctionCallNode(args, id, funArgs);
       }
 }
Пример #41
0
 public UnExprNode(NodeArgs args) : this(args, args.ChildNodes[0].GetContent(), args.ChildNodes[1])
 {
 }
Пример #42
0
 public Node(NodeArgs args)
 {
     this.args = args;
 }
Пример #43
0
 public BinExprNode(NodeArgs args) 
   : this(args, args.ChildNodes[0], args.ChildNodes[1].GetContent(), args.ChildNodes[2]) {  }
Пример #44
0
    }//method

    private AstNode CreateNode(ActionRecord reduceAction, SourceSpan sourceSpan, AstNodeList childNodes) {
      NonTerminal nt = reduceAction.NonTerminal;
      AstNode result;
      NodeArgs nodeArgs = new NodeArgs(_context, nt, sourceSpan, childNodes);

      if (nt.NodeCreator != null) {
        result = nt.NodeCreator(nodeArgs);
        if (result != null)  return result;
      }

      Type defaultNodeType = _context.Compiler.Grammar.DefaultNodeType;
      Type ntNodeType = nt.NodeType ?? defaultNodeType ?? typeof(AstNode);

      // Check if NonTerminal is a list
      // List nodes are produced by .Plus() or .Star() methods of BnfElement
      // In this case, we have a left-recursive list formation production:   
      //     ntList -> ntList + delim? + ntElem
      //  We check if we have already created the list node for ntList (in the first child); 
      //  if yes, we use this child as a result directly, without creating new list node. 
      //  The other incoming child - the last one - is a new list member; 
      // we simply add it to child list of the result ntList node. Optional "delim" node is simply thrown away.
      bool isList = nt.IsSet(TermOptions.IsList);
      if (isList && childNodes.Count > 1 && childNodes[0].Term == nt) {
        result = childNodes[0];
        AstNode newChild = childNodes[childNodes.Count - 1];
        newChild.Parent = result; 
        result.ChildNodes.Add(newChild);
        return result;
      }
      //Check for StarList produced by MakeStarRule; in this case the production is:  ntList -> Empty | Elem+
      // where Elem+ is non-empty list of elements. The child list we are actually interested in is one-level lower
      if (nt.IsSet(TermOptions.IsStarList) && childNodes.Count == 1) {
        childNodes = childNodes[0].ChildNodes;
      }
      // Check for "node-bubbling" case. For identity productions like 
      //   A -> B
      // the child node B is usually a subclass of node A, 
      // so child node B can be used directly in place of the A. So we simply return child node as a result. 
      // TODO: probably need a grammar option to enable/disable this behavior explicitly
      bool canBubble = (Data.Grammar.FlagIsSet(LanguageFlags.BubbleNodes)) && 
        !isList && !nt.IsSet(TermOptions.IsPunctuation) && childNodes.Count == 1 && (childNodes[0].Term is NonTerminal);
      if (canBubble) {
        NonTerminal childNT = childNodes[0].Term as NonTerminal;
        Type childNodeType = childNT.NodeType ?? defaultNodeType ?? typeof(AstNode);
        if (childNodeType == ntNodeType || childNodeType.IsSubclassOf(ntNodeType))
          return childNodes[0];
      }
      // Try using Grammar's CreateNode method
      result = Data.Grammar.CreateNode(_context, reduceAction, sourceSpan, childNodes);
      if (result != null) 
        return result; 

      //Finally create node directly. For perf reasons we try using "new" for AstNode type (faster), and
      // activator for all custom types (slower)
      if (ntNodeType == typeof(AstNode))
        return new AstNode(nodeArgs);

     // if (ntNodeType.GetConstructor(new Type[] {typeof(AstNodeList)}) != null) 
       // return (AstNode)Activator.CreateInstance(ntNodeType, childNodes);
      if (ntNodeType.GetConstructor(new Type[] {typeof(NodeArgs)}) != null) 
        return (AstNode) Activator.CreateInstance(ntNodeType, nodeArgs);
      //The following should never happen - we check that constructor exists when we validate grammar.
      string msg = string.Format(
@"AST Node class {0} does not have a constructor for automatic node creation. 
Provide a constructor with a single NodeArgs parameter, or use NodeCreator delegate property in NonTerminal.", ntNodeType);
      throw new GrammarErrorException(msg);
    }
Пример #45
0
        }//method

        private AstNode CreateNode(ActionRecord reduceAction, SourceSpan sourceSpan, AstNodeList childNodes)
        {
            NonTerminal nt = reduceAction.NonTerminal;
            AstNode     result;
            NodeArgs    nodeArgs = new NodeArgs(_context, nt, sourceSpan, childNodes);

            if (nt.NodeCreator != null)
            {
                result = nt.NodeCreator(nodeArgs);
                if (result != null)
                {
                    return(result);
                }
            }

            Type defaultNodeType = _context.Compiler.Grammar.DefaultNodeType;
            Type ntNodeType      = nt.NodeType ?? defaultNodeType ?? typeof(AstNode);

            // Check if NonTerminal is a list
            // List nodes are produced by .Plus() or .Star() methods of BnfElement
            // In this case, we have a left-recursive list formation production:
            //     ntList -> ntList + delim? + ntElem
            //  We check if we have already created the list node for ntList (in the first child);
            //  if yes, we use this child as a result directly, without creating new list node.
            //  The other incoming child - the last one - is a new list member;
            // we simply add it to child list of the result ntList node. Optional "delim" node is simply thrown away.
            bool isList = nt.IsSet(TermOptions.IsList);

            if (isList && childNodes.Count > 1 && childNodes[0].Term == nt)
            {
                result = childNodes[0];
                AstNode newChild = childNodes[childNodes.Count - 1];
                newChild.Parent = result;
                result.ChildNodes.Add(newChild);
                return(result);
            }
            //Check for StarList produced by MakeStarRule; in this case the production is:  ntList -> Empty | Elem+
            // where Elem+ is non-empty list of elements. The child list we are actually interested in is one-level lower
            if (nt.IsSet(TermOptions.IsStarList) && childNodes.Count == 1)
            {
                childNodes = childNodes[0].ChildNodes;
            }
            // Check for "node-bubbling" case. For identity productions like
            //   A -> B
            // the child node B is usually a subclass of node A,
            // so child node B can be used directly in place of the A. So we simply return child node as a result.
            // TODO: probably need a grammar option to enable/disable this behavior explicitly
            bool canBubble = (Data.Grammar.FlagIsSet(LanguageFlags.BubbleNodes)) &&
                             !isList && !nt.IsSet(TermOptions.IsPunctuation) && childNodes.Count == 1 && (childNodes[0].Term is NonTerminal);

            if (canBubble)
            {
                NonTerminal childNT       = childNodes[0].Term as NonTerminal;
                Type        childNodeType = childNT.NodeType ?? defaultNodeType ?? typeof(AstNode);
                if (childNodeType == ntNodeType || childNodeType.IsSubclassOf(ntNodeType))
                {
                    return(childNodes[0]);
                }
            }
            // Try using Grammar's CreateNode method
            result = Data.Grammar.CreateNode(_context, reduceAction, sourceSpan, childNodes);
            if (result != null)
            {
                return(result);
            }

            //Finally create node directly. For perf reasons we try using "new" for AstNode type (faster), and
            // activator for all custom types (slower)
            if (ntNodeType == typeof(AstNode))
            {
                return(new AstNode(nodeArgs));
            }

            // if (ntNodeType.GetConstructor(new Type[] {typeof(AstNodeList)}) != null)
            // return (AstNode)Activator.CreateInstance(ntNodeType, childNodes);
            if (ntNodeType.GetConstructor(new Type[] { typeof(NodeArgs) }) != null)
            {
                return((AstNode)Activator.CreateInstance(ntNodeType, nodeArgs));
            }
            //The following should never happen - we check that constructor exists when we validate grammar.
            string msg = string.Format(
                @"AST Node class {0} does not have a constructor for automatic node creation. 
Provide a constructor with a single NodeArgs parameter, or use NodeCreator delegate property in NonTerminal.", ntNodeType);

            throw new GrammarErrorException(msg);
        }
Пример #46
0
 private AstNode CreateCondClauseNode(NodeArgs args)
 {
     AstNode test = args.ChildNodes[0];
       StatementListNode command = args.ChildNodes[1] as StatementListNode;
       return new CondClauseNode(args, test, command);
 }