**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 扫描定位声明和引用
103 lines
2.5 KiB
Rust
103 lines
2.5 KiB
Rust
pub mod lexer;
|
|
pub mod ast;
|
|
pub mod parser;
|
|
pub mod interpreter;
|
|
pub mod error;
|
|
pub mod analysis;
|
|
|
|
use lexer::Lexer;
|
|
use parser::Parser;
|
|
use interpreter::Interpreter;
|
|
use error::RuntimeError;
|
|
|
|
use std::io::Write;
|
|
use std::io::stdin;
|
|
use std::io::stdout;
|
|
|
|
pub fn print_errors(errors: &[RuntimeError]) {
|
|
for err in errors {
|
|
eprintln!("{}", err);
|
|
}
|
|
}
|
|
|
|
pub fn run_file(filename: &str, src: String) {
|
|
let (tokens, lex_errors) = Lexer::new(&src).tokenize();
|
|
if !lex_errors.is_empty() {
|
|
print_errors(&lex_errors);
|
|
std::process::exit(65);
|
|
}
|
|
|
|
let mut parser = Parser::new(tokens);
|
|
let (stmts, parse_errors) = parser.parse();
|
|
if !parse_errors.is_empty() {
|
|
print_errors(&parse_errors);
|
|
std::process::exit(65);
|
|
}
|
|
|
|
let script_dir = std::path::Path::new(filename)
|
|
.parent()
|
|
.map(|p| p.to_string_lossy().to_string())
|
|
.unwrap_or_else(|| ".".to_string());
|
|
|
|
let mut interpreter = Interpreter::with_current_dir(script_dir);
|
|
if let Err(e) = interpreter.interpret(stmts) {
|
|
eprintln!("Error: {}", e);
|
|
std::process::exit(70);
|
|
}
|
|
}
|
|
|
|
pub fn run_repl() {
|
|
println!("Welcome to Aster REPL!");
|
|
println!("Type ':exit' to quit.");
|
|
println!("Type ':reset' to reset the interpreter state.");
|
|
|
|
let mut interpreter = Interpreter::new();
|
|
let mut line = String::new();
|
|
|
|
loop {
|
|
print!(">>> ");
|
|
stdout().flush().unwrap();
|
|
|
|
line.clear();
|
|
if let Err(e) = stdin().read_line(&mut line) {
|
|
eprintln!("Error reading input: {}", e);
|
|
continue;
|
|
}
|
|
|
|
let line = line.trim();
|
|
if line.is_empty() {
|
|
continue;
|
|
}
|
|
|
|
match line {
|
|
":exit" => {
|
|
println!("Goodbye!");
|
|
break;
|
|
}
|
|
":reset" => {
|
|
interpreter = Interpreter::new();
|
|
println!("Interpreter reset.");
|
|
continue;
|
|
}
|
|
_ => {}
|
|
}
|
|
|
|
let (tokens, lex_errors) = Lexer::new(line).tokenize();
|
|
if !lex_errors.is_empty() {
|
|
print_errors(&lex_errors);
|
|
continue;
|
|
}
|
|
|
|
let mut parser = Parser::new(tokens);
|
|
let (stmts, parse_errors) = parser.parse();
|
|
if !parse_errors.is_empty() {
|
|
print_errors(&parse_errors);
|
|
continue;
|
|
}
|
|
|
|
if let Err(e) = interpreter.interpret(stmts) {
|
|
eprintln!("Error: {}", e);
|
|
}
|
|
}
|
|
}
|