Skip to content

Commit

Permalink
Merge pull request #158 from radon-project/stdlib-io
Browse files Browse the repository at this point in the history
feat: added `io` stdlib support.
  • Loading branch information
Almas-Ali authored Jun 1, 2024
2 parents add0f36 + ee8db7b commit e7287e9
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 0 deletions.
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)
}
}

0 comments on commit e7287e9

Please sign in to comment.