update dcgs to handle ; and ->
This commit is contained in:
@@ -4,6 +4,7 @@ use prolog_parser::tabled_rc::*;
|
|||||||
use std::cell::{Cell, RefCell};
|
use std::cell::{Cell, RefCell};
|
||||||
use std::collections::{BTreeSet, HashMap, VecDeque};
|
use std::collections::{BTreeSet, HashMap, VecDeque};
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
|
use std::fmt;
|
||||||
use std::ops::{Add, AddAssign, Index, IndexMut, Sub};
|
use std::ops::{Add, AddAssign, Index, IndexMut, Sub};
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
@@ -660,6 +661,18 @@ pub enum Addr {
|
|||||||
Str(usize)
|
Str(usize)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Addr {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
&Addr::Con(ref c) => write!(f, "Addr::Con({})", c),
|
||||||
|
&Addr::Lis(l) => write!(f, "Addr::Lis({})", l),
|
||||||
|
&Addr::HeapCell(h) => write!(f, "Addr::HeapCell({})", h),
|
||||||
|
&Addr::StackCell(fr, sc)=> write!(f, "Addr::StackCell({}, {})", fr, sc),
|
||||||
|
&Addr::Str(s) => write!(f, "Addr::Str({})", s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl PartialEq<Ref> for Addr {
|
impl PartialEq<Ref> for Addr {
|
||||||
fn eq(&self, r: &Ref) -> bool {
|
fn eq(&self, r: &Ref) -> bool {
|
||||||
self.as_var() == Some(*r)
|
self.as_var() == Some(*r)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
:- module(dcgs, [(-->)/2, phrase/2, phrase/3]).
|
:- module(dcgs, [(-->)/2, phrase/2, phrase/3]).
|
||||||
|
|
||||||
:- use_module(library(lists), [append/3]).
|
:- use_module(library(lists), [append/3]).
|
||||||
|
:- use_module(library(terms)).
|
||||||
|
|
||||||
:- op(1200, xfx, -->).
|
:- op(1200, xfx, -->).
|
||||||
|
|
||||||
@@ -19,40 +20,51 @@ phrase(G, Ls0, Ls2) :-
|
|||||||
phrase(G, Ls0, Ls1) :-
|
phrase(G, Ls0, Ls1) :-
|
||||||
call(G, Ls0, Ls1).
|
call(G, Ls0, Ls1).
|
||||||
|
|
||||||
term_expansion(Term0, (ModHead :- ModBody)) :-
|
term_expansion(Term0, Term) :-
|
||||||
|
numbervars(Term0, 0, N),
|
||||||
|
expand_dcgs(Term0, N, Term).
|
||||||
|
|
||||||
|
expand_dcgs(Term0, N, (ModHead :- ModBody)) :-
|
||||||
nonvar(Term0),
|
nonvar(Term0),
|
||||||
Term0 = (Head, [SC | SCs] --> Body),
|
Term0 = (Head, [SC | SCs] --> Body),
|
||||||
!,
|
!,
|
||||||
nonvar(Head),
|
nonvar(Head),
|
||||||
Head =.. [RuleName | Args],
|
Head =.. [RuleName | Args],
|
||||||
append([SC | SCs], '$VAR'(N), SemiContextArgs),
|
append([SC | SCs], '$VAR'(N1), SemiContextArgs),
|
||||||
append(Args, ['$VAR'(0), SemiContextArgs], ModArgs),
|
append(Args, ['$VAR'(N), SemiContextArgs], ModArgs),
|
||||||
ModHead =.. [RuleName | ModArgs],
|
ModHead =.. [RuleName | ModArgs],
|
||||||
nonvar(Body),
|
nonvar(Body),
|
||||||
expand_body(Body, ModBody, 0, N).
|
expand_body(Body, ModBody, 0, N1).
|
||||||
term_expansion(Term0, (ModHead :- ModBody)) :-
|
expand_dcgs(Term0, N, (ModHead :- ModBody)) :-
|
||||||
nonvar(Term0),
|
nonvar(Term0),
|
||||||
Term0 = (Head --> Body),
|
Term0 = (Head --> Body),
|
||||||
nonvar(Head),
|
nonvar(Head),
|
||||||
Head =.. [RuleName | Args],
|
Head =.. [RuleName | Args],
|
||||||
append(Args, ['$VAR'(0), '$VAR'(N)], ModArgs),
|
append(Args, ['$VAR'(N), '$VAR'(N1)], ModArgs),
|
||||||
ModHead =.. [RuleName | ModArgs],
|
ModHead =.. [RuleName | ModArgs],
|
||||||
nonvar(Body),
|
nonvar(Body),
|
||||||
expand_body(Body, ModBody, 0, N).
|
expand_body(Body, ModBody, N, N1).
|
||||||
|
|
||||||
expand_body(Term0, (ModTerm, ModTerms), N0, N) :-
|
expand_body(Term0, (ModTerm, ModTerms), N0, N) :-
|
||||||
nonvar(Term0), Term0 = (Term, Terms), !,
|
nonvar(Term0), Term0 = (Term, Terms), !,
|
||||||
nonvar(Term),
|
nonvar(Term),
|
||||||
expand_body_term(Term, ModTerm, N0, N1),
|
expand_body_term(Term, ModTerm, N0, N1),
|
||||||
expand_body(Terms, ModTerms, N1, N).
|
expand_body(Terms, ModTerms, N1, N).
|
||||||
expand_body(Term0, ModTerm, N0, N) :-
|
expand_body(Term0, ModTerm, N0, N) :-
|
||||||
nonvar(Term0), expand_body_term(Term0, ModTerm, N0, N).
|
nonvar(Term0),
|
||||||
|
expand_body_term(Term0, ModTerm, N0, N).
|
||||||
|
|
||||||
expand_body_term([], true, N, N) :- !.
|
expand_body_term([], true, N, N) :- !.
|
||||||
expand_body_term([Arg|Args], ModTerm, N0, N) :-
|
expand_body_term([Arg|Args], ModTerm, N0, N) :-
|
||||||
!, N is N0 + 1,
|
!, N is N0 + 1,
|
||||||
append([Arg|Args], '$VAR'(N), ModArgs),
|
append([Arg|Args], '$VAR'(N), ModArgs),
|
||||||
ModTerm = ('$VAR'(N0) = ModArgs).
|
ModTerm = ('$VAR'(N0) = ModArgs).
|
||||||
|
expand_body_term((P -> Q), (PModTerm -> QModTerm), N0, N) :-
|
||||||
|
!, expand_body(P, PModTerm, N0, N1),
|
||||||
|
expand_body(Q, QModTerm, N1, N).
|
||||||
|
expand_body_term((P ; Q), (PModTerm ; QModTerm), N0, N) :-
|
||||||
|
!, expand_body(P, PModTerm, N0, N),
|
||||||
|
expand_body(Q, QModTerm, N0, N).
|
||||||
expand_body_term(CommaTerm, ModTerm, N, N) :-
|
expand_body_term(CommaTerm, ModTerm, N, N) :-
|
||||||
CommaTerm =.. [{} | BodyTerms], !,
|
CommaTerm =.. [{} | BodyTerms], !,
|
||||||
comma_ify(BodyTerms, ModTerm).
|
comma_ify(BodyTerms, ModTerm).
|
||||||
|
|||||||
Reference in New Issue
Block a user