コード例 #1
0
ファイル: EvalNode.cs プロジェクト: loki3/loki-pl1
        /// <summary>
        /// Evaluates a token, possibly requesting the previous and next values.
        /// Returns a value.
        /// </summary>
        /// <param name="token">token representing a function or variable</param>
        /// <param name="scope">used to request functions, variables, and delimiters if there's no scope attached to the node</param>
        /// <param name="nodes">used to request previous and next nodes</param>
        internal static Value Do(DelimiterNode node, IScope scope, INodeRequestor nodes, ILineRequestor requestor)
        {
            if (node.Value != null)
            {	// node has already been evaluated
                return node.Value;
            }
            else if (node.Token != null)
            {	// function/variable or built-in
                Token token = node.Token;
                Value value = scope.GetValue(token);
                if (value is ValueFunction && nodes != null)
                {
                    ValueFunction function = value as ValueFunction;
                    // get previous & next nodes if needed
                    DelimiterNode previous = (function.ConsumesPrevious ? nodes.GetPrevious() : null);
                    DelimiterNode next = (function.ConsumesNext ? nodes.GetNext() : null);

                    scope.FunctionName = token.Value;

                    // evaluate
                    try
                    {
                        return function.Eval(previous, next, scope, scope, nodes, requestor);
                    }
                    catch (Loki3Exception e)
                    {	// this function is the correct context if there isn't already one there
                        if (!e.Errors.ContainsKey(Loki3Exception.keyFunction))
                            e.AddFunction(token.Value);
                        if (!e.Errors.ContainsKey(Loki3Exception.keyScope))
                            e.AddScope(scope);
                        throw e;
                    }
                }
                else if (value != null)
                {
                    return value;
                }
                else
                {
                    return EvalBuiltin.Do(token);
                }
            }
            else if (node.List != null)
            {	// delimited list of nodes
                Value value = null;
                DelimiterList list = node.List;
                IScope listScope = (list.Scope != null ? list.Scope : scope);
                DelimiterType type = list.Delimiter.DelimiterType;

                // get contents as a Value
                switch (type)
                {
                    case DelimiterType.AsString:
                        value = new ValueString(list.Original);
                        break;
                    case DelimiterType.AsValue:
                        value = EvalList.Do(list.Nodes, listScope);
                        break;
                    case DelimiterType.AsArray:
                        List<Value> values = new List<Value>(list.Nodes.Count);
                        foreach (DelimiterNode subnode in list.Nodes)
                        {	// note: 'nodes' is null so functions don't get evaled
                            Value subvalue = Do(subnode, listScope, null, requestor);
                            values.Add(subvalue);
                        }
                        value = new ValueArray(values);
                        break;
                    case DelimiterType.AsEvaledArray:
                        value = EvalList.DoEvaledArray(list.Nodes, listScope);
                        break;
                    case DelimiterType.AsRaw:
                        value = new ValueRaw(list, listScope);
                        break;
                    case DelimiterType.AsArrayOfRaw:
                        List<Value> rawvalues = new List<Value>(list.Nodes.Count);
                        foreach (DelimiterNode subnode in list.Nodes)
                            rawvalues.Add(new ValueRaw(subnode, listScope));
                        value = new ValueArray(rawvalues);
                        break;
                }

                // run contents through a function if specified
                ValueFunction function = list.Delimiter.Function;
                if (function == null)
                    return value;
                DelimiterNode next = new DelimiterNodeValue(value);
                return function.Eval(null, next, scope, scope, nodes, requestor);
            }
            return new ValueNil();
        }
コード例 #2
0
ファイル: EvalList.cs プロジェクト: loki3/loki-pl1
            /// <summary>Evaluate this node, possibly consuming adjacent nodes</summary>
            internal void Eval(IScope scope, INodeRequestor nodes, ILineRequestor requestor)
            {
                if (m_state != NodeState.Node && m_state != NodeState.Function)
                    return;

                // get new value
                if (m_state == NodeState.Node)
                {	// hasn't been evaled at all
                    m_value = EvalNode.Do(m_node, scope, nodes, requestor);
                }
                else if (m_state == NodeState.Function)
                {	// previously resolved to a function
                    DelimiterNode previous = (m_func.ConsumesPrevious ? previous = nodes.GetPrevious() : null);
                    DelimiterNode next = (m_func.ConsumesNext ? next = nodes.GetNext() : null);
                    if ((m_func.ConsumesPrevious || m_func.ConsumesNext) && (previous == null && next == null))
                    {	// no prev/next parameters passed, perhaps it can use body?
                        if (m_func.RequiresBody() && requestor != null)
                        {	// tack on body if present
                            m_value = EvalList.DoAddBody(m_func, scope, requestor);
                        }
                        else
                        {	// function can't be evaled further
                            m_state = NodeState.Value;
                            return;
                        }
                    }
                    else
                    {
                        if (!m_func.ConsumesPrevious && !m_func.ConsumesNext && !m_func.RequiresBody() && !m_func.ForceEval)
                        {	// function can't be evaled further
                            m_state = NodeState.Value;
                            return;
                        }
                        m_value = m_func.Eval(previous, next, scope, scope, nodes, requestor);
                    }
                }

                if (m_value == null)
                {	// e.g. because node was a comment
                    m_state = NodeState.Empty;
                    return;
                }

                // store new info about this node
                m_node = new DelimiterNodeValue(m_value);
                m_order = (int)m_value.Order;
                if (m_value.Type == ValueType.Function)
                {
                    m_state = NodeState.Function;
                    m_func = m_value as ValueFunction;
                }
                else
                {
                    m_state = NodeState.Value;
                }
            }