diff --git a/core/errors.py b/core/errors.py index 8ccf4f9..9fc0949 100755 --- a/core/errors.py +++ b/core/errors.py @@ -55,6 +55,7 @@ class Error: pos_end: Position error_name: str details: Optional[str] + context: Optional[Context] = None def as_string(self) -> str: """Return error as string""" diff --git a/core/tokens.py b/core/tokens.py index 5d89863..a471d8b 100755 --- a/core/tokens.py +++ b/core/tokens.py @@ -15,6 +15,7 @@ "argparser", "array", "colorlib", + "io", "math", "radiation", "system", diff --git a/examples/files.rn b/examples/files.rn index 14b7eca..3680c05 100644 --- a/examples/files.rn +++ b/examples/files.rn @@ -2,6 +2,14 @@ f = File("examples/files.rn") contents = f.read() f.close() +f.is_closed() print(contents) +print("-------------") + +f = File("examples/files.rn") +print(f.readline()) +print(f.readlines()) + +f.close() diff --git a/examples/io-test.rn b/examples/io-test.rn new file mode 100644 index 0000000..a03e25a --- /dev/null +++ b/examples/io-test.rn @@ -0,0 +1,17 @@ +import io + +var int_num = io.Input.get_int("Enter an integer number: ") +print(int_num) + +var float_num = io.Input.get_float("Enter a float number: ") +print(float_num) + +var str_val = io.Input.get_string("Enter a string: ") +print(str_val) + +var password = io.Input.get_password("Enter a password: ") +print(password) + +# issue here +var val = input("Enter a value: ") +io.Output.write(val) diff --git a/stdlib/io.rn b/stdlib/io.rn new file mode 100644 index 0000000..9e4bee8 --- /dev/null +++ b/stdlib/io.rn @@ -0,0 +1,53 @@ +import radiation + + +class Input { + static fun get_int(text="") { + const _val = input(text) + try { + return int(_val) + } catch as err { + raise radiation.ValueError("Invalid input") + } + } + + static fun get_float(text="") { + const _val = input(text) + try { + return float(_val) + } catch as err { + raise radiation.ValueError("Invalid input") + } + } + + static fun get_string(text="") { + return input(text) + } + + static fun get_bool(text="") { + const _val = String(input(text)) + if _val.casefold() == "true" { + return true + } elif _val.casefold() == "false" { + return false + } else { + raise radiation.ValueError("Invalid input") + } + } + + static fun get_password(text="") { + var ns = {"text": text} + pyapi("import getpass; val = getpass.getpass(text)", ns) + return ns["val"] + } +} + +class Output { + static fun write(...values, sep=" ", end="\n") { + var output = "" + for value in values { + output += str(value) + sep + } + print(output + end) + } +} \ No newline at end of file