Skip to content

Commit

Permalink
feat: add current source position tracking to Evaluator and update so…
Browse files Browse the repository at this point in the history
…urcemap generation

Signed-off-by: Akash <[email protected]>
  • Loading branch information
SkySingh04 committed Jan 17, 2025
1 parent 9b68de9 commit c223a49
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
23 changes: 22 additions & 1 deletion kclvm/evaluator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use generational_arena::{Arena, Index};
use indexmap::IndexMap;
use kclvm_runtime::val_plan::KCL_PRIVATE_VAR_PREFIX;
use lazy::{BacktrackMeta, LazyEvalScope};
use node::KCLSourceMap;
use node::{KCLSourceMap , SourcePos};
use proxy::{Frame, Proxy};
use rule::RuleEvalContextRef;
use schema::SchemaEvalContextRef;
Expand Down Expand Up @@ -92,6 +92,8 @@ pub struct Evaluator<'ctx> {
pub ast_id: RefCell<AstIndex>,
// Source map for code generated
pub source_map: std::option::Option<KCLSourceMap>,
// Current source position
pub current_source_pos: RefCell<Option<SourcePos>>,
}

#[derive(Clone)]
Expand Down Expand Up @@ -151,11 +153,30 @@ impl<'ctx> Evaluator<'ctx> {
backtrack_meta: RefCell::new(Default::default()),
ast_id: RefCell::new(AstIndex::default()),
source_map: Some(KCLSourceMap::new()),
current_source_pos: RefCell::new(None),
}
}

/// Evaluate the program and return the JSON and YAML result.
pub fn run(self: &Evaluator<'ctx> ) -> Result<(String, String)> {
//ab yaha pe kuch circus karna h
// The modules variable represents the AST modules from the main package of the KCL program. It's retrieved via self.program.get_modules_for_pkg(kclvm_ast::MAIN_PKG).

// Looking at the code structure:

// Type: Vec<ProgramUnit>

// Contains AST nodes for each KCL source file in the main package
// Source: kclvm_ast::ast::Program

// Each module represents a single KCL source file
// Contains declarations, statements, and other AST nodes
// Usage:

// Passed to compile_ast_modules() for compilation
// Contains source locations needed for sourcemap generation
// Each module has file path, line numbers, and column information
// This is where we need to start tracking source locations for the sourcemap, as these modules contain the original source position information.
let modules = self.program.get_modules_for_pkg(kclvm_ast::MAIN_PKG);
self.init_scope(kclvm_ast::MAIN_PKG);
self.compile_ast_modules(&modules);
Expand Down
30 changes: 22 additions & 8 deletions kclvm/evaluator/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@ use crate::union::union_entry;
use crate::{backtrack_break_here, backtrack_update_break};
use crate::{error as kcl_error, GLOBAL_LEVEL, INNER_LEVEL};
use crate::{EvalResult, Evaluator};


pub struct KCLSourceMap {
version: u8,
sources: Vec<String>,
mappings: HashMap<String, Vec<Mapping>>,
}

pub struct SourcePos {
line: u32,
column: u32,
}

pub struct Mapping {
generated_line: u32,
generated_column: u32,
Expand Down Expand Up @@ -69,9 +72,12 @@ impl<'ctx> TypedResultWalker<'ctx> for Evaluator<'ctx> {
*/

fn walk_stmt(&self, stmt: &'ctx ast::Node<ast::Stmt>) -> Self::Result {
backtrack_break_here!(self, stmt);
self.update_ctx_panic_info(stmt);
self.update_ast_id(stmt);
self.current_source_pos = Some(SourcePos {
line: stmt.pos().1 as u32,
column: stmt.pos().2 as u32,
}).into();

let yaml_start_line = self.get_current_yaml_line();
let value = match &stmt.node {
ast::Stmt::TypeAlias(type_alias) => self.walk_type_alias_stmt(type_alias),
ast::Stmt::Expr(expr_stmt) => self.walk_expr_stmt(expr_stmt),
Expand All @@ -87,7 +93,15 @@ impl<'ctx> TypedResultWalker<'ctx> for Evaluator<'ctx> {
ast::Stmt::Schema(schema_stmt) => self.walk_schema_stmt(schema_stmt),
ast::Stmt::Rule(rule_stmt) => self.walk_rule_stmt(rule_stmt),
};
backtrack_update_break!(self, stmt);

// Store mapping after YAML generation
if let Some(pos) = *self.current_source_pos.borrow() {
if let Some(ref mut sm) = self.source_map {
let vec = sm.mappings.entry(stmt.filename.clone()).or_insert_with(Vec::new);
vec.push((pos.line, yaml_start_line));
}
}

value
}

Expand Down Expand Up @@ -1274,7 +1288,7 @@ impl<'ctx> Evaluator<'ctx> {
}
} else {
// If variable exists in the scope and update it, if not, add it to the scope.
if !self.store_variable_in_current_scope(name, value.clone()) {
if (!self.store_variable_in_current_scope(name, value.clone())) {
self.add_variable(name, self.undefined_value());
self.store_variable(name, value);
}
Expand Down Expand Up @@ -1357,7 +1371,7 @@ impl<'ctx> Evaluator<'ctx> {
}
} else {
// If variable exists in the scope and update it, if not, add it to the scope.
if !self.store_variable_in_current_scope(name, value.clone()) {
if (!self.store_variable_in_current_scope(name, value.clone())) {
self.add_variable(name, self.undefined_value());
self.store_variable(name, value);
}
Expand Down

0 comments on commit c223a49

Please sign in to comment.