Skip to content

(feat) Type Inference #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 28 commits into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions optd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ futures = "0.3.31"
iceberg = { version = "0.4.0", default-features = false }
iceberg-catalog-memory = "0.4.0"
ordered-float = "5.0.0"
tempfile = "3.19.1"
tokio = { version = "1.44.2", features = ["macros", "rt"] }
trait-variant = "0.1.2"

Expand Down
36 changes: 27 additions & 9 deletions optd/src/dsl/analyzer/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use super::{
hir::{ExprMetadata, Identifier, NoMetadata, TypedSpan},
};
use crate::dsl::analyzer::hir::Value;
use std::{collections::HashMap, sync::Arc};
use std::{
collections::{BTreeMap, HashMap},
sync::Arc,
};

/// A stack-based variable binding system that implements lexical scoping.
///
Expand Down Expand Up @@ -118,20 +121,35 @@ impl<M: ExprMetadata> Context<M> {
self.current_scope.insert(name, val);
}

/// Gets all values from all scopes in the context.
/// Gets all values from all scopes in the context, ordered by their identifiers.
/// (for deterministic output)
///
/// This retrieves values from all lexical scopes, including both the current
/// scope and all previous (outer) scopes.
/// scope and all previous (outer) scopes. The values are ordered by their
/// identifiers (keys) for consistent output.
///
/// If a variable exists in multiple scopes, the innermost definition takes precedence
/// in accordance with lexical scoping rules.
///
/// # Returns
///
/// A vector containing references to all values in the context.
/// A vector containing references to all values in the context, ordered by their identifiers.
pub fn get_all_values(&self) -> Vec<&Value<M>> {
self.previous_scopes
.iter()
.flat_map(|scope| scope.values())
.chain(self.current_scope.values())
.collect()
let mut sorted_values: BTreeMap<&Identifier, &Value<M>> = BTreeMap::new();

// Process scopes from outermost to innermost to maintain lexical scoping rules
// (later insertions with the same key will overwrite earlier ones)
for scope in &self.previous_scopes {
for (key, value) in scope.iter() {
sorted_values.insert(key, value);
}
}

for (key, value) in &self.current_scope {
sorted_values.insert(key, value);
}

sorted_values.into_values().collect()
}
}

Expand Down
Loading