diff --git a/src/back/msl.rs b/src/back/msl.rs index 74fa85376..75b4a3841 100644 --- a/src/back/msl.rs +++ b/src/back/msl.rs @@ -1,4 +1,4 @@ -use std::fmt::{Error as FmtError, Write}; +use std::fmt::{Display, Error as FmtError, Formatter, Write}; pub struct Options { @@ -15,6 +15,94 @@ impl From for Error { } } +trait Indexed { + const CLASS: &'static str; + fn id(&self) -> usize; +} + +impl Indexed for crate::Token { + const CLASS: &'static str = "Struct"; + fn id(&self) -> usize { self.index() } +} +impl Indexed for crate::Token { + const CLASS: &'static str = "global"; + fn id(&self) -> usize { self.index() } +} +impl Indexed for crate::Token { + const CLASS: &'static str = "function"; + fn id(&self) -> usize { self.index() } +} + +struct MemberIndex(usize); +impl Indexed for MemberIndex { + const CLASS: &'static str = "field"; + fn id(&self) -> usize { self.0 } +} + +struct ParameterIndex(usize); +impl Indexed for ParameterIndex { + const CLASS: &'static str = "param"; + fn id(&self) -> usize { self.0 } +} + + +enum Name<'a, I> { + Custom(&'a str), + Index(I), +} + +impl Display for Name<'_, I> { + fn fmt(&self, formatter: &mut Formatter<'_>) -> Result<(), FmtError> { + match *self { + Name::Custom(name) => formatter.write_str(name), + Name::Index(ref index) => write!(formatter, "{}{}", I::CLASS, index.id()), + } + } +} + +trait AsName { + fn or_index(&self, index: I) -> Name; +} + +impl AsName for Option { + fn or_index(&self, index: I) -> Name { + match *self { + Some(ref name) => Name::Custom(name), + None => Name::Index(index), + } + } +} + +struct TypedVar<'a, T>(&'a crate::Type, &'a T, &'a crate::Storage); + +impl Display for TypedVar<'_, T> { + fn fmt(&self, formatter: &mut Formatter<'_>) -> Result<(), FmtError> { + match *self.0 { + crate::Type::Void => { + write!(formatter, "void {}", self.1) + }, + crate::Type::Scalar { kind, .. } => { + write!(formatter, "{} {}", scalar_kind_string(kind), self.1) + }, + crate::Type::Vector { size, kind, .. } => { + write!(formatter, "{}{} {}", scalar_kind_string(kind), vector_size_string(size), self.1) + }, + crate::Type::Matrix { columns, rows, kind, .. } => { + write!(formatter, "{}{}x{} {}", scalar_kind_string(kind), vector_size_string(columns), vector_size_string(rows), self.1) + } + crate::Type::Array { ref base, length } => { + write!(formatter, "{}[{}]", TypedVar(base, self.1, self.2), length) + } + crate::Type::Pointer { ref base, .. } => { + write!(formatter, "{}*{}", TypedVar(base, &"", self.2), self.1) + } + crate::Type::Struct(token) => { + write!(formatter, "{} {}", self.2[token].name.or_index(token), self.1) + } + } + } +} + pub struct Writer { out: W, } @@ -36,59 +124,6 @@ fn vector_size_string(size: crate::VectorSize) -> &'static str { } impl Writer { - fn put_type_pre(&mut self, ty: &crate::Type) -> Result<(), FmtError> { - match *ty { - crate::Type::Void => { - write!(self.out, "void") - }, - crate::Type::Scalar { kind, .. } => { - write!(self.out, "{}", scalar_kind_string(kind)) - }, - crate::Type::Vector { size, kind, .. } => { - write!(self.out, "{}{}", scalar_kind_string(kind), vector_size_string(size)) - }, - crate::Type::Matrix { columns, rows, kind, .. } => { - write!(self.out, "{}{}x{}", scalar_kind_string(kind), vector_size_string(columns), vector_size_string(rows)) - } - crate::Type::Array { ref base, .. } => { - self.put_type_pre(base) - } - _ => panic!("Unknown type {:?}", ty), - } - } - - fn put_type_post(&mut self, ty: &crate::Type) -> Result<(), FmtError> { - match *ty { - crate::Type::Array { length, .. } => { - write!(self.out, "[{}]", length) - } - _ => Ok(()) - } - } - - fn put_struct_name( - &mut self, - token: crate::Token, - module: &crate::Module, - ) -> Result<(), FmtError> { - match module.struct_declarations[token].name { - Some(ref name) => write!(self.out, "{}", name), - None => write!(self.out, "_Struct{}", token.index()), - } - } - - fn put_struct_member_name( - &mut self, - token: crate::Token, - index: usize, - module: &crate::Module, - ) -> Result<(), FmtError> { - match module.struct_declarations[token].members[index].name { - Some(ref name) => write!(self.out, "{}", name), - None => write!(self.out, "_field{}", index), - } - } - pub fn write(&mut self, module: &crate::Module) -> Result<(), Error> { writeln!(self.out, "#include ")?; writeln!(self.out, "#include ")?; @@ -96,20 +131,41 @@ impl Writer { writeln!(self.out, "")?; for (token, decl) in module.struct_declarations.iter() { - write!(self.out, "struct ")?; - self.put_struct_name(token, module)?; - writeln!(self.out, " {{")?; + writeln!(self.out, "struct {} {{", decl.name.or_index(token))?; for (index, member) in decl.members.iter().enumerate() { - write!(self.out, "\t")?; - self.put_type_pre(&member.ty)?; - write!(self.out, " ")?; - self.put_struct_member_name(token, index, module)?; - self.put_type_post(&member.ty)?; - writeln!(self.out, ";")?; + let name = member.name.or_index(MemberIndex(index)); + let tv = TypedVar(&member.ty, &name, &module.struct_declarations); + writeln!(self.out, "\t{};", tv)?; } writeln!(self.out, "}};")?; } + let mut globals_used = Vec::new(); + writeln!(self.out, "")?; + for (fun_token, fun) in module.functions.iter() { + let fun_name = fun.name.or_index(fun_token); + let fun_tv = TypedVar(&fun.return_type, &fun_name, &module.struct_declarations); + writeln!(self.out, "{}(", fun_tv)?; + for (index, ty) in fun.parameter_types.iter().enumerate() { + let name = Name::Index(ParameterIndex(index)); + let tv = TypedVar(ty, &name, &module.struct_declarations); + writeln!(self.out, "\t{},", tv)?; + } + for (_, expr) in fun.expressions.iter() { + if let crate::Expression::GlobalVariable(token) = *expr { + if !globals_used.contains(&token) { + globals_used.push(token); + let var = &module.global_variables[token]; + let name = var.name.or_index(token); + let tv = TypedVar(&var.ty, &name, &module.struct_declarations); + writeln!(self.out, "\t{},", tv)?; + } + } + } + writeln!(self.out, ") {{")?; + writeln!(self.out, "}}")?; + } + Ok(()) } } diff --git a/src/lib.rs b/src/lib.rs index 49b18f8a3..bfb0e63a1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,12 +40,6 @@ pub enum ScalarKind { Float, } -#[derive(Debug)] -pub struct PointerDeclaration { - pub base: Type, - pub length: u32, -} - #[derive(Debug)] pub struct StructMember { pub name: Option,