refactor: 集中 AST 缓存逻辑实现并清理无关依赖

This commit is contained in:
2026-06-05 23:53:30 +08:00
parent 50fb4bab23
commit fe3331d995
9 changed files with 109 additions and 234 deletions
-1
View File
@@ -10,7 +10,6 @@ description = "OxideJS - Shared kernel (CodeForge, ShapeForge, PropForge, String
oxide_compiler = { path = "../oxide_compiler" }
oxide_parser = { path = "../oxide_parser" }
oxide_types = { path = "../oxide_types" }
bumpalo.workspace = true
dashmap.workspace = true
hashbrown.workspace = true
rustc-hash.workspace = true
+1 -84
View File
@@ -1,94 +1,11 @@
use std::hash::{Hash, Hasher};
use std::sync::Arc;
use dashmap::DashMap;
use oxide_compiler::compiler::Compiler;
use oxide_compiler::module::CompiledModule;
use oxide_parser::{Expression, Statement};
pub fn structural_hash(program: &oxide_parser::Program) -> u64 {
let mut h = rustc_hash::FxHasher::default();
for stmt in &program.body {
hash_statement(stmt, &mut h);
}
h.finish()
}
fn hash_statement(stmt: &Statement, h: &mut rustc_hash::FxHasher) {
match stmt {
Statement::ExpressionStatement(es) => {
0u8.hash(h);
hash_expression(&es.expression, h);
}
Statement::VariableDeclaration(decl) => {
1u8.hash(h);
(decl.declarations.len() as u32).hash(h);
}
Statement::ReturnStatement(ret) => {
2u8.hash(h);
if let Some(arg) = &ret.argument {
hash_expression(arg, h);
}
}
Statement::IfStatement(ifs) => {
3u8.hash(h);
hash_expression(&ifs.test, h);
hash_statement(&ifs.consequent, h);
if let Some(alt) = &ifs.alternate {
hash_statement(alt, h);
}
}
Statement::WhileStatement(wh) => {
4u8.hash(h);
hash_expression(&wh.test, h);
hash_statement(&wh.body, h);
}
Statement::ForStatement(fr) => {
5u8.hash(h);
hash_statement(&fr.body, h);
}
Statement::BlockStatement(block) => {
6u8.hash(h);
for s in &block.body {
hash_statement(s, h);
}
}
_ => {
std::mem::discriminant(stmt).hash(h);
}
}
}
fn hash_expression(expr: &Expression, h: &mut rustc_hash::FxHasher) {
match expr {
Expression::BinaryExpression(bin) => {
0u8.hash(h);
std::mem::discriminant(&bin.operator).hash(h);
hash_expression(&bin.left, h);
hash_expression(&bin.right, h);
}
Expression::UnaryExpression(un) => {
1u8.hash(h);
std::mem::discriminant(&un.operator).hash(h);
hash_expression(&un.argument, h);
}
Expression::CallExpression(_) => {
2u8.hash(h);
}
Expression::LogicalExpression(log) => {
3u8.hash(h);
std::mem::discriminant(&log.operator).hash(h);
hash_expression(&log.left, h);
hash_expression(&log.right, h);
}
_ => {
std::mem::discriminant(expr).hash(h);
}
}
}
pub use oxide_compiler::compiler::structural_hash;
pub struct CodeForge {
map: DashMap<u64, Arc<CompiledModule>>,