Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add C++ translator #270

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions enderpy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ edition = "2021"
[dependencies]
enderpy_python_parser = { path = "../parser" , version = "0.1.0" }
enderpy_python_type_checker = { path = "../typechecker" , version = "0.1.0" }
corepy_python_translator = { path = "../translator", version = "0.1.0" }
clap = { version = "4.5.17", features = ["derive"] }
miette.workspace = true
2 changes: 2 additions & 0 deletions enderpy/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pub enum Commands {
},
/// Type check
Check { path: PathBuf },
/// Translate to C++
Translate { path: PathBuf },
/// Symbol table
Symbols { path: PathBuf },

Expand Down
30 changes: 30 additions & 0 deletions enderpy/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ use std::{
fs::{self, File},
io::{self, Read},
path::{Path, PathBuf},
sync::Arc,
};

use clap::Parser as ClapParser;
use cli::{Cli, Commands};
use enderpy_python_parser::{get_row_col_position, parser::parser::Parser, Lexer};
use enderpy_python_type_checker::{build::BuildManager, find_project_root, settings::Settings};
use corepy_python_translator::translator::CppTranslator;
use miette::{bail, IntoDiagnostic, Result};

mod cli;
Expand All @@ -18,6 +20,7 @@ fn main() -> Result<()> {
Commands::Tokenize {} => tokenize(),
Commands::Parse { file } => parse(file),
Commands::Check { path } => check(path),
Commands::Translate { path } => translate(path),
Commands::Watch => watch(),
Commands::Symbols { path } => symbols(path),
}
Expand Down Expand Up @@ -134,6 +137,33 @@ fn check(path: &Path) -> Result<()> {
Ok(())
}

fn translate(path: &Path) -> Result<()> {
if path.is_dir() {
bail!("Path must be a file");
}
let root = find_project_root(path);
let python_executable = Some(get_python_executable()?);
let typeshed_path = get_typeshed_path()?;
let settings = Settings {
typeshed_path,
python_executable,
};
let build_manager = BuildManager::new(settings);
build_manager.build(root);
build_manager.build_one(root, path);
let id = build_manager.paths.get(path).unwrap();
let file = build_manager.files.get(&id).unwrap();
let checker = Arc::new(build_manager.type_check(path, &file));
let mut translator = CppTranslator::new(checker.clone(), &file);
translator.translate();
println!("{:?}", file.tree);
println!("====");
println!("{}", translator.output);
println!("====");
print!("{}", checker.clone().dump_types());
Ok(())
}

fn watch() -> Result<()> {
todo!()
}
Expand Down
8 changes: 7 additions & 1 deletion parser/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,13 @@ impl Constant {
} else {
Cow::Borrowed("false")
}
}
},
ConstantValue::Int => Cow::Borrowed(
&source[self.node.start as usize..self.node.end as usize],
),
ConstantValue::Float => Cow::Borrowed(
&source[self.node.start as usize..self.node.end as usize],
),
_ => todo!("Call the parser and get the value"),
}
}
Expand Down
9 changes: 9 additions & 0 deletions translator/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "corepy_python_translator"
version = "0.1.0"
edition = "2021"

[dependencies]
enderpy_python_parser = { path = "../parser", version = "0.1.0" }
enderpy_python_type_checker = {path = "../typechecker", version = "0.1.0" }
log = { version = "0.4.17" }
1 change: 1 addition & 0 deletions translator/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod translator;
Loading
Loading