本文整理汇总了C#中IScope.GetValue方法的典型用法代码示例。如果您正苦于以下问题:C# IScope.GetValue方法的具体用法?C# IScope.GetValue怎么用?C# IScope.GetValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IScope
的用法示例。
在下文中一共展示了IScope.GetValue方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Eval
internal override Value Eval(Value arg, IScope scope)
{
Map map = arg.AsMap;
// get parameters
Value value = null;
if (map["function"] is ValueFunction)
{
value = map["function"];
}
else if (map["functionKey"] is ValueString)
{
string name = map["functionKey"].AsString;
value = scope.GetValue(new Token(name));
}
ValueFunction function = value as ValueFunction;
if (function == null)
{
Loki3Exception e = new Loki3Exception();
if (value == null)
e.AddMissingKey(new ValueString("function"));
else
e.AddWrongType(ValueType.Function, value.Type);
throw e;
}
IScope thescope;
if (map["map"].IsNil)
{ // nothing specified, use current scope
thescope = scope;
}
else
{ // either use map or scope passed in
ValueMap themap = map["map"] as ValueMap;
thescope = (themap.Scope != null ? themap.Scope : new ScopeChain(themap.AsMap));
}
return new BoundFunction(function, thescope);
}
示例2: Do
/// <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();
}
示例3: ComputeParam
/// <summary>
/// If the pattern's metadata says it matches raw, return the unevaled value,
/// otherwise return the evaled value
/// </summary>
/// <param name="pattern">pattern (i.e. the param's pattern metadata)</param>
/// <param name="param">parameter node to either eval or wrap</param>
private Value ComputeParam(Value pattern, DelimiterNode param,
IScope paramScope, INodeRequestor nodes, ILineRequestor requestor)
{
Value keyType = null;
// does the pattern metadata ask for 'raw'?
if (pattern.Metadata != null && pattern.Metadata.TryGetValue(PatternData.keyType, out keyType) &&
keyType.Type == ValueType.String && keyType.AsString == ValueClasses.ClassOf(ValueType.Raw))
{
// if the param would eval to something raw, then we should eval it rather than try to wrap it
if (param.Token != null)
{
Value value = paramScope.GetValue(param.Token);
if (value != null && value.Type == ValueType.Raw)
return EvalNode.Do(param, paramScope, nodes, requestor);
}
// otherwise, is the param something that's not raw?
if (param.List == null || param.List.Delimiter.DelimiterType != DelimiterType.AsRaw)
{ // wrap unevaled value as raw
return new ValueRaw(param, paramScope);
}
}
return EvalNode.Do(param, paramScope, nodes, requestor);
}
示例4: NodeEval
internal NodeEval(DelimiterNode node, IScope scope)
{
m_node = node;
Token token = node.Token;
if (token != null)
{
Value value = scope.GetValue(token);
m_func = value as ValueFunction;
if (m_func != null)
{ // function
m_state = NodeState.Node;
m_order = (int)m_func.Order;
}
else if (value != null)
{ // variable
m_value = value;
m_state = NodeState.Value;
}
else
{ // built-in
try
{
m_value = EvalBuiltin.DoNoThrow(token);
if (m_value != null)
m_state = NodeState.Value;
}
catch (Exception) { }
}
}
else if (node.List != null)
{
m_state = NodeState.Node;
m_order = (int)Order.Lowest;
}
else if (node.Value != null)
{
m_value = node.Value;
m_order = (int)m_value.Order;
m_func = m_value as ValueFunction;
m_state = (m_func == null ? NodeState.Value : NodeState.Function);
}
}
示例5: Eval
internal override Value Eval(Value arg, IScope scope)
{
Map map = arg.AsMap;
Value collection = map["collection"];
// todo: consolidate these bodies, one from an explicit param & one from the following lines
Value valueBody = map.ContainsKey("body") ? map["body"] : map[ValueFunction.keyBody];
IScope bodyScope = scope;
if (valueBody is ValueRaw)
{
IScope tempScope = (valueBody as ValueRaw).Scope;
if (tempScope != null)
bodyScope = tempScope;
}
// todo: abstract iteration to avoid these ifs
Value result = ValueNil.Nil;
if (collection is ValueString)
{
string s = collection.AsString;
foreach (char c in s)
{
IScope local = new ScopeChain(bodyScope);
PatternAssign assign = new PatternAssign(map, local, true/*bCreate*/);
assign.Assign(new ValueString(c.ToString()));
result = EvalBody.Do(valueBody, local);
local.Exit();
}
}
else if (collection is ValueArray)
{
List<Value> list = collection.AsArray;
foreach (Value v in list)
{
IScope local = new ScopeChain(bodyScope);
PatternAssign assign = new PatternAssign(map, local, true/*bCreate*/);
assign.Assign(v);
result = EvalBody.Do(valueBody, local);
local.Exit();
}
}
else if (collection is ValueMap)
{
Dictionary<string, Value> dict = collection.AsMap.Raw;
foreach (string key in dict.Keys)
{
List<Value> list = new List<Value>();
list.Add(new ValueString(key));
list.Add(dict[key]);
IScope local = new ScopeChain(bodyScope);
PatternAssign assign = new PatternAssign(map, local, true/*bCreate*/);
assign.Assign(new ValueArray(list));
result = EvalBody.Do(valueBody, local);
local.Exit();
}
}
else if (collection is ValueLine)
{
List<DelimiterList> list = collection.AsLine;
// if delimiter is specified, wrap each line w/ it
ValueDelimiter delim = scope.GetValue(new Token(map["delim"].AsString)) as ValueDelimiter;
if (delim != null)
{
List<DelimiterList> delimList = new List<DelimiterList>();
int indent = (list.Count > 0 ? list[0].Indent : 0);
foreach (DelimiterList line in list)
{
DelimiterList newLine = line;
// wrap lines & nested lines with proper delimiter, except for nested values that get evaled
if (line.Indent == indent || (delim.DelimiterType != DelimiterType.AsValue && line.Indent >= indent))
{
List<DelimiterNode> nodes = new List<DelimiterNode>();
string original = line.Original;
if (delim.DelimiterType == DelimiterType.AsString && line.Indent > indent)
{ // put the indentation back if portions of the body were indented
original = "";
for (int i = 0; i < line.Indent - indent; i++)
original += " ";
original += line.Original;
}
nodes.Add(new DelimiterNodeList(new DelimiterList(delim, line.Nodes, line.Indent, "", original, line.Scope)));
newLine = new DelimiterList(delim, nodes, indent, "", original, line.Scope);
}
delimList.Add(newLine);
}
list = delimList;
}
// for each line, eval it then eval the body
ILineRequestor lines = new LineConsumer(list);
while (lines.HasCurrent())
{
IScope local = new ScopeChain(bodyScope);
Value value = EvalLines.DoOne(lines, local);
PatternAssign assign = new PatternAssign(map, local, true/*bCreate*/);
assign.Assign(value);
result = EvalBody.Do(valueBody, local);
//.........这里部分代码省略.........
示例6: Eval
internal override Value Eval(Value arg, IScope scope)
{
Map map = arg.AsMap;
ValueMap mapOrScope = map["map"] as ValueMap;
Value prevValue = map["previous"];
Value nextValue = map["next"];
Value value = null;
if (map["line"] is ValueRaw)
{
value = new EvalValue();
nextValue = map["line"];
}
else if (map["functionKey"] is ValueString)
{
string name = map["functionKey"].AsString;
value = scope.GetValue(new Token(name));
}
ValueFunction function = value as ValueFunction;
IScope where = mapOrScope.Scope;
if (where != null)
where.AddOnExit(function, prevValue, nextValue);
return ValueNil.Nil;
}
示例7: GetFromKeyOrValue
/// <summary>If :key is present, lookup value, else if :value is present, return value.</summary>
internal static Value GetFromKeyOrValue(Map map, IScope scope)
{
// either lookup up value w/ specified key or just get specified value
Value value = null;
Value key = map["key"];
if (key != ValueNil.Nil)
{
Token token = new Token(key.AsString);
value = scope.GetValue(token);
if (value == null)
throw new Loki3Exception().AddBadToken(token);
}
else if (map.ContainsKey("value"))
{
value = map["value"];
}
if (value == null)
// todo: better error
throw new Loki3Exception().AddMissingKey(new ValueString("key or value required"));
return value;
}