Skip to content

Commit

Permalink
Add the wasm backend
Browse files Browse the repository at this point in the history
  • Loading branch information
oovm committed Dec 12, 2023
1 parent f6f40bc commit 6cc95b5
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 61 deletions.
2 changes: 1 addition & 1 deletion projects/valkyrie-ast/src/helper/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub trait NumberInterpreter {
fn interpret(&mut self, n: &NumberLiteralNode) -> Result<Self::Output, NyarError>;
}

pub(crate) struct WrapDisplay<'a, T> {
pub struct WrapDisplay<'a, T> {
inner: &'a T,
}
impl<'a, T> WrapDisplay<'a, T> {
Expand Down
22 changes: 22 additions & 0 deletions projects/valkyrie-types/src/backends/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use crate::{ModuleItem, ModuleResolver, ValkyrieStructure};
use nyar_wasm::{StructureType, WasmBuilder, WasmSymbol, WasmType};

impl ModuleResolver {
pub fn build_wasm(&self) -> WasmBuilder {
let mut builder = WasmBuilder::default();

for item in self.items.values() {
match item {
ModuleItem::Imported(_) => {}
ModuleItem::Structure(v) => builder.insert_type(WasmType::Structure(StructureType {
symbol: WasmSymbol::from(v.name()),
fields: Default::default(),
span: Default::default(),
})),
}
}
builder
}
}

impl ValkyrieStructure {}
38 changes: 38 additions & 0 deletions projects/valkyrie-types/src/fields/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use nyar_error::FileSpan;
use std::sync::Arc;
use valkyrie_ast::{ExpressionKind, IdentifierNode};

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct FieldDefinition {
pub(crate) symbol: Arc<str>,
pub(crate) typing: Option<ExpressionKind>,
pub(crate) optional: bool,
pub(crate) span: FileSpan,
}

impl FieldDefinition {
pub fn new(name: &IdentifierNode) -> Self {
Self { symbol: Arc::from(name.name.as_str()), typing: None, optional: false, span: Default::default() }
}
pub fn name(&self) -> String {
self.symbol.to_string()
}
pub fn set_type(&mut self, typing: ExpressionKind) {
self.typing = Some(typing);
}
pub fn get_type(&self) -> Option<&ExpressionKind> {
self.typing.as_ref()
}
pub fn get_optional(&self) -> bool {
self.optional
}
pub fn set_optional(&mut self, optional: bool) {
self.optional = optional;
}
pub fn get_span(&self) -> FileSpan {
self.span
}
pub fn set_span(&mut self, span: FileSpan) {
self.span = span;
}
}
6 changes: 4 additions & 2 deletions projects/valkyrie-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ mod functions;
mod modifiers;
mod modules;
// #[cfg(test)]
mod backends;
mod definitions;
mod fields;
mod packages;
pub mod structures;
pub mod testing;
Expand All @@ -26,8 +28,6 @@ mod types;
mod utils;
mod values;

pub use self::modules::{ModuleItem, ModuleResolver, ValkyrieModule};

pub use self::{
builtin::{
images::ValkyrieImage,
Expand All @@ -37,8 +37,10 @@ pub use self::{
},
collection::dict::ValkyrieDict,
definitions::{enumerates::ValkyrieEnumerate, interfaces::ValkyrieInterface, names::ValkyrieName},
fields::FieldDefinition,
functions::{ValkyrieFunction, ValkyrieFunctionType, ValkyrieMonomorphicFunction},
modifiers::{FeatureType, InitializeType, MutableType},
modules::{ModuleItem, ModuleResolver, ValkyrieModule},
packages::ids::{ValkyrieID, ValkyrieUniverse},
types::{
atomic_type::ValkyrieAtomicType, literal_type::ValkyrieLiteralType, union_type::ValkyrieUnionType,
Expand Down
23 changes: 17 additions & 6 deletions projects/valkyrie-types/src/modules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use crate::{values::symbols::AsSymbol, FileCache, FileID, ValkyrieStructure, Val
use indexmap::{map::Entry, IndexMap};
use nyar_error::{Failure, NyarError, Result, Success, Validation};
use nyar_wasm::Operation;
use std::mem::take;
use std::{
fmt::{Debug, Formatter},
mem::take,
};
use valkyrie_ast::{ClassDeclaration, NamespaceDeclaration, ProgramRoot, StatementKind};
use valkyrie_parser::{ProgramContext, StatementNode};

Expand All @@ -12,21 +15,29 @@ pub struct ValkyrieModule {}
#[derive(Debug, Default)]
pub struct ModuleResolver {
/// The declared namespace
namespace: Option<ValkyrieSymbol>,
pub(crate) namespace: Option<ValkyrieSymbol>,
/// main function of the file
main: Vec<StatementNode>,
pub(crate) main: Vec<StatementNode>,
/// The declared items in file
items: IndexMap<String, ModuleItem>,
pub(crate) items: IndexMap<String, ModuleItem>,
/// Collect errors
errors: Vec<NyarError>,
pub(crate) errors: Vec<NyarError>,
}

#[derive(Debug)]
pub enum ModuleItem {
Imported(ValkyrieSymbol),
Structure(ValkyrieStructure),
}

impl Debug for ModuleItem {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::Imported(v) => Debug::fmt(v, f),
Self::Structure(v) => Debug::fmt(v, f),
}
}
}

pub(crate) trait AsModuleItem {
type Output = ();
fn send_module(self, ctx: &mut ModuleResolver) -> Result<Self::Output>;
Expand Down
25 changes: 18 additions & 7 deletions projects/valkyrie-types/src/structures/mod.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
use crate::{
types::{field_type::FieldDefinition, method_type::MethodDefinition, *},
ValkyrieSymbol,
};
use crate::{types::method_type::MethodDefinition, FieldDefinition, ValkyrieSymbol};
use indexmap::{map::Values, IndexMap};
use nyar_error::{NyarError, Result};
use std::ops::AddAssign;
use valkyrie_ast::{IdentifierNode, NamePathNode};
use std::{
fmt::{Debug, Formatter},
ops::AddAssign,
};
use valkyrie_ast::{helper::WrapDisplay, IdentifierNode, NamePathNode};

mod codegen;

#[derive(Clone, Debug, Eq, PartialEq)]
#[derive(Clone, Eq, PartialEq)]
pub struct ValkyrieStructure {
pub(crate) symbol: ValkyrieSymbol,
pub(crate) fields: IndexMap<String, FieldDefinition>,
pub(crate) methods: IndexMap<String, MethodDefinition>,
}

impl Debug for ValkyrieStructure {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Structure")
.field("symbol", &WrapDisplay::new(&self.symbol))
.field("fields", &self.fields)
.field("methods", &self.methods)
.finish()
}
}

impl AddAssign<FieldDefinition> for ValkyrieStructure {
fn add_assign(&mut self, rhs: FieldDefinition) {
self.fields.insert(rhs.name(), rhs);
}
}

impl AddAssign<MethodDefinition> for ValkyrieStructure {
fn add_assign(&mut self, rhs: MethodDefinition) {
self.methods.insert(rhs.name(), rhs);
Expand Down
37 changes: 0 additions & 37 deletions projects/valkyrie-types/src/types/field_type/mod.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1 @@
use super::*;

mod codegen;

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct FieldDefinition {
symbol: Arc<str>,
typing: Option<ExpressionKind>,
optional: bool,
span: FileSpan,
}

impl FieldDefinition {
pub fn new(name: &IdentifierNode) -> Self {
Self { symbol: Arc::from(name.name.as_str()), typing: None, optional: false, span: Default::default() }
}
pub fn name(&self) -> String {
self.symbol.to_string()
}
pub fn set_type(&mut self, typing: ExpressionKind) {
self.typing = Some(typing);
}
pub fn get_type(&self) -> Option<&ExpressionKind> {
self.typing.as_ref()
}
pub fn get_optional(&self) -> bool {
self.optional
}
pub fn set_optional(&mut self, optional: bool) {
self.optional = optional;
}
pub fn get_span(&self) -> FileSpan {
self.span
}
pub fn set_span(&mut self, span: FileSpan) {
self.span = span;
}
}
2 changes: 1 addition & 1 deletion projects/valkyrie-types/src/types/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
types::{atomic_type::ValkyrieDocument, field_type::FieldDefinition, method_type::MethodDefinition},
types::{atomic_type::ValkyrieDocument, method_type::MethodDefinition},
utils::primitive_type,
ValkyrieDict, ValkyrieID, ValkyrieString, ValkyrieStructure, ValkyrieValue,
};
Expand Down
8 changes: 1 addition & 7 deletions projects/valkyrie-types/src/values/symbols/mod.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
use super::*;
use std::fmt::Display;

#[derive(Clone, Eq, PartialEq)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ValkyrieSymbol {
pub(crate) path: Vec<Arc<str>>,
pub(crate) span: FileSpan,
}

impl Debug for ValkyrieSymbol {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.path.join("∷"))
}
}

impl Display for ValkyrieSymbol {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.path.join("∷"))
Expand Down

0 comments on commit 6cc95b5

Please sign in to comment.