Browse Source

Initial work on interfaces

master
Colin Reeder 3 years ago
parent
commit
d36c70dc74
2 changed files with 81 additions and 11 deletions
  1. +76
    -11
      src/main.rs
  2. +5
    -0
      src/parser.lalrpop

+ 76
- 11
src/main.rs View File

@@ -11,6 +11,7 @@ pub enum Type {
U16,
U32,
U64,
Interface(Interface),
}

#[derive(Debug)]
@@ -19,18 +20,82 @@ pub struct TypeDef {
ty: Type,
}

#[derive(Debug)]
pub struct Interface {
fields: std::collections::HashMap<String, Type>,
}

enum SplitType {
Native(SplitTypeNative),
Interface(Interface),
}

impl From<Type> for SplitType {
fn from(ty: Type) -> SplitType {
match ty {
Type::I8 => SplitType::Native(SplitTypeNative::I8),
Type::I16 => SplitType::Native(SplitTypeNative::I16),
Type::I32 => SplitType::Native(SplitTypeNative::I32),
Type::I64 => SplitType::Native(SplitTypeNative::I64),
Type::U8 => SplitType::Native(SplitTypeNative::U8),
Type::U16 => SplitType::Native(SplitTypeNative::U16),
Type::U32 => SplitType::Native(SplitTypeNative::U32),
Type::U64 => SplitType::Native(SplitTypeNative::U64),
Type::Interface(interface) => SplitType::Interface(interface),
}
}
}

enum SplitTypeNative {
I8,
I16,
I32,
I64,
U8,
U16,
U32,
U64,
}

fn print_interface(name: &str, interface: Interface) {
println!("struct {} {{", name);
for (field, ty) in interface.fields {
match ty.into() {
SplitType::Interface(_sub) => {
unimplemented!();
}
SplitType::Native(native) => {
println!("{}: {}", field, native_to_rs(native));
}
}
}
println!("}}");
}

fn native_to_rs(native: SplitTypeNative) -> &'static str {
use SplitTypeNative::*;
match native {
I8 => "i8",
I16 => "i16",
I32 => "i32",
I64 => "i64",
U8 => "u8",
U16 => "u16",
U32 => "u32",
U64 => "u64",
}
}

fn main() {
let result = parser::TypeFileParser::new().parse("Test = uint8\nTestAgain = int32").unwrap();
let result = parser::TypeFileParser::new().parse("Test = uint8\nTestAgain = int32\nInterface = {hmm: uint32}").unwrap();
for def in result {
println!("type {} = {};", def.name, match def.ty {
Type::I8 => "i8",
Type::I16 => "i16",
Type::I32 => "i32",
Type::I64 => "i64",
Type::U8 => "u8",
Type::U16 => "u16",
Type::U32 => "u32",
Type::U64 => "u64",
});
match def.ty.into() {
SplitType::Interface(interface) => {
print_interface(&def.name, interface);
},
SplitType::Native(native) => {
println!("type {} = {};", def.name, native_to_rs(native));
}
}
}
}

+ 5
- 0
src/parser.lalrpop View File

@@ -11,6 +11,11 @@ Type: crate::Type = {
"uint16" => crate::Type::U16,
"uint32" => crate::Type::U32,
"uint64" => crate::Type::U64,
Interface => crate::Type::Interface(<>),
};

Interface: crate::Interface = "{" <f:InterfaceField*> "}" => crate::Interface { fields: f.into_iter().collect() };

InterfaceField: (String, crate::Type) = <key:Ident> ":" <ty:Type> => (key.to_owned(), ty);

pub TypeFile: Vec<crate::TypeDef> = (TypeDef)*;

Loading…
Cancel
Save