Skip to content

Commit

Permalink
Add readme and final spec for v0
Browse files Browse the repository at this point in the history
  • Loading branch information
Saverio976 committed Dec 20, 2023
1 parent 3424b98 commit 2e4e3a3
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 39 deletions.
49 changes: 10 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@

## Documentation

-- **Comentary**
- **Comentary**

```c
// This is a comment
```

-- **Variables Declaration**
- **Variables Declaration**

```hs
@Int a = 1;
@String b = "hello";
```

-- **Variables Assignment**
- **Variables Assignment**

```hs
a = 1;
Expand All @@ -33,17 +33,11 @@ b = "hello";
@Bool a = True;
@Bool b = False;
@Int c = 1;
@List[Int] d = [1, 2, 3];
@Char e = 'a';
@String f = "hello";
@List[Char] g = ['a', 'b', 'c'];
@StringView f = "hello";
```

- **Built-in Global Variables**

```c
@List[String] ARGS = ["programfilepath", "arg1", "arg2"];
```
There is a `Void` type that can be used to return nothing.

- **Function Declaration**

Expand All @@ -53,6 +47,11 @@ fn add(a: Int, b: Int) -> Int
// the next line is the `return`
<- a + b;
};

export fn sub(a: Int, b: Int) -> Int
{
<- a - b;
};
```

- **Function Call**
Expand Down Expand Up @@ -80,23 +79,6 @@ fn add(a: Int, b: Int, c: Int) -> Int
};
```

- **Built-in Functions**

```c
// print to stdout
print("hello");
// print to stderr
printErr("hello");
// get a line from stdin
getLine();
// transform a type to a string
str(1);
// get the type of a value in string format
type(a);
// call a function with string
call("add", [1, 2]);
```
- **Generic Functions**

```rust
Expand Down Expand Up @@ -141,17 +123,6 @@ while (i < 10)
};
```

```c
@List[Int] lst = [1, 2, 3];
foreach (a in lst)
{
if (a == 2)
{
break;
};
};
```
- **Imports**

```c
Expand Down
47 changes: 47 additions & 0 deletions docs/ByteCodeSpec.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,40 @@
0x01 0x00 0x00 0x00
```

## Type Section

```
0x01
```

### Function Type

```
0x60
```

#### Number of Parameters

- for 2 parameters
```
0x02
```
#### Parameters Type
```
0x7F: i32
0x7D: f32
```
#### Return Type
```
0x7F: i32
0x7D: f32
0x00: void
```
## Function Section
```
Expand Down Expand Up @@ -176,3 +210,16 @@ else, push `0`
Jump to the label
Label is a number
### Operations
#### i32
##### i32.add
```
0x6a
```
Add the 2 values on the top of the stack.
Push the result to the stack.
90 changes: 90 additions & 0 deletions docs/ByteCodeSpecEx.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
```
header: 0x00 0x61 0x73 0x6D
version: 0x01 0x00 0x00 0x00
type section: 0x01
- size of this section
- number of types
- 0:
- header: 0x60 (function prototype)
- number of parameters: 0x02
- parameters type (no need for this if number of parameters is 0):
- 0:
i32: 0x7F
- 1:
i32: 0x7F
- number of results: 0x01
- return type (no need for this if number of results is 0):
- 0:
i32: 0x7F
function section: 0x03
- size of this section
- number of functions: 0x02
- 0: index of the type function in section type
- 1: index of the type function in section type
memory section: 0x05
- size of this section
- has maximum size: 0x00 (0x01 if has maximum size)
- minimum size: 0x13
- maximum size (if has maximum size): 0x20
export section: 0x07
- size of this section
- number of exports: 0x01
- exports:
- 0:
name length: 0x04
name: "main"
type (func: 0, table: 1, memory: 2, global: 3): 0x00
index: 0x00
code section: 0x0A
- size of this section
- number of code: 0x01
- 0:
size of this code
number of locals type: 0x01
- 0:
number of elements: 0x01
type of elements: 0x7F (i32)
end of this code: 0x0B
```

## Exemple

`add.wat`
```wat
(module
(func $add (param $lhs i32) (param $rhs i32) (result i32) (local $smth i32)
local.get $lhs
local.get $rhs
local.set $smth
local.get $rhs
i32.add)
(export "add" (func $add))
)
```

`add.wasm` (with `wat2wasm` `hexdump -C`)
```
00000000 00 61 73 6d 01 00 00 00 01 07 01 60 02 7f 7f 01 |.asm.......`....|
00000010 7f 03 02 01 00 07 07 01 03 61 64 64 00 00 0a 0f |.........add....|
00000020 01 0d 01 01 7f 20 00 20 01 21 02 20 01 6a 0b |..... . .!. .j.|
0000002f
```

`add.wasm.wat` (with `wasm2wat`)
```
(module
(type (;0;) (func (param i32 i32) (result i32)))
(func (;0;) (type 0) (param i32 i32) (result i32)
(local i32)
local.get 0
local.get 1
local.set 2
local.get 1
i32.add)
(export "add" (func 0)))
```
1 change: 1 addition & 0 deletions docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ made in Haskell.

[README](README.md)
[Byte Code Spec](ByteCodeSpec.md)
[Byte Code Spec Ex](ByteCodeSpecEx.md)

0 comments on commit 2e4e3a3

Please sign in to comment.