Skip to content

Commit cbbd39f

Browse files
authored
Merge pull request #9 from angelcaru/update-docs
Update the docs
2 parents 1263db4 + 6d5f36d commit cbbd39f

File tree

10 files changed

+115
-88
lines changed

10 files changed

+115
-88
lines changed

docs/arrays.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
start index to the specified end index
1414

1515
```rn linenums="1" title="methods.rn"
16-
arr = [1, 2, 3, 4, 5]
16+
const arr = [1, 2, 3, 4, 5]
1717
print(arr_len(arr)) # 5
1818
1919
arr_push(arr, 6)
@@ -40,8 +40,8 @@ print(arr_slice(arr, 0, 5)) # [1, 2, 3, 4, 5]
4040
- `*` (repetition)
4141

4242
```rn linenums="1" title="operators.rn"
43-
arr1 = [1, 2, 3]
44-
arr2 = [4, 5, 6]
43+
const arr1 = [1, 2, 3]
44+
const arr2 = [4, 5, 6]
4545
4646
print(arr1 + arr2) # [1, 2, 3, 4, 5, 6]
4747
print(arr1 * 2) # [1, 2, 3, 1, 2, 3]

docs/classes.md

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Object Oriented Programming (OOP) is a programming paradigm that uses objects
66
and their interactions to design and program applications. It is based on the
77
concept of objects rather than just functions and procedures. These objects
88
are organized into classes, which allow individual objects to be grouped
9-
together. Most modern programming languages including Java, C/C++, and Python
9+
together. Most modern programming languages including Java, C++, and Python
1010
are object-oriented languages, and many older programming languages now have
1111
object-oriented versions.
1212

@@ -32,42 +32,22 @@ object by simply calling the class like as a function. It is followed by the
3232
name of the class and the arguments in parentheses. The arguments are optional.
3333

3434
```rn linenums="1" title="objects.rn"
35-
person = Person()
35+
const person = Person()
3636
```
3737

3838
## Fields
3939

4040
Fields are the variables that are declared inside a class. They are used to
4141
store data. They are also called instance variables because they are unique
42-
to each instance of the class. They are declared using the `var` keyword. It
43-
is followed by the name of the field and the type of the field.
44-
The type of the field is optional.
42+
to each instance of the class. They are declared like any variable.
4543

4644
```rn linenums="1" title="fields.rn"
4745
class Person {
48-
name = "John"
49-
age = 20
46+
var name = "John"
47+
var age = 20
5048
}
5149
```
5250

53-
## Constructors
54-
55-
Constructors are special methods that are used to initialize the fields of a
56-
class. They are called when an object is created. They are declared using the
57-
`fun` keyword. It is followed by the name (class name) of the constructor and
58-
the parameters in parentheses. The parameters are optional.
59-
60-
```rn linenums="1" title="constructors.rn"
61-
class Person {
62-
fun Person(name, age) {
63-
this.name = name
64-
this.age = age
65-
}
66-
}
67-
68-
person = Person("John", 20)
69-
```
70-
7151
## Methods
7252

7353
Methods are the functions that are declared inside a class. They are used to
@@ -90,3 +70,27 @@ class Person {
9070
person = Person("John", 20)
9171
person.say_hello() # Output: Hello, John!
9272
```
73+
74+
## Magic Methods/Operator Overloading
75+
You may have noticed we declared a method called `__constructor__` in the above example. This is an example of a magic method. Magic methods are used for operator overloading. Here is an incomplete list:
76+
77+
| Method Name | Operator | Example | Example if we used regular function calls instead of operators |
78+
|-------------|----------|---------|----------------------------------------------------------------|
79+
`__constructor__` | Class instantiation | `var foo = Foo(1, 2, 3)` | `var foo = create(Foo); foo.__constructor(1, 2, 3)`[^create_func] |
80+
`__add__` | Addition | `a + b` | `a.__add__(b)` |
81+
`__sub__` | Subtraction | `a - b` | `a.__sub__(b)` |
82+
`__mul__` | Multiplication | `a * b` | `a.__mul__(b)` |
83+
`__div__` | Division | `a / b` | `a.__div__(b)` |
84+
`__pow__` | Exponentiation | `a ^ b` | `a.__pow__(b)` |
85+
`__eq__` | Equality | `a == b` | `a.__eq__(b)` |
86+
`__ne__` | Non-equality | `a != b` | `a.__ne__(b)` |
87+
`__call__` | Calling | `f(1, 2, 3)` | `f.__call__(1, 2, 3)` |
88+
`__getitem__` | Subscripting | `a[b]` | `a.__getitem__(b)` |
89+
`__setitem__` | Subscripting | `a[b] = c` | `a.__setitem(b, c)` |
90+
`__contains__` | `in` | `a in b` | `b.__contains__(a)` |
91+
`__truthy__` | Implicit conversions to bool | `if x { ... }` | `if x.__truthy__() { ... }`[^truthy_errors][^truthy_recursion]
92+
93+
94+
[^create_func]: `create` doesn't actually exist. It's just pseudocode
95+
[^truthy_errors]: If `__truthy__` throws an error, it is ignored and treated as if it returned `false`
96+
[^truthy_recursion]: The `__truthy__` operator of the returned object will be called recursively until it is a `bool`

docs/data-types.md

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
The basic types are:
66

7-
- `int` - integer number.
8-
- `float` - floating point number.
7+
- `number` - floating point number
98
- `bool` - boolean value.
109
- `string` - string of characters.
1110

@@ -15,8 +14,7 @@ Arrays are declared using the `[]` syntax. The type of the array is the type
1514
of the elements it contains.
1615

1716
```rn linenums="1" title="arrays.rn"
18-
a = [1, 2, 3] # a is an array of ints
19-
b = [1.0, 2.0, 3.0] # b is an array of floats
17+
a = [1, 2, 3] # a is an array of numbers
2018
c = ["a", "b", "c"] # c is an array of strings
2119
2220
# Arrays can be nested
@@ -28,22 +26,21 @@ e = [] # e is an empty array of unknown type
2826

2927
## Hashmaps
3028

31-
Hashmaps (or objects) are declared using the `{}` syntax.
32-
The type of the object is the type of the fields it contains.
29+
Hashmaps are declared using the `{}` syntax.
3330

34-
```rn linenums="1" title="objects.rn"
35-
# a is an object with fields x and y of type int
36-
a = { x: 1, y: 2 };
37-
# b is an object with fields x and y of type float
38-
b = { x: 1.0, y: 2.0 };
39-
# c is an object with fields x and y of type string
40-
c = { x: "a", y: "b" };
31+
```rn linenums="1" title="hashmaps.rn"
32+
const a = { "x": 1, "y": 2 }
33+
const b = { "x": 1.0, "y": 2.0 }
34+
const c = { "x": "a", "y": "b" }
4135
42-
# Objects can be nested
43-
# d is an object with fields x and w of type object
44-
d = { x: { y: 1, z: 2 }, w: { y: 3, z: 4 } };
36+
# Hashmaps can be nested
37+
const d = { "x": { "y": 1, "z": 2 }, "w": { "y": 3, "z": 4 } }
4538
46-
# Objects can be empty
47-
# e is an empty object of unknown type
48-
e = {};
39+
# Hashmaps can be empty
40+
const e = {}
41+
42+
# Hashmaps can be initialized with keys known at runtime
43+
const key = "foo"
44+
const f = {key: "bar"}
45+
print(f["foo"]) # -> bar
4946
```

docs/error-handling.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ the code that handles the exception.
1313
```rn linenums="1" title="exceptions.rn"
1414
try {
1515
# code that may throw an exception (in this case, zero division)
16-
a = 1 / 0
16+
1 / 0
1717
} catch as err {
1818
# code that handles the exception
1919
print("Exception caught: " + err)
@@ -33,7 +33,7 @@ use the exception message, you can omit the variable.
3333
```rn linenums="1" title="exceptions.rn"
3434
try {
3535
# code that may throw an exception
36-
a = 1 / 0
36+
1 / 0
3737
} catch as _ {
3838
# code that handles the exception
3939
print("Exception caught")
@@ -65,10 +65,10 @@ if 2 != 4 {
6565
```rn
6666
Radiation (most recent call last):
6767
File <stdin>, line 2
68-
ValueError: 2 + 2 != 4
68+
ValueError: 2 != 4
6969
70-
raise radiation.ValueError("2 + 2 != 4")
71-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
70+
raise radiation.ValueError("2 != 4")
71+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7272
7373
```
7474

@@ -92,7 +92,7 @@ raise CustomError("custom-error.rn")
9292
```rn
9393
Radiation (most recent call last):
9494
File <stdin>, line 5
95-
FunctionError: Something went wrong in custom-error.rn
95+
CustomError: Something went wrong in custom-error.rn
9696
9797
raise CustomError("custom-error.rn")
9898
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

docs/file-handling.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ read and write files in Radon.
1414

1515
When opening a file, we can specify the mode in which we want to open the file.
1616

17-
The modes are:
17+
The modes are the same as in C fopen() or Python open():
1818

1919
- `r`: Read mode. Opens the file for reading. The file must exist.
2020
- `w`: Write mode. Opens the file for writing. If the file does not exist,
@@ -39,24 +39,24 @@ instance of `File` class by passing the file path to the constructor. We can
3939
then use the `read` method to read the contents of the file.
4040

4141
```rn linenums="1" title="file-handling.rn"
42-
file = File("file.txt")
43-
content = file.read()
42+
const file = File("file.txt")
43+
const content = file.read()
4444
print(content)
4545
```
4646

4747
You can also read the file line by line using the `readline` method.
4848

4949
```rn linenums="1" title="file-handling.rn"
50-
file = File("file.txt")
51-
line = file.readline()
50+
const file = File("file.txt")
51+
const line = file.readline()
5252
print(line)
5353
```
5454

5555
You can also read all the lines of the file using the `readlines` method.
5656

5757
```rn linenums="1" title="file-handling.rn"
58-
file = File("file.txt")
59-
lines = file.readlines()
58+
const file = File("file.txt")
59+
const lines = file.readlines()
6060
print(lines)
6161
```
6262

@@ -66,7 +66,7 @@ To write to a file, we use the `write` method. We can pass the content to the
6666
`write` method to write to the file.
6767

6868
```rn linenums="1" title="file-handling.rn"
69-
file = File("file.txt")
69+
const file = File("file.txt", "w")
7070
file.write("Hello, World!")
7171
```
7272

@@ -76,7 +76,7 @@ After reading or writing to a file, it is important to close the file. We can
7676
use the `close` method to close the file.
7777

7878
```rn linenums="1" title="file-handling.rn"
79-
file = File("file.txt")
79+
const file = File("file.txt", "r")
8080
# code that reads or writes to the file
8181
file.close()
8282
```

docs/functions.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fun add(a, b) {
2424
### Anonymous functions
2525

2626
```rn linenums="1" title="anonymous_functions.rn"
27-
add = fun (a, b) {
27+
const add = fun (a, b) {
2828
return a + b
2929
}
3030
```
@@ -38,12 +38,17 @@ fun add(a, b) -> a + b
3838
## Calling functions
3939

4040
Calling a function is done by using the function name followed by the arguments
41-
in parentheses.
41+
in parentheses, like in all C-like languages.
4242

4343
```rn linenums="1" title="calling_functions.rn"
4444
add(1, 2) # Output: 3
4545
```
4646

47+
There are also keyword arguments:
48+
```rn linenums="1" title="kwargs.rn"
49+
add(a=34, b=35) # Output: 69
50+
```
51+
4752
That's it! You now know how to call functions in Radon.
4853

4954
## Function parameters
@@ -75,3 +80,18 @@ fun new_user(name="Guest") {
7580
new_user() # Output: Hello, Guest!
7681
new_user("World") # Output: Hello, World!
7782
```
83+
84+
## Variadic parameters
85+
```rn linenums="1" title="variadics.rn"
86+
fun sum(...nums) {
87+
var ret = 0
88+
for x in nums {
89+
ret += x
90+
}
91+
return ret
92+
}
93+
94+
sum(1, 2, 3) # Output: 6
95+
sum(1, 2, 3, 4) # Output: 10
96+
sum() # Output: 0
97+
```

docs/input-output.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ followed by the argument in parentheses. The argument are mandatory. It takes
1717
only one argument. We can concatenate data types using the `+` operator.
1818

1919
```rn linenums="1" title="input.rn"
20-
name = input("Enter your name: ")
20+
const name = input("Enter your name: ")
2121
print("Hello, " + name + "!") # Output: Hello, World!
2222
```

docs/loops.md

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ o
105105
Here we are using `HashMap`. The loop will run for each key in the `HashMap`.
106106

107107
```rn linenums="1" title="for_hashmap.rn"
108-
hash = {"name": "John", "age": 30}
108+
const hash = {"name": "John", "age": 30}
109109
for i in hash {
110110
print("Key: " + i)
111111
print("Value: " + hash[i])
@@ -127,10 +127,10 @@ With while loop we can specify the condition.
127127
The loop will run until the condition is true.
128128

129129
```rn linenums="1" title="while.rn"
130-
i = 0
130+
var i = 0
131131
while i < 5 {
132132
print(i)
133-
nonlocal i += 1
133+
i += 1
134134
}
135135
```
136136

@@ -144,9 +144,6 @@ while i < 5 {
144144
4
145145
```
146146

147-
We have used `nonlocal` keyword to update the value of `i` in the loop.
148-
If we don't use `nonlocal` then it will run into infinite loop.
149-
150147
## Loop control statements
151148

152149
In Radon we have 2 loop control statements.
@@ -229,14 +226,14 @@ for i=0 to 5 {
229226
While loop example:
230227

231228
```rn linenums="1" title="while_continue.rn"
232-
i = 0
229+
var i = 0
233230
while i < 5 {
234231
if i == 3 {
235-
nonlocal i += 1
232+
i += 1
236233
continue
237234
}
238235
print(i)
239-
nonlocal i += 1
236+
i += 1
240237
}
241238
```
242239

0 commit comments

Comments
 (0)