Skip to content

Commit

Permalink
Resolve namespace in module context
Browse files Browse the repository at this point in the history
  • Loading branch information
oovm committed Dec 12, 2023
1 parent 0cbfa7d commit 23507be
Show file tree
Hide file tree
Showing 12 changed files with 129 additions and 138 deletions.
8 changes: 0 additions & 8 deletions projects/valkyrie-types/src/helpers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1 @@
use crate::ValkyrieCodegen;

pub trait FromFrontend<Item> {
fn build(&self, state: &mut ValkyrieCodegen) -> nyar_error::Result<Item>;
}

pub trait IntoBackend<Item> {
fn build(&self, state: &mut ValkyrieCodegen) -> nyar_error::Result<Item>;
}
5 changes: 3 additions & 2 deletions projects/valkyrie-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#![feature(generators)]
#![feature(lazy_cell)]
#![feature(extend_one)]
#![feature(associated_type_defaults)]

pub mod helpers;
pub mod projects;
Expand All @@ -14,14 +15,14 @@ mod builtin;
mod collection;
mod functions;
mod modifiers;
mod modules;
// #[cfg(test)]
mod definitions;
mod packages;
pub mod testing;
pub mod third_party;
mod types;
mod utils;

mod values;

pub use self::{
Expand All @@ -43,7 +44,7 @@ pub use self::{
atomic_type::ValkyrieAtomicType, class_type::ValkyrieStructure, literal_type::ValkyrieLiteralType,
union_type::ValkyrieUnionType, variant_type::ValkyrieVariantType, ValkyrieType,
},
values::ValkyrieValue,
values::{symbols::ValkyrieSymbol, ValkyrieValue},
};
pub use nyar_error::{
Failure, FileCache, FileID, MissingError, NyarError as ValkyrieError, Result as ValkyrieResult, RuntimeError, Success,
Expand Down
82 changes: 82 additions & 0 deletions projects/valkyrie-types/src/modules/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use crate::{ValkyrieStructure, ValkyrieSymbol};
use indexmap::IndexMap;
use nyar_error::{NyarError, Result};
use nyar_wasm::Operation;
use std::mem::take;
use valkyrie_ast::{NamespaceDeclaration, ProgramRoot, StatementKind};
use valkyrie_parser::StatementNode;

pub struct ValkyrieModule {}

/// Convert file to module
pub struct ModuleContext {
/// The declared namespace
namespace: Option<ValkyrieSymbol>,
/// main function of the file
main: Vec<StatementNode>,
/// The declared items in file
items: IndexMap<String, ModuleItem>,
/// Collect errors
errors: Vec<NyarError>,
}

pub enum ModuleItem {
Structure(ValkyrieStructure),
}

trait AsModuleItem {
type Output = ();
fn send_module(self, ctx: &mut ModuleContext) -> Result<Self::Output>;
}

impl AsModuleItem for ProgramRoot {
fn send_module(self, ctx: &mut ModuleContext) -> Result<Self::Output> {
for statement in self.statements {
statement.send_module(ctx)?
}
Ok(())
}
}

impl AsModuleItem for StatementKind {
fn send_module(self, ctx: &mut ModuleContext) -> Result<Self::Output> {
match self {
Self::Nothing => {}
Self::Document(_) => {}
Self::Annotation(_) => {}
Self::Namespace(v) => v.send_module(ctx)?,
Self::Import(_) => {}
Self::Class(_) => {}
Self::Union(_) => {}
Self::Enumerate(_) => {}
Self::Trait(_) => {}
Self::Extends(_) => {}
Self::Function(_) => {}
Self::Variable(_) => {}
Self::Guard(_) => {}
Self::While(_) => {}
Self::For(_) => {}
Self::Control(_) => {}
Self::Expression(_) => {}
}
Ok(())
}
}

impl AsModuleItem for NamespaceDeclaration {
fn send_module(self, ctx: &mut ModuleContext) -> Result<Self::Output> {
todo!()
}
}

impl ModuleContext {
pub fn visit(&mut self, root: ProgramRoot) -> Vec<NyarError> {
let progress = root.send_module(self);
let mut errors = take(&mut self.errors);
match progress {
Ok(_) => {}
Err(e) => errors.push(e),
}
errors
}
}
23 changes: 0 additions & 23 deletions projects/valkyrie-types/src/types/class_type/codegen.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1 @@
use super::*;

impl FromFrontend<ValkyrieStructure> for ClassDeclaration {
fn build(&self, state: &mut ValkyrieCodegen) -> Result<ValkyrieStructure> {
let mut output = ValkyrieStructure::new(&state.current_namespace, &self.name);
// state.register_class(output.clone());

for x in &self.terms {
match x {
ClassTerm::Macro(_) => {}
ClassTerm::Field(v) => match output.add_field(v.build(state)?) {
Err(e) if !state.interactive => state.add_error(e),
_ => {}
},
ClassTerm::Method(v) => match output.add_method(v.build(state)?) {
Err(e) if !state.interactive => state.add_error(e),
_ => {}
},
ClassTerm::Domain(_) => {}
}
}
Ok(output)
}
}
4 changes: 0 additions & 4 deletions projects/valkyrie-types/src/types/class_type/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use super::*;
use crate::types::method_type::MethodDefinition;
use indexmap::map::Values;
use nyar_error::NyarError;
use std::ops::AddAssign;

mod codegen;

Expand Down
57 changes: 0 additions & 57 deletions projects/valkyrie-types/src/types/field_type/codegen.rs
Original file line number Diff line number Diff line change
@@ -1,58 +1 @@
use super::*;

impl FromFrontend<FieldDefinition> for FieldDeclaration {
fn build(&self, state: &mut ValkyrieCodegen) -> Result<FieldDefinition> {
let mut output = FieldDefinition::new(&self.name);
match &self.typing {
Some(s) => output.set_type(s.clone()),
None => {}
}
Ok(output)
}
}

impl IntoBackend<FieldType> for FieldDefinition {
fn build(&self, state: &mut ValkyrieCodegen) -> nyar_error::Result<FieldType> {
let mut output = FieldType::new(Symbol::from(self.name()));
output.mutable = true;
match self.get_type() {
Some(ExpressionKind::Symbol(v)) => match v.to_string().as_str() {
"u32" => output.r#type = NyarType::U32,
"u64" => output.r#type = NyarType::I64,
"i32" => output.r#type = NyarType::I32,
"i64" => output.r#type = NyarType::I64,
"f32" => output.r#type = NyarType::F32,
"f64" => output.r#type = NyarType::F64,
"Any" => output.r#type = NyarType::Any,
"core::primitive::u32" => output.r#type = NyarType::U32,
s => output.r#type = NyarType::Named { symbol: Symbol::new(s), nullable: self.get_optional() },
},
Some(ExpressionKind::GenericCall(v)) => match &v.base {
ExpressionKind::Symbol(s) => match s.to_string().as_str() {
"Array" => match &v.term {
GenericCallTerm::Associated(_) => {}
GenericCallTerm::Generic(g) => match g.terms.first() {
None => {}
Some(v) => match &v.value {
ExpressionKind::Symbol(v) => match v.to_string().as_str() {
"u8" => output.r#type = NyarType::Array { inner: Box::new(NyarType::U8), nullable: false },
_ => {}
},
_ => {}
},
},
},
_ => {}
},
s => {
println!("UNKNOWN_FIELD_GENERIC: {s:?}")
}
},
Some(s) => {
println!("UNKNOWN_FIELD_TYPE: {s:?}")
}
None => {}
}
Ok(output)
}
}
17 changes: 0 additions & 17 deletions projects/valkyrie-types/src/types/function_type/codegen.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1 @@
use super::*;
use nyar_wasm::FunctionType;

impl FromFrontend<FunctionDefinition> for FunctionDeclaration {
fn build(&self, state: &mut ValkyrieCodegen) -> Result<FunctionDefinition> {
let mut output = FunctionDefinition::new(&state.current_namespace, &self.name)?;

Ok(output)
}
}

impl IntoBackend<FunctionType> for FunctionDefinition {
fn build(&self, state: &mut ValkyrieCodegen) -> Result<FunctionType> {
let mut output = FunctionType::new(Symbol::from(self.name()));

Ok(output)
}
}
6 changes: 0 additions & 6 deletions projects/valkyrie-types/src/types/method_type/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ pub struct MethodDefinition {
span: FileSpan,
}

impl FromFrontend<MethodDefinition> for MethodDeclaration {
fn build(&self, state: &mut ValkyrieCodegen) -> nyar_error::Result<MethodDefinition> {
todo!()
}
}

impl MethodDefinition {
pub fn new(name: &IdentifierNode) -> Self {
todo!()
Expand Down
9 changes: 5 additions & 4 deletions projects/valkyrie-types/src/types/mod.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
use crate::{
helpers::{FromFrontend, IntoBackend},
types::{atomic_type::ValkyrieDocument, field_type::FieldDefinition},
types::{atomic_type::ValkyrieDocument, field_type::FieldDefinition, method_type::MethodDefinition},
utils::primitive_type,
ValkyrieCodegen, ValkyrieDict, ValkyrieID, ValkyrieString, ValkyrieStructure, ValkyrieValue,
ValkyrieDict, ValkyrieID, ValkyrieString, ValkyrieStructure, ValkyrieValue,
};
use indexmap::IndexMap;
use indexmap::{map::Values, IndexMap};
use itertools::Itertools;
use nyar_collection::NyarTuple;
use nyar_error::{FileSpan, NyarError, Result};
use shredder::{marker::GcSafe, Gc, Scan};
use std::{
any::type_name,
fmt::{Debug, Display},
hash::{Hash, Hasher},
ops::AddAssign,
sync::Arc,
};
use valkyrie_ast::{
Expand Down
10 changes: 7 additions & 3 deletions projects/valkyrie-types/src/values/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use nyar_error::FileSpan;
use shredder::{marker::GcSafe, Gc, Scan, Scanner};
use std::fmt::{Debug, Formatter};
use valkyrie_ast::helper::ValkyrieNode;

use std::{
fmt::{Debug, Formatter},
sync::Arc,
};
use valkyrie_ast::NamePathNode;
mod der;
mod jupyter;
mod ser;
pub mod symbols;

#[cfg(feature = "polars")]
use crate::builtin::data_frame::ValkyrieDataFrame;
Expand Down
31 changes: 31 additions & 0 deletions projects/valkyrie-types/src/values/symbols/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use super::*;

pub struct ValkyrieSymbol {
path: Vec<Arc<str>>,
span: FileSpan,
}

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

trait AsValkyrieSymbol {
fn as_valkyrie_symbol(&self) -> ValkyrieSymbol;
}

impl AsValkyrieSymbol for NamePathNode {
fn as_valkyrie_symbol(&self) -> ValkyrieSymbol {
ValkyrieSymbol { path: self.path.iter().map(|s| Arc::from(s.name.as_str())).collect(), span: self.span.clone() }
}
}

impl ValkyrieSymbol {
pub fn new<S: AsValkyrieSymbol>(id: S) -> Self {
id.as_valkyrie_symbol()
}
pub fn with_span(self, span: FileSpan) -> Self {
Self { span, ..self }
}
}
15 changes: 1 addition & 14 deletions projects/valkyrie-types/tests/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::{path::Path, process::Command};
use valkyrie_types::{testing::assert_type, ValkyrieCodegen, ValkyrieID, ValkyrieInterface, ValkyrieList};
use valkyrie_types::{testing::assert_type, ValkyrieID, ValkyrieInterface, ValkyrieList};

#[test]
fn ready() {
Expand All @@ -14,18 +13,6 @@ fn test_primitive() {
assert_type(value, "Float64", "std::primitive::Float64");
}

#[test]
fn test_parse() {
let mut codegen = ValkyrieCodegen::default();
let file = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/test.vk").canonicalize().unwrap();
for e in codegen.load_local(file) {
codegen.print_error(e)
}
codegen.build_wasm(Path::new(env!("CARGO_MANIFEST_DIR")).join("target/debug/valkyrie/test.wasm")).unwrap();
let log = Command::new("vcc").arg("build").output().expect("Failed to execute command");
println!("{log:?}")
}

#[test]
fn test_list_index() {
let out = ValkyrieList::from_iter(vec!['1', '2', '3', '4', '5', '6', '7', '8', '9']);
Expand Down

0 comments on commit 23507be

Please sign in to comment.