diff --git a/docs/arrays.md b/docs/arrays.md index d512f22..7ead592 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 @@ -34,7 +34,7 @@ print(arr_find(arr, 1)) # 2 print(arr_slice(arr, 0, 5)) # [1, 2, 3, 4, 5] ``` -## Array operators (Development) +## Array operators - `+` (concatenation) - `*` (repetition) @@ -63,19 +63,17 @@ print(arr1 * 2) # [1, 2, 3, 1, 2, 3] - `is_array()` - returns `true` if the value is an array, otherwise `false` ```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 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 6799611..2a9b42d 100644 --- a/docs/built-in-functions.md +++ b/docs/built-in-functions.md @@ -14,7 +14,13 @@ 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. @@ -25,8 +31,8 @@ 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 cf5e6e4..97b6fde 100644 --- a/docs/classes.md +++ b/docs/classes.md @@ -32,7 +32,7 @@ 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" -person = Person(); +person = Person() ``` ## Fields @@ -77,16 +77,16 @@ the return type. The parameters and the return type are optional. ```py linenums="1" title="methods.rn" class Person { - fun Person(name, age) { + fun __constructor__(name, age) { this.name = name this.age = age } - fun sayHello() { + fun say_hello() { print("Hello, " + this.name + "!") } } person = Person("John", 20) -person.sayHello() # Output: Hello, John! +person.say_hello() # Output: Hello, John! ``` diff --git a/docs/data-types.md b/docs/data-types.md index 07a8d19..61a3938 100644 --- a/docs/data-types.md +++ b/docs/data-types.md @@ -15,24 +15,21 @@ 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" -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 +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 -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 -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" // a is an object with fields x and y of type int diff --git a/docs/exceptions.md b/docs/error-handling.md similarity index 54% rename from docs/exceptions.md rename to docs/error-handling.md index a1f3702..0565fe5 100644 --- a/docs/exceptions.md +++ b/docs/error-handling.md @@ -1,17 +1,18 @@ -# Exceptions +# Error Handling -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,33 @@ 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="custom-error.rn" +fun CustomError(file) { + return "Something went wrong in " + file +} + +raise CustomError("custom-error.rn") +``` + +**Output:** + +```py +Radiation (most recent call last): + File , line 10 +FunctionError: Something went wrong in risky_operation + + raise FunctionError("risky_operation") + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +``` + 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 diff --git a/docs/modules.md b/docs/modules.md index b1108b4..68b5dff 100644 --- a/docs/modules.md +++ b/docs/modules.md @@ -18,7 +18,7 @@ name should have to be in Pascal Case `PascalCase`. ```py linenums="1" title="Hello.rn" class Hello { - fun Hello() { + fun __constructor__() { print("Hello, World!") } } @@ -30,5 +30,5 @@ 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 98d28c3..6582b1a 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. 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