Skip to content
This repository has been archived by the owner on Aug 24, 2022. It is now read-only.

Commit

Permalink
Set up code to generate structs
Browse files Browse the repository at this point in the history
  • Loading branch information
sunjay committed Oct 31, 2019
1 parent 7555633 commit 78a25b4
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
50 changes: 49 additions & 1 deletion src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,52 @@ use crate::gc_lib::GC_LIB_HEADER_FILENAME;
use crate::runtime::RUNTIME_HEADER_FILENAME;
use crate::dino_std::DINO_STD_HEADER_FILENAME;

#[derive(Debug)]
pub struct CStruct {
/// The mangled name of the struct.
///
/// In this case, "mangled" just refers to the fact that the symbol name has been changed from
/// what it was in the original program to something more appropriate for code generation.
pub mangled_name: String,
pub fields: Vec<CStructField>,
}

impl fmt::Display for CStruct {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let Self {mangled_name, fields} = self;

writeln!(f, "struct {} {{", mangled_name)?;

for field in fields {
writeln!(f, "{},", field)?;
}

write!(f, "}}")
}
}

#[derive(Debug)]
pub struct CStructField {
/// The mangled name of the struct field.
///
/// In this case, "mangled" just refers to the fact that the symbol name has been changed from
/// what it was in the original program to something more appropriate for code generation.
pub mangled_name: String,
/// The type of the struct field
pub ty: String,
}

impl fmt::Display for CStructField {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let Self {mangled_name, ty} = self;
write!(f, "{}: {}", mangled_name, ty)
}
}

#[derive(Debug)]
pub struct CExecutableProgram {
/// The structs generated for the program
pub structs: Vec<CStruct>,
/// The list of functions, not including the entry point
///
/// Each of these MUST have a unique name
Expand All @@ -29,7 +73,11 @@ impl fmt::Display for CExecutableProgram {
writeln!(f, "#include \"{}\"", RUNTIME_HEADER_FILENAME)?;
writeln!(f, "#include \"{}\"\n", DINO_STD_HEADER_FILENAME)?;

let Self {functions, entry_point} = self;
let Self {structs, functions, entry_point} = self;

for struct_decl in structs {
writeln!(f, "{}", struct_decl)?;
}

// Output forward declarations so we don't have to worry about outputting the functions in
// a specific order
Expand Down
18 changes: 16 additions & 2 deletions src/trans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,29 @@ pub fn executable(prog: &ir::Program, program_scope: &ProgramDecls) -> Result<CE

let ProgramDecls {top_level_decls: mod_scope, prims} = program_scope;

let mut structs = Vec::new();
let methods = gen_types(types, mod_scope, prims, &mut structs)?;

let mut entry_point = None;
let functions = gen_functions(functions, mod_scope, prims, &mut entry_point)?;
let mut functions = gen_functions(functions, mod_scope, prims, &mut entry_point)?;
functions.extend(methods);

let entry_point = match entry_point {
Some(entry_point) => entry_point,
None => return Err(Error::NoEntryPoint),
};

Ok(CExecutableProgram {functions, entry_point})
Ok(CExecutableProgram {structs, functions, entry_point})
}

/// Returns the functions generated for the methods of all the types
fn gen_types(
types: &[ir::Struct],
mod_scope: &DeclMap,
prims: &Primitives,
structs: &mut Vec<CStruct>,
) -> Result<Vec<CFunction>, Error> {
unimplemented!()
}

fn gen_functions(
Expand Down

0 comments on commit 78a25b4

Please sign in to comment.