-
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
6 changed files
with
170 additions
and
25 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
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,6 +1,23 @@ | ||
use contract::ContractDefinition; | ||
use folidity_parser::ast::Source; | ||
|
||
mod ast; | ||
mod contract; | ||
mod decls; | ||
mod global_symbol; | ||
mod symtable; | ||
mod types; | ||
|
||
#[cfg(test)] | ||
mod tests; | ||
|
||
/// Resolves the contract's parsed tree into the semantically analysed and typed-checked definition. | ||
/// | ||
/// # Errors | ||
/// [`ContractDefinition`] may contain errors stored in the `diagnostics` field. | ||
pub fn resolve_semantics(source: &Source) -> ContractDefinition { | ||
let mut definition = ContractDefinition::default(); | ||
let delay = definition.resolve_declarations(source); | ||
|
||
definition | ||
} |
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use crate::resolve_semantics; | ||
use folidity_parser::parse; | ||
|
||
const DECL_SRC: &str = r#" | ||
struct MyStruct { | ||
a: int, | ||
b: address | ||
} | ||
enum MyEnum { | ||
A, | ||
B | ||
} | ||
model MyModel: ParentModel { | ||
c: int, | ||
b: string, | ||
} | ||
state NoState(MyModel) | ||
"#; | ||
|
||
#[test] | ||
fn test_first_pass() { | ||
let tree = parse(DECL_SRC).unwrap(); | ||
|
||
let def = resolve_semantics(&tree); | ||
assert_eq!(def.structs.len(), 1); | ||
assert_eq!(def.enums.len(), 1); | ||
assert_eq!(def.models.len(), 1); | ||
assert_eq!(def.states.len(), 1); | ||
|
||
let e = &def.enums[0]; | ||
assert_eq!(e.variants.len(), 2); | ||
|
||
assert!(e.variants.contains_key("A")); | ||
assert!(e.variants.contains_key("B")); | ||
} |
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 +1,63 @@ | ||
use crate::ast::{List, Mapping, Set, Type, TypeVariant}; | ||
use crate::contract::ContractDefinition; | ||
use crate::global_symbol::GlobalSymbol; | ||
use folidity_diagnostics::Report; | ||
use folidity_parser::ast as parsed_ast; | ||
use folidity_parser::ast::Identifier; | ||
|
||
/// Maps type from parsed AST to semantically resolved type. | ||
/// - Primitive types are simply mapped 1-1. | ||
/// - User defined types (e.g. structs, enums) are looked up in the global symbol table. | ||
/// - List types are recursively mapped. | ||
pub fn map_type(contract: &mut ContractDefinition, ty: &parsed_ast::Type) -> Result<Type, ()> { | ||
let variant = match &ty.ty { | ||
parsed_ast::TypeVariant::Int => TypeVariant::Int, | ||
parsed_ast::TypeVariant::Uint => TypeVariant::Uint, | ||
parsed_ast::TypeVariant::Float => TypeVariant::Float, | ||
parsed_ast::TypeVariant::Char => TypeVariant::Char, | ||
parsed_ast::TypeVariant::String => TypeVariant::String, | ||
parsed_ast::TypeVariant::Hex => TypeVariant::Hex, | ||
parsed_ast::TypeVariant::Address => TypeVariant::Address, | ||
parsed_ast::TypeVariant::Unit => TypeVariant::Address, | ||
parsed_ast::TypeVariant::Bool => TypeVariant::Bool, | ||
parsed_ast::TypeVariant::Set(s) => { | ||
let set_ty = map_type(contract, &s.ty)?; | ||
TypeVariant::Set(Set::new(Box::new(set_ty))) | ||
} | ||
parsed_ast::TypeVariant::List(l) => { | ||
let list_ty = map_type(contract, &l.ty)?; | ||
TypeVariant::List(List::new(Box::new(list_ty))) | ||
} | ||
parsed_ast::TypeVariant::Mapping(m) => { | ||
let m_from_ty = map_type(contract, &m.from_ty)?; | ||
let m_to_ty = map_type(contract, &m.to_ty)?; | ||
TypeVariant::Mapping(Mapping::new( | ||
Box::new(m_from_ty), | ||
m.relation.clone(), | ||
Box::new(m_to_ty), | ||
)) | ||
} | ||
parsed_ast::TypeVariant::Custom(user_ty) => { | ||
if let Some(symbol) = contract.declaration_symbols.get(&user_ty.name) { | ||
match symbol { | ||
GlobalSymbol::Struct(info) => TypeVariant::Struct(info.clone()), | ||
GlobalSymbol::Model(info) => TypeVariant::Model(info.clone()), | ||
GlobalSymbol::Enum(info) => TypeVariant::Enum(info.clone()), | ||
GlobalSymbol::State(info) => TypeVariant::State(info.clone()), | ||
GlobalSymbol::Function(info) => TypeVariant::Function(info.clone()), | ||
} | ||
} else { | ||
contract.diagnostics.push(Report::semantic_error( | ||
user_ty.loc.clone(), | ||
format!("`{}` is not defined", user_ty.name), | ||
)); | ||
return Err(()); | ||
} | ||
} | ||
}; | ||
|
||
Ok(Type { | ||
loc: ty.loc.clone(), | ||
ty: variant, | ||
}) | ||
} |