Skip to content

Commit

Permalink
Define the unknown symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
oovm committed May 28, 2024
1 parent 46a8c6d commit 2a51da4
Show file tree
Hide file tree
Showing 14 changed files with 51 additions and 33 deletions.
4 changes: 2 additions & 2 deletions projects/valkyrie-types/src/frontends/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
functions::{FunctionBody, FunctionInstance, FunctionParameter},
helpers::{AsIdentifier, Hir2Mir},
structures::ValkyrieResource,
ModuleItem, ResolveContext, ValkyrieClass, ValkyrieEnumeration, ValkyrieField, ValkyrieFlagation, ValkyrieFrom,
NamespaceItem, ResolveContext, ValkyrieClass, ValkyrieEnumeration, ValkyrieField, ValkyrieFlagation, ValkyrieFrom,
ValkyrieImportFunction, ValkyrieMethod, ValkyrieNativeFunction, ValkyrieSemanticNumber, ValkyrieType, ValkyrieUnite,
ValkyrieVariant,
};
Expand Down Expand Up @@ -118,7 +118,7 @@ impl Hir2Mir for ImplementsStatement {
if x.path.last().unwrap().name.as_ref().eq("TypeCast") {
let id = self.target.as_identifier();
match store.items.get_mut(&id) {
Some(ModuleItem::Structure(class)) => {
Some(NamespaceItem::Structure(class)) => {
for item in &self.body {
match item {
TraitTerm::Macro(_) => {
Expand Down
4 changes: 2 additions & 2 deletions projects/valkyrie-types/src/functions/arithmetic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use super::*;

impl AddAssign<ValkyrieImportFunction> for ResolveContext {
fn add_assign(&mut self, rhs: ValkyrieImportFunction) {
self.items.insert(rhs.function_name.clone(), ModuleItem::External(rhs));
self.items.insert(rhs.function_name.clone(), NamespaceItem::External(rhs));
}
}

impl AddAssign<ValkyrieNativeFunction> for ResolveContext {
fn add_assign(&mut self, rhs: ValkyrieNativeFunction) {
self.items.insert(rhs.function_name.clone(), ModuleItem::Function(rhs));
self.items.insert(rhs.function_name.clone(), NamespaceItem::Function(rhs));
}
}
2 changes: 1 addition & 1 deletion projects/valkyrie-types/src/functions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
helpers::{Hir2Mir, Mir2Lir},
ModuleItem, ResolveContext, ValkyrieType,
NamespaceItem, ResolveContext, ValkyrieType,
};
use indexmap::IndexMap;
use nyar_wasm::{DependentGraph, Identifier, WasiExport, WasiFunction, WasiImport};
Expand Down
4 changes: 2 additions & 2 deletions projects/valkyrie-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ mod types;

pub use crate::{
functions::{ValkyrieImportFunction, ValkyrieNativeFunction},
modules::{ModuleItem, ResolveContext, ValkyrieModule},
structures::{PrimitiveType, ValkyrieClass, ValkyrieField, ValkyrieFrom, ValkyrieInto, ValkyrieMethod, ValkyriePrimitive},
modules::{NamespaceItem, ResolveContext, ValkyrieModule},
structures::{ValkyrieClass, ValkyrieField, ValkyrieFrom, ValkyrieInto, ValkyrieMethod, ValkyriePrimitive},
types::{
encoding_type::ValkyrieSemanticNumber, enumeration_types::ValkyrieEnumeration, flag_types::ValkyrieFlagation,
unite_types::ValkyrieUnite, variant_type::ValkyrieVariant, ValkyrieType,
Expand Down
6 changes: 5 additions & 1 deletion projects/valkyrie-types/src/modules/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,23 @@ impl ResolveContext {
}
}

impl Mir2Lir for ModuleItem {
impl Mir2Lir for NamespaceItem {
type Output = ();
type Context<'a> = &'a ResolveContext;

fn to_lir<'a>(&self, graph: &mut DependentGraph, context: Self::Context<'a>) -> Result<Self::Output> {
match self {
Self::Resource(s) => s.to_lir(graph, context),
Self::Structure(s) => s.to_lir(graph, context),
Self::Primitive(s) => s.to_lir(graph, context),
Self::Variant(s) => s.to_lir(graph, context),
Self::Function(s) => s.to_lir(graph, context),
Self::External(s) => s.to_lir(graph, context),
Self::Flags(s) => s.to_lir(graph, context),
Self::Enums(s) => s.to_lir(graph, context),
Self::Unknown(_) => {
unreachable!()
}
}
}
}
6 changes: 5 additions & 1 deletion projects/valkyrie-types/src/modules/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,20 @@ impl Debug for ResolveContext {
.finish()
}
}
impl Debug for ModuleItem {
impl Debug for NamespaceItem {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::External(v) => Debug::fmt(v, f),
Self::Function(v) => Debug::fmt(v, f),
Self::Resource(v) => Debug::fmt(v, f),
Self::Primitive(v) => Debug::fmt(v, f),
Self::Structure(v) => Debug::fmt(v, f),
Self::Flags(v) => Debug::fmt(v, f),
Self::Enums(v) => Debug::fmt(v, f),
Self::Variant(v) => Debug::fmt(v, f),
NamespaceItem::Unknown(_) => {
unreachable!()
}
}
}
}
6 changes: 4 additions & 2 deletions projects/valkyrie-types/src/modules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub struct ResolveContext {
/// Mapping local name to global name
pub(crate) name_mapping: HashMap<Vec<Arc<str>>, ModuleImportsMap>,
/// The declared items in file
pub(crate) items: IndexMap<Identifier, ModuleItem>,
pub(crate) items: IndexMap<Identifier, NamespaceItem>,
/// Collect errors
errors: Vec<NyarError>,
/// Collect spread statements
Expand All @@ -51,7 +51,9 @@ pub struct ModuleImportsMap {
local: HashMap<Identifier, Identifier>,
}

pub enum ModuleItem {
pub enum NamespaceItem {
/// A unresolved symbol
Unknown(Identifier),
Resource(ValkyrieResource),
Structure(ValkyrieClass),
Primitive(ValkyriePrimitive),
Expand Down
14 changes: 14 additions & 0 deletions projects/valkyrie-types/src/structures/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ impl Mir2Lir for ValkyrieClass {
}
}

impl Mir2Lir for ValkyriePrimitive {
type Output = ();
type Context<'a> = &'a ResolveContext;

fn to_lir<'a>(&self, graph: &mut DependentGraph, context: Self::Context<'a>) -> Result<Self::Output> {
for method in self.methods.values() {
method.to_lir(graph, context)?
}
for from in self.from.iter() {
from.to_lir(graph, &self.primitive_name)?
}
Ok(())
}
}
impl Mir2Lir for ValkyrieField {
type Output = WasiRecordField;
type Context<'a> = &'a ResolveContext;
Expand Down
12 changes: 9 additions & 3 deletions projects/valkyrie-types/src/structures/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ impl Debug for ValkyrieClass {
debug.finish()
}
}

impl Debug for ValkyriePrimitive {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let debug = &mut f.debug_struct("Primitive");
debug.field("symbol", &WrapDisplay::new(&self.primitive_name)).field("wrapper", &self.wrapper);
debug.finish()
}
}
impl Debug for ValkyrieField {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Field").field("name", &self.field_name).field("wasi", &self.wasi_alias).finish()
Expand All @@ -24,11 +30,11 @@ impl Debug for ValkyrieField {

impl AddAssign<ValkyrieClass> for ResolveContext {
fn add_assign(&mut self, rhs: ValkyrieClass) {
self.items.insert(rhs.class_name.clone(), ModuleItem::Structure(rhs));
self.items.insert(rhs.class_name.clone(), NamespaceItem::Structure(rhs));
}
}
impl AddAssign<ValkyrieResource> for ResolveContext {
fn add_assign(&mut self, rhs: ValkyrieResource) {
self.items.insert(rhs.resource_name.clone(), ModuleItem::Resource(rhs));
self.items.insert(rhs.resource_name.clone(), NamespaceItem::Resource(rhs));
}
}
18 changes: 3 additions & 15 deletions projects/valkyrie-types/src/structures/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::{
functions::{FunctionBody, FunctionInstance},
helpers::Mir2Lir,
modules::{ModuleItem, ResolveContext},
modules::{NamespaceItem, ResolveContext},
ValkyrieImportFunction,
};
use indexmap::IndexMap;
use nyar_error::Result;
use nyar_wasm::{DependentGraph, Identifier, WasiImport, WasiResource};
use nyar_wasm::{DependentGraph, Identifier, WasiImport, WasiResource, WasiType};
use ordered_float::NotNan;
use std::{
collections::BTreeMap,
Expand Down Expand Up @@ -34,25 +34,13 @@ pub struct ValkyriePrimitive {
/// The name of the primitive
pub primitive_name: Identifier,
/// primitive type had no fields, only primitive type wrapper
pub wrapper: PrimitiveType,
pub wrapper: WasiType,
pub imports: IndexMap<Arc<str>, ValkyrieImportFunction>,
pub methods: IndexMap<Arc<str>, ValkyrieMethod>,
pub from: Vec<ValkyrieFrom>,
pub into: Vec<ValkyrieInto>,
}

#[derive(Clone, Eq, PartialEq)]
pub enum PrimitiveType {
/// Equivalent to the wasm type `i32`
I32,
/// Equivalent to the wasm type `i64`
I64,
/// Equivalent to the wasm type `f32`
F32,
/// Equivalent to the wasm type `f64`
F64,
}

#[derive(Clone, Eq, PartialEq)]
pub struct ValkyrieClass {
pub class_name: Identifier,
Expand Down
2 changes: 1 addition & 1 deletion projects/valkyrie-types/src/types/enumeration_types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct ValkyrieEnumeration {

impl AddAssign<ValkyrieEnumeration> for ResolveContext {
fn add_assign(&mut self, rhs: ValkyrieEnumeration) {
self.items.insert(rhs.enumeration_name.clone(), ModuleItem::Enums(rhs));
self.items.insert(rhs.enumeration_name.clone(), NamespaceItem::Enums(rhs));
}
}

Expand Down
2 changes: 1 addition & 1 deletion projects/valkyrie-types/src/types/flag_types/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::*;

impl AddAssign<ValkyrieFlagation> for ResolveContext {
fn add_assign(&mut self, rhs: ValkyrieFlagation) {
self.items.insert(rhs.flags_name.clone(), ModuleItem::Flags(rhs));
self.items.insert(rhs.flags_name.clone(), NamespaceItem::Flags(rhs));
}
}

Expand Down
2 changes: 1 addition & 1 deletion projects/valkyrie-types/src/types/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
helpers::{Hir2Mir, Mir2Lir},
ModuleItem, ResolveContext, ValkyrieField, ValkyrieSemanticNumber, ValkyrieVariant,
NamespaceItem, ResolveContext, ValkyrieField, ValkyrieSemanticNumber, ValkyrieVariant,
};
use indexmap::IndexMap;
use nyar_error::SourceSpan;
Expand Down
2 changes: 1 addition & 1 deletion projects/valkyrie-types/src/types/unite_types/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use nyar_wasm::WasiVariantType;

impl AddAssign<ValkyrieUnite> for ResolveContext {
fn add_assign(&mut self, rhs: ValkyrieUnite) {
self.items.insert(rhs.unite_name.clone(), ModuleItem::Variant(rhs));
self.items.insert(rhs.unite_name.clone(), NamespaceItem::Variant(rhs));
}
}

Expand Down

0 comments on commit 2a51da4

Please sign in to comment.