Example #1
0
        private static AST.Clause DcgClauseToNormalClause(AST.DcgClause dcgClause)
        {
            var dcgGoalConverter = new AST.DcgGoalConverter();

            var body = dcgClause.Body.Select(dcgGoalConverter.DcgGoalToNormalGoal).ToArray();

            return(new AST.Clause
            {
                Head = new AST.Goal
                {
                    PredicateName = dcgClause.Head.PredicateName,
                    Arguments = new []
                    {
                        dcgClause.Head.Arguments,
                        new []                 // add 2 extra arguments, L0 and Ln
                        {
                            new Variable("L0"),
                            new Variable("L" + dcgGoalConverter.DcgSubgoalCount)
                        }
                    }.SelectMany(a => a).ToArray()
                },

                Body = body
            });
        }
Example #2
0
        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 ()
            };
        }
Example #3
0
        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()
            });
        }