Skip to content

Commit

Permalink
docs: ...
Browse files Browse the repository at this point in the history
  • Loading branch information
yazaldefilimone committed Aug 21, 2024
1 parent 429298f commit 5244620
Show file tree
Hide file tree
Showing 5 changed files with 250 additions and 229 deletions.
262 changes: 42 additions & 220 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,256 +1,78 @@
<samp>

**Stella** is a type checker for Lua. It helps you catch type errors before your code runs, making your Lua code safer and more reliable.
Stella is a type checker for Lua that adds TypeScript-like type safety to your code. It helps catch errors early, ensures your code runs smoothly, and works with your existing Lua code without requiring any changes.

### Installation

<table>
<tr>
<td><strong>Stella</strong></td>
<td><strong>Lua</strong></td>
</tr>
#### Install Dependencies

<tr>
<td>
##### On Linux or Mac

```lua
type Array<T> = {T}

function binary_search(arr: Array<number>, target: number): option<number>
local low: number = 1
local high: number = #arr

while low <= high do
local mid: number = math.floor((low + high) / 2)
local guess: number = arr[mid]

if guess == target then
return mid
elseif guess > target then
high = mid - 1
else
low = mid + 1
end
end

return nil
end

local numbers: Array<number> = {1, 3, 5, 7, 9, 11}
local result: option<number> = binary_search(numbers, 7)
```

</td>
<td>

```lua
function binary_search(arr, target)
local low = 1
local high = #arr

while low <= high do
local mid = math.floor((low + high) / 2)
local guess = arr[mid]

if guess == target then
return mid
elseif guess > target then
high = mid - 1
else
low = mid + 1
end
end

return nil
end

local numbers = {1, 3, 5, 7, 9, 11}
local result = binary_search(numbers, 7)
```sh
# Install Rust if you haven't already.
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
```

</td>
</tr>
</table>

##### Install Stella

```sh
# Install Stella
cargo install stellla_checker

1. Generics

<table>
<tr>
<td><strong>Stella</strong></td> <td><strong>Lua</strong></td>
</tr>

<tr>
<td>

```lua
type fn<T, R> = function(n: T, b: T): R;

local do_thing: fn<number, string> = function (n, b)
local a = n + 10
return "hei, stella checker :)"
end
```

</td>
<td>

```lua
local do_thing = function (n, b)
local a = n + 10
return "hei, stella checker :)"
end
# Check if Stella is installed correctly
stella --version
```

</td>
</tr>
</table>

2. Function Types

<table>
<tr>
<td><strong>Stella</strong></td> <td><strong>Lua</strong></td>
</tr>

<tr>
<td>
### Simple exemple!

```lua
type Apply<T> = function(num: T): T;

type ApplyTwiceType<T> = function(n: number, fn: Apply<T>): T;

local apply_twice: ApplyTwiceType<number> = function(num, fn)
print(fn)
return fn(fn(num))
end
type Fn<T, R> = function(param: T): R

local function inc(n: number): number
return n + 1
end

local result = apply_twice(3, inc)
```
type Array<T> = {T}

</td>
<td>
type ProcessListType<T> = function(list: Array<T>, apply_fn: Fn<T, T>): Array<T>

```lua
local apply_twice = function(num, fn)
print(fn)
return fn(fn(num))
local process_list: ProcessListType<number> = function(list, apply_fn)
local result = {}
for i = 1, #list do
table.insert(result, apply_fn(list[i]))
end
return result
end

local function inc(n)
return n + 1
end

local result = apply_twice(3, inc)
```

</td>
</tr>
</table>

3. optionals

<table>
<tr>
<td><strong>Stella</strong></td> <td><strong>Lua</strong></td>
</tr>

<tr>
<td>

```lua
function divide(a: number, b: number): option<number>
if b == 0 then
return nil
end
return a / b
local function increment(n: number): number
return n + 1
end

local result: option<number> = divide(10, 0)
```

</td>
<td>

```lua
function divide(a, b)
if b == 0 then
return nil
end
return a / b
local function double(n: number): number
return n * 2
end

local result = divide(10, 0)
```

</td>
</tr>
</table>
local numbers = {1, 2, 3, 4}

4. unions
-- Apply the 'increment' function to each number in the list
local incremented_numbers = process_list(numbers, increment) -- ok :)

<table>
<tr>
<td><strong>Stella</strong></td> <td><strong>Lua</strong></td>
</tr>
-- Apply the 'double' function to each number in the list
local doubled_numbers = process_list(numbers, double) -- ok :)

<tr>
<td>

```lua
type Either<T, U> = union<T, U>

function get_value(flag: boolean): Either<number, string>
if flag then
return 42
else
return "forty-two"
end
end
--- error
local numbers_error = {1, 2, 3, 4, "hello"}

local value: Either<number, string> = get_value(true)
```

</td>
<td>

```lua
function get_value(flag)
if flag then
return 42
else
return "forty-two"
end
end
-- ERROR >>> expected `table<number>`, found `table<string, number>`
-- in `numbers_error`
local incremented_numbers = process_list(numbers_error, increment)

local value = get_value(true)
```

</td>
</tr>
</table>

<samp>


```sh
cargo build --release
stella check process_list.lua

# Run the type checker
#

./stella check tests/golden_tests/nested_functions.lua


# you can see ast
#
./stella compile tests/golden_tests/nested_functions.lua
# let me know if you have any questions or suggestions :) I hope you have a amazing day!

```

- [Stella Virtual Machine](https://github.com/yazaldefilimone/stella-compiler)
- [A Quick Guide](./guide.md)

- [Stella Virtual Machine (maybe coming soon)](https://github.com/yazaldefilimone/stella-compiler)
Loading

0 comments on commit 5244620

Please sign in to comment.