Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added io stdlib support. #158

Merged
merged 4 commits into from
Jun 1, 2024
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
1 change: 1 addition & 0 deletions core/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
1 change: 1 addition & 0 deletions core/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"argparser",
"array",
"colorlib",
"io",
"math",
"radiation",
"system",
Expand Down
8 changes: 8 additions & 0 deletions examples/files.rn
Original file line number Diff line number Diff line change
Expand Up @@ -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()
17 changes: 17 additions & 0 deletions examples/io-test.rn
Original file line number Diff line number Diff line change
@@ -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)
53 changes: 53 additions & 0 deletions stdlib/io.rn
Original file line number Diff line number Diff line change
@@ -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)
}
}