Skip to content

Commit

Permalink
Merge pull request #165 from radon-project/stdlib-os
Browse files Browse the repository at this point in the history
feat: added new stdlib `os`.
  • Loading branch information
Almas-Ali authored Jun 3, 2024
2 parents c946db7 + 2b41c72 commit a406e17
Show file tree
Hide file tree
Showing 4 changed files with 266 additions and 4 deletions.
8 changes: 4 additions & 4 deletions core/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,10 @@ def get_comparison_ne(self, other: Value) -> ResultTuple:
def radonify(value: object, pos_start: Position, pos_end: Position, context: Context) -> Value:
def _radonify(value: object) -> Value:
match value:
case True:
return Boolean.true()
case False:
return Boolean.false()
case dict():
return HashMap({k: radonify(v, pos_start, pos_end, context) for k, v in value.items()})
case list():
Expand All @@ -915,10 +919,6 @@ def _radonify(value: object) -> Value:
return String(value)
case int() | float():
return Number(value)
case True:
return Boolean.true()
case False:
return Boolean.false()
case None:
return Null.null()
case _ if inspect.isfunction(value):
Expand Down
1 change: 1 addition & 0 deletions core/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"colorlib",
"io",
"math",
"os",
"radiation",
"system",
"string",
Expand Down
18 changes: 18 additions & 0 deletions examples/os_examples.rn
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import os

var cwd = os.getcwd()
print(cwd)

var is_file = os.path.isfile("examples/posts.json")
print(is_file)

var is_dir = os.path.isdir("examples")
print(is_dir)

var list_dir = os.listdir("tests")
print(list_dir)
# print(arr_len(list_dir))

# for file in list_dir {
# print(file)
# }
243 changes: 243 additions & 0 deletions stdlib/os.rn
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
fun getcwd() {
"Get the current working directory."

var ns = {}
pyapi("import os; cwd = os.getcwd()", ns)
return ns["cwd"]
}

fun listdir(path=".") {
"List the files in the current directory."

var ns = {"path": path}
pyapi("import os; list = os.listdir(path)", ns)
return ns["list"]
}

fun mkdir(name) {
"Create a directory."

var ns = {"name": name}
pyapi("import os; value = os.mkdir(name)", ns)
return ns["value"]
}

fun mkdirs(name) {
"Create a directory and any missing parent directories."

var ns = {"name": name}
pyapi("import os; value = os.mkdirs(name)", ns)
return ns["value"]
}

fun rmdir(name) {
"Remove a directory."

var ns = {"name": name}
pyapi("import os; value = os.rmdir(name)", ns)
return ns["value"]
}

fun rmdirs(name) {
"Remove a directory and any missing parent directories."

var ns = {"name": name}
pyapi("import os; value = os.rmdirs(name)", ns)
return ns["value"]
}

fun remove(name) {
"Remove a file."

var ns = {"name": name}
pyapi("import os; value = os.remove(name)", ns)
return ns["value"]
}

fun rename(old, new) {
"Rename a file or directory."

var ns = {"old": old, "new": new}
pyapi("import os; value = os.rename(old, new)", ns)
return ns["value"]
}

fun symlink(src, dst) {
"Create a symbolic link."

var ns = {"src": src, "dst": dst}
pyapi("import os; value = os.symlink(src, dst)", ns)
return ns["value"]
}

fun readlink(path) {
"Read the target of a symbolic link."

var ns = {"path": path}
pyapi("import os; value = os.readlink(path)", ns)
return ns["value"]
}

fun stat(path) {
"Get the status of a file."

var ns = {"path": path}
pyapi("import os; value = os.stat(path)", ns)
return ns["value"]
}

fun lstat(path) {
"Get the status of a file without following symbolic links."

var ns = {"path": path}
pyapi("import os; value = os.lstat(path)", ns)
return ns["value"]
}

fun walk(top) {
"Walk a directory tree."

var ns = {"top": top}
pyapi("import os; value = os.walk(top)", ns)
return ns["value"]
}

fun chmod(path, mode) {
"Change the mode of a file."

var ns = {"path": path, "mode": mode}
pyapi("import os; value = os.chmod(path, mode)", ns)
return ns["value"]
}

fun chown(path, uid, gid) {
"Change the owner and group of a file."

var ns = {"path": path, "uid": uid, "gid": gid}
pyapi("import os; value = os.chown(path, uid, gid)", ns)
return ns["value"]
}

fun utime(path, times) {
"Change the access and modification times of a file."

var ns = {"path": path, "times": times}
pyapi("import os; value = os.utime(path, times)", ns)
return ns["value"]
}

fun link(src, dst) {
"Create a hard link."

var ns = {"src": src, "dst": dst}
pyapi("import os; value = os.link(src, dst)", ns)
return ns["value"]
}

fun unlink(path) {
"Remove a file."

var ns = {"path": path}
pyapi("import os; value = os.unlink(path)", ns)
return ns["value"]
}

fun chdir(path) {
"Change the current working directory."

var ns = {"path": path}
pyapi("import os; value = os.chdir(path)", ns)
return ns["value"]
}

fun access(path, mode) {
"Check if a file can be accessed."

var ns = {"path": path, "mode": mode}
pyapi("import os; value = os.access(path, mode)", ns)
return ns["value"]
}


class _Path {
"Path manipulation functions."

fun __constructor__() {}

static fun join(...args) {
"Join path components together."

var ns = {"args": args}
pyapi("import os; val = os.path.join(args)", ns)
return ns["val"]
}

static fun isfile(path) {
"Check if a path is a file."

var ns = {"path": path}
pyapi("import os; val = os.path.isfile(path)", ns)
return ns["val"]
}

static fun isdir(path) {
"Check if a path is a directory."

var ns = {"path": path}
pyapi("import os; val = os.path.isdir(path)", ns)
return ns["val"]
}

static fun islink(path) {
"Check if a path is a symbolic link."

var ns = {"path": path}
pyapi("import os; val = os.path.islink(path)", ns)
return ns["val"]
}

static fun ismount(path) {
"Check if a path is a mount point."

var ns = {"path": path}
pyapi("import os; val = os.path.ismount(path)", ns)
return ns["val"]
}

static fun exists(path) {
"Check if a path exists."

var ns = {"path": path}
pyapi("import os; val = os.path.exists(path)", ns)
return ns["val"]
}
}

var path = _Path


class Test
{
var property1 = "Hello, World!"

fun __constructor__()
{

}

fun func1(arg1, arg2=true)
{
"Description for func1"
}

fun func2(arg1=null)
{
"Description for func2"
return true
}

fun no_description() {
return null
}
}

0 comments on commit a406e17

Please sign in to comment.