bdffe9e3c5
**Workspace 拆分** - aster-core: 纯库,zero-dependency,包含 lexer/parser/ast/interpreter/error/analysis - aster: REPL 二进制,薄封装 aster-core - aster-lsp: LSP 语言服务器 **VS Code 扩展 (vscode-ext/)** - TextMate 语法高亮 (.ast 文件) - 语言配置 (注释切换、括号配对、自动缩进) - LSP 客户端 (extension.js) **LSP 服务端功能** - Diagnostics: 实时显示 lex/parse 错误红色波浪线 - Completion: 关键字 + 内置函数 + 用户定义符号补全 - Hover: 悬停显示变量/函数信息 - Signature Help: 函数参数提示 - Goto Definition: Ctrl+Click 跳转到声明处 - Find References: 查找所有引用位置 - Rename: F2 重命名符号 **新增 analysis 模块** - collect_symbols: AST 遍历收集符号 - find_declaration/find_all_references: Token 扫描定位声明和引用
51 lines
1.5 KiB
Rust
51 lines
1.5 KiB
Rust
use super::Value;
|
|
use std::collections::HashMap;
|
|
use std::rc::Rc;
|
|
use std::cell::RefCell;
|
|
|
|
#[derive(Debug)]
|
|
pub struct Env {
|
|
/// (value, is_mutable) — `is_mutable` is true for `let`, false for `const`.
|
|
pub values: HashMap<String, (Value, bool)>,
|
|
pub parent: Option<Rc<RefCell<Env>>>,
|
|
}
|
|
|
|
impl Env {
|
|
pub fn new(parent: Option<Rc<RefCell<Env>>>) -> Self {
|
|
Self {
|
|
values: HashMap::new(),
|
|
parent,
|
|
}
|
|
}
|
|
|
|
pub fn define(&mut self, name: String, val: Value, mutable: bool) {
|
|
self.values.insert(name, (val, mutable));
|
|
}
|
|
|
|
/// Assign a new value to an existing binding. Returns `Err(msg)` if the
|
|
/// binding exists but was declared with `const` (immutable).
|
|
pub fn assign(&mut self, name: &str, val: Value) -> Result<bool, String> {
|
|
if let Some((_, false)) = self.values.get(name) {
|
|
return Err(format!("Cannot reassign constant '{}'", name));
|
|
}
|
|
if self.values.contains_key(name) {
|
|
self.values.insert(name.to_string(), (val, true));
|
|
Ok(true)
|
|
} else if let Some(parent) = &self.parent {
|
|
parent.borrow_mut().assign(name, val)
|
|
} else {
|
|
Ok(false)
|
|
}
|
|
}
|
|
|
|
pub fn get(&self, name: &str) -> Option<Value> {
|
|
if let Some((val, _)) = self.values.get(name) {
|
|
Some(val.clone())
|
|
} else if let Some(parent) = &self.parent {
|
|
parent.borrow().get(name)
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
}
|