Skip to content

Commit

Permalink
move parse_file into Context and add debug field for debug ast transf…
Browse files Browse the repository at this point in the history
…orm with placeholder implementation
  • Loading branch information
nthiad committed Oct 19, 2023
1 parent e601c22 commit 5b1cc75
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 9 deletions.
16 changes: 16 additions & 0 deletions compiler/noirc_frontend/src/debug/ast.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use noirc_errors::Location;
use chumsky::prelude::recursive;
use crate::parser::ParsedModule;
use crate::ast;

// debug struct
// assert values
// variable values
// function arguments
// program gets linearized so maybe show arguments as if that wasn't the case

fn walk_expr(expr: &mut ast::Expression) {
}

pub fn insert_debug_symbols(module: &mut ParsedModule) {
}
2 changes: 2 additions & 0 deletions compiler/noirc_frontend/src/debug/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod ast;
pub use ast::insert_debug_symbols;
5 changes: 2 additions & 3 deletions compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use super::{
},
errors::{DefCollectorErrorKind, DuplicateType},
};
use crate::hir::def_map::{parse_file, LocalModuleId, ModuleData, ModuleId};
use crate::hir::def_map::{LocalModuleId, ModuleData, ModuleId};
use crate::hir::resolution::import::ImportDirective;
use crate::hir::Context;

Expand Down Expand Up @@ -525,8 +525,7 @@ impl<'a> ModCollector<'a> {
context.visited_files.insert(child_file_id, location);

// Parse the AST for the module we just found and then recursively look for it's defs
let (ast, parsing_errors) = parse_file(&context.file_manager, child_file_id);
let ast = ast.into_sorted();
let (ast, parsing_errors) = context.parse_file(child_file_id);

errors.extend(
parsing_errors.iter().map(|e| (e.clone().into(), child_file_id)).collect::<Vec<_>>(),
Expand Down
7 changes: 3 additions & 4 deletions compiler/noirc_frontend/src/hir/def_map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,8 @@ impl CrateDefMap {
}

// First parse the root file.
let root_file_id = context.crate_graph[crate_id].root_file_id;
let (ast, parsing_errors) = parse_file(&context.file_manager, root_file_id);
let ast = ast.into_sorted();
let root_file_id = context.get_root_id(crate_id);
let (ast, parsing_errors) = context.parse_file(root_file_id);

#[cfg(feature = "aztec")]
let ast = match super::aztec_library::transform(ast, &crate_id, context) {
Expand Down Expand Up @@ -257,7 +256,7 @@ pub struct Contract {
pub events: Vec<StructId>,
}

/// Given a FileId, fetch the File, from the FileManager and parse it's content
/// Given a FileId, fetch the File, from the FileManager and parse it's content.
pub fn parse_file(fm: &FileManager, file_id: FileId) -> (ParsedModule, Vec<ParserError>) {
let file = fm.fetch_file(file_id);
parse_program(file.source())
Expand Down
22 changes: 20 additions & 2 deletions compiler/noirc_frontend/src/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ pub(crate) mod aztec_library;

use crate::graph::{CrateGraph, CrateId, Dependency};
use crate::hir_def::function::FuncMeta;
use crate::debug;
use crate::node_interner::{FuncId, NodeInterner, StructId};
use def_map::{Contract, CrateDefMap};
use fm::FileManager;
use crate::parser::{parse_program, SortedModule, ParserError};
use def_map::{Contract, CrateDefMap, parse_file};
use fm::{FileManager, FileId};
use noirc_errors::Location;
use std::collections::BTreeMap;

Expand All @@ -25,6 +27,7 @@ pub struct Context {
pub crate_graph: CrateGraph,
pub(crate) def_maps: BTreeMap<CrateId, CrateDefMap>,
pub file_manager: FileManager,
pub debug: bool,

/// A map of each file that already has been visited from a prior `mod foo;` declaration.
/// This is used to issue an error if a second `mod foo;` is declared to the same file.
Expand Down Expand Up @@ -53,6 +56,7 @@ impl Context {
crate_graph,
file_manager,
storage_slots: BTreeMap::new(),
debug: false,
}
}

Expand Down Expand Up @@ -195,4 +199,18 @@ impl Context {
*next_slot
})
}

/// Given a FileId, fetch the File, from the FileManager and parse its content,
/// applying sorting and debug transforms if debug mode is enabled.
pub fn parse_file(&self, file_id: FileId) -> (SortedModule, Vec<ParserError>) {
println!("file_id={:?}", file_id);
let (mut ast, parsing_errors) = parse_file(&self.file_manager, file_id);
debug::ast::insert_debug_symbols(&mut ast);
let ast = ast.into_sorted();
(ast, parsing_errors)
}

pub fn get_root_id(&self, crate_id: CrateId) -> FileId {
self.crate_graph[crate_id].root_file_id
}
}
1 change: 1 addition & 0 deletions compiler/noirc_frontend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#![warn(clippy::semicolon_if_nothing_returned)]

pub mod ast;
pub mod debug;
pub mod graph;
pub mod lexer;
pub mod monomorphization;
Expand Down

0 comments on commit 5b1cc75

Please sign in to comment.