-
Notifications
You must be signed in to change notification settings - Fork 6
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
1 parent
83ba6ed
commit 2b359b6
Showing
6 changed files
with
114 additions
and
51 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,4 @@ | ||
//! A collection of collectors (heh) necessary for the well-being of the type system. | ||
pub mod constants; | ||
pub mod primitives; |
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,57 @@ | ||
//! Collect all primitive union type constants in the program in order to build our primitive union types properly. There are three primitive union types: `char`, `int` and `string`, so this module collects all character, integer and string constants. | ||
use std::collections::HashSet; | ||
use std::convert::Infallible; | ||
|
||
use fir::{Fallible, Fir, Node, OriginIdx, RefIdx, Traversal}; | ||
use flatten::{AstInfo, FlattenData}; | ||
|
||
#[derive(Default)] | ||
pub struct ConstantCollector { | ||
pub(crate) integers: HashSet<RefIdx>, | ||
pub(crate) characters: HashSet<RefIdx>, | ||
pub(crate) strings: HashSet<RefIdx>, | ||
} | ||
|
||
impl ConstantCollector { | ||
pub fn new() -> ConstantCollector { | ||
ConstantCollector::default() | ||
} | ||
|
||
fn add_integer(&mut self, idx: OriginIdx) { | ||
self.integers.insert(RefIdx::Resolved(idx)); | ||
} | ||
|
||
fn add_character(&mut self, idx: OriginIdx) { | ||
self.characters.insert(RefIdx::Resolved(idx)); | ||
} | ||
|
||
fn add_string(&mut self, idx: OriginIdx) { | ||
self.strings.insert(RefIdx::Resolved(idx)); | ||
} | ||
} | ||
|
||
impl Traversal<FlattenData<'_>, Infallible> for ConstantCollector { | ||
fn traverse_constant( | ||
&mut self, | ||
_: &Fir<FlattenData<'_>>, | ||
node: &Node<FlattenData<'_>>, | ||
_: &RefIdx, | ||
) -> Fallible<Infallible> { | ||
match node.data.ast { | ||
AstInfo::Node(ast::Ast { | ||
node: ast::Node::Constant(value), | ||
.. | ||
}) => match value { | ||
ast::Value::Integer(_) => self.add_integer(node.origin), | ||
ast::Value::Char(_) => self.add_character(node.origin), | ||
ast::Value::Str(_) => self.add_string(node.origin), | ||
// do nothing - the other constants are not part of primitive union types | ||
_ => {} | ||
}, | ||
_ => unreachable!("Fir constant with non-node AST info. this is an interpreter error"), | ||
}; | ||
|
||
Ok(()) | ||
} | ||
} |
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