-
-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement
CompositeType
and MemberAcccess Expression
- Loading branch information
1 parent
3b83aae
commit d85bddd
Showing
10 changed files
with
291 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use std::any::Any; | ||
use std::collections::HashMap; | ||
|
||
use super::base::DataType; | ||
|
||
#[derive(Clone)] | ||
pub struct CompositeType { | ||
pub name: String, | ||
pub members: HashMap<String, Box<dyn DataType>>, | ||
} | ||
|
||
impl DataType for CompositeType { | ||
fn literal(&self) -> String { | ||
self.name.to_string() | ||
} | ||
|
||
fn equals(&self, other: &Box<dyn DataType>) -> bool { | ||
if other.is_any() { | ||
return true; | ||
} | ||
|
||
let composite_type: Box<dyn DataType> = Box::new(self.clone()); | ||
if other.is_variant_contains(&composite_type) { | ||
return true; | ||
} | ||
|
||
if let Some(other_composite) = other.as_any().downcast_ref::<CompositeType>() { | ||
if self.name.ne(&other_composite.name) { | ||
return false; | ||
} | ||
|
||
return self.members.eq(&other_composite.members); | ||
} | ||
|
||
false | ||
} | ||
|
||
fn as_any(&self) -> &dyn Any { | ||
self | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use std::any::Any; | ||
use std::cmp::Ordering; | ||
use std::collections::HashMap; | ||
|
||
use gitql_ast::types::base::DataType; | ||
use gitql_ast::types::composite::CompositeType; | ||
|
||
use super::base::Value; | ||
|
||
#[derive(Clone)] | ||
pub struct CompositeValue { | ||
pub name: String, | ||
pub members: HashMap<String, Box<dyn Value>>, | ||
} | ||
|
||
impl Value for CompositeValue { | ||
fn literal(&self) -> String { | ||
let mut str = String::new(); | ||
let last_position = self.members.len() - 1; | ||
str += "("; | ||
for (pos, member) in self.members.iter().enumerate() { | ||
str += &member.1.literal(); | ||
if pos != last_position { | ||
str += ", "; | ||
} | ||
} | ||
str += ")"; | ||
str | ||
} | ||
|
||
fn equals(&self, other: &Box<dyn Value>) -> bool { | ||
if let Some(other_composite) = other.as_any().downcast_ref::<CompositeValue>() { | ||
return self.name.eq(&other_composite.name) | ||
&& self.members.eq(&other_composite.members); | ||
} | ||
false | ||
} | ||
|
||
fn compare(&self, _other: &Box<dyn Value>) -> Option<Ordering> { | ||
None | ||
} | ||
|
||
fn data_type(&self) -> Box<dyn DataType> { | ||
let name = self.name.to_string(); | ||
let mut members: HashMap<String, Box<dyn DataType>> = HashMap::new(); | ||
for member in self.members.iter() { | ||
members.insert(member.0.to_string(), member.1.data_type().clone()); | ||
} | ||
Box::new(CompositeType { name, members }) | ||
} | ||
|
||
fn as_any(&self) -> &dyn Any { | ||
self | ||
} | ||
} |
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.