Skip to content

Commit

Permalink
Fix parsing pairs with extra parentheses. (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
saeed-zil authored Dec 9, 2024
1 parent e263680 commit e8ded91
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 22 deletions.
44 changes: 22 additions & 22 deletions src/simplified_representation/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ impl AstConverting for SrEmitter {
NodeTypeMapKey::EnclosedAddressMapKeyType(key) => key.visit(self)?,
NodeTypeMapKey::AddressMapKeyType(key) => key.visit(self)?,
};
let identifier = self.pop_ir_identifier()?;
self.stack
.push(StackObject::TypeDefinition(identifier.into()));
Ok(TraversalResult::SkipChildren)
}
fn emit_type_map_value(
Expand All @@ -179,39 +182,26 @@ impl AstConverting for SrEmitter {
match node {
NodeTypeMapValue::MapValueTypeOrEnumLikeIdentifier(value) => {
value.visit(self)?;
let value = self.pop_ir_identifier()?;
let key = self.pop_ir_identifier()?;
let map = SrType {
main_type: "Map".to_string(),
sub_types: vec![key.into(), value.into()],
address_type: None,
};
self.stack.push(StackObject::TypeDefinition(map));
let identifier = self.pop_ir_identifier()?;
self.stack
.push(StackObject::TypeDefinition(identifier.into()));
}
NodeTypeMapValue::MapKeyValue(value) => {
value.visit(self)?;
}
NodeTypeMapValue::MapValueParenthesizedType(value) => {
value.visit(self)?;
let value = self.pop_type_definition()?;
let key = self.pop_ir_identifier()?;
let key = self.pop_type_definition()?;
let map = SrType {
main_type: "Map".to_string(),
sub_types: vec![key.into(), value],
sub_types: vec![key, value],
address_type: None,
};
self.stack.push(StackObject::TypeDefinition(map));
}
NodeTypeMapValue::MapValueParenthesizedType(value) => {
value.visit(self)?;
}
NodeTypeMapValue::MapValueAddressType(value) => {
value.visit(self)?;
let value = self.pop_type_definition()?;
let key = self.pop_ir_identifier()?;
let map = SrType {
main_type: "Map".to_string(),
sub_types: vec![key.into(), value],
address_type: None,
};
self.stack.push(StackObject::TypeDefinition(map));
}
};
}
Expand Down Expand Up @@ -270,6 +260,14 @@ impl AstConverting for SrEmitter {
NodeScillaType::MapType(key, value) => {
let _ = key.visit(self)?;
let _ = value.visit(self)?;
let value = self.pop_type_definition()?;
let key = self.pop_type_definition()?;
let map = SrType {
main_type: "Map".to_string(),
sub_types: vec![key, value],
address_type: None,
};
self.stack.push(StackObject::TypeDefinition(map));
}
NodeScillaType::FunctionType(_a, _b) => {
unimplemented!()
Expand Down Expand Up @@ -666,7 +664,9 @@ impl AstConverting for SrEmitter {
match mode {
TreeTraversalMode::Enter => {
match node {
NodeTypeMapValueArguments::EnclosedTypeMapValue(_) => todo!(),
NodeTypeMapValueArguments::EnclosedTypeMapValue(n) => {
n.visit(self)?;
}
NodeTypeMapValueArguments::GenericMapValueArgument(g) => {
g.visit(self)?;
let identifier = self.pop_ir_identifier()?;
Expand Down
10 changes: 10 additions & 0 deletions tests/contracts/Map.scilla
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
scilla_version 0
contract DifferentMaps
()

field first_map: Map String BNum
= Emp String BNum
field status3days: Map String (Pair (ByStr20) (BNum))
= Emp String (Pair (ByStr20) (BNum))
field reward_pairs : Map ByStr20 (List Uint128)
= Emp (ByStr20) (List Uint128)
34 changes: 34 additions & 0 deletions tests/full_contract_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,40 @@ fn test_parse() -> Result<(), Box<dyn Error>> {
Ok(())
}

#[test]
fn test_map_contract_parse() {
let contract_path = PathBuf::from("tests/contracts/Map.scilla");
let contract = Contract::parse(&contract_path).unwrap();
assert_eq!(
contract,
Contract {
name: "DifferentMaps".to_string(),
init_params: FieldList::default(),
fields: FieldList(vec![
Field::new(
"first_map",
Type::Map(Box::new(Type::String), Box::new(Type::BNum))
),
Field::new(
"status3days",
Type::Map(
Box::new(Type::String),
Box::new(Type::Pair(Box::new(Type::ByStr20), Box::new(Type::BNum)))
)
),
Field::new(
"reward_pairs",
Type::Map(
Box::new(Type::ByStr20),
Box::new(Type::List(Box::new(Type::Uint128)))
)
)
]),
transitions: TransitionList::default()
}
);
}

#[test]
fn test_bystr_contract_parse() {
let contract_path = PathBuf::from("tests/contracts/ByStr.scilla");
Expand Down

0 comments on commit e8ded91

Please sign in to comment.