This commit is contained in:
abdul124 2024-12-24 17:40:48 +08:00
parent 763ab87b32
commit ad2704ecfa
4 changed files with 67 additions and 4 deletions

View File

@ -1 +0,0 @@
../../target/release/libnac3artiq.so

View File

@ -43,7 +43,7 @@ use nac3core::{
OptimizationLevel,
},
nac3parser::{
ast::{Constant, ExprKind, Located, Stmt, StmtKind, StrRef},
ast::{Constant, ExprKind, Located, Location, Stmt, StmtKind, StrRef},
parser::parse_program,
},
symbol_resolver::SymbolResolver,
@ -168,9 +168,10 @@ impl Nac3 {
Ok((module.getattr("__name__")?.extract()?, source_file.to_string(), source))
})?;
let parser_result = parse_program(&source, source_file.into())
let parser_result = parse_program(&source, source_file.clone().into())
.map_err(|e| exceptions::PySyntaxError::new_err(format!("parse error: {e}")))?;
let mut module_content: Vec<Stmt> = Vec::default();
for mut stmt in parser_result {
let include = match stmt.node {
StmtKind::ClassDef { ref decorator_list, ref mut body, ref mut bases, .. } => {
@ -232,9 +233,28 @@ impl Nac3 {
};
if include {
self.top_levels.push((stmt, module_name.clone(), module.clone()));
module_content.push(stmt.clone());
// self.top_levels.push((stmt, module_name.clone(), module.clone()));
}
}
let mut module_body: Vec<Stmt> = Vec::default();
for stmt in module_content.iter() {
match &stmt.node {
StmtKind::FunctionDef { .. } => module_body.push(stmt.clone()),
_ => {}
}
}
// let module_def = StmtKind::ModuleDef {
// name: module_name.clone().into(),
// body: module_body
// };
// let module_stmt = Located::new(Location::new(1, 1, source_file.into()), module_def);
// self.top_levels.push((module_stmt, module_name, module.clone()));
for stmt in module_content {
self.top_levels.push((stmt, module_name.clone().into(), module.clone()));
}
Ok(())
}
@ -1044,6 +1064,34 @@ impl Nac3 {
),
];
// let mut def_idx: usize = 1;
// let mut builtin_methods = Vec::new();
// let mut top_levels: Vec<TopLevelComponent> = Vec::new();
// for (name, signature, callback) in builtins {
// let function_def = TopLevelDef::Function {
// name: format!("__builtins__.{name}").into(),
// simple_name: name,
// signature: signature.ret,
// var_id: Vec::new(),
// instance_to_symbol: HashMap::new(),
// instance_to_stmt: HashMap::new(),
// resolver: None,
// codegen_callback: Some(callback),
// loc: None
// };
// top_levels.push((
// Arc::new(RwLock::new(function_def)),
// "__builtins__",
// None
// ));
// builtin_methods.push((
// name,
// signature.ret,
// DefinitionId(def_idx)
// ));
// def_idx += 1;
// }
let builtins_mod = PyModule::import(py, "builtins").unwrap();
let id_fn = builtins_mod.getattr("id").unwrap();
let numpy_mod = PyModule::import(py, "numpy").unwrap();

View File

@ -115,6 +115,10 @@ pub enum StmtKind<U = ()> {
type_comment: Option<String>,
config_comment: Vec<Ident>,
},
// ModuleDef {
// name: Ident,
// body: Vec<Stmt<U>>,
// },
ClassDef {
name: Ident,
bases: Vec<Expr<U>>,
@ -626,6 +630,12 @@ pub mod fold {
type_comment: Foldable::fold(type_comment, folder)?,
config_comment: Foldable::fold(config_comment, folder)?,
}),
// StmtKind::ModuleDef { name, body } => {
// Ok(StmtKind::ModuleDef {
// name: Foldable::fold(name, folder)?,
// body: Foldable::fold(body, folder)?,
// })
// }
StmtKind::ClassDef { name, bases, keywords, body, decorator_list, config_comment } => {
Ok(StmtKind::ClassDef {
name: Foldable::fold(name, folder)?,

View File

@ -224,6 +224,12 @@ impl TopLevelComposer {
let defined_names = &mut self.defined_names;
match &ast.node {
// ast::StmtKind::ModuleDef { name: module_name, body } => {
// // Add any exclusions to module naming here
// // Module classes and functions are handled separately
// // Register module name and fields here if any
// Ok(())
// }
ast::StmtKind::ClassDef { name: class_name, bases, body, .. } => {
if self.keyword_list.contains(class_name) {
return Err(format!(