Skip to content

Commit

Permalink
refactor: implement compiler snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
plusvic committed Sep 20, 2023
1 parent 3e31c1e commit 7ec49b9
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 73 deletions.
10 changes: 7 additions & 3 deletions yara-x/src/compiler/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ pub(in crate::compiler) struct Context<'a, 'src, 'sym> {
/// Information about the rules compiled so far.
pub rules: &'a Vec<RuleInfo>,

/// Rule that is being compiled.
pub current_rule: &'a RuleInfo,

/// A vector that contains the IR for the patterns declared in the current
/// rule, accompanied by their corresponding [`PatternId`].
pub current_rule_patterns:
Expand Down Expand Up @@ -94,6 +91,13 @@ impl<'a, 'src, 'sym> Context<'a, 'src, 'sym> {
self.rules.get(rule_id.0 as usize).unwrap()
}

/// Returns the [`RuleInfo`] structure corresponding to the rule currently
/// being compiled.
#[inline]
pub fn get_current_rule(&self) -> &RuleInfo {
self.rules.last().unwrap()
}

/// Given a pattern identifier (e.g. `$a`, `#a`, `@a`) search for it in
/// the current rule and return its [`PatternID`].
///
Expand Down
7 changes: 4 additions & 3 deletions yara-x/src/compiler/ir/ast2ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,17 +355,18 @@ pub(in crate::compiler) fn expr_from_ast(
// A global rule can depend on another global rule. And non-global
// rules can depend both on global rules and non-global ones.
if let SymbolKind::Rule(rule_id) = symbol.kind() {
let current_rule = ctx.get_current_rule();
let used_rule = ctx.get_rule(*rule_id);
if ctx.current_rule.is_global && !used_rule.is_global {
if current_rule.is_global && !used_rule.is_global {
return Err(CompileError::from(
CompileErrorInfo::wrong_rule_dependency(
ctx.report_builder,
ctx.ident_pool
.get(ctx.current_rule.ident_id)
.get(current_rule.ident_id)
.unwrap()
.to_string(),
ident.name.to_string(),
ctx.current_rule.ident_span,
current_rule.ident_span,
used_rule.ident_span,
ident.span,
),
Expand Down
Loading

0 comments on commit 7ec49b9

Please sign in to comment.