Skip to content

Commit

Permalink
Fix total ordering in ascript types & ensure struct ids are interned
Browse files Browse the repository at this point in the history
  • Loading branch information
acweathersby committed May 2, 2024
1 parent 40b1ad2 commit 7113893
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
12 changes: 8 additions & 4 deletions crates/radlr-ascript/build_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ fn collect_types(adb: &mut AscriptDatabase) {
Some(node) => {
extract_type_data(node.get_type().to_cardinal(), types);
}
None => {}
None => {
panic!("GraphNode should be resolved");
}
},

_ => {}
Expand Down Expand Up @@ -257,8 +259,9 @@ pub fn process_struct_node(
strct: &AST_Struct,
g_id: GrammarIdentities,
) -> RadlrResult<StructInitializer> {
let s_store = adb.db.string_store();
let name = &strct.ty[2..];
let struct_id = StringId::from(name);
let struct_id = StringId(name.intern(s_store));

let mut initializer = StructInitializer {
name: struct_id,
Expand All @@ -272,6 +275,7 @@ pub fn process_struct_node(

let ast_struct = adb.structs.entry(struct_id).or_insert_with(|| {
existing_struct = false;

AscriptStruct {
id: struct_id,
name: name.to_string(),
Expand Down Expand Up @@ -304,7 +308,7 @@ pub fn process_struct_node(
}
}

initializer.props.insert(StringId::from(prop.id.to_string()), Initializer {
initializer.props.insert(StringId(prop.id.to_string().intern(s_store)), Initializer {
ty: AscriptType::Undefined,
name: prop_name,
output_graph: None,
Expand All @@ -313,7 +317,7 @@ pub fn process_struct_node(
});
}
ast @ ASTNode::AST_Token(..) => {
let tok_id = StringId::from("tok");
let tok_id = StringId("tok".intern(s_store));
initializer.props.insert(tok_id, Initializer {
ty: AscriptType::Undefined,
name: tok_id,
Expand Down
50 changes: 47 additions & 3 deletions crates/radlr-ascript/types/ast_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl ValueObj for AscriptPropType {
}
}

#[derive(Debug, Clone, Copy, Default, Eq, Hash, PartialOrd, Ord)]
#[derive(Debug, Clone, Copy, Default, Eq, Hash)]
pub enum AscriptType {
#[default]
Undefined,
Expand Down Expand Up @@ -54,6 +54,41 @@ impl AscriptType {
}
}

impl PartialOrd for AscriptType {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}

impl Ord for AscriptType {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
use std::cmp::Ordering::*;
use AscriptType::*;
let result = match (self, other) {
(Undefined, Undefined) => Equal,

(Undefined, _) => Less,

(_, Undefined) => Greater,

(Scalar(c), Scalar(d)) => c.cmp(d),
(Aggregate(c), Aggregate(d)) => c.cmp(d),
(NonTerminalVal(c), NonTerminalVal(d)) => c.cmp(d),

(NonTerminalVal(_), Scalar(_)) => Less,
(Scalar(_), NonTerminalVal(_)) => Greater,

(Scalar(_), Aggregate(_)) => Less,
(Aggregate(_), Scalar(_)) => Greater,

(NonTerminalVal(_), Aggregate(_)) => Less,
(Aggregate(_), NonTerminalVal(_)) => Greater,
};

result
}
}

impl PartialEq for AscriptType {
fn eq(&self, other: &Self) -> bool {
if std::mem::discriminant(self) != std::mem::discriminant(other) {
Expand Down Expand Up @@ -497,13 +532,22 @@ pub enum AscriptAggregateType {

impl PartialOrd for AscriptAggregateType {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
self.scalar_precedence().partial_cmp(&other.scalar_precedence())
Some(self.cmp(other))
}
}

impl Ord for AscriptAggregateType {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.scalar_precedence().cmp(&other.scalar_precedence())
use AscriptAggregateType::*;
match (self, other) {
(Vec { val_type: a }, Vec { val_type: b }) => a.cmp(b),
(Map { val_type: a, key_type: b }, Map { val_type: c, key_type: d }) => match a.cmp(c) {
std::cmp::Ordering::Equal => b.cmp(d),
cmp => cmp,
},
(Vec { .. }, Map { .. }) => std::cmp::Ordering::Less,
(Map { .. }, Vec { .. }) => std::cmp::Ordering::Greater,
}
}
}

Expand Down

0 comments on commit 7113893

Please sign in to comment.