Skip to content

Commit

Permalink
One general function for printing function help + help returns null
Browse files Browse the repository at this point in the history
  • Loading branch information
Vardan2009 committed May 29, 2024
1 parent 02b4035 commit 1c912dc
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion core/builtin_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def execute_help(self, exec_ctx: Context) -> RTResult[Value]:
if obj is None:
return RTResult[Value]().failure(Error(self.pos_start, self.pos_end, "TypeError", "Argument is null"))
print(obj.__help_repr__())
return RTResult[Value]().success(obj)
return RTResult[Value]().success(Null.null())

@args([])
def execute_input_int(self, exec_ctx: Context) -> RTResult[Value]:
Expand Down
15 changes: 9 additions & 6 deletions core/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1210,8 +1210,8 @@ def __help_repr__(self) -> str:
result: str = f"Help on object {self.parent_class.name}:\n\nclass {self.parent_class.name}\n"
for k in self.symbol_table.symbols:
f = self.symbol_table.symbols[k]
if isinstance(f, BaseFunction):
result += f"| fun {k}({', '.join([a.__str__() for a in f.arg_names])})\n|\t {f.desc}\n|\n"
if isinstance(f, Function):
result += f.__help_repr_method__()
elif isinstance(f, Value) and k != "this":
result += f"| {k} = {f!r}\n"
return result
Expand Down Expand Up @@ -1300,8 +1300,8 @@ def __help_repr__(self) -> str:
result: str = f"Help on object {self.name}:\n\nclass {self.name}\n"
for k in self.symbol_table.symbols:
f = self.symbol_table.symbols[k]
if isinstance(f, BaseFunction):
result += f"| fun {k}({', '.join([a.__str__() for a in f.arg_names])})\n|\t {f.desc}\n|\n"
if isinstance(f, Function):
result += f.__help_repr_method__()
elif isinstance(f, Value) and k != "this":
result += f"| {k} = {f!r}\n"
return result
Expand Down Expand Up @@ -1353,13 +1353,16 @@ class Function(BaseFunction):
should_auto_return: bool

def __help_repr__(self) -> str:
return f"Help on function {self.name}\n{self.__help_repr_method__()}"

def __help_repr_method__(self) -> str:
arg_strs: list[str] = []
for i in range(len(self.arg_names)):
if self.defaults[i] is not None:
arg_strs.append(f"{self.arg_names[i]} = {self.defaults[i]}")
arg_strs.append(f"{self.arg_names[i]} = {self.defaults[i].__repr__()}")
else:
arg_strs.append(self.arg_names[i])
return f"Help on function {self.name}\n| fun {self.name}({', '.join(arg_strs)})\n|\t{self.desc}\n"
return f"| fun {self.name}({', '.join(arg_strs)})\n|\t{self.desc}\n"

def __init__(
self,
Expand Down
2 changes: 1 addition & 1 deletion tests/help.rn.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"code": 0, "stdout": "\nString\n\nA String is a sequence of characters.\n\nMethods:\n len(str) -> Returns the length of the string.\n\nString standard library methods:\n find(str) -> Find a character in a string and return its index (-1 if not found)\n to_int() -> Magic method to convert string to int if possible\n\nExample: \"Hello World!\"\n\nHelp on object Test:\n\nclass Test\n| property1 = \"Hello, World!\"\n| fun __constructor__()\n|\t [No Description]\n|\n| fun func1(arg1, arg2)\n|\t Description for func1\n|\n| fun func2(arg1)\n|\t Description for func2\n|\n| fun no_description()\n|\t [No Description]\n|\n\nHelp on object Test:\n\nclass Test\n| property1 = \"Hello, World!\"\n| fun __constructor__()\n|\t [No Description]\n|\n| fun func1(arg1, arg2)\n|\t Description for func1\n|\n| fun func2(arg1)\n|\t Description for func2\n|\n| fun no_description()\n|\t [No Description]\n|\n\nHelp on function standalone\n| fun standalone(arg1, arg2)\n|\tThis is a standalone function\n\n", "stderr": ""}
{"code": 0, "stdout": "\nString\n\nA String is a sequence of characters.\n\nMethods:\n len(str) -> Returns the length of the string.\n\nString standard library methods:\n find(str) -> Find a character in a string and return its index (-1 if not found)\n to_int() -> Magic method to convert string to int if possible\n\nExample: \"Hello World!\"\n\nHelp on object Test:\n\nclass Test\n| property1 = \"Hello, World!\"\n| fun __constructor__()\n|\t[No Description]\n| fun func1(arg1, arg2)\n|\tDescription for func1\n| fun func2(arg1)\n|\tDescription for func2\n| fun no_description()\n|\t[No Description]\n\nHelp on object Test:\n\nclass Test\n| property1 = \"Hello, World!\"\n| fun __constructor__()\n|\t[No Description]\n| fun func1(arg1, arg2)\n|\tDescription for func1\n| fun func2(arg1)\n|\tDescription for func2\n| fun no_description()\n|\t[No Description]\n\nHelp on function standalone\n| fun standalone(arg1, arg2)\n|\tThis is a standalone function\n\n", "stderr": ""}

0 comments on commit 1c912dc

Please sign in to comment.