Skip to content

Commit

Permalink
Merge pull request #4 from IamTheCarl/devel
Browse files Browse the repository at this point in the history
0.6 update
  • Loading branch information
IamTheCarl authored May 31, 2024
2 parents ae4a0a0 + 618d1be commit dca8f30
Show file tree
Hide file tree
Showing 65 changed files with 7,970 additions and 7,375 deletions.
439 changes: 230 additions & 209 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
workspace = { members = ["crates/common_data_types"] }
workspace = { members = ["crates/common_data_types", "crates/macros"] }
[package]
name = "command_cad"
version = "0.5.0"
version = "0.6.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand All @@ -24,7 +24,6 @@ lazy_static = "1.4"
log = "0.4"
nom = "7.1"
nom_locate = "4.2"
ouroboros = "0.18"
paste = "1.0"
semver = { version = "1.0", features = [ "serde" ] }
serde = { version = "1.0", features = [ "derive" ] }
Expand All @@ -33,11 +32,14 @@ serde_yaml = "0.9"
uom = "0.35"
whoami = "1.4"
stderrlog = "0.6"
atty = "0.2"
tempfile = "3.10"
common_data_types = { path = "crates/common_data_types" }
nalgebra = "0.32"
arrayvec = "0.7.4"
ouroboros = "0.18.3"
bezier-rs = "=0.4.0"
macros = { path = "crates/macros" }
glam = "0.24"

[build-dependencies]
csv = "1.3.0"
Expand Down
2 changes: 2 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub struct Row {
pub information_kind: bool,
pub solid_angle_kind: bool,
pub temperature_kind: bool,
pub pixel_kind: bool,
pub singular: String,
pub plural: String,
pub abbreviation: String,
Expand Down Expand Up @@ -78,6 +79,7 @@ fn main() {
ratio_type_hint.set_is_information(row.information_kind);
ratio_type_hint.set_is_solid_angle(row.solid_angle_kind);
ratio_type_hint.set_is_temperature(row.temperature_kind);
ratio_type_hint.set_is_pixel(row.pixel_kind);

let dimension = Dimension {
length: row.length,
Expand Down
18 changes: 9 additions & 9 deletions crates/common_data_types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use ordered_float::NotNan;
use paste::paste;
use serde::Serialize;

pub type RawNumber = f64;
pub type Number = NotNan<RawNumber>;
pub type RawFloat = f64;
pub type Float = NotNan<RawFloat>;
pub use ordered_float::{FloatIsNan, ParseNotNanError};
pub use std::f64::consts;

Expand Down Expand Up @@ -49,6 +49,7 @@ impl RatioTypeHint {
const INFORMATION_MASK: u8 = 0x04;
const SOLID_ANGLE_MASK: u8 = 0x08;
const TEMPRATURE_MASK: u8 = 0x10;
const PIXEL_MASK: u8 = 0x20;

bit_getter_setter!(Self::ANGLE_KIND_MASK, angle);
bit_getter_setter!(
Expand All @@ -58,6 +59,7 @@ impl RatioTypeHint {
bit_getter_setter!(Self::INFORMATION_MASK, information);
bit_getter_setter!(Self::SOLID_ANGLE_MASK, solid_angle);
bit_getter_setter!(Self::TEMPRATURE_MASK, temperature);
bit_getter_setter!(Self::PIXEL_MASK, pixel);
}

impl std::ops::BitOr for RatioTypeHint {
Expand All @@ -79,6 +81,7 @@ impl std::fmt::Debug for RatioTypeHint {
.field("is_information", &self.is_information())
.field("is_solid_angle", &self.is_solid_angle())
.field("is_temperature", &self.is_temperature())
.field("is_pixel", &self.is_pixel())
.finish()
}
}
Expand Down Expand Up @@ -196,7 +199,7 @@ impl std::ops::Div<i8> for Dimension {
}

impl Dimension {
pub fn zero() -> Self {
pub const fn zero() -> Self {
Self {
length: 0,
mass: 0,
Expand All @@ -209,10 +212,7 @@ impl Dimension {
}
}

pub fn angle() -> Self {
let mut ratio_type_hint = RatioTypeHint(0);
ratio_type_hint.set_is_angle(true);

pub const fn angle() -> Self {
Self {
length: 0,
mass: 0,
Expand All @@ -221,11 +221,11 @@ impl Dimension {
thermodynamic_temprature: 0,
amount_of_substance: 0,
luminous_intensity: 0,
ratio_type_hint,
ratio_type_hint: RatioTypeHint(RatioTypeHint::ANGLE_KIND_MASK),
}
}

pub fn length() -> Self {
pub const fn length() -> Self {
Self {
length: 1,
mass: 0,
Expand Down
13 changes: 13 additions & 0 deletions crates/macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "macros"
version = "0.1.0"
edition = "2021"


[lib]
proc-macro = true

[dependencies]
quote = "1.0"
syn = { version = "2.0", features = ["derive"] }
proc-macro2 = "1.0"
112 changes: 112 additions & 0 deletions crates/macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, DeriveInput, Fields, Meta};

#[proc_macro_derive(Struct, attributes(default))]
pub fn derive_struct(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);

let struct_name = input.ident;

let contains_span = input.generics.type_params().any(|param| param.ident == "S");

let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
let struct_name_string = struct_name.to_string();
let struct_definition_name = struct_name.to_string();
let struct_definition_name2 = struct_definition_name.clone();

let function_generics = if !contains_span {
quote! { <S: crate::script::parsing::Span> }
} else {
quote! {}
};

let fields = match input.data {
syn::Data::Struct(data) => match data.fields {
Fields::Named(fields) => fields.named,
_ => panic!("Struct must have named fields."),
},
_ => {
panic!("Only structs can be made available to scripts.");
}
};
let define_fields = fields.clone().into_iter().map(|field| {
let name = field.ident.unwrap().to_string();
let ty = field.ty;

let mut default = quote! { None };

for attribute in field.attrs.iter() {
if let Meta::NameValue(name_value) = &attribute.meta {
if name_value.path.is_ident("default") {
let value = &name_value.value;
default = quote! { Some(crate::script::parsing::Litteral::parse(S::from_str(#value)).unwrap().1) };
}
}
}

quote! {
MemberVariable {
name: S::from_str(#name),
ty: MemberVariableType {
ty: <#ty as crate::script::execution::types::TypedObject>::get_type(),
constraints: None,
default_value: #default,
}
}
}
});

let extract_fields = fields.clone().into_iter().map(|field| {
let name = field.ident.unwrap();
let name_string = name.to_string();
let ty = field.ty;

quote! {
let #name = members.remove(#name_string).unwrap();
let #name = #name.downcast::<#ty>(span)?;
}
});

let import_fields = fields.clone().into_iter().map(|field| {
let name = field.ident.unwrap();
quote! { #name }
});

quote! {
impl #impl_generics #struct_name #ty_generics #where_clause {
pub fn unpack_struct #function_generics (span: &S, structure: crate::script::execution::types::Structure<S>) -> std::result::Result<Self, crate::script::execution::Failure<S>> {
// Check that it's the correct type.
if structure.name() == #struct_name_string {
let mut members = Rc::unwrap_or_clone(structure.members);

// Extract fields.
#(#extract_fields)*

// Import fields into the struct.
Ok(#struct_name {
#(#import_fields),*
})
} else {
Err(crate::script::execution::Failure::ExpectedGot(span.clone(), #struct_name_string.into(), structure.name().to_string().into()))
}
}
}
impl #impl_generics #struct_name #ty_generics #where_clause {
fn define_struct #function_generics (context: &mut crate::script::execution::ExecutionContext<'_, S>) {
context.stack.new_variable_str(
#struct_definition_name,
StructDefinition {
definition: Rc::new(parsing::StructDefinition {
name: S::from_str(#struct_definition_name2),
members: vec![
#(#define_fields),*
],
}),
}
.into(),
);
}
}
}.into()
}
2 changes: 2 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ Create a simple ring spacer.
cargo run -- form examples/fornjot_demos.ccm spacer --output ~/output.stl -- 10mm 5mm 5mm
```

### Star

Create a star.
```
cargo run -- form examples/fornjot_demos.ccm star --output ~/output.stl -- 5 10mm 5mm 5mm
Expand Down
Loading

0 comments on commit dca8f30

Please sign in to comment.