diff --git a/core/builtin_classes/file_object.py b/core/builtin_classes/file_object.py index f8c1ca7..5fe2512 100644 --- a/core/builtin_classes/file_object.py +++ b/core/builtin_classes/file_object.py @@ -47,14 +47,14 @@ def read(self, ctx: Context) -> RTResult[Value]: @args([]) @method - def readline(ctx): + def readline(self, ctx: Context): res = RTResult[Value]() - self = ctx.symbol_table.get("this") try: value = self.file.readline() return res.success(String(value)) except OSError as e: - return res.failure(RTError(None, None, f"Could not read from file: {e.strerror}", None)) + pos = Position(-1, -1, -1, "", "") + return res.failure(RTError(pos, pos, f"Could not read from file: {e.strerror}", ctx)) @args([]) @method diff --git a/core/builtin_classes/string_object.py b/core/builtin_classes/string_object.py index 9029551..f87c478 100644 --- a/core/builtin_classes/string_object.py +++ b/core/builtin_classes/string_object.py @@ -118,15 +118,15 @@ def endswith(self, ctx: Context) -> RTResult[Value]: ) return res.success(Boolean(self.value.endswith(string.value))) - @args(["string"], [String("")]) + @args(["string"], [String(" ")]) @method def split(self, ctx: Context) -> RTResult[Value]: res = RTResult[Value]() - string = ctx.symbol_table.get("string") + string = ctx.symbol_table.get("string") # String object assert string is not None if not isinstance(string, String): return res.failure(RTError(string.pos_start, string.pos_end, "Cannot split a non-string", string.context)) - return res.success(Array([String(i) for i in self.value.split(string.value)])) + return res.success(Array([String(i) for i in self.value.split(str(string))])) @args(["string"], [String("")]) @method diff --git a/examples/args_test.rn b/examples/args_test.rn index 0fc5cc0..cb6f64f 100755 --- a/examples/args_test.rn +++ b/examples/args_test.rn @@ -1,6 +1,6 @@ -import Argparser +import argparser -parser = Argparser.Argparser() +var parser = argparser.Argparser() parser.add_flag("--version", "-v", "Show version") parser.add_named("--output", "Output file") parser.add_pos_opt("filename", "File") diff --git a/examples/arrays.rn b/examples/arrays.rn index c187c00..511720b 100755 --- a/examples/arrays.rn +++ b/examples/arrays.rn @@ -1,13 +1,13 @@ -import Array +import array -arr = Array.Array([1,2,3,4,5,6,7,8,9,10]) +const arr = array.Array([1,2,3,4,5,6,7,8,9,10]) arr.append(11) print(arr.to_string()) arr.pop(0) print(arr.to_string()) -print(arr.len()) +print(len(arr)) arr.extend(["this", "is", "a", "array"]) print(arr.to_string()) diff --git a/examples/classes.rn b/examples/classes.rn index 9391c25..fa62024 100755 --- a/examples/classes.rn +++ b/examples/classes.rn @@ -9,10 +9,10 @@ class Person { } print("Enter a name, or type 'exit' to quit.") -inp = input("Name: ") +var inp = input("Name: ") while inp != "exit" { - p = Person(inp) + var p = Person(inp) print(p.Format()) inp = input("Name: ") p.single_line() @@ -30,6 +30,6 @@ class Some { fun to_object() -> "Name is : "+ this.name + " Value is : "+ str(this.value) } -cls_ = Some() -obj = cls_.to_object() +var cls_ = Some() +var obj = cls_.to_object() print(obj) diff --git a/examples/colors_test.rn b/examples/colors_test.rn index 3fd78aa..5d46377 100755 --- a/examples/colors_test.rn +++ b/examples/colors_test.rn @@ -1,58 +1,58 @@ -import Colorlib +import colorlib # Print colored text to the console using the colorlib library -print(Colorlib.white("My name is John Doe")) -print(Colorlib.red("My name is John Doe")) -print(Colorlib.green("My name is John Doe")) -print(Colorlib.yellow("My name is John Doe")) -print(Colorlib.blue("My name is John Doe")) -print(Colorlib.purple("My name is John Doe")) -print(Colorlib.cyan("My name is John Doe")) -print(Colorlib.gray("My name is John Doe")) -print(Colorlib.black("My name is John Doe")) -print(Colorlib.bgWhite("My name is John Doe")) -print(Colorlib.bgRed("My name is John Doe")) -print(Colorlib.bgGreen("My name is John Doe")) -print(Colorlib.bgYellow("My name is John Doe")) -print(Colorlib.bgBlue("My name is John Doe")) -print(Colorlib.bgPurple("My name is John Doe")) -print(Colorlib.bgCyan("My name is John Doe")) -print(Colorlib.bgGray("My name is John Doe")) -print(Colorlib.bgBlack("My name is John Doe")) -print(Colorlib.bold("My name is John Doe")) -print(Colorlib.italic("My name is John Doe")) -print(Colorlib.underline("My name is John Doe")) -print(Colorlib.inverse("My name is John Doe")) -print(Colorlib.hidden("My name is John Doe")) -print(Colorlib.strikethrough("My name is John Doe")) -print(Colorlib.blackBright("My name is John Doe")) -print(Colorlib.redBright("My name is John Doe")) -print(Colorlib.greenBright("My name is John Doe")) -print(Colorlib.yellowBright("My name is John Doe")) -print(Colorlib.blueBright("My name is John Doe")) -print(Colorlib.purpleBright("My name is John Doe")) -print(Colorlib.cyanBright("My name is John Doe")) -print(Colorlib.whiteBright("My name is John Doe")) -print(Colorlib.bgBlackBright("My name is John Doe")) -print(Colorlib.bgRedBright("My name is John Doe")) -print(Colorlib.bgGreenBright("My name is John Doe")) -print(Colorlib.bgYellowBright("My name is John Doe")) -print(Colorlib.bgBlueBright("My name is John Doe")) -print(Colorlib.bgPurpleBright("My name is John Doe")) -print(Colorlib.bgCyanBright("My name is John Doe")) -print(Colorlib.bgWhiteBright("My name is John Doe")) -print(Colorlib.reset("My name is John Doe")) -print(Colorlib.bold("My name is John Doe")) -print(Colorlib.italic("My name is John Doe")) -print(Colorlib.underline("My name is John Doe")) -print(Colorlib.inverse("My name is John Doe")) -print(Colorlib.hidden("My name is John Doe")) -print(Colorlib.strikethrough("My name is John Doe")) -print(Colorlib.bgReset("My name is John Doe")) -print(Colorlib.bgBoldBright("My name is John Doe")) +print(colorlib.white("My name is John Doe")) +print(colorlib.red("My name is John Doe")) +print(colorlib.green("My name is John Doe")) +print(colorlib.yellow("My name is John Doe")) +print(colorlib.blue("My name is John Doe")) +print(colorlib.purple("My name is John Doe")) +print(colorlib.cyan("My name is John Doe")) +print(colorlib.gray("My name is John Doe")) +print(colorlib.black("My name is John Doe")) +print(colorlib.bgWhite("My name is John Doe")) +print(colorlib.bgRed("My name is John Doe")) +print(colorlib.bgGreen("My name is John Doe")) +print(colorlib.bgYellow("My name is John Doe")) +print(colorlib.bgBlue("My name is John Doe")) +print(colorlib.bgPurple("My name is John Doe")) +print(colorlib.bgCyan("My name is John Doe")) +print(colorlib.bgGray("My name is John Doe")) +print(colorlib.bgBlack("My name is John Doe")) +print(colorlib.bold("My name is John Doe")) +print(colorlib.italic("My name is John Doe")) +print(colorlib.underline("My name is John Doe")) +print(colorlib.inverse("My name is John Doe")) +print(colorlib.hidden("My name is John Doe")) +print(colorlib.strikethrough("My name is John Doe")) +print(colorlib.blackBright("My name is John Doe")) +print(colorlib.redBright("My name is John Doe")) +print(colorlib.greenBright("My name is John Doe")) +print(colorlib.yellowBright("My name is John Doe")) +print(colorlib.blueBright("My name is John Doe")) +print(colorlib.purpleBright("My name is John Doe")) +print(colorlib.cyanBright("My name is John Doe")) +print(colorlib.whiteBright("My name is John Doe")) +print(colorlib.bgBlackBright("My name is John Doe")) +print(colorlib.bgRedBright("My name is John Doe")) +print(colorlib.bgGreenBright("My name is John Doe")) +print(colorlib.bgYellowBright("My name is John Doe")) +print(colorlib.bgBlueBright("My name is John Doe")) +print(colorlib.bgPurpleBright("My name is John Doe")) +print(colorlib.bgCyanBright("My name is John Doe")) +print(colorlib.bgWhiteBright("My name is John Doe")) +print(colorlib.reset("My name is John Doe")) +print(colorlib.bold("My name is John Doe")) +print(colorlib.italic("My name is John Doe")) +print(colorlib.underline("My name is John Doe")) +print(colorlib.inverse("My name is John Doe")) +print(colorlib.hidden("My name is John Doe")) +print(colorlib.strikethrough("My name is John Doe")) +print(colorlib.bgReset("My name is John Doe")) +print(colorlib.bgBoldBright("My name is John Doe")) -# chaining multiple styles and Colorlibs combined multiple times -print(Colorlib.bold(Colorlib.bgPurple(Colorlib.white("My name is John Doe")))) -print(Colorlib.italic(Colorlib.bgRed(Colorlib.green("My name is John Doe")))) -print(Colorlib.underline(Colorlib.bgYellow(Colorlib.blue("My name is John Doe")))) -print(Colorlib.inverse(Colorlib.bgBlue(Colorlib.yellow("My name is John Doe")))) +# chaining multiple styles and colorlibs combined multiple times +print(colorlib.bold(colorlib.bgPurple(colorlib.white("My name is John Doe")))) +print(colorlib.italic(colorlib.bgRed(colorlib.green("My name is John Doe")))) +print(colorlib.underline(colorlib.bgYellow(colorlib.blue("My name is John Doe")))) +print(colorlib.inverse(colorlib.bgBlue(colorlib.yellow("My name is John Doe")))) diff --git a/examples/files.rn b/examples/files.rn index 0122400..34cab54 100644 --- a/examples/files.rn +++ b/examples/files.rn @@ -1,15 +1,14 @@ - -const f = File("examples/files.rn") -const contents = f.read() -f.close() -f.is_closed() +const f1 = File("examples/files.rn") +const contents = f1.read() +f1.close() +f1.is_closed() print(contents) print("-------------") -f = File("examples/files.rn") -print(f.readline()) -print(f.readlines()) +const f2 = File("examples/files.rn") +print(f2.readline()) +print(f2.readlines()) -f.close() +f2.close() diff --git a/examples/functions.rn b/examples/functions.rn deleted file mode 100755 index 6f03e14..0000000 --- a/examples/functions.rn +++ /dev/null @@ -1,31 +0,0 @@ -# This is a very useful piece of software - -fun oopify(prefix) -> prefix + "oop" - -fun join(elements, separator) - result = "" - length = arrlen(elements) - - for i = 0 to length then - result = result + elements/i - if i != length - 1 then result = result + separator - end - - return result -end - -fun map(elements, func) - new_elements = [] - - for i = 0 to arrlen(elements) then - append(new_elements, func(elements/i)) - end - - return new_elements -end - -print("Greetings universe!") - -for i = 0 to 5 then - print(join(map(["l", "sp"], oopify), ", ")) -end diff --git a/examples/hashmap.rn b/examples/hashmap.rn index 0ced516..5c3d7a5 100644 --- a/examples/hashmap.rn +++ b/examples/hashmap.rn @@ -1,13 +1,13 @@ # HashMap data type in Radon -hashmap = { +var hashmap = { "name": "John", "age": 23, "address": "123 Main St" } print(hashmap) -value = hashmap["name"] +var value = hashmap["name"] print(value) hashmap["bcd"] = 234 @@ -16,7 +16,7 @@ hashmap["abc"] = 456 print(hashmap) while true { - key = input("Enter key: ") + var key = input("Enter key: ") if key == "exit" {break} print(hashmap[key]) } diff --git a/examples/import_test.rn b/examples/import_test.rn deleted file mode 100755 index 1bf23e8..0000000 --- a/examples/import_test.rn +++ /dev/null @@ -1,48 +0,0 @@ -################################## -# Testing stdlib Math library -################################## - -# require("Math") - -# math = Math() -# print(math.PI) # Ligal - -# print(Math().PI) # Illigal - - -################################## -# Testing System library -################################## - -# require("System") - -# sys = System() -# print(sys.getInfo()) - - -################################## -# Testing external library -################################## - -# require("simple.rn") - - -################################## -# Testing String library -################################## - -# require("String") - -#! -import String - -data = "Hello World !" -str = String.String(data) - -print(str.len()) - -!# -import Math - -print(Math.PI) # Ligal - diff --git a/examples/io-test.rn b/examples/io-test.rn index 63c538d..3520d19 100644 --- a/examples/io-test.rn +++ b/examples/io-test.rn @@ -12,5 +12,17 @@ print(str_val) var password = io.Input.get_password("Enter a password: ") print(password) -var val = input("Enter a value: ") -io.Output.write(val, sep="\n") +var val = null +var vals = [] +print("Type 'exit' to stop.") + +while val != "exit" { + val = input("Enter a value: ") + arr_append(vals, val) +} + +print("unpacking is not implemented yet.") +io.Output.write(vals, sep="\n") + +print("after unpacking, the result will be like this.") +io.Output.write("dsf", "sdf", "sdf", "sdf", "sdf", "exit", sep="\n") diff --git a/examples/json_testing.rn b/examples/json_testing.rn index 8777351..a1efbca 100644 --- a/examples/json_testing.rn +++ b/examples/json_testing.rn @@ -1,7 +1,7 @@ -f = File("examples/posts.json", "r") +var f = File("examples/posts.json", "r") -json = Json() -posts = json.loads(f.read()) +var json = Json() +var posts = json.loads(f.read()) f.close() assert arr_len(posts) == 100 diff --git a/examples/loops.rn b/examples/loops.rn index 252d2e1..14ad44a 100755 --- a/examples/loops.rn +++ b/examples/loops.rn @@ -18,17 +18,19 @@ for i in [1, 2, 3, 4, 5] { print(i) } +#! Comment starts from here for i in [1, 2, 3, 4, 5] { if i == 3 { - break + break # it is exiting the program; not just the loop it self. } print(i) } - +!# # While loop -#! -while i < 10 { - print(i) - i = i + 1 + +var j = 11 +while j < 15 { + print(j) + j += 1 } diff --git a/examples/new_syntax.rn b/examples/new_syntax.rn index aa17a6f..f412f77 100755 --- a/examples/new_syntax.rn +++ b/examples/new_syntax.rn @@ -1,69 +1,65 @@ # PASSED -# class Num { -# -# fun Num() { -# print("Num") -# } -# -# fun is_even(number) { -# if number % 2 == 0 { -# return true -# } else { -# return false -# } -# } -# -# } -# -# fun main() { -# num = Num() -# print(num.is_even(2)) -# -# } -# -# main() +class Num { + + fun __constructor__() {} + + fun is_even(number) { + if number % 2 == 0 { + return true + } else { + return false + } + } +} + +fun main() { + var num = Num() + print(num.is_even(2)) +} + +main() # PASSED -# for i = 0 to 10 step 2 { -# print(i) -# } +for i = 0 to 10 step 2 { + print(i) +} # PASSED -# while true { -# usr = input("> ") -# if usr == "exit" { -# break -# } -# print(usr) -# } +while true { + var usr = input("> ") + if usr == "exit" { + break + } + print(usr) +} # PASSED -# if 3 == 4 { -# print("OK") -# -# } elif 3 == 0 { -# print("YES") -# -# } else { -# print("NO") -# } +if 3 == 4 { + print("OK") + +} elif 3 == 0 { + print("YES") + +} else { + print("NO") +} # PASSED -# fun something() -> true -# a = fun () -> print("this is") -# a() +fun something() -> true +var a = fun () -> print("this is") +a() ############################################ -# FAILING -# obj = { -# "name" : "John", -# "age" : 30, -# "city" : "New York" -# } +# PASSED +var obj = { + "name" : "John", + "age" : 30, + "city" : "New York" +} -# i = 3 -# i += 4 -# print(i) +var j = 3 +j += 4 +print(j) diff --git a/examples/operator_overloading.rn b/examples/operator_overloading.rn index 2405261..a832e87 100644 --- a/examples/operator_overloading.rn +++ b/examples/operator_overloading.rn @@ -21,12 +21,12 @@ class WrongOp { } } -a = WrongOp(69) +var a = WrongOp(69) a.dump("a") -b = WrongOp(420) +var b = WrongOp(420) b.dump("b") -c = a + b +var c = a + b c.dump("a + b") -d = a - b +var d = a - b d.dump("a - b") diff --git a/examples/operators-bug.rn b/examples/operators-bug.rn deleted file mode 100644 index ab956a1..0000000 --- a/examples/operators-bug.rn +++ /dev/null @@ -1,12 +0,0 @@ -x = 5 -y = 2 - -x /= y -print(x) # 2.5 OK as floor division. - -x //= y -print(x) # 1.0 Failed as Integer division - -assert 5 / 5 == 1.0 # 1.0 OK as floor division. - -assert 5 // 5 == 1 # Invalid Syntax: Token cannot appear after previous tokens \ No newline at end of file diff --git a/examples/os_examples.rn b/examples/os_examples.rn index e46ace3..8979ca5 100644 --- a/examples/os_examples.rn +++ b/examples/os_examples.rn @@ -11,8 +11,8 @@ print(is_dir) var list_dir = os.listdir("tests") print(list_dir) -# print(arr_len(list_dir)) +print(len(list_dir)) -# for file in list_dir { -# print(file) -# } \ No newline at end of file +for file in list_dir { + print(file) +} \ No newline at end of file diff --git a/examples/pyapi_advanced.rn b/examples/pyapi_advanced.rn index bca9f53..d602804 100644 --- a/examples/pyapi_advanced.rn +++ b/examples/pyapi_advanced.rn @@ -1,5 +1,7 @@ +## This is an example of how to use the `pyapi()` `BuiltInFunction` in Radon +## Syntax: pyapi("python code", namespace HashMap{}) -> load all variables to namespace, which is accessable by Radon later on. -ns = {} +var ns = {} ns["cb"] = fun(x, y) -> (x + y) * 101 @@ -12,10 +14,10 @@ def my_func(x, y): print(f'Return value (from Radon function in Python): {cb(34, 35)}') ", ns) -func = ns["my_func"] +var func = ns["my_func"] print("func is: " + print_ret(func)) -ret = func("hello", "world") +var ret = func("hello", "world") print("Return value (from Python function in Radon): " + str(ret)) diff --git a/examples/python_api.rn b/examples/python_api.rn deleted file mode 100755 index 791bb70..0000000 --- a/examples/python_api.rn +++ /dev/null @@ -1,27 +0,0 @@ -## This is an example of how to use the `pyapi()` `BuiltInFunction` in Radon -## Syntax: pyapi("python code") -> String (output of the python code) - -ns = {} - -pyapi("import os; cwd = os.getcwd()", ns) -print("Current working directory: " + ns["cwd"]) - -pyapi("import os; files = os.listdir()", ns) -print("Files in current directory: " + str(ns["files"])) - -pyapi("import math; pi = math.pi", ns) -print("Value of pi: " + ns["pi"]) - -pyapi("sum_ = 2 + 2", ns) -print("Sum of 2 + 2: " + ns["sum_"]) - -ns["x"] = x = 6 -ns["y"] = y = 7 - -pyapi("sum_ = x + y", ns) -print("Sum of "+str(x)+" + "+str(y)+": " + ns["sum_"]) - -ns["xs"] = xs = [1, 2, 3, 4, 9] - -pyapi("sum_ = sum(xs)", ns) -print("Sum of "+str(xs)+": " + ns["sum_"]) diff --git a/examples/requests_test.rn b/examples/requests_test.rn index 963cc2c..b4efd7f 100644 --- a/examples/requests_test.rn +++ b/examples/requests_test.rn @@ -1,16 +1,16 @@ # API testing -request = Requests() -json = Json() +var request = Requests() +var json = Json() # get request to the API -# response_get = request.get("https://jsonplaceholder.typicode.com/posts") +# var response_get = request.get("https://jsonplaceholder.typicode.com/posts") # print(response_get) # print(type(response_get)) # print(response_get.status_code) -# radon_object = json.loads(response_get) +# var radon_object = json.loads(response_get) # print(type(radon_object)) # print(arr_len(radon_object)) @@ -20,34 +20,34 @@ json = Json() # } # post request to the API -# response_post = request.post("https://jsonplaceholder.typicode.com/posts", {"title": "foo", "body": "bar", "userId": 1}) +# var response_post = request.post("https://jsonplaceholder.typicode.com/posts", {"title": "foo", "body": "bar", "userId": 1}) # print(response_post) # print(type(response_post)) -# radon_object = json.loads(response_post) +# var radon_object = json.loads(response_post) # print(radon_object) # print(type(radon_object)) # put request to the API -# response_put = request.put("https://jsonplaceholder.typicode.com/posts/1", {"id": 1, "title": "foo", "body": "bar", "userId": 1}) +# var response_put = request.put("https://jsonplaceholder.typicode.com/posts/1", {"id": 1, "title": "foo", "body": "bar", "userId": 1}) # print(response_put) # print(type(response_put)) -# radon_object = json.loads(response_put) +# var radon_object = json.loads(response_put) # print(radon_object) # print(type(radon_object)) # delete request to the API -# response_delete = request.delete("https://jsonplaceholder.typicode.com/posts/1") +# var response_delete = request.delete("https://jsonplaceholder.typicode.com/posts/1") # print(response_delete) # print(type(response_delete)) -# radon_object = json.loads(response_delete) +# var radon_object = json.loads(response_delete) # print(radon_object) # print(type(radon_object)) # patch request to the API -# response_patch = request.patch("https://jsonplaceholder.typicode.com/posts/1", {"title": "foo"}) +# var response_patch = request.patch("https://jsonplaceholder.typicode.com/posts/1", {"title": "foo"}) # print(response_patch) # print(type(response_patch)) -# radon_object = json.loads(response_patch) +# var radon_object = json.loads(response_patch) # print(radon_object) # print(type(radon_object)) diff --git a/examples/scoping.rn b/examples/scoping.rn index 74f905e..5f3ed46 100755 --- a/examples/scoping.rn +++ b/examples/scoping.rn @@ -1,8 +1,8 @@ # NOTE: this is supposed to fail -m = 69 +var m = 69 if true { - y = 420 - m = y + m + var y = 420 + var m = y + m print(m) } print(m) diff --git a/examples/simple-grep.rn b/examples/simple-grep.rn index 9c8bc41..8772f8c 100644 --- a/examples/simple-grep.rn +++ b/examples/simple-grep.rn @@ -1,40 +1,40 @@ -import Argparser +import argparser fun main() { - parser = Argparser.Argparser() + var parser = argparser.Argparser() parser.add_flag("--help", "-h", "Show this help text and exit") parser.add_pos_opt("query", "String to query", required=true) parser.add_pos_opt("file", "File to query string in", required=true) parser.add_flag("--line-numbers", "-n", "Show line numbers") parser.add_named("--max-lines", "Maximum amount of lines to show", conversor=int) - args = parser.parse(argv[:]) + var args = parser.parse(argv[:]) if args["--help"] { print(parser.usage(argv[0])) exit() } - f = File(args["file"], "r") - lines = (String(f.read())).split("\n") + var f = File(args["file"], "r") + var lines = (String(f.read())).split("\n") f.close() - matched_lines = [] - i = 0 + var matched_lines = [] + var i = 0 for line in lines { if args["query"] in line { arr_append(matched_lines, [i, line]) } - nonlocal i++ + i++ } if not is_null(args["--max-lines"]) { - nonlocal matched_lines = matched_lines[:args["--max-lines"]] + var matched_lines = matched_lines[:args["--max-lines"]] } for line in matched_lines { - s = line[1] + var s = line[1] if args["--line-numbers"] { - nonlocal s = args["file"] + ":" + line[0] + ": " + s + var s = args["file"] + ":" + line[0] + ": " + s } print(s) } diff --git a/examples/simple.rn b/examples/simple.rn index 35d9db3..e2b6926 100755 --- a/examples/simple.rn +++ b/examples/simple.rn @@ -2,26 +2,34 @@ # Linear search algorithm fun find(list, value) { for i = 0 to len(list) { - if i == value { + if list[i] == value { return i } } return -1 } -# list = [1, 2, 3, 4, 5] -# index = find(list, 3) -# print(index) +var list = [1, 2, 3, 4, 5] +var index = find(list, 3) +print(index) -fun iseven(num) -> if (num%2) == 0 { print("even") } else { print("odd") } +fun iseven(num) -> if (num % 2) == 0 { return true } else { return false } # returning null !! +print(iseven(234)) +print("iseven(42): " + str(iseven(42))) +print("iseven(2341): " + str(iseven(2341))) class Algorithm { + fun __constructor__(){} # without constructor class is raiseing function is not defined. + fun find(list, value) { for i = 0 to len(list) { - if i == value { + if list[i] == value { return i } } return -1 } -} \ No newline at end of file +} + +var algo = Algorithm() +print(algo.find(list, 3)) diff --git a/examples/slicing.rn b/examples/slicing.rn index 968c19d..dca91c4 100755 --- a/examples/slicing.rn +++ b/examples/slicing.rn @@ -1,35 +1,34 @@ # Slicing # Array of Numbers slicing -my_arr = [1,2,3,4,5,6,7,8,9,0] -print(my_arr[0]) -print(my_arr[0:5]) -print(my_arr[0:9:2]) -# print(my_arr[]) -# print(my_arr[-1]) +var my_nums_array = [1,2,3,4,5,6,7,8,9,0] +print(my_nums_array[0]) +print(my_nums_array[0:5]) +print(my_nums_array[0:9:2]) +print(my_nums_array[-1]) # Array of Strings slicing -my_str_arr = ["This", "is", "an", "example", "string"] -print(my_str_arr[0]) -print(my_str_arr[0:3]) -print(my_str_arr[0:5:2]) -# print(my_str_arr[]) +var my_str_array = ["This", "is", "an", "example", "string"] +print(my_str_array[0]) +print(my_str_array[0:3]) +print(my_str_array[0:5:2]) +print(my_str_array[-5]) +print(my_str_array[-5:-1]) +print(my_str_array[-5:-1:2]) # Complex Array slicing -my_complex_arr = [1, "This", 2, "is", 3, "an", 4, "example", 5, "string", 4.64, 2.4556, 324.2345] -print(my_complex_arr[0]) -print(my_complex_arr[11]) -print(my_complex_arr[0:9:1]) -print(my_complex_arr[0:10:2]) -# print(my_complex_arr[]) +var my_complex_array = [1, "This", 2, "is", 3, "an", 4, "example", 5, "string", 4.64, 2.4556, 324.2345] +print(my_complex_array[0]) +print(my_complex_array[11]) +print(my_complex_array[0:9:1]) +print(my_complex_array[0:10:2]) # String slicing -my_str = "This is an example string" +var my_str = "This is an example string" print(my_str[0:4:1]) print(my_str[5:7]) print(my_str[9]) -# print(my_str[]) # Straight forward slicing print("string"[0:3]) @@ -37,11 +36,11 @@ print([1,2,3,4,5,6,7,8,9,0][0:5]) # updating values in array -my_arr = [1,2,3,4,5,6,7,8,9,0] -my_arr[0] = 10 -print(my_arr) +var my_array_test = [1,2,3,4,5,6,7,8,9,0] +my_array_test[0] = 10 +print(my_array_test) # updating values in string -my_str = "This is an example string" -new_str = my_str[19] = "S" -print(new_str) +var my_str_test = "This is an example string" +var new_str_test = my_str_test[19] = "S" +print(new_str_test) diff --git a/examples/syntax.rn b/examples/syntax.rn index 4ef210f..0292233 100755 --- a/examples/syntax.rn +++ b/examples/syntax.rn @@ -31,12 +31,12 @@ # ^= - Exponentiation and assign # Variable definition -a = 10 -b = 20 +var a = 10 +var b = 20 print(a + b) # 30 -c = "Hello" -d = "World" +var c = "Hello" +var d = "World" print(c + " " + d) # Hello World # Conditional statement @@ -49,7 +49,7 @@ if a > b { } # For loop -x = 9 # Multiplication table of 9 +var x = 9 # Multiplication table of 9 for i = 1 to 11 { print(str(x) + " X " + str(i) + " = " + str(x * i)) @@ -58,7 +58,7 @@ for i = 1 to 11 { # While loop while x > 0 { print(x) - x = x - 1 + x -= 1 } # Function definition @@ -69,7 +69,7 @@ fun add(a, b) { print(add(10, 20)) # 30 # Anonymous function -sub = fun (a, b) { +var sub = fun (a, b) { return a - b } @@ -82,7 +82,7 @@ print(mul(10, 20)) # 200 # Class definition class Person { # Constructor - fun Person(name, age) { + fun __constructor__(name, age) { this.name = name this.age = age } @@ -97,6 +97,6 @@ class Person { } # Use a class -person = Person("Almas", 21) -details = "Name is : " + person.get_name() + ", Age : " + str(person.get_age()) +var person = Person("Almas", 21) +var details = "Name is : " + person.get_name() + ", Age : " + str(person.get_age()) print(details) diff --git a/radon.py b/radon.py index 7a68faa..d4f144a 100755 --- a/radon.py +++ b/radon.py @@ -18,6 +18,7 @@ pass import core as base_core +from core.colortools import Log from core.parser import Context from core.lexer import Position @@ -133,8 +134,13 @@ def main(argv: list[str]) -> None: base_core.global_symbol_table.set("argv", base_core.radonify(argv, pos, pos, Context(""))) if source_file is not None: head, _ = os.path.split(source_file) - with open(source_file, "r") as f: - source = f.read() + try: + with open(source_file, "r") as f: + source = f.read() + except FileNotFoundError: + print(Log.deep_error(f"[!] FileNotFound: {Log.deep_error(source_file, bold=True)}")) + exit(1) + (result, error, should_exit) = base_core.run(source_file, source, import_cwd=head) if error: diff --git a/stdlib/array.rn b/stdlib/array.rn index 9719190..d944cbf 100644 --- a/stdlib/array.rn +++ b/stdlib/array.rn @@ -16,10 +16,10 @@ class Array { fun append(item) -> arr_append(this.list, item) fun pop(index) -> arr_pop(this.list, index) fun extend(list) -> arr_extend(this.list, list) - fun find(index) -> arr_find(this.list, index) + fun find(index) -> (this.list)[index] fun slice(start, end) -> ((this.list)[start:end]) - fun __len__() -> arr_len(this.list) + fun __len__() -> len(this.list) fun is_empty() -> this.list == [] fun to_string() -> str(this.list) fun is_array() -> true diff --git a/stdlib/os.rn b/stdlib/os.rn index 142bb1d..4a856d0 100644 --- a/stdlib/os.rn +++ b/stdlib/os.rn @@ -215,29 +215,3 @@ class _Path { 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 - } -} - diff --git a/test.py b/test.py index 953dc57..0c18ebe 100755 --- a/test.py +++ b/test.py @@ -40,7 +40,7 @@ def run_tests(directory: str = "tests") -> int: ["mypy", "."], stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=dict(**os.environ, MYPY_FORCE_COLOR="1") ) - failed_tests = [] + failed_tests: list[str] = [] for test in os.listdir(directory): json_file = f"{directory}/{test}.json" if not test.endswith(".rn"): @@ -85,7 +85,7 @@ def run_tests(directory: str = "tests") -> int: return 1 -def record_tests(directory="tests") -> int: +def record_tests(directory: str = "tests") -> int: for test in os.listdir(directory): if not test.endswith(".rn"): continue