Skip to content

Commit

Permalink
feat: Implement initial support for import from (#41)
Browse files Browse the repository at this point in the history
* chore: Major refactor to incorporate wit
* feat: Imports and Enums WIP
* chore: Refactor imports and functions
* feat: Implement initial support for import from
* chore: obey the great paperclip
* chore: fix ci
* chore: rerun cargo fmt
* chore: fix ci clippy
  • Loading branch information
esoterra authored Mar 6, 2024
1 parent 925ec0c commit 5f89d54
Show file tree
Hide file tree
Showing 39 changed files with 2,892 additions and 1,382 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ jobs:
cargo --version
- name: Build
run: cargo build
run: cargo build --workspace

- name: Format
run: cargo fmt --check

- name: Lint
run: cargo clippy
run: cargo clippy -- -D warnings

- name: Run tests
run: cargo test
run: cargo test --workspace
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
/target
.vscode

test.wat
# So that files from debugging don't get committed
# Only applies to wat/wasm files in root
# Not e.g. crates/codegen/allocator.wat
*.wat
*.wasm
37 changes: 35 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ logos = { workspace = true }
wasm-encoder ={ workspace = true }
cranelift-entity = { workspace = true }
wat = { workspace = true }
wit-parser = { workspace = true }

[dev-dependencies]
pretty_assertions = { workspace = true }
Expand Down Expand Up @@ -67,3 +68,4 @@ wat = "1.200.0"
pretty_assertions = "1.1.0"
wasmtime = { version = "15.0.0", features = ["component-model"]}
wasmprinter = "0.2.75"
wit-parser = "0.200.0"
1 change: 1 addition & 0 deletions crates/ast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ repository = { workspace = true }
miette = { workspace = true }
claw-common = { workspace = true }
cranelift-entity = { workspace = true }
wit-parser = { workspace = true }
40 changes: 21 additions & 19 deletions crates/ast/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;

use cranelift_entity::{entity_impl, PrimaryMap};

use crate::expressions::ExpressionData;
use crate::{expressions::ExpressionData, PackageName, TypeDefId, TypeDefinition};
use claw_common::Source;

use super::{
Expand Down Expand Up @@ -32,6 +32,7 @@ pub struct Component {

// Top level items
pub imports: PrimaryMap<ImportId, Import>,
pub type_defs: PrimaryMap<TypeDefId, TypeDefinition>,
pub globals: PrimaryMap<GlobalId, Global>,
pub functions: PrimaryMap<FunctionId, Function>,

Expand All @@ -53,6 +54,7 @@ impl Component {
Self {
src,
imports: Default::default(),
type_defs: Default::default(),
globals: Default::default(),
functions: Default::default(),
types: Default::default(),
Expand Down Expand Up @@ -135,11 +137,26 @@ impl Component {

///
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Import {
pub enum Import {
Plain(PlainImport),
ImportFrom(ImportFrom),
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct PlainImport {
pub ident: NameId,
pub external_type: ExternalType,
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct ImportFrom {
/// The first name is the imported item's name
/// The second optional name is an alias
pub items: Vec<(NameId, Option<NameId>)>,
pub package: PackageName,
pub interface: String,
}

///
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum ExternalType {
Expand All @@ -151,26 +168,11 @@ pub enum ExternalType {
pub struct Function {
pub exported: bool,
pub ident: NameId,
pub arguments: Vec<(NameId, TypeId)>,
pub return_type: Option<TypeId>,
pub params: Vec<(NameId, TypeId)>,
pub results: Option<TypeId>,
pub body: Vec<StatementId>,
}

pub trait FnTypeInfo {
fn get_args(&self) -> &[(NameId, TypeId)];
fn get_return_type(&self) -> Option<TypeId>;
}

impl FnTypeInfo for Function {
fn get_args(&self) -> &[(NameId, TypeId)] {
self.arguments.as_slice()
}

fn get_return_type(&self) -> Option<TypeId> {
self.return_type
}
}

///
#[derive(Debug, Clone)]
pub struct Global {
Expand Down
18 changes: 16 additions & 2 deletions crates/ast/src/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ pub trait ContextEq<Context> {
#[derive(Debug, PartialEq, Clone)]
pub enum Expression {
Identifier(Identifier),
Enum(EnumLiteral),
Literal(Literal),
Call(Call),
Unary(UnaryExpression),
Expand All @@ -89,14 +90,14 @@ impl ContextEq<super::Component> for ExpressionId {
let self_span = context.expr().get_span(*self);
let other_span = context.expr().get_span(*other);
if self_span != other_span {
dbg!((self_span, other_span));
dbg!(self_span, other_span);
return false;
}

let self_expr = context.expr().get_exp(*self);
let other_expr = context.expr().get_exp(*other);
if !self_expr.context_eq(other_expr, context) {
dbg!((self_expr, other_expr));
dbg!(self_expr, other_expr);
return false;
}
true
Expand Down Expand Up @@ -133,6 +134,19 @@ impl ContextEq<super::Component> for Identifier {
}
}

#[derive(Debug, PartialEq, Clone)]
pub struct EnumLiteral {
pub enum_name: NameId,
pub case_name: NameId,
}

impl ContextEq<super::Component> for EnumLiteral {
fn context_eq(&self, other: &Self, context: &super::Component) -> bool {
context.get_name(self.enum_name) == context.get_name(other.enum_name)
&& context.get_name(self.case_name) == context.get_name(other.case_name)
}
}

#[derive(Debug, PartialEq, Clone)]
pub enum Literal {
Integer(u64),
Expand Down
2 changes: 2 additions & 0 deletions crates/ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ pub mod types;
use cranelift_entity::entity_impl;
use miette::SourceSpan;

pub use wit_parser::PackageName;

pub type Span = SourceSpan;

pub use component::*;
Expand Down
51 changes: 23 additions & 28 deletions crates/ast/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ use super::{Component, NameId};
pub struct TypeId(u32);
entity_impl!(TypeId, "type");

#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct TypeDefId(u32);
entity_impl!(TypeDefId, "typedef");

/// The type for all values
#[derive(Debug, Hash, Clone)]
pub enum ValType {
// Result Type
Result { ok: TypeId, err: TypeId },
Result(ResultType),
Primitive(PrimitiveType),
}

Expand All @@ -37,25 +40,22 @@ pub enum PrimitiveType {
String,
}

#[derive(Debug, Hash, Clone)]
pub struct ResultType {
pub ok: TypeId,
pub err: TypeId,
}

impl ValType {
pub fn eq(&self, other: &Self, comp: &Component) -> bool {
match (self, other) {
(
ValType::Result {
ok: l_ok,
err: l_err,
},
ValType::Result {
ok: r_ok,
err: r_err,
},
) => {
let l_ok = comp.get_type(*l_ok);
let r_ok = comp.get_type(*r_ok);
(ValType::Result(left), ValType::Result(right)) => {
let l_ok = comp.get_type(left.ok);
let r_ok = comp.get_type(right.ok);
let ok_eq = l_ok.eq(r_ok, comp);

let l_err = comp.get_type(*l_err);
let r_err = comp.get_type(*r_err);
let l_err = comp.get_type(left.err);
let r_err = comp.get_type(right.err);
let err_eq = l_err.eq(r_err, comp);

ok_eq && err_eq
Expand All @@ -68,21 +68,16 @@ impl ValType {

#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub enum TypeDefinition {
// TODO
Record(RecordTypeDef),
}

#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct FnType {
pub arguments: Vec<(NameId, TypeId)>,
pub return_type: Option<TypeId>,
pub struct RecordTypeDef {
fields: Vec<(NameId, TypeId)>,
}

impl super::FnTypeInfo for FnType {
fn get_args(&self) -> &[(NameId, TypeId)] {
self.arguments.as_slice()
}

fn get_return_type(&self) -> Option<TypeId> {
self.return_type
}
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct FnType {
pub params: Vec<(NameId, TypeId)>,
pub results: Option<TypeId>,
}
Loading

0 comments on commit 5f89d54

Please sign in to comment.