private static IArgument ArgumentToAst(Runtime.ISolutionTreeNode arg_node) { var variable_node = arg_node ["variable"]; if (variable_node != null) { return(new Variable(ToString(variable_node.Variables ["Name"]))); } var atom_node = arg_node ["atom"]; if (atom_node != null) { return(new Atom(ToString(atom_node ["lowercase"].Variables ["Name"]))); } var list_node = arg_node ["list"]; if (list_node != null) { return(new List(ParseArgumentList(list_node))); } throw new Exception("An 'arg' is expected to be either an 'atom' or a 'variable' or a 'list'."); }
private static AST.Goal GoalToAst(Runtime.ISolutionTreeNode goal_node) { return(new AST.Goal { PredicateName = ToString(goal_node ["lowercase"].Variables ["Name"]), Arguments = ParseArgumentList(goal_node) }); }
static IEnumerable <Runtime.ISolutionTreeNode> SelectPath(Runtime.ISolutionTreeNode node, string goalName) { while (node != null) { yield return(node); node = node [goalName]; } }
private static string SolutionToString(Runtime.ISolutionTreeNode solution) { const string variableName = "NextState"; return(string.Join(Environment.NewLine, SelectPath(solution ["run"], "solve") .Where(n => n.Variables.Contains(variableName)) .Select(n => n.Variables[variableName].Accept(new Runtime.ArgumentPrinter())))); }
private static AST.Program ConvertToAst(Runtime.ISolutionTreeNode unnamed_top_node) { var clause_nodes = ParsePossiblyEmptyList(unnamed_top_node ["run"] ["program"], "clause_list", "clause"); var dcgClauses = new List <AST.DcgClause> (); var prologClauses = new List <AST.Clause> (); foreach (var clause_node in clause_nodes) { var dgc_clause_node = clause_node ["dcg_clause"]; if (dgc_clause_node != null) { var head = GoalToAst(dgc_clause_node ["dcg_head"] ["goal"]); var dcgClause = new AST.DcgClause { Head = new AST.Goal { PredicateName = head.PredicateName, Arguments = head.Arguments }, Body = ParsePossiblyEmptyList(dgc_clause_node ["dcg_body"], "dcg_goal_list", "dcg_goal").Select(DcgGoalToAst).ToArray() }; dcgClauses.Add(dcgClause); } else { var prolog_clause_node = clause_node ["prolog_clause"]; if (prolog_clause_node != null) { var prologClause = new AST.Clause { Head = GoalToAst(prolog_clause_node ["head"] ["goal"]), Body = ParsePossiblyEmptyList(prolog_clause_node ["body"], "goal_list", "goal").Select(GoalToAst).ToArray() }; prologClauses.Add(prologClause); } else { throw new Exception("A 'clause' is expected to be either a 'dcg_clause' or a 'prolog_clause'."); } } } return(new AST.Program { Clauses = prologClauses.ToArray(), DcgClauses = dcgClauses.ToArray() }); }
static IEnumerable <string> SolutionToStringRecursive(Runtime.ISolutionTreeNode solution) { for (;;) { solution = solution ["arglist"]; if (solution == null) { break; } yield return(solution ["arg"].Variables ["Arg"].Accept(new Runtime.ArgumentPrinter())); } }
private static AST.IDcgGoal DcgGoalToAst(Runtime.ISolutionTreeNode node) { Runtime.ISolutionTreeNode n; if ((n = node ["goal"]) != null) { var goal = GoalToAst(n); return(new AST.DcgSubgoal { PredicateName = goal.PredicateName, Arguments = goal.Arguments }); } if ((n = node ["embedded_goal"]) != null) { var goal = GoalToAst(n ["goal"]); return(new AST.DcgNonDcgGoal { PredicateName = goal.PredicateName, Arguments = goal.Arguments }); } if ((n = node ["variable"]) != null) { return(new AST.DcgLiteral { Value = new Variable(ToString(n.Variables ["Name"])) }); } Runtime.IConcreteValue variable; if ((variable = node.Variables ["Literal"]) != null) { return(new AST.DcgLiteral { Value = new Atom(ToString(variable)) }); } throw new Exception("Unrecognized DCG goal."); }
static IEnumerable <Runtime.ISolutionTreeNode> ParsePossiblyEmptyList(Runtime.ISolutionTreeNode node, string listName, string elementName) { if (node != null) { while (true) { node = node [listName]; if (node == null) { break; } var elementNode = node [elementName]; if (elementNode != null) { yield return(elementNode); } } } }
public static Dictionary <string, Runtime.IConcreteValue> GetTopLevelVariables(Runtime.ISolutionTreeNode s) { return(s.Variables.ToDictionary(v => v.Key, v => v.Value)); }
private static IArgument [] ParseArgumentList(Runtime.ISolutionTreeNode goal_node) { return(ParsePossiblyEmptyList(goal_node, "arg_list", "arg").Select(ArgumentToAst).ToArray()); }
static string SolutionToString(Runtime.ISolutionTreeNode solution) { return(string.Join(" ", SolutionToStringRecursive(solution))); }