Skip to content

Commit

Permalink
Add support for various functions of string lib
Browse files Browse the repository at this point in the history
  • Loading branch information
aranega committed Sep 19, 2024
1 parent cf1a008 commit a36da34
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 1 deletion.
66 changes: 66 additions & 0 deletions pyecoreocl/dummy_rules/primitives_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,72 @@ def rule_substring(emitter, ctx):
emitter.inline(f")[{arg1}:{arg2}]")


@primitive_rule
def rule_to_integer(emitter, ctx):
emitter.inline("int(")
emitter.visit(ctx.expression)
emitter.inline(")")


@primitive_rule
def rule_to_real(emitter, ctx):
emitter.inline("float(")
emitter.visit(ctx.expression)
emitter.inline(")")


@primitive_rule
def rule_to_upper_case(emitter, ctx):
emitter.visit(ctx.expression)
emitter.inline(".upper()")


@primitive_rule
def rule_index_of(emitter, ctx):
emitter.visit(ctx.expression)
emitter.inline(".index(")
args = [arg.text for arg in ctx.argExp().body]
args = ", ".join(args)
emitter.inline(f"{args})")


@primitive_rule
def rule_to_lower_case(emitter, ctx):
emitter.visit(ctx.expression)
emitter.inline(".lower()")


@primitive_rule
def rule_equals_ignore_case(emitter, ctx):
emitter.inline("(")
emitter.visit(ctx.expression)
emitter.inline(".upper() == ")
emitter.visit(ctx.argExp())
emitter.inline(".upper())")


@primitive_rule
def rule_at(emitter, ctx):
emitter.visit(ctx.expression)
emitter.inline("[")
emitter.visit(ctx.argExp())
emitter.inline("]")


@primitive_rule
def rule_characters(emitter, ctx):
emitter.inline("list(")
emitter.visit(ctx.expression)
emitter.inline(")")


@primitive_rule
def rule_toBoolean(emitter, ctx):
emitter.inline("(")
emitter.visit(ctx.expression)
emitter.inline(" in ('True', 'true'))")


def default_primitive_call(emitter, ctx):
emitter.visit(ctx.expression)
emitter.inline(f".{ctx.attname.text}(")
Expand Down
40 changes: 39 additions & 1 deletion tests/test_object_ptypes_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""

from dataclasses import dataclass
from operator import indexOf


def test__string_size():
Expand All @@ -27,4 +28,41 @@ def test__concat():


def test__substring():
assert !'abcd'.substring(1, 2)! == "bc"
assert !'abcd'.substring(1, 2)! == "bc"


def test__to_integer():
assert !'12'.toInteger()! == 12


def test__to_real():
assert !'12'.toReal()! == 12.0


def test__string_upper_case():
assert !'abc'.toUpperCase()! == 'ABC'


def test__string_lower_case():
assert !'ABC'.toLowerCase()! == 'abc'


def test__string_index_of():
assert !'abc'.indexOf('b')! == 1


def test__string_equals_ignore_case():
assert !'abc'.equalsIgnoreCase('AbC')! is True


def test__string_at():
assert !'abc'.at(1)! == "b"


def test__string_characters():
assert !'abc'.characters()! == ['a', 'b', 'c']


def test__to_boolean():
assert !'True'.toBoolean()! is True
assert !'False'.toBoolean()! is False

0 comments on commit a36da34

Please sign in to comment.