Skip to content

Commit

Permalink
First commit!
Browse files Browse the repository at this point in the history
  • Loading branch information
jtenner committed Sep 3, 2024
0 parents commit e72abb5
Show file tree
Hide file tree
Showing 59 changed files with 18,433 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: test

on:
push:
branches:
- master
- main
pull_request:

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: erlef/setup-beam@v1
with:
otp-version: "26.0.2"
gleam-version: "1.3.2"
rebar3-version: "3"
# elixir-version: "1.15.4"
- run: gleam deps download
- run: gleam test
- run: gleam format --check src test
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.beam
*.ez
/build
erl_crash.dump

node_modules
bun.lockb
update_stuff.sh
21 changes: 21 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"[javascript]": {
"editor.defaultFormatter": "biomejs.biome"
},
"[typescript]": {
"editor.defaultFormatter": "biomejs.biome"
},
"[javascriptreact]": {
"editor.defaultFormatter": "biomejs.biome"
},
"[typescriptreact]": {
"editor.defaultFormatter": "biomejs.biome"
},
"[gleam]": {
"editor.defaultFormatter": "gleam.gleam"
},
"typescript.tsdk": "./node_modules/typescript/lib",
"editor.tabSize": 2,
"editor.defaultFormatter": "biomejs.biome",
"editor.formatOnSave": true
}
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright © 2024 Joshua Tenner <tenner.joshua@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# shinecoder

[![Package Version](https://img.shields.io/hexpm/v/shinecoder)](https://hex.pm/packages/shinecoder)
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/shinecoder/)

```sh
gleam add shinecoder@1
```
```gleam
import shinecoder
pub fn main() {
// TODO: An example of the project in use
}
```

Further documentation can be found at <https://hexdocs.pm/shinecoder>.

## Development

```sh
gleam run # Run the project
gleam test # Run the tests
```
21 changes: 21 additions & 0 deletions gleam.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name = "shinecoder"
version = "0.0.1"

# Fill out these fields if you intend to generate HTML documentation or publish
# your project to the Hex package manager.
#
description = "A Web Assembly Encoder and Decoder written in Gleam"
licences = ["MIT"]
# repository = { type = "github", user = "", repo = "" }
# links = [{ title = "Website", href = "" }]
#
# For a full reference of all the available options, you can have a look at
# https://gleam.run/writing-gleam/gleam-toml/.

[dependencies]
gleam_stdlib = ">= 0.34.0 and < 2.0.0"
ieee_float = ">= 1.0.0 and < 2.0.0"
pprint = ">= 1.0.3 and < 2.0.0"

[dev-dependencies]
gleeunit = ">= 1.0.0 and < 2.0.0"
17 changes: 17 additions & 0 deletions manifest.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# This file was generated by Gleam
# You typically do not need to edit this file

packages = [
{ name = "glam", version = "2.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "glam", source = "hex", outer_checksum = "66EC3BCD632E51EED029678F8DF419659C1E57B1A93D874C5131FE220DFAD2B2" },
{ name = "gleam_erlang", version = "0.25.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "054D571A7092D2A9727B3E5D183B7507DAB0DA41556EC9133606F09C15497373" },
{ name = "gleam_stdlib", version = "0.39.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "2D7DE885A6EA7F1D5015D1698920C9BAF7241102836CE0C3837A4F160128A9C4" },
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
{ name = "ieee_float", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "ieee_float", source = "hex", outer_checksum = "A9C8728DE26A8AA563FF2F49F8802C8875A41BAE86024B34A458C6EB821F1F00" },
{ name = "pprint", version = "1.0.3", build_tools = ["gleam"], requirements = ["glam", "gleam_stdlib"], otp_app = "pprint", source = "hex", outer_checksum = "76BBB92E23D12D954BD452686543F29EDE8EBEBB7FC0ACCBCA66EEF276EC3A06" },
]

[requirements]
gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" }
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
ieee_float = { version = ">= 1.0.0 and < 2.0.0" }
pprint = { version = ">= 1.0.3 and < 2.0.0" }
208 changes: 208 additions & 0 deletions src/builder/expression_builder.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
import gleam/option.{type Option, None, Some}
import internal/finger_tree.{type FingerTree}
import internal/structure/types.{
type BlockType, type Expr, type Instruction, Block, Expr, If, Loop,
}

type BlockDefinition {
EmptyTop
OuterMostBlock(instructions: FingerTree(Instruction))
IfBlock(bt: BlockType, instructions: FingerTree(Instruction))
ElseBlock(
bt: BlockType,
if_instructions: FingerTree(Instruction),
else_instructions: FingerTree(Instruction),
)
LoopBlock(bt: BlockType, instructions: FingerTree(Instruction))
InlineBlock(bt: BlockType, instructions: FingerTree(Instruction))
}

pub opaque type ExpressionBuilder {
ExpressionBuilder(
result: Option(Expr),
top: BlockDefinition,
block_stack: FingerTree(BlockDefinition),
)
}

pub fn new() -> ExpressionBuilder {
ExpressionBuilder(None, OuterMostBlock(finger_tree.empty), finger_tree.empty)
}

pub fn begin_if(builder: ExpressionBuilder, bt: BlockType) -> ExpressionBuilder {
let ExpressionBuilder(result, top, block_stack) = builder
ExpressionBuilder(
result,
IfBlock(bt, finger_tree.empty),
block_stack
|> finger_tree.push(top),
)
}

pub fn begin_else(builder: ExpressionBuilder) {
case builder.top {
IfBlock(bt, instructions) ->
ExpressionBuilder(
..builder,
top: ElseBlock(bt, instructions, finger_tree.empty),
)
EmptyTop -> panic as "Stack state underflow"
_ -> panic as "Invalid stack state, cannot start else block"
}
}

pub fn begin_loop(
builder: ExpressionBuilder,
bt: BlockType,
) -> ExpressionBuilder {
ExpressionBuilder(
builder.result,
LoopBlock(bt, finger_tree.empty),
builder.block_stack
|> finger_tree.push(builder.top),
)
}

pub fn begin_block(
builder: ExpressionBuilder,
bt: BlockType,
) -> ExpressionBuilder {
ExpressionBuilder(
builder.result,
InlineBlock(bt, finger_tree.empty),
builder.block_stack
|> finger_tree.push(builder.top),
)
}

pub fn end(builder: ExpressionBuilder) {
case builder {
ExpressionBuilder(None, OuterMostBlock(instructions), block_stack)
if block_stack == finger_tree.empty
-> Ok(ExpressionBuilder(Some(Expr(instructions)), EmptyTop, block_stack))
ExpressionBuilder(None, IfBlock(bt, if_instructions), block_stack) ->
do_append(block_stack, If(bt, if_instructions, None))
ExpressionBuilder(
None,
ElseBlock(bt, if_instructions, else_instructions),
block_stack,
) ->
do_append(block_stack, If(bt, if_instructions, Some(else_instructions)))
ExpressionBuilder(None, LoopBlock(bt, loop_instructions), block_stack) ->
do_append(block_stack, Loop(bt, Expr(loop_instructions)))
ExpressionBuilder(None, InlineBlock(bt, block_instructions), block_stack) ->
do_append(block_stack, Block(bt, Expr(block_instructions)))
ExpressionBuilder(_, EmptyTop, _) -> Error("Stack state underflow")
_ -> Error("Invalid stack state")
}
}

fn do_append(
next_stack: FingerTree(BlockDefinition),
next_instruction: Instruction,
) {
case next_stack |> finger_tree.pop() {
Ok(#(OuterMostBlock(outer_instructions), next_stack)) ->
Ok(ExpressionBuilder(
None,
OuterMostBlock(
outer_instructions
|> finger_tree.push(next_instruction),
),
next_stack,
))

Ok(#(IfBlock(outer_bt, outer_if_instructions), next_stack)) ->
Ok(ExpressionBuilder(
None,
IfBlock(
outer_bt,
outer_if_instructions |> finger_tree.push(next_instruction),
),
next_stack,
))
Ok(#(
ElseBlock(outer_bt, outer_if_instructions, outer_else_instructions),
next_stack,
)) ->
Ok(ExpressionBuilder(
None,
ElseBlock(
outer_bt,
outer_if_instructions,
outer_else_instructions |> finger_tree.push(next_instruction),
),
next_stack,
))

Ok(#(LoopBlock(outer_bt, outer_instructions), next_stack)) ->
Ok(ExpressionBuilder(
None,
LoopBlock(
outer_bt,
outer_instructions |> finger_tree.push(next_instruction),
),
next_stack,
))

Ok(#(InlineBlock(outer_bt, outer_instructions), next_stack)) ->
Ok(ExpressionBuilder(
None,
InlineBlock(
outer_bt,
outer_instructions |> finger_tree.push(next_instruction),
),
next_stack,
))
_ -> Error("Stack state underflow")
}
}

pub fn push(builder: ExpressionBuilder, inst: Instruction) {
case builder.top {
OuterMostBlock(instructions) ->
ExpressionBuilder(
None,
OuterMostBlock(instructions |> finger_tree.push(inst)),
builder.block_stack,
)
IfBlock(bt, instructions) ->
ExpressionBuilder(
None,
IfBlock(bt, instructions |> finger_tree.push(inst)),
builder.block_stack,
)
ElseBlock(bt, if_instructions, else_instructions) ->
ExpressionBuilder(
None,
ElseBlock(
bt,
if_instructions,
else_instructions |> finger_tree.push(inst),
),
builder.block_stack,
)
LoopBlock(bt, instructions) ->
ExpressionBuilder(
None,
LoopBlock(bt, instructions |> finger_tree.push(inst)),
builder.block_stack,
)
InlineBlock(bt, instructions) ->
ExpressionBuilder(
None,
InlineBlock(bt, instructions |> finger_tree.push(inst)),
builder.block_stack,
)
_ -> panic as "Expression completed, cannot push instruction"
}
}

pub fn end_unwrap(builder) {
case builder {
ExpressionBuilder(None, OuterMostBlock(instructions), stack)
if stack == finger_tree.empty
-> Expr(instructions)
_ -> panic as "Unable to unwrap, invalid expression state"
}
}
7 changes: 7 additions & 0 deletions src/builder/instructions/any.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import builder/expression_builder.{type ExpressionBuilder}
import internal/structure/types.{AnyConvertExtern}

pub fn convert_extern(builder: ExpressionBuilder) {
builder
|> expression_builder.push(AnyConvertExtern)
}
Loading

0 comments on commit e72abb5

Please sign in to comment.