-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
223 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,108 @@ | ||
use std::rc::Rc; | ||
|
||
use folidity_diagnostics::Report; | ||
use folidity_semantics::{ | ||
ContractDefinition, | ||
GlobalSymbol, | ||
SymbolInfo, | ||
}; | ||
use z3::{ | ||
ast::Dynamic, | ||
ast::{ | ||
Ast, | ||
Dynamic, | ||
}, | ||
Context, | ||
Solver, | ||
Sort, | ||
}; | ||
|
||
use crate::ast::Declaration; | ||
use crate::{ | ||
ast::{ | ||
Constraint, | ||
Declaration, | ||
Z3Expression, | ||
}, | ||
transformer::transform_expr, | ||
z3_cfg, | ||
}; | ||
|
||
// | ||
|
||
#[derive(Debug, Clone)] | ||
#[derive(Debug)] | ||
pub struct SymbolicExecutor<'ctx> { | ||
/// Global solver of the executor. | ||
solver: Solver<'ctx>, | ||
/// | ||
/// We encapsulate it as it can't be easily transferred between scopes. | ||
context: Context, | ||
/// List of resolved declaration to verify. | ||
pub declarations: Vec<Declaration<'ctx>>, | ||
pub contexts: Vec<Context>, | ||
/// Symbol counter to track boolean constants across the program. | ||
pub symbol_counter: u32, | ||
pub diagnostics: Vec<Report>, | ||
} | ||
|
||
impl<'ctx> SymbolicExecutor<'ctx> { | ||
pub fn new(context: &'ctx Context) -> Self { | ||
pub fn new(context: Context) -> Self { | ||
Self { | ||
solver: Solver::new(context), | ||
context, | ||
contexts: vec![], | ||
declarations: vec![], | ||
diagnostics: vec![], | ||
symbol_counter: 0, | ||
} | ||
} | ||
|
||
pub fn parse_declarations(&mut self, contract: &ContractDefinition) -> Result<(), ()> { | ||
let mut error = false; | ||
let mut diagnostics = Vec::new(); | ||
|
||
for (i, m) in contract.models.iter().enumerate() { | ||
let context = Context::new(&z3_cfg()); | ||
let z3_exprs: Vec<Z3Expression> = m | ||
.bounds | ||
.iter() | ||
.filter_map(|e| { | ||
match transform_expr(e, &context, &mut diagnostics, self) { | ||
Ok(c) => Some(c), | ||
Err(_) => { | ||
error = true; | ||
None | ||
} | ||
} | ||
}) | ||
.collect(); | ||
|
||
// let decl = Declaration { | ||
// decl_sym: GlobalSymbol::Model(SymbolInfo::new(m.loc.clone(), i)), | ||
// parent: m.parent.clone(), | ||
// constraints, | ||
// }; | ||
|
||
// self.declarations.push(decl); | ||
} | ||
|
||
if error { | ||
return Err(()); | ||
} | ||
Ok(()) | ||
} | ||
|
||
/// Create a Z3 constant with the current symbol counter as a name while increasing | ||
/// the counter. | ||
pub fn create_constant( | ||
pub fn create_constant<'a>( | ||
&mut self, | ||
sort: &Sort<'ctx>, | ||
context: &'ctx Context, | ||
) -> (Dynamic<'ctx>, u32) { | ||
sort: &Sort<'a>, | ||
context: &'a Context, | ||
) -> (Dynamic<'a>, u32) { | ||
let id = self.symbol_counter; | ||
let c = Dynamic::new_const(context, id, sort); | ||
let c = Dynamic::new_const(&context, id, sort); | ||
self.symbol_counter += 1; | ||
(c, id) | ||
} | ||
|
||
/// Retrieve the context of the internal `solver`. | ||
pub fn context(&self) -> &Context { | ||
&self.context | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.