void drop_node (PrologAtom * atom, tinyxml2 :: XMLElement * node) { char * node_name = (char *) node -> Name (); AREA area; int ap = area_cat (area, 0, "<"); ap = area_cat (area, ap, node_name); ap = area_cat (area, ap, ">"); PrologAtom * node_atom = new PrologAtom (area); root -> attachClause (root -> pair (root -> pair (root -> atom (atom), root -> pair (root -> atom (atomC (node_name)), root -> pair (root -> atom (node_atom), root -> earth ()))), root -> earth ())); const tinyxml2 :: XMLAttribute * attr = node -> FirstAttribute (); term_reader reader; while (attr != 0) { PrologAtom * attribute_atom = atomC ((char *) attr -> Name ()); if (attribute_atom == 0) return; reader . init (root, (char *) attr -> Value ()); PrologElement * el = reader . readElement (); if (el == 0) el = root -> text ((char *) attr -> Value ()); else if (el -> isAtom () && strcmp (el -> getAtom () -> name (), (char *) attr -> Value ()) != 0) { delete el; el = root -> atomC ((char *) attr -> Value ()); } root -> attachClause (root -> pair (root -> pair (root -> atom (node_atom), root -> pair (root -> atom (attribute_atom), root -> pair (el, root -> earth () ))), root -> earth ())); attr = attr -> Next (); } char * text = (char *) node -> GetText (); if (text != 0) {root -> attachClause (root -> pair (root -> pair (root -> atom (node_atom), root -> pair (root -> text (text), root -> earth ())), root -> earth ()));} tinyxml2 :: XMLElement * child = node -> FirstChildElement (); while (child != 0) {drop_node (node_atom, child); child = child -> NextSiblingElement ();} }
bool code (PrologElement * parameters, PrologResolution * resolution) { PrologElement * path = 0; PrologElement * atom = 0; while (parameters -> isPair ()) { PrologElement * el = parameters -> getLeft (); if (el -> isText ()) path = el; if (el -> isAtom ()) atom = el; if (el -> isVar ()) atom = el; parameters = parameters -> getRight (); } if (atom == 0 || path == 0) return false; tinyxml2 :: XMLDocument doc; if (tinyxml2 :: XML_NO_ERROR != doc . LoadFile ((const char *) path -> getText ())) return false; tinyxml2 :: XMLElement * node = doc . FirstChildElement (); if (node == 0) return false; if (atom -> isVar ()) atom -> setAtom (atomC ((char *) node -> Name ())); drop_node (atom -> getAtom (), node); return true; }
int main(int argc, char* argv[]) { std::shared_ptr<Empty> blank; std::shared_ptr<Program> prg(new Program()); std::shared_ptr<SharedDecl> shDecl1(new SharedDecl("y", blank)); prg->Add(shDecl1); std::shared_ptr<Node> initX(new Atom("2")); std::shared_ptr<SharedDecl> shDecl2(new SharedDecl("x", initX)); prg->Add(shDecl2); std::shared_ptr<Sub> sub1(new Sub("Main")); sub1->AddParam("a"); sub1->AddParam("b"); prg->Add(sub1); std::shared_ptr<Atom> atomA(new Atom("a")); std::shared_ptr<Atom> atomB(new Atom("b")); std::shared_ptr<BinaryOp> initRes(new BinaryOp("add", atomA, atomB)); std::shared_ptr<VarDecl> resDecl(new VarDecl("res", initRes)); sub1->Add(resDecl); std::shared_ptr<Atom> atom3i(new Atom("3")); std::shared_ptr<Atom> atom5i(new Atom("5")); std::shared_ptr<BinaryOp> newParam(new BinaryOp("add", atom3i, atom5i)); std::shared_ptr<Allocation> allocat(new Allocation(newParam)); std::shared_ptr<Atom> atomArr(new Atom("arr")); std::shared_ptr<Assignation> asignNew3(new Assignation(atomArr, allocat)); sub1->Add(asignNew3); std::shared_ptr<Atom> atomArrBis(new Atom("arr")); std::shared_ptr<Deallocation> deallocat(new Deallocation(atomArrBis)); sub1->Add(deallocat); std::shared_ptr<Atom> atomC(new Atom("res")); std::shared_ptr<BinaryOp> subCsumAB(new BinaryOp("substract", atomC, initRes)); std::shared_ptr<Assignation> incC(new Assignation(atomC, subCsumAB)); sub1->Add(incC); std::shared_ptr<Atom> atom0(new Atom("0")); std::shared_ptr<BinaryOp> cond1cond(new BinaryOp("equals", atomC, atom0)); std::shared_ptr<If> cond1(new If(cond1cond, "10", "20")); sub1->Add(cond1); std::shared_ptr<Atom> atom1(new Atom("1")); std::shared_ptr<Atom> atom10(new Atom("10")); std::shared_ptr<For> for1(new For("i", atom1, atom10, atom1)); sub1->Add(for1); std::shared_ptr<BinaryOp> addC1(new BinaryOp("add", atomC, atom1)); std::shared_ptr<Assignation> incResActually(new Assignation(atomC, addC1)); for1->Add(incResActually); std::shared_ptr<Loop> loop1(new Loop(cond1cond)); loop1->Add(for1); // don't double reference ever in practice... loop1->Add(addC1); sub1->Add(loop1); std::shared_ptr<Call> call1(new Call("testFun", "")); call1->AddParam(atomA); call1->AddParam(addC1); sub1->Add(call1); std::shared_ptr<Return> ret1(new Return(atom0)); sub1->Add(ret1); XMLDumpVisitor v; prg->Accept(&v); return 0; }