|
|
@@ -1,6 +1,9 @@ |
|
|
|
use lalrpop_util::lalrpop_mod; |
|
|
|
lalrpop_mod!(pub parser); |
|
|
|
|
|
|
|
mod output; |
|
|
|
mod split_type; |
|
|
|
|
|
|
|
#[derive(Debug)] |
|
|
|
pub enum Type { |
|
|
|
I8, |
|
|
@@ -25,65 +28,8 @@ 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", |
|
|
|
} |
|
|
|
trait LanguageOutput { |
|
|
|
fn write_defs(&self, f: &mut dyn std::io::Write, defs: Vec<TypeDef>) -> std::io::Result<()>; |
|
|
|
} |
|
|
|
|
|
|
|
fn main() { |
|
|
@@ -91,14 +37,6 @@ fn main() { |
|
|
|
let mut content = String::new(); |
|
|
|
std::io::stdin().read_to_string(&mut content).expect("Failed to read stdin"); |
|
|
|
let result = parser::TypeFileParser::new().parse(&content).unwrap(); |
|
|
|
for def in result { |
|
|
|
match def.ty.into() { |
|
|
|
SplitType::Interface(interface) => { |
|
|
|
print_interface(&def.name, interface); |
|
|
|
}, |
|
|
|
SplitType::Native(native) => { |
|
|
|
println!("type {} = {};", def.name, native_to_rs(native)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
output::rust::RustOutput.write_defs(&mut std::io::stdout(), result).unwrap(); |
|
|
|
} |