本文整理汇总了C#中IMacroContext.GetArgsAndBody方法的典型用法代码示例。如果您正苦于以下问题:C# IMacroContext.GetArgsAndBody方法的具体用法?C# IMacroContext.GetArgsAndBody怎么用?C# IMacroContext.GetArgsAndBody使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IMacroContext
的用法示例。
在下文中一共展示了IMacroContext.GetArgsAndBody方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: replace
public static LNode replace(LNode node, IMacroContext context)
{
var args_body = context.GetArgsAndBody(true);
var args = args_body.A;
var body = args_body.B;
if (args.Count >= 1)
{
var patterns = new Pair<LNode, LNode>[args.Count];
for (int i = 0; i < patterns.Length; i++)
{
var pair = args[i];
if (pair.Calls(S.Lambda, 2)) {
LNode pattern = pair[0], repl = pair[1];
if (pattern.Calls(S.Braces, 1) && repl.Calls(S.Braces)) {
pattern = pattern.Args[0];
repl = repl.WithTarget(S.Splice);
}
patterns[i] = Pair.Create(pattern, repl);
} else {
string msg = "Expected 'pattern => replacement'.";
if (pair.Descendants().Any(n => n.Calls(S.Lambda, 2)))
msg += " " + "(Using '=>' already? Put the pattern on the left-hand side in parentheses.)";
return Reject(context, pair, msg);
}
}
int replacementCount;
var output = Replace(body, patterns, out replacementCount);
if (replacementCount == 0)
context.Write(Severity.Warning, node, "No patterns recognized; no replacements were made.");
return output.AsLNode(S.Splice);
}
return null;
}
示例2: saveAndRestore
public static LNode saveAndRestore(LNode node, IMacroContext context)
{
var tmp_0 = context.GetArgsAndBody(true);
var args = tmp_0.Item1;
var body = tmp_0.Item2;
if (args.Count == 1) {
LNode newValue = null;
{
var tmp_1 = args[0];
LNode property;
if (tmp_1.Calls(CodeSymbols.Assign, 2) && (property = tmp_1.Args[0]) != null && (newValue = tmp_1.Args[1]) != null || (property = tmp_1) != null) {
string mainProp = KeyNameComponentOf(property).Name;
string varPrefix = "old" + mainProp + "_";
LNode varName, varDecl = TempVarDecl(property, out varName, varPrefix);
LNode tryFinally = LNode.Call(CodeSymbols.Try, LNode.List(LNode.Call(CodeSymbols.Braces, LNode.List(body)).SetStyle(NodeStyle.Statement), LNode.Call(CodeSymbols.Finally, LNode.List(LNode.Call(CodeSymbols.Braces, LNode.List(LNode.Call(CodeSymbols.Assign, LNode.List(property, varName)).SetStyle(NodeStyle.Operator))).SetStyle(NodeStyle.Statement)))));
if (newValue != null) {
return LNode.Call(CodeSymbols.Splice, LNode.List(varDecl, LNode.Call(CodeSymbols.Assign, LNode.List(property, newValue)).SetStyle(NodeStyle.Operator), tryFinally));
} else {
return LNode.Call(CodeSymbols.Splice, LNode.List(varDecl, tryFinally));
}
}
}
}
return null;
}
示例3: static_matchCode
public static LNode static_matchCode(LNode node, IMacroContext context)
{
if (node.AttrNamed(S.Static) == null && !node.HasSpecialName)
return null; // handled by normal matchCode macro
var args_body = context.GetArgsAndBody(false);
VList<LNode> args = args_body.Item1, body = args_body.Item2;
if (args.Count != 1)
return Reject(context, args[1], "Expected only one expression to match");
var expression = context.PreProcess(AutoStripBraces(args[0]));
var cases = GetCases(body, context.Sink);
// The `default:` case is represented by an empty list of patterns.
if (cases.WithoutLast(1).Any(pair => pair.Key.IsEmpty))
context.Write(Severity.Error, node, "The `default:` case must be the last one, because the cases are tested in the order they appear, so no case after `default:` can be matched.");
MMap<Symbol, LNode> captures = new MMap<Symbol, LNode>();
foreach (Pair<VList<LNode>, VList<LNode>> pair in cases)
{
var patterns = pair.Key.IsEmpty ? new VList<LNode>((LNode)null) : pair.Key;
foreach (var pattern in patterns)
{
captures.Clear();
VList<LNode> _;
if (pattern == null || LNodeExt.MatchesPattern(expression, pattern, ref captures, out _)) {
captures[_hash] = expression; // define $#
captures.Remove(__);
return ReplaceCaptures(pair.Value.AsLNode(S.Splice), captures);
}
}
}
return F.Call(S.Splice); // none of the cases matched
}
示例4: useSymbols
public static LNode useSymbols(LNode input, IMacroContext context)
{
bool inType = context.Ancestors.Any(parent => {
var kind = EcsValidators.SpaceDefinitionKind(parent);
return kind != null && kind != S.Namespace;
});
var args_body = context.GetArgsAndBody(true);
args_body.B = context.PreProcess(args_body.B);
return UseSymbolsCore(input.Attrs, args_body.A, args_body.B, context, inType);
}
示例5: replace
public static LNode replace(LNode node, IMacroContext context)
{
var args_body = context.GetArgsAndBody(true);
var args = args_body.A;
var body = args_body.B;
if (args.Count == 1 && args[0].Calls(S.Tuple)) args = args[0].Args; // LESv2
if (args.Count >= 1)
{
bool preprocess = node.Calls("replacePP");
var patterns = new Pair<LNode, LNode>[args.Count];
for (int i = 0; i < patterns.Length; i++)
{
var pair = args[i];
if (pair.Calls(S.Lambda, 2)) {
LNode pattern = pair[0], repl = pair[1];
if (preprocess)
{
pattern = context.PreProcess(pattern);
repl = context.PreProcess(repl);
}
if (pattern.Calls(S.Braces)) {
if (pattern.ArgCount == 1)
pattern = pattern.Args[0];
else
context.Write(Severity.Error, pattern, "The braces must contain only a single statement. To search for braces literally, use `{{ ... }}`");
}
if (repl.Calls(S.Braces))
repl = repl.Args.AsLNode(S.Splice);
// Avoid StackOverflowException when pattern is $Id (sadly, it
// is uncatchable so it can crash LeMP.exe and even Visual Studio)
if (LNodeExt.GetCaptureIdentifier(pattern) != null)
return Reject(context, pattern, "The left side of `=>` cannot be a capture. Remove the `$`.");
patterns[i] = Pair.Create(pattern, repl);
} else {
string msg = "Expected 'pattern => replacement'.";
if (pair.Descendants().Any(n => n.Calls(S.Lambda, 2)))
msg += " " + "(Using '=>' already? Put the pattern on the left-hand side in parentheses.)";
return Reject(context, pair, msg);
}
}
int replacementCount;
var output = Replace(body, patterns, out replacementCount);
if (replacementCount == 0)
context.Sink.Warning(node, "No patterns recognized; no replacements were made.");
return output.AsLNode(S.Splice);
}
return null;
}
示例6: useSequenceExpressions
public static LNode useSequenceExpressions(LNode node, IMacroContext context)
{
var tmp_10 = context.GetArgsAndBody(true);
var args = tmp_10.Item1;
var body = tmp_10.Item2;
if (args.Count > 0)
context.Sink.Error(node[1], "#useSequenceExpressions does not support arguments.");
{
context.ScopedProperties[_useSequenceExpressionsIsRunning] = G.BoxedTrue;
try {
body = context.PreProcess(body);
} finally {
context.ScopedProperties.Remove(_useSequenceExpressionsIsRunning);
}
}
var ers = new EliminateRunSequences(context);
return ers.EliminateSequenceExpressions(body, true).AsLNode(S.Splice);
}
示例7: use_symbols
public static LNode use_symbols(LNode input, IMacroContext context)
{
var args_body = context.GetArgsAndBody(true);
// Decode options (TODO: invent a simpler approach)
string prefix = "sy_";
var inherited = new HashSet<Symbol>();
foreach (var pair in MacroContext.GetOptions(args_body.A))
{
if (pair.Key.Name == "prefix" && pair.Value.IsId)
prefix = pair.Value.Name.Name;
else if (pair.Key.Name == "inherit" && pair.Value.Value is Symbol)
inherited.Add((Symbol)pair.Value.Value);
else if (pair.Key.Name == "inherit" && (pair.Value.Calls(S.Braces) || pair.Value.Calls(S.Tuple)) && pair.Value.Args.All(n => n.Value is Symbol))
foreach (var arg in pair.Value.Args)
inherited.Add((Symbol)arg.Value);
else
context.Sink.Write(Severity.Warning, pair.Value, "Unrecognized parameter. Expected prefix:id or inherit:{@@A; @@B; ...})");
}
// Replace all symbols while collecting a list of them
var symbols = new Dictionary<Symbol, LNode>();
RVList<LNode> output = args_body.B.SmartSelect(stmt => stmt.ReplaceRecursive(n => {
var sym = n.Value as Symbol;
if (n.IsLiteral && sym != null)
return symbols[sym] = LNode.Id(prefix + Ecs.EcsNodePrinter.SanitizeIdentifier(sym.Name));
return null;
}));
// Return updated code with variable declaration at the top for all non-inherit symbols used.
var _Symbol = F.Id("Symbol");
var vars = (from sym in symbols
where !inherited.Contains(sym.Key)
select F.Call(S.Assign, sym.Value,
F.Call(S.Cast, F.Literal(sym.Key.Name), _Symbol))).ToList();
if (vars.Count > 0)
output.Insert(0, F.Call(S.Var, Range.Single(_Symbol).Concat(vars))
.WithAttrs(input.Attrs.Add(F.Id(S.Static)).Add(F.Id(S.Readonly))));
return F.Call(S.Splice, output);
}
示例8: handler
[LexicalMacro("matchCode (var) { case ...: ... }; // In LES, use a => b instead of case a: b", "Attempts to match and deconstruct a Loyc tree against a series of cases with patterns, e.g. " + "`case $a + $b:` expects a tree that calls `+` with two parameters, placed in new variables called a and b. " + "`break` is not required or recognized at the end of each case's handler (code block). " + "Use `$(...x)` to gather zero or more parameters into a list `x`. " + "Use `case pattern1, pattern2:` in EC# to handle multiple cases with the same handler.")] public static LNode matchCode(LNode node, IMacroContext context)
{
var args_body = context.GetArgsAndBody(true);
VList<LNode> args = args_body.Item1, body = args_body.Item2;
if (args.Count != 1 || body.Count < 1)
return null;
var cases = GetCases(body, context.Sink);
if (cases.IsEmpty)
return null;
var output = new WList<LNode>();
var @var = MaybeAddTempVarDecl(args[0], output);
var ifClauses = new List<Pair<LNode,LNode>>();
var cmc = new CodeMatchContext {
Context = context
};
foreach (var @case in cases) {
cmc.ThenClause.Clear();
LNode testExpr = null;
if (@case.Key.Count > 0) {
if (cmc.IsMultiCase = @case.Key.Count > 1) {
cmc.UsageCounters.Clear();
testExpr = @case.Key.Aggregate((LNode) null, (test, pattern) => {
test = LNode.MergeBinary(test, cmc.MakeTopTestExpr(pattern, @var), S.Or);
return test;
});
foreach (var pair in cmc.UsageCounters.Where(p => p.Value < @case.Key.Count)) {
if (cmc.NodeVars.ContainsKey(pair.Key))
cmc.NodeVars[pair.Key] = true;
if (cmc.ListVars.ContainsKey(pair.Key))
cmc.ListVars[pair.Key] = true;
}
} else
testExpr = cmc.MakeTopTestExpr(@case.Key[0], @var);
}
var handler = @case.Value;
if (cmc.ThenClause.Count > 0)
handler = LNode.MergeLists(F.Braces(cmc.ThenClause), handler, S.Braces);
ifClauses.Add(Pair.Create(testExpr, handler));
}
LNode ifStmt = null;
for (int i = ifClauses.Count - 1; i >= 0; i--) {
if (ifClauses[i].Item1 == null) {
if (ifStmt == null)
ifStmt = ifClauses[i].Item2;
else
context.Sink.Write(Severity.Error, node, "The default case must appear last, and there can be only one.");
} else {
if (ifStmt == null)
ifStmt = F.Call(S.If, ifClauses[i].Item1, ifClauses[i].Item2);
else
ifStmt = F.Call(S.If, ifClauses[i].Item1, ifClauses[i].Item2, ifStmt);
}
}
if (cmc.NodeVars.Count > 0)
output.Add(F.Call(S.Var, ListExt.Single(F.Id("LNode")).Concat(cmc.NodeVars.OrderBy(v => v.Key.Name).Select(kvp => kvp.Value ? F.Call(S.Assign, F.Id(kvp.Key), F.Null) : F.Id(kvp.Key)))));
if (cmc.ListVars.Count > 0) {
LNode type = LNode.Call(CodeSymbols.Of, LNode.List(LNode.Id((Symbol) "VList"), LNode.Id((Symbol) "LNode")));
output.Add(F.Call(S.Var, ListExt.Single(type).Concat(cmc.ListVars.OrderBy(v => v.Key.Name).Select(kvp => kvp.Value ? LNode.Call(CodeSymbols.Assign, LNode.List(F.Id(kvp.Key), LNode.Call(CodeSymbols.Default, LNode.List(type)))).SetStyle(NodeStyle.Operator) : F.Id(kvp.Key)))));
}
if (output.Count == 0)
return ifStmt;
else {
output.Add(ifStmt);
return F.Braces(output.ToVList());
}
}
示例9: matchCode
public static LNode matchCode(LNode node, IMacroContext context)
{
if (node.AttrNamed(S.Static) != null)
return null; // this case is handled by static_matchCode macro
var args_body = context.GetArgsAndBody(false);
VList<LNode> args = args_body.Item1, body = args_body.Item2;
if (args.Count != 1 || body.Count < 1)
return null;
var cases = GetCases(body, context.Sink);
if (cases.IsEmpty)
return null;
var output = new WList<LNode>();
var @var = MaybeAddTempVarDecl(context, args[0], output);
var ifClauses = new List<Pair<LNode, LNode>>();
var cmc = new CodeMatchContext {
Context = context
};
foreach (var @case in cases)
{
cmc.ThenClause.Clear();
// e.g. case [$(..._)] Foo($x + 1, $y) =>
// LNode x, y, tmp9;
// if (var.Calls((Symbol) "Foo", 2) && (tmp9 = var.Args[0]).Calls(CodeSymbols.Plus, 2)
// && (x = tmp9.Args[0]) != null // this will never be null, but we want to put it the assignment in the 'if' statement
// && 1.Equals(tmp9.Args[1].Value) && (y = var.Args[1]) != null) { ... }
LNode testExpr = null;
if (@case.Key.Count > 0) {
if (cmc.IsMultiCase = @case.Key.Count > 1) {
cmc.UsageCounters.Clear();
testExpr = @case.Key.Aggregate((LNode) null, (test, pattern) => {
test = LNode.MergeBinary(test, cmc.MakeTopTestExpr(pattern, @var), S.Or);
return test;
});
foreach (var pair in cmc.UsageCounters.Where(p => p.Value < @case.Key.Count)) {
if (cmc.NodeVars.ContainsKey(pair.Key))
cmc.NodeVars[pair.Key] = true;
if (cmc.ListVars.ContainsKey(pair.Key))
cmc.ListVars[pair.Key] = true;
}
} else
testExpr = cmc.MakeTopTestExpr(@case.Key[0], @var);
}
var handler = @case.Value.AsLNode(S.Braces);
if (cmc.ThenClause.Count > 0)
handler = LNode.MergeLists(F.Braces(cmc.ThenClause), handler, S.Braces);
ifClauses.Add(Pair.Create(testExpr, handler));
}
LNode ifStmt = null;
for (int i = ifClauses.Count - 1; i >= 0; i--)
{
if (ifClauses[i].Item1 == null) {
if (ifStmt == null)
ifStmt = ifClauses[i].Item2;
else
context.Sink.Error(node, "The default case must appear last, and there can be only one.");
} else {
if (ifStmt == null)
ifStmt = F.Call(S.If, ifClauses[i].Item1, ifClauses[i].Item2);
else
ifStmt = F.Call(S.If, ifClauses[i].Item1, ifClauses[i].Item2, ifStmt);
}
}
if (cmc.NodeVars.Count > 0)
output.Add(F.Call(S.Var, ListExt.Single(F.Id("LNode")).Concat(
cmc.NodeVars.OrderBy(v => v.Key.Name).Select(kvp => kvp.Value ? F.Call(S.Assign, F.Id(kvp.Key), F.Null) : F.Id(kvp.Key)))));
if (cmc.ListVars.Count > 0) {
LNode type = LNode.Call(CodeSymbols.Of, LNode.List(LNode.Id((Symbol) "VList"), LNode.Id((Symbol) "LNode"))).SetStyle(NodeStyle.Operator);
output.Add(F.Call(S.Var, ListExt.Single(type).Concat(
cmc.ListVars.OrderBy(v => v.Key.Name).Select(kvp => kvp.Value ? LNode.Call(CodeSymbols.Assign, LNode.List(F.Id(kvp.Key), LNode.Call(CodeSymbols.Default, LNode.List(type)))).SetStyle(NodeStyle.Operator) : F.Id(kvp.Key)))));
}
if (output.Count == 0)
return ifStmt;
else {
output.Add(ifStmt);
return F.Braces(output.ToVList());
}
}
示例10: to_csharp
public static LNode to_csharp(LNode node, IMacroContext context)
{
var args_body = context.GetArgsAndBody(true);
return F.Call(S.Splice, args_body.B.SmartSelect(stmt => StmtToCSharp(stmt, context, false, null)));
}