Skip to content

Commit

Permalink
embedded adl stdlib
Browse files Browse the repository at this point in the history
  • Loading branch information
millergarym committed Apr 4, 2023
1 parent 6d44cc5 commit 8658ee9
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 7 deletions.
119 changes: 119 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rust/compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ genco = "0.17.3"
convert_case = "0.6.0"
backtrace-on-stack-overflow = "0.3.0"
regex = "1.7.3"
rust-embed = "6.6.1"
54 changes: 54 additions & 0 deletions rust/compiler/src/adlstdlib/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use std::path::PathBuf;
use std::borrow::Cow;

use anyhow::anyhow;

use rust_embed::RustEmbed;

use crate::adlgen::sys::adlast2::ModuleName;

#[derive(RustEmbed)]
#[folder = "../../adl/stdlib/"]
struct Asset;

pub fn std_modules() -> Vec<ModuleName> {
let mut mods = vec![];
for name in Asset::iter() {
let mut path = PathBuf::from(name.to_string());
if let Some(e) = path.extension() {
if e == "adl" {
path.set_extension("");
mods.push(path.to_str().unwrap().replace("/", "."));
}
}
};
mods
}

pub fn get_stdlib(mn: &ModuleName, ext: &str) -> Option<Cow<'static, [u8]>> {
let mut fname = mn.replace(".", "/");
fname.push_str(".adl");
if ext != "" {
fname.push_str("-");
fname.push_str(ext);
}
if let Some(f) = Asset::get(fname.as_str()) {
return Some(f.data);
}
None
}

pub(crate) fn dump(opts: &crate::cli::DumpStdlibOpts) -> Result<(), anyhow::Error> {
std::fs::create_dir_all(&opts.outputdir).map_err(|_| anyhow!("can't create output dir '{:?}'", opts.outputdir))?;
for name in Asset::iter() {
let mut path = opts.outputdir.clone();
path.push(name.as_ref());
if let Some(data) = Asset::get(name.as_ref()) {
std::fs::create_dir_all(path.parent().unwrap()).map_err(|_| anyhow!("can't create output dir for '{:?}'", &path))?;
std::fs::write(&path, data.data.as_ref()).map_err(|s| anyhow!("can't write file '{:?}' error {}", &path, s))?;
} else {
return Err(anyhow!("could get the contents for {}", name));
}
}
Ok(())
}
15 changes: 11 additions & 4 deletions rust/compiler/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ pub mod rust;
pub mod tsgen;
pub mod verify;





pub fn run_cli() -> i32 {
let cli = Cli::parse();

Expand All @@ -19,6 +15,7 @@ pub fn run_cli() -> i32 {
Command::Ast(opts) => ast::ast(&opts),
Command::Rust(opts) => rust::rust(&opts),
Command::Tsgen(opts) => tsgen::tsgen(&opts),
Command::WriteStdlib(opts) => crate::adlstdlib::dump(&opts),
};
match r {
Ok(_) => 0,
Expand Down Expand Up @@ -48,7 +45,17 @@ pub enum Command {
/// generate rust code for the some ADL modules
Rust(RustOpts),
/// generate typescript code for the some ADL modules
#[clap(name="typescript")]
Tsgen(TsOpts),
/// dump the embedded stdlib to the filesystem.
WriteStdlib(DumpStdlibOpts),
}

#[derive(Debug, Args)]
pub struct DumpStdlibOpts {
/// writes generated code to the specified directory
#[arg(long, short='O', value_name="DIR")]
pub outputdir: PathBuf,
}

#[derive(Debug, Args)]
Expand Down
5 changes: 3 additions & 2 deletions rust/compiler/src/cli/tsgen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ pub fn tsgen(opts: &TsOpts) -> anyhow::Result<()> {
let mut writer = TreeWriter::new(opts.output.outputdir.clone(), manifest)?;

if !opts.include_rt {
if opts.runtime_dir == None || opts.ts_style == None {
return Err(anyhow!("Invalid flags; --runtime-dir and --ts-style only valid if --include-runtime is set"));
if opts.runtime_dir == None {
eprintln!("Invalid flags; --runtime-dir only valid if --include-rt is set");
// return Err(anyhow!("Invalid flags; --runtime-dir only valid if --include-rt is set"));
}
}

Expand Down
1 change: 1 addition & 0 deletions rust/compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ pub mod cli;
pub mod parser;
pub mod processing;
pub mod utils;
pub mod adlstdlib;

mod adlgen_dev;
23 changes: 22 additions & 1 deletion rust/compiler/src/processing/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,24 @@ pub trait AdlLoader {

/// Combines a bunch of loaders
pub struct MultiLoader {
embedded: EmbeddedStdlibLoader,
loaders: Vec<Box<dyn AdlLoader>>,
}

impl MultiLoader {
pub fn new(loaders: Vec<Box<dyn AdlLoader>>) -> Self {
MultiLoader { loaders }
MultiLoader {
embedded: EmbeddedStdlibLoader {},
loaders,
}
}
}

impl AdlLoader for MultiLoader {
fn load(&mut self, module_name: &adlast::ModuleName) -> Result<Option<Module0>, anyhow::Error> {
if let Some(module) = self.embedded.load(module_name)? {
return Ok(Some(module));
}
for loader in &mut self.loaders {
if let Some(module) = loader.load(module_name)? {
return Ok(Some(module));
Expand All @@ -47,6 +54,20 @@ impl AdlLoader for MultiLoader {
}
}

pub struct EmbeddedStdlibLoader {}

impl AdlLoader for EmbeddedStdlibLoader {
fn load(&mut self, module_name: &adlast::ModuleName) -> Result<Option<Module0>, anyhow::Error> {
match crate::adlstdlib::get_stdlib(module_name, "") {
Some(data) => match std::str::from_utf8(data.as_ref()) {
Ok(content) => return parse(&content).map(|m| Some(m)),
Err(err) => return Err(anyhow::Error::from(err)),
},
None => return Ok(None),
}
}
}

pub struct DirTreeLoader {
root: PathBuf,
}
Expand Down

0 comments on commit 8658ee9

Please sign in to comment.