Skip to content

Commit

Permalink
Merge pull request #8 from Vardan2009/master
Browse files Browse the repository at this point in the history
Added custom syntax highlighting
  • Loading branch information
Almas-Ali authored Oct 28, 2024
2 parents 71f6046 + 67ff951 commit 1263db4
Show file tree
Hide file tree
Showing 16 changed files with 187 additions and 79 deletions.
6 changes: 3 additions & 3 deletions docs/arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,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"
```rn linenums="1" title="methods.rn"
arr = [1, 2, 3, 4, 5]
print(arr_len(arr)) # 5
Expand All @@ -39,7 +39,7 @@ print(arr_slice(arr, 0, 5)) # [1, 2, 3, 4, 5]
- `+` (concatenation)
- `*` (repetition)

```py linenums="1" title="operators.rn"
```rn linenums="1" title="operators.rn"
arr1 = [1, 2, 3]
arr2 = [4, 5, 6]
Expand All @@ -62,7 +62,7 @@ print(arr1 * 2) # [1, 2, 3, 1, 2, 3]
- `to_string()` - returns the string representation of the array
- `is_array()` - returns `true` if the value is an array, otherwise `false`

```py linenums="1" title="array-standard-library.rn"
```rn linenums="1" title="array-standard-library.rn"
import Array # Include the Array standard library
# Create an array instance using the Array class
Expand Down
60 changes: 60 additions & 0 deletions docs/assets/radon-highlight.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
var script1 = document.createElement('script');
script1.src = 'https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.4.0/highlight.min.js';
script1.defer = true;
script1.onload = function () {
hljs.registerLanguage('rn', function (hljs) {
return {
contains: [
{
className: 'keyword',
begin: '\\b(class|fun|if|else|true|false|null)\\b'
},
{
className: 'string',
begin: '"', end: '"',
contains: [
{
className: 'escape',
begin: '\\\\.', end: hljs.IMMEDIATE_RE
}
]
},
{
className: 'number',
begin: '\\b\\d+\\b'
},
{
className: 'special',
begin: '(?<=\\b(?:class|import|fun)\\s+)\\w+'
},
{
className: 'special',
begin: '\_\_constructor\_\_|this'
},
{
className: 'fncall',
begin: '(?<=\\b)(\\w+)\\s*\\('
},
{
className: 'fncall',
begin: '(\\)|\\()'
},
{
className: 'comment',
begin: '#',
end: '\\n'
},
{
className: 'identifier',
begin: '(\\w+)'
},
{
className: 'operator',
begin: '(\\+|-|=|!|not|and\\^)'
}
]
};
});
hljs.highlightAll();
};
document.head.appendChild(script1);
10 changes: 5 additions & 5 deletions docs/classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Let's start by creating a class. We can create a class using the `class`
keyword. It is followed by the name of the class and the body of the class.
The body of the class is enclosed in curly braces.

```py linenums="1" title="classes.rn"
```rn linenums="1" title="classes.rn"
class Person {
# Class body
}
Expand All @@ -31,7 +31,7 @@ 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"
```rn linenums="1" title="objects.rn"
person = Person()
```

Expand All @@ -43,7 +43,7 @@ to each instance of the class. They are declared using the `var` keyword. It
is followed by the name of the field and the type of the field.
The type of the field is optional.

```js linenums="1" title="fields.rn"
```rn linenums="1" title="fields.rn"
class Person {
name = "John"
age = 20
Expand All @@ -57,7 +57,7 @@ class. They are called when an object is created. They are declared using the
`fun` keyword. It is followed by the name (class name) of the constructor and
the parameters in parentheses. The parameters are optional.

```js linenums="1" title="constructors.rn"
```rn linenums="1" title="constructors.rn"
class Person {
fun Person(name, age) {
this.name = name
Expand All @@ -75,7 +75,7 @@ define the behavior of the class. They are declared using the `fun` keyword.
It is followed by the name of the method, the parameters in parentheses, and
the return type. The parameters and the return type are optional.

```py linenums="1" title="methods.rn"
```rn linenums="1" title="methods.rn"
class Person {
fun __constructor__(name, age) {
this.name = name
Expand Down
4 changes: 2 additions & 2 deletions docs/control-flow.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ statement is used to execute code if the condition is false. The `elif`
statement is used to execute code if the condition is false and another
condition is true. The `else` statement is optional.

```js linenums="1" title="conditional-statements.rn"
```rn linenums="1" title="conditional-statements.rn"
if true {
print("true")
Expand All @@ -17,7 +17,7 @@ if true {
}
```

```js linenums="1" title="conditional-statements.rn"
```rn linenums="1" title="conditional-statements.rn"
if true {
print("true")
Expand Down
32 changes: 16 additions & 16 deletions docs/data-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,36 @@ 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"
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
```rn 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
// Arrays can be nested
d = [[1, 2], [3, 4]] // d is an array of arrays of ints
# Arrays can be nested
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
# Arrays can be empty
e = [] # e is an empty array of unknown type
```

## Hashmaps

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
```rn linenums="1" title="objects.rn"
# a is an object with fields x and y of type int
a = { x: 1, y: 2 };
// b is an object with fields x and y of type float
# b is an object with fields x and y of type float
b = { x: 1.0, y: 2.0 };
// c is an object with fields x and y of type string
# c is an object with fields x and y of type string
c = { x: "a", y: "b" };
// Objects can be nested
// d is an object with fields x and w of type object
# Objects can be nested
# d is an object with fields x and w of type object
d = { x: { y: 1, z: 2 }, w: { y: 3, z: 4 } };
// Objects can be empty
// e is an empty object of unknown type
# Objects can be empty
# e is an empty object of unknown type
e = {};
```
20 changes: 10 additions & 10 deletions docs/error-handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ 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"
```rn linenums="1" title="exceptions.rn"
try {
// code that may throw an exception (in this case, zero division)
# code that may throw an exception (in this case, zero division)
a = 1 / 0
} catch as err {
// code that handles the exception
# code that handles the exception
print("Exception caught: " + err)
}
```
Expand All @@ -30,12 +30,12 @@ Don't forget to use the `as` keyword to assign the exception to a variable.
The variable can be used to get the exception message. If you don't want to
use the exception message, you can omit the variable.

```js linenums="1" title="exceptions.rn"
```rn linenums="1" title="exceptions.rn"
try {
// code that may throw an exception
# code that may throw an exception
a = 1 / 0
} catch as _ {
// code that handles the exception
# code that handles the exception
print("Exception caught")
}
```
Expand All @@ -52,7 +52,7 @@ In Radon, errors can be raised explicitly using the `raise` keyword. This is use

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"
```rn linenums="1" title="exceptions.rn"
import radiation
if 2 != 4 {
Expand All @@ -62,7 +62,7 @@ if 2 != 4 {

**Output:**

```py
```rn
Radiation (most recent call last):
File <stdin>, line 2
ValueError: 2 + 2 != 4
Expand All @@ -79,7 +79,7 @@ error message.

## Example

```js linenums="1" title="custom-error.rn"
```rn linenums="1" title="custom-error.rn"
fun CustomError(file) {
return "Something went wrong in " + file
}
Expand All @@ -89,7 +89,7 @@ raise CustomError("custom-error.rn")

**Output:**

```py
```rn
Radiation (most recent call last):
File <stdin>, line 5
FunctionError: Something went wrong in custom-error.rn
Expand Down
12 changes: 6 additions & 6 deletions docs/file-handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,23 @@ To manipulate files in Radon, we use built-in `File` class. We can create a new
instance of `File` class by passing the file path to the constructor. We can
then use the `read` method to read the contents of the file.

```py linenums="1" title="file-handling.rn"
```rn linenums="1" title="file-handling.rn"
file = File("file.txt")
content = file.read()
print(content)
```

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

```py linenums="1" title="file-handling.rn"
```rn linenums="1" title="file-handling.rn"
file = File("file.txt")
line = file.readline()
print(line)
```

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

```py linenums="1" title="file-handling.rn"
```rn linenums="1" title="file-handling.rn"
file = File("file.txt")
lines = file.readlines()
print(lines)
Expand All @@ -65,7 +65,7 @@ print(lines)
To write to a file, we use the `write` method. We can pass the content to the
`write` method to write to the file.

```py linenums="1" title="file-handling.rn"
```rn linenums="1" title="file-handling.rn"
file = File("file.txt")
file.write("Hello, World!")
```
Expand All @@ -75,15 +75,15 @@ file.write("Hello, World!")
After reading or writing to a file, it is important to close the file. We can
use the `close` method to close the file.

```py linenums="1" title="file-handling.rn"
```rn linenums="1" title="file-handling.rn"
file = File("file.txt")
# code that reads or writes to the file
file.close()
```

Check the file is closed or not using the `is_closed` method.

```py linenums="1" title="file-handling.rn"
```rn linenums="1" title="file-handling.rn"
file.is_closed()
```

Expand Down
12 changes: 6 additions & 6 deletions docs/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@ In Radon, there are three types of functions:

### Named functions

```js linenums="1" title="named_functions.rn"
```rn linenums="1" title="named_functions.rn"
fun add(a, b) {
return a + b
}
```

### Anonymous functions

```js linenums="1" title="anonymous_functions.rn"
```rn linenums="1" title="anonymous_functions.rn"
add = fun (a, b) {
return a + b
}
```

### One-liner functions

```js linenums="1" title="one_liner_functions.rn"
```rn linenums="1" title="one_liner_functions.rn"
fun add(a, b) -> a + b
```

Expand All @@ -40,7 +40,7 @@ fun add(a, b) -> a + b
Calling a function is done by using the function name followed by the arguments
in parentheses.

```py linenums="1" title="calling_functions.rn"
```rn linenums="1" title="calling_functions.rn"
add(1, 2) # Output: 3
```

Expand All @@ -54,7 +54,7 @@ The parameters are optional.

We can also leave out the parentheses if there are no parameters.

```py linenums="1" title="function_parameters.rn"
```rn linenums="1" title="function_parameters.rn"
fun say_hello(name) {
print("Hello, " + name + "!")
}
Expand All @@ -67,7 +67,7 @@ say_hello("World") # Output: Hello, World!
Default parameters are used to assign a default value to a parameter. If the
parameter is not passed, the default value is used.

```py linenums="1" title="default_parameters.rn"
```rn linenums="1" title="default_parameters.rn"
fun new_user(name="Guest") {
print("Hello, " + name + "!")
}
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Some of the features of Radon include:

## Login Logic

```js linenums="1" title="Login.rn"
```rn linenums="1" title="Login.rn"
# This is a Radon test file for the Radon Programming Language.
class Network {
Expand Down
Loading

0 comments on commit 1263db4

Please sign in to comment.