Skip to content

Commit

Permalink
Merge pull request #4 from Vardan2009/master
Browse files Browse the repository at this point in the history
Updated Docs + Error Handling Page
  • Loading branch information
Almas-Ali authored Jun 1, 2024
2 parents 0f24822 + d8883a6 commit 66c939b
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 43 deletions.
14 changes: 6 additions & 8 deletions docs/arrays.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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]
Expand Down
12 changes: 9 additions & 3 deletions docs/built-in-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

Expand Down
8 changes: 4 additions & 4 deletions docs/classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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!
```
19 changes: 8 additions & 11 deletions docs/data-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 36 additions & 8 deletions docs/exceptions.md → docs/error-handling.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 <stdin>, 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
Expand Down
4 changes: 2 additions & 2 deletions docs/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
}
}
Expand All @@ -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
```
14 changes: 8 additions & 6 deletions docs/standard-library.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 1 addition & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 66c939b

Please sign in to comment.