Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions apyds/ds.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,24 @@ auto rule_match(ds::rule_t* rule_1, ds::rule_t* rule_2, int length) -> std::uniq
return std::unique_ptr<ds::rule_t>(result);
}

auto term_rename(ds::term_t* term, ds::term_t* prefix_and_suffix, int length) -> std::unique_ptr<ds::term_t> {
auto result = reinterpret_cast<ds::term_t*>(operator new(length));
if (result->rename(term, prefix_and_suffix, reinterpret_cast<std::byte*>(result) + length) == nullptr) [[unlikely]] {
operator delete(result);
return std::unique_ptr<ds::term_t>(nullptr);
}
return std::unique_ptr<ds::term_t>(result);
}

auto rule_rename(ds::rule_t* rule, ds::rule_t* prefix_and_suffix, int length) -> std::unique_ptr<ds::rule_t> {
auto result = reinterpret_cast<ds::rule_t*>(operator new(length));
if (result->rename(rule, prefix_and_suffix, reinterpret_cast<std::byte*>(result) + length) == nullptr) [[unlikely]] {
operator delete(result);
return std::unique_ptr<ds::rule_t>(nullptr);
}
return std::unique_ptr<ds::rule_t>(result);
}

PYBIND11_MODULE(_ds, m) {
auto string_t = py::class_<ds::string_t>(m, "String");
auto item_t = py::class_<ds::item_t>(m, "Item");
Expand Down Expand Up @@ -130,6 +148,8 @@ PYBIND11_MODULE(_ds, m) {
term_t.def_static("ground", term_ground);
rule_t.def_static("ground", rule_ground);
rule_t.def_static("match", rule_match);
term_t.def_static("rename", term_rename);
rule_t.def_static("rename", rule_rename);

auto search_t = py::class_<ds::search_t>(m, "Search");
search_t.def(py::init<ds::length_t, ds::length_t>());
Expand Down
29 changes: 29 additions & 0 deletions apyds/rule_t.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,34 @@ def __matmul__(self, other: Rule) -> Rule | None:
return None
return Rule(rule, capacity)

def rename(self, prefix_and_suffix: Rule) -> Rule | None:
"""Rename all variables in this rule by adding prefix and suffix.

Args:
prefix_and_suffix: A rule with only a conclusion that is a list with two inner lists.
Each inner list contains 0 or 1 item representing the prefix and suffix.
Example: Rule("((pre_) (_suf))") adds "pre_" as prefix and "_suf" as suffix.

Returns:
The renamed rule, or None if renaming fails.

Example:
>>> a = Rule("`x")
>>> b = Rule("((pre_) (_suf))")
>>> str(a.rename(b))
'----\\n`pre_x_suf\\n'
>>>
>>> # With empty prefix (only suffix)
>>> c = Rule("`x")
>>> d = Rule("(() (_suf))")
>>> str(c.rename(d))
'----\\n`x_suf\\n'
"""
capacity = buffer_size()
rule = ds.Rule.rename(self.value, prefix_and_suffix.value, capacity)
if rule is None:
return None
return Rule(rule, capacity)

def __repr__(self) -> str:
return f"Rule[\n{self}]"
27 changes: 27 additions & 0 deletions apyds/term_t.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,30 @@ def ground(self, other: Term, scope: str | None = None) -> Term | None:
if term is None:
return None
return Term(term, capacity)

def rename(self, prefix_and_suffix: Term) -> Term | None:
"""Rename all variables in this term by adding prefix and suffix.

Args:
prefix_and_suffix: A term representing a list with two inner lists.
Each inner list contains 0 or 1 item representing the prefix and suffix.
Example: Term("((pre_) (_suf))") adds "pre_" as prefix and "_suf" as suffix.

Returns:
The renamed term, or None if renaming fails.

Example:
>>> a = Term("`x")
>>> b = Term("((pre_) (_suf))")
>>> str(a.rename(b)) # "`pre_x_suf"
>>>
>>> # With empty prefix (only suffix)
>>> c = Term("`x")
>>> d = Term("(() (_suf))")
>>> str(c.rename(d)) # "`x_suf"
"""
capacity = buffer_size()
term = ds.Term.rename(self.value, prefix_and_suffix.value, capacity)
if term is None:
return None
return Term(term, capacity)
20 changes: 20 additions & 0 deletions atsds/ds.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,24 @@ auto rule_match(ds::rule_t* rule_1, ds::rule_t* rule_2, int length) -> std::uniq
return std::unique_ptr<ds::rule_t>(result);
}

auto term_rename(ds::term_t* term, ds::term_t* prefix_and_suffix, int length) -> std::unique_ptr<ds::term_t> {
auto result = reinterpret_cast<ds::term_t*>(operator new(length));
if (result->rename(term, prefix_and_suffix, reinterpret_cast<std::byte*>(result) + length) == nullptr) [[unlikely]] {
operator delete(result);
return std::unique_ptr<ds::term_t>(nullptr);
}
return std::unique_ptr<ds::term_t>(result);
}

auto rule_rename(ds::rule_t* rule, ds::rule_t* prefix_and_suffix, int length) -> std::unique_ptr<ds::rule_t> {
auto result = reinterpret_cast<ds::rule_t*>(operator new(length));
if (result->rename(rule, prefix_and_suffix, reinterpret_cast<std::byte*>(result) + length) == nullptr) [[unlikely]] {
operator delete(result);
return std::unique_ptr<ds::rule_t>(nullptr);
}
return std::unique_ptr<ds::rule_t>(result);
}

auto search_add(ds::search_t* search, const std::string& text) -> bool {
return search->add(text);
}
Expand Down Expand Up @@ -146,6 +164,8 @@ EMSCRIPTEN_BINDINGS(ds) {
term_t.class_function("ground", term_ground, em::return_value_policy::take_ownership());
rule_t.class_function("ground", rule_ground, em::return_value_policy::take_ownership());
rule_t.class_function("match", rule_match, em::return_value_policy::take_ownership());
term_t.class_function("rename", term_rename, em::return_value_policy::take_ownership());
rule_t.class_function("rename", rule_rename, em::return_value_policy::take_ownership());

auto search_t = em::class_<ds::search_t>("Search");
search_t.constructor<ds::length_t, ds::length_t>();
Expand Down
58 changes: 58 additions & 0 deletions atsds/tsds.mts
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,35 @@ export class term_t extends _common_t<dst.Term> {
}
return new term_t(term, capacity);
}

/**
* Rename all variables in this term by adding prefix and suffix.
*
* @param prefix_and_suffix - A term representing a list with two inner lists.
* Each inner list contains 0 or 1 item representing the prefix and suffix.
* Example: "((pre_) (_suf))" adds "pre_" as prefix and "_suf" as suffix.
* @returns The renamed term, or null if renaming fails.
*
* @example
* ```typescript
* const a = new term_t("`x");
* const b = new term_t("((pre_) (_suf))");
* console.log(a.rename(b).toString()); // "`pre_x_suf"
*
* // With empty prefix (only suffix)
* const c = new term_t("`x");
* const d = new term_t("(() (_suf))");
* console.log(c.rename(d).toString()); // "`x_suf"
* ```
*/
rename(prefix_and_suffix: term_t): term_t | null {
const capacity = buffer_size();
const term = ds.Term.rename(this.value, prefix_and_suffix.value, capacity);
if (term === null) {
return null;
}
return new term_t(term, capacity);
}
}

/**
Expand Down Expand Up @@ -461,6 +490,35 @@ export class rule_t extends _common_t<dst.Rule> {
}
return new rule_t(rule, capacity);
}

/**
* Rename all variables in this rule by adding prefix and suffix.
*
* @param prefix_and_suffix - A rule with only a conclusion that is a list with two inner lists.
* Each inner list contains 0 or 1 item representing the prefix and suffix.
* Example: "((pre_) (_suf))" adds "pre_" as prefix and "_suf" as suffix.
* @returns The renamed rule, or null if renaming fails.
*
* @example
* ```typescript
* const a = new rule_t("`x");
* const b = new rule_t("((pre_) (_suf))");
* console.log(a.rename(b).toString()); // "----\n`pre_x_suf\n"
*
* // With empty prefix (only suffix)
* const c = new rule_t("`x");
* const d = new rule_t("(() (_suf))");
* console.log(c.rename(d).toString()); // "----\n`x_suf\n"
* ```
*/
rename(prefix_and_suffix: rule_t): rule_t | null {
const capacity = buffer_size();
const rule = ds.Rule.rename(this.value, prefix_and_suffix.value, capacity);
if (rule === null) {
return null;
}
return new rule_t(rule, capacity);
}
}

/**
Expand Down
10 changes: 1 addition & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions tests/test_rule.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,33 @@ test("match", () => {
fail = new rule_t("(`q <- `p)");
expect(mp.match(fail)).toBeNull();
});

test("rename_simple", () => {
const a = new rule_t("`x");
const b = new rule_t("((pre_) (_suf))");
expect(a.rename(b).toString()).toBe("----\n`pre_x_suf\n");
});

test("rename_empty_prefix", () => {
const a = new rule_t("`x");
const b = new rule_t("(() (_suf))");
expect(a.rename(b).toString()).toBe("----\n`x_suf\n");
});

test("rename_empty_suffix", () => {
const a = new rule_t("`x");
const b = new rule_t("((pre_) ())");
expect(a.rename(b).toString()).toBe("----\n`pre_x\n");
});

test("rename_with_premises", () => {
const a = new rule_t("`p\n`q\n----------\n`r\n");
const b = new rule_t("((pre_) (_suf))");
expect(a.rename(b).toString()).toBe("`pre_p_suf\n`pre_q_suf\n----------\n`pre_r_suf\n");
});

test("rename_invalid", () => {
const a = new rule_t("`x");
const b = new rule_t("item");
expect(a.rename(b)).toBeNull();
});
30 changes: 30 additions & 0 deletions tests/test_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,33 @@ def test_match() -> None:

fail = apyds.Rule("`q <- `p")
assert mp @ fail is None


def test_rename_simple() -> None:
a = apyds.Rule("`x")
b = apyds.Rule("((pre_) (_suf))")
assert str(a.rename(b)) == "----\n`pre_x_suf\n"


def test_rename_empty_prefix() -> None:
a = apyds.Rule("`x")
b = apyds.Rule("(() (_suf))")
assert str(a.rename(b)) == "----\n`x_suf\n"


def test_rename_empty_suffix() -> None:
a = apyds.Rule("`x")
b = apyds.Rule("((pre_) ())")
assert str(a.rename(b)) == "----\n`pre_x\n"


def test_rename_with_premises() -> None:
a = apyds.Rule("`p\n`q\n----------\n`r\n")
b = apyds.Rule("((pre_) (_suf))")
assert str(a.rename(b)) == "`pre_p_suf\n`pre_q_suf\n----------\n`pre_r_suf\n"


def test_rename_invalid() -> None:
a = apyds.Rule("`x")
b = apyds.Rule("item")
assert a.rename(b) is None
30 changes: 30 additions & 0 deletions tests/test_term.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,33 @@ test("ground_scope", () => {
const b = new term_t("((x y `a `b) (y x `b `c))");
expect(a.ground(b, "x").toString()).toBe("`c");
});

test("rename_simple", () => {
const a = new term_t("`x");
const b = new term_t("((pre_) (_suf))");
expect(a.rename(b).toString()).toBe("`pre_x_suf");
});

test("rename_empty_prefix", () => {
const a = new term_t("`x");
const b = new term_t("(() (_suf))");
expect(a.rename(b).toString()).toBe("`x_suf");
});

test("rename_empty_suffix", () => {
const a = new term_t("`x");
const b = new term_t("((pre_) ())");
expect(a.rename(b).toString()).toBe("`pre_x");
});

test("rename_list", () => {
const a = new term_t("(`x `y)");
const b = new term_t("((p_) (_s))");
expect(a.rename(b).toString()).toBe("(`p_x_s `p_y_s)");
});

test("rename_invalid", () => {
const a = new term_t("`x");
const b = new term_t("item");
expect(a.rename(b)).toBeNull();
});
Loading