Esempio n. 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);
            }
        }
Esempio n. 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);
     }
 }
Esempio n. 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;
 }
Esempio n. 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;
 }
 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
 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);
 }
Esempio n. 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);
 }
Esempio n. 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;
 }
Esempio n. 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);
 }
 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
Esempio n. 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;
 }
Esempio n. 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);
 }
Esempio n. 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);
   }
 }
Esempio n. 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);
 }
Esempio n. 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);
 }
Esempio n. 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);
    }
Esempio n. 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);
     }
 }
Esempio n. 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);
 }
Esempio n. 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.
        }
Esempio n. 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();
        }
Esempio n. 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);
        }
Esempio n. 23
0
 public VarRefNode(NodeArgs args, string name)
     : base(args)
 {
     ChildNodes.Clear();
       Name = name;
 }
Esempio n. 24
0
 public VarRefNode(NodeArgs args, AstNode idNode)
     : base(args)
 {
     ChildNodes.Clear();
       Name = idNode.GetContent();
 }
Esempio n. 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]);
 }
Esempio n. 26
0
 public VarRefNode(NodeArgs args, string name) : base(args)
 {
     ChildNodes.Clear();
     Name = name;
 }
 public StatementListNode(NodeArgs args) : base(args)
 {
 }
Esempio n. 28
0
 public AssigmentNode(NodeArgs args)  : this(args, args.ChildNodes[0], args.ChildNodes[2])
 {
 }
Esempio n. 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);
 }
Esempio n. 30
0
 private AstNode CreateBeginNode(NodeArgs args)
 {
     return new StatementListNode(args, args.ChildNodes[1].ChildNodes);
 }
Esempio n. 31
0
 public VarRefNode(NodeArgs args, AstNode idNode) : base(args)
 {
     ChildNodes.Clear();
     Name = idNode.GetContent();
 }
Esempio n. 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;
 }
Esempio n. 33
0
 public StatementListNode(NodeArgs args)
     : base(args)
 {
 }
Esempio n. 34
0
 public AssigmentNode(NodeArgs args)  : this(args, args.ChildNodes[0], args.ChildNodes[2]) {  }
Esempio n. 35
0
 public VarRefNode(NodeArgs args)
     : this(args, args.ChildNodes[0])
 {
 }
Esempio n. 36
0
 public VarRefNode(NodeArgs args) : this(args, args.ChildNodes[0])
 {
 }
Esempio n. 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);
 }
Esempio n. 38
0
 private AstNode CreateLambdaNode(NodeArgs args)
 {
     AstNode funParams = args.ChildNodes[1];
       AstNode funBody = args.ChildNodes[2];
       return new AnonFunctionNode(args, funParams, funBody);
 }
Esempio n. 39
0
 public UnExprNode(NodeArgs args) : this(args, GetContent(args.ChildNodes[0]), args.ChildNodes[1]) {  }
Esempio n. 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);
       }
 }
Esempio n. 41
0
 public UnExprNode(NodeArgs args) : this(args, args.ChildNodes[0].GetContent(), args.ChildNodes[1])
 {
 }
Esempio n. 42
0
 public Node(NodeArgs args)
 {
     this.args = args;
 }
Esempio n. 43
0
 public BinExprNode(NodeArgs args) 
   : this(args, args.ChildNodes[0], args.ChildNodes[1].GetContent(), args.ChildNodes[2]) {  }
Esempio n. 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);
    }
Esempio n. 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);
        }
Esempio n. 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);
 }