Skip to content

Commit

Permalink
more checks
Browse files Browse the repository at this point in the history
  • Loading branch information
edg-l committed Jan 20, 2024
1 parent 843cdec commit ffbf029
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 141 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

54 changes: 51 additions & 3 deletions crates/concrete_check/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use std::{collections::HashMap, ops::Range};

use ariadne::{ColorGenerator, Label, Report, ReportKind};
use ast_helper::AstHelper;
use concrete_ast::{imports::ImportStmt, modules::Module, Program};
use concrete_ast::{common::Ident, imports::ImportStmt, modules::Module, Program};
use concrete_session::Session;
use itertools::Itertools;
use thiserror::Error;

mod ast_helper;
pub mod ast_helper;

#[derive(Error, Debug, Clone)]
pub enum CheckError<'p> {
Expand All @@ -16,6 +16,12 @@ pub enum CheckError<'p> {
module: &'p Module,
import: &'p ImportStmt,
},
#[error("import symbol {:?} not found in module {}", symbol, module.name.name)]
ImportSymbolMissing {
module: &'p Module,
import: &'p ImportStmt,
symbol: &'p Ident,
},
}

impl<'p> CheckError<'p> {
Expand Down Expand Up @@ -59,6 +65,32 @@ impl<'p> CheckError<'p> {
.with_message("Failed to find import.")
.finish()
}
CheckError::ImportSymbolMissing {
module,
import,
symbol,
} => {
let contex_module_span = module.span;
let import_span = import.span;
let offset = symbol.span.from;
Report::build(ReportKind::Error, path.clone(), offset)
.with_code("E1")
.with_label(
Label::new((path.clone(), contex_module_span.into()))
.with_message(format!("In module {:?}.", module.name.name)),
)
.with_label(
Label::new((path.clone(), import_span.into()))
.with_message("In this import statement"),
)
.with_label(
Label::new((path, symbol.span.into()))
.with_message(format!("Failed to find symbol {:?}", symbol.name))
.with_color(colors.next()),
)
.with_message("Failed to find import.")
.finish()
}
}
}
}
Expand All @@ -76,6 +108,7 @@ pub fn check_program<'p>(

let mut imports = HashMap::new();

// check modules
for import in &module.imports {
let target_module = helper.get_module_from_import(&import.module);

Expand All @@ -86,8 +119,23 @@ pub fn check_program<'p>(

let target_module = target_module.unwrap();

// check if symbol exists
for symbol in &import.symbols {
// todo: check if symbol exists.
let name = &symbol.name;
let exists = target_module.functions.get(name).is_some()
|| target_module.constants.get(name).is_some()
|| target_module.structs.get(name).is_some()
|| target_module.types.get(name).is_some();

if !exists {
errors.push(CheckError::ImportSymbolMissing {
module,
import,
symbol,
});
continue;
}

imports.insert(symbol.name.clone(), target_module);
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/concrete_codegen_mlir/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition = "2021"
[dependencies]
bumpalo = { version = "3.14.0", features = ["std"] }
concrete_ast = { path = "../concrete_ast"}
concrete_check = { version = "0.1.0", path = "../concrete_check" }
concrete_session = { path = "../concrete_session"}
itertools = "0.12.0"
llvm-sys = "170.0.1"
Expand Down
127 changes: 0 additions & 127 deletions crates/concrete_codegen_mlir/src/ast_helper.rs

This file was deleted.

3 changes: 1 addition & 2 deletions crates/concrete_codegen_mlir/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use concrete_ast::{
types::TypeSpec,
Program,
};
use concrete_check::ast_helper::{AstHelper, ModuleInfo};
use concrete_session::Session;
use melior::{
dialect::{
Expand All @@ -26,8 +27,6 @@ use melior::{
Context as MeliorContext,
};

use crate::ast_helper::{AstHelper, ModuleInfo};

pub fn compile_program(
session: &Session,
ctx: &MeliorContext,
Expand Down
1 change: 0 additions & 1 deletion crates/concrete_codegen_mlir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ use llvm_sys::{
};
use module::MLIRModule;

mod ast_helper;
mod codegen;
mod context;
mod error;
Expand Down
8 changes: 4 additions & 4 deletions crates/concrete_parser/src/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,12 @@ pub Program: ast::Program = {
// Modules

pub(crate) Module: ast::modules::Module = {
<lo:@L> "mod" <name:Ident> "{" <imports:ImportList?> <contents:ModuleItems> "}" <hi:@R> => {
<lo:@L> "mod" <name:Ident> "{" <imports:ImportList?> <contents:ModuleItems?> "}" <hi:@R> => {
ast::modules::Module {
doc_string: None,
imports: imports.unwrap_or_else(Vec::new),
name,
contents,
contents: contents.unwrap_or_else(Vec::new),
span: Span::new(lo, hi),
}
}
Expand Down Expand Up @@ -270,7 +270,7 @@ pub(crate) Param: ast::functions::Param = {

pub(crate) FunctionDef: ast::functions::FunctionDef = {
<is_pub:"pub"?> "fn" <name:Ident> <generic_params:GenericParams?> "(" <params:Comma<Param>> ")" <ret_type:FunctionRetType?> "{"
<statements:StatementList>
<statements:StatementList?>
"}" => {
ast::functions::FunctionDef {
decl: ast::functions::FunctionDecl {
Expand All @@ -280,7 +280,7 @@ pub(crate) FunctionDef: ast::functions::FunctionDef = {
params,
ret_type,
},
body: statements
body: statements.unwrap_or_else(Vec::new)
}
}
}
Expand Down
27 changes: 23 additions & 4 deletions crates/concrete_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ mod ModuleName {
"##;
let lexer = Lexer::new(source);
let parser = grammar::ProgramParser::new();
dbg!(parser.parse(lexer).unwrap());
parser.parse(lexer).unwrap();
}

#[test]
Expand All @@ -104,7 +104,7 @@ mod ModuleName {
}"##;
let lexer = Lexer::new(source);
let parser = grammar::ProgramParser::new();
dbg!(parser.parse(lexer).unwrap());
parser.parse(lexer).unwrap();
}

#[test]
Expand All @@ -116,7 +116,7 @@ mod ModuleName {
}"##;
let lexer = Lexer::new(source);
let parser = grammar::ProgramParser::new();
dbg!(parser.parse(lexer).unwrap());
parser.parse(lexer).unwrap();
}

#[test]
Expand All @@ -128,6 +128,25 @@ mod ModuleName {
}"##;
let lexer = Lexer::new(source);
let parser = grammar::ProgramParser::new();
dbg!(parser.parse(lexer).unwrap());
parser.parse(lexer).unwrap();
}

#[test]
fn parse_empty_mod() {
let source = r##"mod MyMod {
}"##;
let lexer = Lexer::new(source);
let parser = grammar::ProgramParser::new();
parser.parse(lexer).unwrap();
}

#[test]
fn parse_empty_fn() {
let source = r##"mod MyMod {
fn hello() {}
}"##;
let lexer = Lexer::new(source);
let parser = grammar::ProgramParser::new();
parser.parse(lexer).unwrap();
}
}
11 changes: 11 additions & 0 deletions examples/missing_import.con
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
mod Simple {
import Other.{hello1};
import Other.Deep.{hello2};
import Hello.{world};

fn main() -> i64 {
return hello1(2) + hello2(2);
}
}

mod Hello {
fn a() {
return 1;
}
}

mod A {

}

0 comments on commit ffbf029

Please sign in to comment.