From 74e5e0cc30a41c1e9678192916df62bc3a69a2f9 Mon Sep 17 00:00:00 2001 From: Peter Ludemann Date: Wed, 5 Apr 2023 14:18:13 -0700 Subject: [PATCH] PORT: Changes for SWI-cpp2.h version 2 --- DESCRIPTION | 2 +- src/rolog_cpp2.txt | 98 ++++++++++++++++++++++++---------------------- 2 files changed, 52 insertions(+), 48 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 6364083..aaa501b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -3,7 +3,7 @@ Type: Package Title: Query 'SWI'-'Prolog' from R Version: 0.9.12 Date: 2023-01-27 -Authors@R: c(person("Matthias", "Gondan", role=c("aut", "com", "cre"), +Author: c(person("Matthias", "Gondan", role=c("aut", "com", "cre"), email="Matthias.Gondan-Rochon@uibk.ac.at", comment="Universität Innsbruck"), person("European Commission", role="fnd", comment="Erasmus+ Programme, 2019-1-EE01-KA203-051708")) diff --git a/src/rolog_cpp2.txt b/src/rolog_cpp2.txt index c5de2ec..fe506ad 100644 --- a/src/rolog_cpp2.txt +++ b/src/rolog_cpp2.txt @@ -74,11 +74,15 @@ RObject pl2r_null() return R_NilValue ; } +// TODO: use this and pl.eq_if_atom(ATOM_na) instead of +// pl.is_atom() && pl == "na" +// PlAtom ATOM_na("na"); + // This helper function checks for na and then translates an individual PlTerm // to a double. double pl2r_double(PlTerm pl) { - if(pl.is_atom() && pl == "na") + if(pl.is_atom() && pl.as_string() == "na") return NA_REAL ; try @@ -138,7 +142,7 @@ NumericMatrix pl2r_realmat(PlTerm pl) // See above for pl2r_double long pl2r_int(PlTerm pl) { - if(pl.is_atom() && pl == "na") + if(pl.is_atom() && pl.as_string() == "na") return NA_INTEGER ; try @@ -195,7 +199,7 @@ IntegerMatrix pl2r_intmat(PlTerm pl) // See above for pl2r_double String pl2r_string(PlTerm pl) { - if(pl.is_atom() && pl == "na") + if(pl.is_atom() && pl.as_string() == "na") return NA_STRING ; return pl.as_string(EncLocale) ; @@ -241,17 +245,17 @@ CharacterMatrix pl2r_charmat(PlTerm pl) // Convert prolog atom to R symbol (handle na, true, false) RObject pl2r_symbol(PlTerm pl) { - if(pl == "na") + if(pl.as_string() == "na") return wrap(NA_LOGICAL) ; - if(pl == "true") + if(pl.as_string() == "true") return wrap(true) ; - if(pl == "false") + if(pl.as_string() == "false") return wrap(false) ; // Empty symbols - if(pl == "") + if(pl.as_string() == "") return Function("substitute")() ; return as(Symbol(pl.as_string(EncUTF8))) ; // TODO: EncLocale? @@ -272,7 +276,7 @@ RObject pl2r_function(PlTerm pl, CharacterVector& names, PlTerm& vars, List opti PlTerm arg = plhead[i] ; // Compounds like mean=100 are translated to named function arguments - if(arg.is_compound() && arg.name() == "=" && arg.arity() == 2) + if(arg.is_compound() && arg.name().as_string() == "=" && arg.arity() == 2) { PlTerm a1 = arg[1] ; PlTerm a2 = arg[2] ; @@ -302,19 +306,19 @@ LogicalVector pl2r_boolvec(PlTerm pl) PlTerm t = pl[i+1] ; if(t.is_atom()) { - if(t == "na") + if(t.as_string() == "na") { r(i) = NA_LOGICAL ; continue ; } - if(t == "true") + if(t.as_string() == "true") { r(i) = 1 ; continue ; } - if(t == "false") + if(t.as_string() == "false") { r(i) = 0 ; continue ; @@ -364,7 +368,7 @@ RObject pl2r_variable(PlTerm pl, CharacterVector& names, PlTerm& vars) PlTerm_var v ; for(int i=0 ; i r, List aoptions) PlTermv rows(r.nrow()) ; for(int i=0 ; i r, List aoptions) PlTermv rows(r.nrow()) ; for(int i=0 ; i r, List aoptions) PlTermv rows(r.nrow()) ; for(int i=0 ; i r, List aoptions) PlTermv rows(r.nrow()) ; for(int i=0 ; i(CAR(r)).c_str())) ; - PlCheck(pl[2].unify_integer(0)) ; + PlCheckFail(pl[1].unify_atom(as(CAR(r)).c_str())) ; + PlCheckFail(pl[2].unify_integer(0)) ; PlCall("compound_name_arity", pl) ; return pl[0] ; } @@ -824,9 +828,9 @@ PlTerm r2pl_compound(Language r, CharacterVector& names, PlTerm& vars, List opti // Convert named arguments to prolog compounds a=X if(n.length() && n(i) != "") - PlCheck(pl[i].unify_term(PlCompound("=", PlTermv(PlTerm_atom(n(i)), arg)))) ; + PlCheckFail(pl[i].unify_term(PlCompound("=", PlTermv(PlTerm_atom(n(i)), arg)))) ; else - PlCheck(pl[i].unify_term(arg)) ; // no name + PlCheckFail(pl[i].unify_term(arg)) ; // no name } return PlCompound(as(CAR(r)).c_str(), pl) ; @@ -852,12 +856,12 @@ PlTerm r2pl_list(List r, CharacterVector& names, PlTerm& vars, List options) // Convert named argument to prolog pair a-X. if(n.length() && n(i) != "") - PlCheck(tail.append(PlCompound("-", PlTermv(PlTerm_atom(n(i)), arg)))) ; + PlCheckFail(tail.append(PlCompound("-", PlTermv(PlTerm_atom(n(i)), arg)))) ; else - PlCheck(tail.append(arg)) ; // no name + PlCheckFail(tail.append(arg)) ; // no name } - PlCheck(tail.close()) ; + PlCheckFail(tail.close()) ; return pl ; } @@ -865,26 +869,26 @@ PlTerm r2pl_list(List r, CharacterVector& names, PlTerm& vars, List options) PlTerm r2pl_function(Function r, CharacterVector& names, PlTerm& vars, List options) { PlTermv fun(2) ; - PlCheck(fun[1].unify_term(r2pl_compound(BODY(r), names, vars, options))) ; + PlCheckFail(fun[1].unify_term(r2pl_compound(BODY(r), names, vars, options))) ; List formals = as(FORMALS(r)) ; size_t len = (size_t) formals.size() ; if(len == 0) { PlTermv pl(3) ; - PlCheck(pl[1].unify_atom("$function")) ; - PlCheck(pl[2].unify_integer(0)) ; + PlCheckFail(pl[1].unify_atom("$function")) ; + PlCheckFail(pl[2].unify_integer(0)) ; PlCall("compound_name_arity", pl) ; - PlCheck(fun[0].unify_term(pl[0])) ; + PlCheckFail(fun[0].unify_term(pl[0])) ; return PlCompound(":-", fun) ; } CharacterVector n = formals.names() ; PlTermv pl(len) ; for(size_t i=0 ; i(as(r)[0]).c_str()) continue ; @@ -1109,11 +1113,11 @@ RObject portray_(RObject query, List options) PlTerm_var vars ; options("atomize") = true ; // translate variables to their R names PlTermv pl(3) ; - PlCheck(pl[0].unify_term(r2pl(query, names, vars, options))) ; + PlCheckFail(pl[0].unify_term(r2pl(query, names, vars, options))) ; PlTerm_tail tail(pl[2]) ; - PlCheck(tail.append(PlCompound("quoted", PlTermv(PlTerm_atom("false"))))) ; - PlCheck(tail.append(PlCompound("spacing", PlTermv(PlTerm_atom("next_argument"))))) ; - PlCheck(tail.close()) ; + PlCheckFail(tail.append(PlCompound("quoted", PlTermv(PlTerm_atom("false"))))) ; + PlCheckFail(tail.append(PlCompound("spacing", PlTermv(PlTerm_atom("next_argument"))))) ; + PlCheckFail(tail.close()) ; PlFrame f ; PlQuery q("term_string", pl) ; @@ -1227,7 +1231,7 @@ PREDICATE(r_eval, 2) PlTerm_var pl ; try { - PlCheck(pl.unify_term(r2pl(Res, names, vars, options))) ; + PlCheckFail(pl.unify_term(r2pl(Res, names, vars, options))) ; } catch(std::exception& ex)