From 6991edf8d8c2b162abaad94cb1d49994931c7e4f Mon Sep 17 00:00:00 2001 From: Vardan Petrosyan Date: Fri, 31 May 2024 23:36:29 +0400 Subject: [PATCH 1/6] Updated Docs + Error Handling Page --- docs/arrays.md | 22 ++++---- docs/built-in-functions.md | 10 +++- docs/classes.md | 20 +++---- docs/data-types.md | 24 ++++----- docs/error-handling.md | 106 +++++++++++++++++++++++++++++++++++++ docs/index.md | 10 ++-- docs/modules.md | 4 +- docs/standard-library.md | 14 ++--- docs/strings.md | 4 +- mkdocs.yml | 2 +- 10 files changed, 164 insertions(+), 52 deletions(-) create mode 100644 docs/error-handling.md diff --git a/docs/arrays.md b/docs/arrays.md index 36191a1..aa825f8 100644 --- a/docs/arrays.md +++ b/docs/arrays.md @@ -1,8 +1,8 @@ # Arrays -## Array methods +## Built-in Array methods -- `arr_len()` - returns the length of the array +- `arr_len()` or `len(arr)` - returns the length of the array - `arr_push(array, item)` - adds an item to the end of the array - `arr_pop(array, index)` - removes an item from the end of the array - `arr_append(array, item)` - adds an item to the end of the array @@ -11,7 +11,7 @@ - `arr_slice(array, start, end)` - returns the items from the specified start index to the specified end index ```py linenums="1" title="methods.rn" -var arr = [1, 2, 3, 4, 5] +arr = [1, 2, 3, 4, 5] print(arr_len(arr)) # 5 arr_push(arr, 6) @@ -32,14 +32,14 @@ print(arr_find(arr, 1)) # 2 print(arr_slice(arr, 0, 5)) # [1, 2, 3, 4, 5] ``` -## Array operators (Development) +## Array operators - `+` (concatenation) - `*` (repetition) ```py linenums="1" title="operators.rn" -var arr1 = [1, 2, 3] -var arr2 = [4, 5, 6] +arr1 = [1, 2, 3] +arr2 = [4, 5, 6] print(arr1 + arr2) # [1, 2, 3, 4, 5, 6] print(arr1 * 2) # [1, 2, 3, 1, 2, 3] @@ -60,19 +60,17 @@ print(arr1 * 2) # [1, 2, 3, 1, 2, 3] ```py linenums="1" title="array-standard-library.rn" -include Array # Include the Array standard library +import Array # Include the Array standard library # Create an array instance using the Array class -var arr = Array([1, 2, 3, 4, 5]) +arr = Array([1, 2, 3, 4, 5]) -print(arr.len()) # 5 +print(len(arr)) # 5 print(arr.is_empty()) # false print(arr.to_string()) # "[1, 2, 3, 4, 5]" print(arr.is_array()) # true -print(arr.map(fun (item) { - return str(item) -})) # ["1", "2", "3", "4", "5"] +print(arr.map(fun (item) -> str(item))) # ["1", "2", "3", "4", "5"] print(arr.append(6)) # [1, 2, 3, 4, 5, 6] print(arr.pop(5)) # [1, 2, 3, 4, 5] diff --git a/docs/built-in-functions.md b/docs/built-in-functions.md index 28881f3..ad3269e 100644 --- a/docs/built-in-functions.md +++ b/docs/built-in-functions.md @@ -10,7 +10,13 @@ Built-in functions are the functions that are built into the language. They are - `clear()` - clears the screen - `exit()` - exits the program -### same as include statement +### Shell commands + +- `help(obj)` - get help about any object +- `license()` - show project license +- `credits()` - show project credits + +### Same as `import` statement - `require()` - same as include statement to include a file or library in the current program @@ -20,7 +26,7 @@ Built-in functions are the functions that are built into the language. They are ### API methods -- `pyapi(string)` - A high-level Python API for Radon. It is used to call Python functions from Radon. (Development) +- `pyapi(string,ns)` - A high-level Python API for Radon. It is used to call Python functions from Radon. ### Typecase methods diff --git a/docs/classes.md b/docs/classes.md index 1830007..8c1e796 100644 --- a/docs/classes.md +++ b/docs/classes.md @@ -21,7 +21,7 @@ class Person { Now that we have created a class, we can create an object. We can create an object by simply calling the class like as a function. It is followed by the name of the class and the arguments in parentheses. The arguments are optional. ```js linenums="1" title="objects.rn" -var person = Person() +person = Person() ``` ## Fields @@ -30,8 +30,8 @@ Fields are the variables that are declared inside a class. They are used to stor ```js linenums="1" title="fields.rn" class Person { - var name = "John" - var age = 20 + name = "John" + age = 20 } ``` @@ -42,12 +42,12 @@ Constructors are special methods that are used to initialize the fields of a cla ```js linenums="1" title="constructors.rn" class Person { fun Person(name, age) { - var this.name = name - var this.age = age + this.name = name + this.age = age } } -var person = Person("John", 20) +person = Person("John", 20) ``` ## Methods @@ -56,9 +56,9 @@ Methods are the functions that are declared inside a class. They are used to def ```py linenums="1" title="methods.rn" class Person { - fun Person(name, age) { - var this.name = name - var this.age = age + fun __constructor__(name, age) { + this.name = name + this.age = age } fun sayHello() { @@ -66,7 +66,7 @@ class Person { } } -var person = Person("John", 20) +person = Person("John", 20) person.sayHello() # Output: Hello, John! ``` diff --git a/docs/data-types.md b/docs/data-types.md index c5f5979..2de32ad 100644 --- a/docs/data-types.md +++ b/docs/data-types.md @@ -14,29 +14,29 @@ The basic types are: Arrays are declared using the `[]` syntax. The type of the array is the type of the elements it contains. ```js linenums="1" title="arrays.rn" -var a = [1, 2, 3] // a is an array of ints -var b = [1.0, 2.0, 3.0] // b is an array of floats -var c = ["a", "b", "c"] // c is an array of strings +a = [1, 2, 3] // a is an array of ints +b = [1.0, 2.0, 3.0] // b is an array of floats +c = ["a", "b", "c"] // c is an array of strings // Arrays can be nested -var d = [[1, 2], [3, 4]] // d is an array of arrays of ints +d = [[1, 2], [3, 4]] // d is an array of arrays of ints // Arrays can be empty -var e = [] // e is an empty array of unknown type +e = [] // e is an empty array of unknown type ``` -## Objects (Development) +## Hashmaps -Objects are declared using the `{}` syntax. The type of the object is the type of the fields it contains. +Hashmaps (or objects) are declared using the `{}` syntax. The type of the object is the type of the fields it contains. ```js linenums="1" title="objects.rn" -var a = {x: 1, y: 2} // a is an object with fields x and y of type int -var b = {x: 1.0, y: 2.0} // b is an object with fields x and y of type float -var c = {x: "a", y: "b"} // c is an object with fields x and y of type string +a = {x: 1, y: 2} // a is an object with fields x and y of type int +b = {x: 1.0, y: 2.0} // b is an object with fields x and y of type float +c = {x: "a", y: "b"} // c is an object with fields x and y of type string // Objects can be nested -var d = {x: {y: 1, z: 2}, w: {y: 3, z: 4}} // d is an object with fields x and w of type object +d = {x: {y: 1, z: 2}, w: {y: 3, z: 4}} // d is an object with fields x and w of type object // Objects can be empty -var e = {} // e is an empty object of unknown type +e = {} // e is an empty object of unknown type ``` diff --git a/docs/error-handling.md b/docs/error-handling.md new file mode 100644 index 0000000..9656db6 --- /dev/null +++ b/docs/error-handling.md @@ -0,0 +1,106 @@ +# Error Handling + +In Radon, error handling is an important part of writing maintainable code. Radon provides a structured way to handle errors using `try-catch` blocks and allows raising errors using the `raise` keyword. + +## Try-Catch Block +The `try-catch` block in Radon is used to handle exceptions that may occur during the execution of a block of code. + +### Syntax +```js linenums="1" title="try-catch.rn" +try +{ + # Code that might throw an exception +} +catch as e +{ + # Code to handle the exception + # e is a string containing the error message + print(e) +} +``` + +### Example +```js linenums="1" title="try-catch.rn" +try +{ + # Attempt to open a file that does not exist + file = File("nonexistentfile.txt","r") +} +catch as e +{ + # Handle the error by printing the error message + print("An error occurred: " + e) +} +``` + +## Raising Errors +In Radon, errors can be raised explicitly using the `raise` keyword. This is useful for enforcing certain conditions or for creating custom error messages. + +Radon has a standard `radiation` module for Error Types (you can type `radiation.errors` in the shell to view a list of available error types) + +### Syntax +```js linenums="1" title="raise.rn" +import radiation + +# Raise an error with a custom message +raise radiation.TypeError("Input must be an integer") + +# Raise an error without a custom message +raise radiation.TypeError +``` + +### Example +```js linenums="1" title="raise.rn" +import radiation + +# Function that only accepts integers +fun checkInteger(input) +{ + if not is_int(input) + { + raise radiation.TypeError("Input must be an integer") + } +} + +try +{ + checkInteger("string") +} +catch as e +{ + print(e) # Output: Input must be an integer +} +``` + +## Custom Error Messages +Radon allows the creation of custom error messages, which can be particularly useful for providing more context-specific error information. + +### Syntax +```js linenums="1" title="raise.rn" +fun ArgError(arg) +{ + return "Argument `" + arg + "` is invalid!" +} +``` + +### Example +```js linenums="1" title="raise.rn" +import radiation + +fun validateArg(arg) +{ + if arg == "bad_arg" + { + raise radiation.ArgError(arg) + } +} + +try +{ + validateArg("bad_arg") +} +catch as e +{ + print(e) # Output: Argument `bad_arg` is invalid! +} +``` \ No newline at end of file diff --git a/docs/index.md b/docs/index.md index 5bbcfc4..5dade82 100644 --- a/docs/index.md +++ b/docs/index.md @@ -52,9 +52,9 @@ Radon is a programming language that is designed to be easy to learn and use. It # This is a Radon test file for the Radon Programming Language. class Network { - fun Network(username, password) { - var this.username = username - var this.password = password + fun __constructor__(username, password) { + this.username = username + this.password = password } fun login() { @@ -68,8 +68,8 @@ class Network { } } -var username = input("Enter you username: ") -var password = input("Enter your password: ") +username = input("Enter you username: ") +password = input("Enter your password: ") var network = Network(username, password) network.login() diff --git a/docs/modules.md b/docs/modules.md index 53de393..da2c070 100644 --- a/docs/modules.md +++ b/docs/modules.md @@ -10,7 +10,7 @@ A module is created by creating a file with the `.rn` extension. The file name i ```py linenums="1" title="Hello.rn" class Hello { - fun Hello() { + fun __constructor__() { print("Hello, World!") } } @@ -21,6 +21,6 @@ class Hello { A module is imported by using the `include` keyword. It is followed by the name of the module. The name of the module should have to be in Pascal Case `PascalCase`. ```py linenums="1" title="importing.rn" -include Hello +import Hello ``` diff --git a/docs/standard-library.md b/docs/standard-library.md index 5a15027..9e7bc01 100644 --- a/docs/standard-library.md +++ b/docs/standard-library.md @@ -5,12 +5,14 @@ ```text * ├── stdlib -│ ├── Argparser.rn -│ ├── Array.rn -│ ├── Math.rn -│ ├── String.rn -│ ├── System.rn -│ └── Winlib.rn +│ ├── argparser.rn +│ ├── array.rn +│ ├── colorlib.rn +│ ├── math.rn +│ ├── radiation.rn +│ ├── string.rn +│ ├── system.rn +│ └── winlib.rn ``` ... and more to come! Under development. \ No newline at end of file diff --git a/docs/strings.md b/docs/strings.md index a354ff2..044e927 100644 --- a/docs/strings.md +++ b/docs/strings.md @@ -7,7 +7,7 @@ - `str_slice(string, start, end)` - returns the substring from the specified start index to the specified end index ```py linenums="1" title="methods.rn" -var str = "Hello, World!" +str = "Hello, World!" print(str_len(str)) # 13 print(str_find(str, 0)) # H @@ -22,7 +22,7 @@ print(str_slice(str, 0, 5)) # Hello - `*` (repetition) ```py linenums="1" title="operators.rn" -var str = "Hello, World!" +str = "Hello, World!" print(str + " " + "Hello, World!") # Hello, World! Hello, World! print(str * 2) # Hello, World!Hello, World! diff --git a/mkdocs.yml b/mkdocs.yml index 942569f..671b71d 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -112,7 +112,7 @@ nav: - Classes: classes.md - Modules: modules.md - Input/Output: input-output.md - # - Exceptions: exceptions.md + - Error Handling: error-handling.md # - File Handling: file-handling.md - Strings: strings.md - Arrays: arrays.md From e32b1ff8ec30b5be18da0ffcda2a872f4d36a1e3 Mon Sep 17 00:00:00 2001 From: Vardan Petrosyan Date: Fri, 31 May 2024 23:41:04 +0400 Subject: [PATCH 2/6] Update error-handling.md --- docs/error-handling.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/error-handling.md b/docs/error-handling.md index 9656db6..c2503f2 100644 --- a/docs/error-handling.md +++ b/docs/error-handling.md @@ -50,7 +50,7 @@ raise radiation.TypeError ``` ### Example -```js linenums="1" title="raise.rn" +```js linenums="1" title="input-check.rn" import radiation # Function that only accepts integers @@ -76,7 +76,7 @@ catch as e Radon allows the creation of custom error messages, which can be particularly useful for providing more context-specific error information. ### Syntax -```js linenums="1" title="raise.rn" +```js linenums="1" title="ArgError.rn" fun ArgError(arg) { return "Argument `" + arg + "` is invalid!" @@ -84,7 +84,7 @@ fun ArgError(arg) ``` ### Example -```js linenums="1" title="raise.rn" +```js linenums="1" title="arg-validation.rn" import radiation fun validateArg(arg) From f9c6121805520639c975f58280bfbae7af45e3f0 Mon Sep 17 00:00:00 2001 From: "Md. Almas Ali" <57622296+Almas-Ali@users.noreply.github.com> Date: Sat, 1 Jun 2024 02:18:30 +0600 Subject: [PATCH 3/6] fix: line length. --- docs/built-in-functions.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/built-in-functions.md b/docs/built-in-functions.md index fbb97a9..2a9b42d 100644 --- a/docs/built-in-functions.md +++ b/docs/built-in-functions.md @@ -31,7 +31,8 @@ They are: ### API methods -- `pyapi(string,ns)` - A high-level Python API for Radon. It is used to call Python functions from Radon. +- `pyapi(string,ns)` - A high-level Python API for Radon. + It is used to call Python functions from Radon. ### Typecase methods From fb320498ceb4e8e68304f7f903a5e23bc2a7c78f Mon Sep 17 00:00:00 2001 From: Vardan Petrosyan Date: Sat, 1 Jun 2024 23:18:30 +0400 Subject: [PATCH 4/6] Merged error-handling with exceptions --- docs/error-handling.md | 106 ----------------------------------------- docs/exceptions.md | 52 +++++++++++++++++--- 2 files changed, 45 insertions(+), 113 deletions(-) delete mode 100644 docs/error-handling.md diff --git a/docs/error-handling.md b/docs/error-handling.md deleted file mode 100644 index c2503f2..0000000 --- a/docs/error-handling.md +++ /dev/null @@ -1,106 +0,0 @@ -# Error Handling - -In Radon, error handling is an important part of writing maintainable code. Radon provides a structured way to handle errors using `try-catch` blocks and allows raising errors using the `raise` keyword. - -## Try-Catch Block -The `try-catch` block in Radon is used to handle exceptions that may occur during the execution of a block of code. - -### Syntax -```js linenums="1" title="try-catch.rn" -try -{ - # Code that might throw an exception -} -catch as e -{ - # Code to handle the exception - # e is a string containing the error message - print(e) -} -``` - -### Example -```js linenums="1" title="try-catch.rn" -try -{ - # Attempt to open a file that does not exist - file = File("nonexistentfile.txt","r") -} -catch as e -{ - # Handle the error by printing the error message - print("An error occurred: " + e) -} -``` - -## Raising Errors -In Radon, errors can be raised explicitly using the `raise` keyword. This is useful for enforcing certain conditions or for creating custom error messages. - -Radon has a standard `radiation` module for Error Types (you can type `radiation.errors` in the shell to view a list of available error types) - -### Syntax -```js linenums="1" title="raise.rn" -import radiation - -# Raise an error with a custom message -raise radiation.TypeError("Input must be an integer") - -# Raise an error without a custom message -raise radiation.TypeError -``` - -### Example -```js linenums="1" title="input-check.rn" -import radiation - -# Function that only accepts integers -fun checkInteger(input) -{ - if not is_int(input) - { - raise radiation.TypeError("Input must be an integer") - } -} - -try -{ - checkInteger("string") -} -catch as e -{ - print(e) # Output: Input must be an integer -} -``` - -## Custom Error Messages -Radon allows the creation of custom error messages, which can be particularly useful for providing more context-specific error information. - -### Syntax -```js linenums="1" title="ArgError.rn" -fun ArgError(arg) -{ - return "Argument `" + arg + "` is invalid!" -} -``` - -### Example -```js linenums="1" title="arg-validation.rn" -import radiation - -fun validateArg(arg) -{ - if arg == "bad_arg" - { - raise radiation.ArgError(arg) - } -} - -try -{ - validateArg("bad_arg") -} -catch as e -{ - print(e) # Output: Argument `bad_arg` is invalid! -} -``` \ No newline at end of file diff --git a/docs/exceptions.md b/docs/exceptions.md index a1f3702..0981adc 100644 --- a/docs/exceptions.md +++ b/docs/exceptions.md @@ -1,17 +1,18 @@ # Exceptions -Radon has a powerfull exception handler. It can handle exceptions and errors in -the program. It can also throw exceptions and errors. +Error handling is an important part of writing maintainable code. +Radon has a powerful exception handler. It can handle exceptions that +may occur during the execution of a block of code. ## Handling exceptions -To handle exceptions, we use the `try` and `catch` blocks. The `try` block +In Radon, `try-catch` blocks are used for error handling. The `try` block contains the code that may throw an exception. The `catch` block contains the code that handles the exception. ```js linenums="1" title="exceptions.rn" try { - // code that may throw an exception + // code that may throw an exception (in this case, zero division) a = 1 / 0 } catch as err { // code that handles the exception @@ -47,9 +48,9 @@ Exception caught ## Raise exceptions -To raise an exception, we use the `raise` keyword followed by the exception -type and the message. We have builtin exceptions in `radiation` module. We can -use them to raise exceptions. +In Radon, errors can be raised explicitly using the `raise` keyword. This is useful for enforcing certain conditions or for creating custom error messages. + +Radon has a standard `radiation` module for Error Types (you can type `radiation.errors` in the shell to view a list of available error types) ```js linenums="1" title="exceptions.rn" import radiation @@ -71,6 +72,43 @@ ValueError: 2 + 2 != 4 ``` +## Defining custom error types + +To define custom errors, you need to define a function that returns a string as the +error message. + +## Example + +```js linenums="1" title="arg-error.rn" +fun FunctionError(func, base_err) -> "Something went wrong in " + func + ": " + base_err + +fun risky_operation(y) +{ + try + { + x = 10 / y + } + catch as e + { + raise FunctionError("risky_operation", e) + } +} + +risky_operation(0) +``` + +**Output:** + +```py +Radiation (most recent call last): + File , line 10 +FunctionError: Something went wrong in risky_operation: Division by zero + + raise FunctionError("risky_operation", e) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +``` + When the exception is raised, the program stops executing and the exception is propagated up the call stack. The exception can be caught by a `try` block. If the exception is not caught, the program stops executing and the exception From 58e82852d355a896cb3e9edb0becd235f0af101f Mon Sep 17 00:00:00 2001 From: Vardan Petrosyan Date: Sat, 1 Jun 2024 23:20:17 +0400 Subject: [PATCH 5/6] Renamed `Exceptions` to `Error Handling` --- docs/{exceptions.md => error-handling.md} | 2 +- mkdocs.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename docs/{exceptions.md => error-handling.md} (99%) diff --git a/docs/exceptions.md b/docs/error-handling.md similarity index 99% rename from docs/exceptions.md rename to docs/error-handling.md index 0981adc..a4586f4 100644 --- a/docs/exceptions.md +++ b/docs/error-handling.md @@ -1,4 +1,4 @@ -# Exceptions +# Error Handling Error handling is an important part of writing maintainable code. Radon has a powerful exception handler. It can handle exceptions that diff --git a/mkdocs.yml b/mkdocs.yml index 2cf3f75..fa7315a 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -112,7 +112,7 @@ nav: - Classes: classes.md - Modules: modules.md - Input/Output: input-output.md - - Exceptions: exceptions.md + - Error Handling: error-handling.md - File Handling: file-handling.md - Strings: strings.md - Arrays: arrays.md From d8883a6a6ad859ad04a45d575a8869177c4c7d71 Mon Sep 17 00:00:00 2001 From: Vardan Petrosyan Date: Sun, 2 Jun 2024 00:12:11 +0400 Subject: [PATCH 6/6] Update error-handling.md --- docs/error-handling.md | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/docs/error-handling.md b/docs/error-handling.md index a4586f4..0565fe5 100644 --- a/docs/error-handling.md +++ b/docs/error-handling.md @@ -79,22 +79,12 @@ error message. ## Example -```js linenums="1" title="arg-error.rn" -fun FunctionError(func, base_err) -> "Something went wrong in " + func + ": " + base_err - -fun risky_operation(y) -{ - try - { - x = 10 / y - } - catch as e - { - raise FunctionError("risky_operation", e) - } +```js linenums="1" title="custom-error.rn" +fun CustomError(file) { + return "Something went wrong in " + file } -risky_operation(0) +raise CustomError("custom-error.rn") ``` **Output:** @@ -102,10 +92,10 @@ risky_operation(0) ```py Radiation (most recent call last): File , line 10 -FunctionError: Something went wrong in risky_operation: Division by zero +FunctionError: Something went wrong in risky_operation - raise FunctionError("risky_operation", e) - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + raise FunctionError("risky_operation") + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ```