Skip to content

Commit

Permalink
docs + return fix + array get index + exponentiation
Browse files Browse the repository at this point in the history
  • Loading branch information
ImShyMike committed Dec 20, 2024
1 parent a72c119 commit ad3afdb
Show file tree
Hide file tree
Showing 27 changed files with 713 additions and 59 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,6 @@ cython_debug/

# PyPI upload script
upload.py

# Generated documentation website
docs/site
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# [![Eryx](https://github.com/ImShyMike/Eryx/blob/main/assets/eryx_small.png)][pypi_url]
[![Build Status](https://github.com/ImShyMike/Eryx/actions/workflows/python-package.yml/badge.svg)](https://github.com/ImShyMike/Eryx/actions/workflows/python-package.yml)
[![License](https://img.shields.io/pypi/l/Eryx)](https://github.com/ImShyMike/Eryx/blob/main/LICENSE)
[![License](https://img.shields.io/pypi/l/Eryx)][license]
[![PyPI](https://img.shields.io/pypi/v/Eryx)][pypi_url]
[![Python Version](https://img.shields.io/pypi/pyversions/Eryx)][pypi_url]
[![PyPI - Wheel](https://img.shields.io/pypi/wheel/Eryx)][pypi_url]

[pypi_url]: https://pypi.org/project/Eryx
[license]: https://github.com/ImShyMike/Eryx/blob/main/LICENSE

## What is Eryx?
Eryx is a decently fast simple dynamically typed programming language similar to javascript/python.
Expand Down Expand Up @@ -37,10 +38,10 @@ The list of subcommands is:
* **test**: Run the test suite

## Documentation
Coming soon...
Full documentation is available [here](https://ImShyMike.github.io/Eryx).

## Thanks
A huge thanks to [tylerlaceby](https://www.youtube.com/@tylerlaceby) for his ["Build a Custom Scripting Language In Typescript"](https://www.youtube.com/playlist?list=PL_2VhOvlMk4UHGqYCLWc6GO8FaPl8fQTh) playlist.

## License
This project is licensed under the [MIT License](LICENSE).
This project is licensed under the [MIT License][license].
Binary file added docs/docs/assets/eryx.ico
Binary file not shown.
Binary file added docs/docs/assets/eryx.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 57 additions & 0 deletions docs/docs/cli.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Using the CLI
The CLI is where all the eryx functionality is. With it you can [run programs](#running-a-program), [start the REPL](#starting-the-repl), [start the web playground](#starting-the-web-playground) and [run the all of the current version's tests](#running-the-tests).

## Check the current installed version
To run a check what version of Eryx you have installed, simply use:
```sh
eryx --version
# Eryx, version 0.1.3
```

## Running a program
To run a program use:
```sh
eryx run <filepath>
```
Supported debug arguments are:

* **--tokenize**: Print the tokenized code
* **--ast**: Print the AST (Abstract Syntax Tree)
* **--result**: Print the result of the code evaluation

## Starting the repl
To start the Eryx REPL (Read-Eval-Print-Loop) use:
```sh
eryx repl
```

This will start an interactive shell that you can use to run Eryx code:
```sh
Eryx v0.1.3
>
```

To exit the REPL use either `CTRL + C` or use the command `exit()`.

Supported debug arguments are:

* **--tokenize**: Print the tokenized code
* **--ast**: Print the AST
* **--result**: Print the result of the code evaluation

## Starting the web playground
The web playground can be used to run programs or use the REPL directly from your browser.
To start it use:

```sh
eryx playground [--ip ("0.0.0.0")] [--port (80)]
```
Default ip is `0.0.0.0` (all available network interfaces) and the default port is `80`.

## Running the tests
Eryx uses [pytest](https://pytest.org) for handling and running tests to make sure the language is working as expected.
To run said tests use:

```sh
eryx test
```
281 changes: 281 additions & 0 deletions docs/docs/examples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,281 @@
# Eryx examples
This page contains many example programs made using eryx and their corresponding output.

## Simple examples

### Variables
```C title="variables.eryx" linenums="1"
let a = 1;
let b = -2;
let c = 3.14;
let d = a;
let e = "Hello, World!";
let f = true;
let g = null;
let h = [1, 2, 3];
let i = {key: "value", num: 10};

print(a)
print(b)
print(c)
print(d)
print(e)
print(f)
print(g)
print(h)
print(i)
```
<details><summary>Output</summary>
```C linenums="1"
1
-2
3.14
1
Hello, World!
true
null
[ 1, 2, 3 ]
{ key: "value", num: 10 }
```
</details>

### Functions
```C title="functions.eryx" linenums="1"
func add(x, y) {
return x + y;
}

print(add(1, 2))
```
<details><summary>Output</summary>
```C linenums="1"
3
```
</details>

### If/Else statements
```C title="if_else.eryx" linenums="1"
let x = 10;
let y = 5;

if (x == y) {
print("x is equal y")
} else {
print("x is not equal y")
}

```
<details><summary>Output</summary>
```C linenums="1"
x is not equal y
```
</details>

### Arithmetic operations
```C title="arithmetic.eryx" linenums="1"
let x = 10;
let y = 5;

print(x + y)
print(x - y)
print(x * y)
print(x / y)
print(x % y)
print(x + y * 2 + x * (y + 2) - 5)
```
<details><summary>Output</summary>
```C linenums="1"
15
5
50
2
0
85
```
</details>

### Comparison operations
```C title="comparisons.eryx" linenums="1"
let x = 10;
let y = 5;

print(x == y)
print(x != y)
print(x < y)
print(x > y)
print(x <= y)
print(x >= y)
```
<details><summary>Output</summary>
```C linenums="1"
false
true
false
true
false
true
```
</details>

## Complex examples

### Factorial
Calculate the factorial of a number.
```C title="factorial.eryx" linenums="1"
func factorial(n) {
if (n <= 1) {
return 1;
}
return n * factorial(n - 1);
}

print(factorial(5))
```
<details><summary>Output</summary>
```C linenums="1"
120
```
</details>

### Fibonacci
Calculate the nth Fibonacci number.
```C title="fibonacci.eryx" linenums="1"
func fibonacci(n) {
if (n <= 0) {
return 0;
}
if (n == 1) {
return 1;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}

print(fibonacci(10))
```
<details> <summary>Output</summary>
```C linenums="1"
55
```
</details>

### GCD
Find the greatest common divisor of two numbers using Euclid's algorithm.
```C title="gcd.eryx" linenums="1"
func gcd(a, b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}

print(gcd(48, 18))
```
<details> <summary>Output</summary>
```C linenums="1"
6
```
</details>

### Prime Check
Check if a number is a prime number.
```C title="is_prime.eryx" linenums="1"
func isPrime(n, divisor) {
if (n <= 1) {
return false;
}
if (divisor == 1) {
return true;
}
if (n % divisor == 0) {
return false;
}
return isPrime(n, divisor - 1);
}

let number = 17;
print(isPrime(number, int(number ** 0.5)))
```
<details> <summary>Output</summary>
```C linenums="1"
true
```
</details>

### Offset adder
Create an adder with an offset then use it.
```C title="adder.eryx" linenums="1"
func makeAdder(offset) {
func add(x, y) {
return x + y + offset;
}

return add;
}

let adder = makeAdder(10);
print(adder(5, 10))
```
<details> <summary>Output</summary>
```C linenums="1"
25
```
</details>

### Sum digits
Sum all digits of a number.
```C title="sum_digits.eryx" linenums="1"
func sumOfDigits(n) {
if (n == 0) {
return 0;
}
return (n % 10) + sumOfDigits(int(n / 10));
}

print(sumOfDigits(12345))
```
<details> <summary>Output</summary>
```C linenums="1"
15
```
</details>

### Reverse number
Reverse the digits of a number.
```C title="reverse_number.eryx" linenums="1"
func reverseNumber(n, reversed) {
if (n == 0) {
return reversed;
}
reversed = reversed * 10 + (n % 10)
return reverseNumber(int(n / 10), reversed);
}

print(reverseNumber(12345, 0))
```
<details> <summary>Output</summary>
```C linenums="1"
54321
```
</details>

### Is sorted
Check if a list is sorted.
```C title="is_sorted.eryx" linenums="1"
func isSorted(arr, idx) {
if (idx == 0) {
return true;
}
if (arr[idx] < arr[idx - 1]) {
return false;
}
return isSorted(arr, idx - 1);
}

let nums = [1, 2, 3, 4, 5];
print(isSorted(nums, len(nums) - 1))
```
<details> <summary>Output</summary>
```C linenums="1"
true
```
</details>
Loading

0 comments on commit ad3afdb

Please sign in to comment.