Browse Source

Split into modules

master
Colin Reeder 2 years ago
parent
commit
87e30c677b
4 changed files with 94 additions and 69 deletions
  1. +7
    -69
      src/main.rs
  2. +1
    -0
      src/output/mod.rs
  3. +53
    -0
      src/output/rust.rs
  4. +33
    -0
      src/split_type.rs

+ 7
- 69
src/main.rs View File

@@ -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();
}

+ 1
- 0
src/output/mod.rs View File

@@ -0,0 +1 @@
pub mod rust;

+ 53
- 0
src/output/rust.rs View File

@@ -0,0 +1,53 @@
use crate::{Interface, TypeDef};
use crate::split_type::{SplitType, SplitTypeNative};

pub struct RustOutput;

impl crate::LanguageOutput for RustOutput {
fn write_defs(&self, f: &mut dyn std::io::Write, defs: Vec<TypeDef>) -> std::io::Result<()> {
for def in defs {
match def.ty.into() {
SplitType::Interface(interface) => {
write_interface(f, &def.name, interface)?;
},
SplitType::Native(native) => {
println!("type {} = {};", def.name, native_to_rs(native));
}
}
}

Ok(())
}
}

fn write_interface(f: &mut dyn std::io::Write, name: &str, interface: Interface) -> std::io::Result<()> {
writeln!(f, "struct {} {{", name)?;
for (field, ty) in interface.fields {
match ty.into() {
SplitType::Interface(_sub) => {
unimplemented!();
}
SplitType::Native(native) => {
writeln!(f, "{}: {}", field, native_to_rs(native))?;
}
}
}
writeln!(f, "}}")?;

Ok(())
}

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",
}
}


+ 33
- 0
src/split_type.rs View File

@@ -0,0 +1,33 @@
use crate::{Interface, Type};

pub 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),
}
}
}

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

Loading…
Cancel
Save