Initialize Aster project with basic structure, including Cargo configuration, lexer, parser, interpreter, and AST definitions. Add a sample script and README documentation. Implement basic error handling and environment management for variable storage.
This commit is contained in:
+53
@@ -0,0 +1,53 @@
|
||||
mod lexer;
|
||||
mod ast;
|
||||
mod parser;
|
||||
mod interpreter;
|
||||
mod error;
|
||||
|
||||
use lexer::Lexer;
|
||||
use parser::Parser;
|
||||
use interpreter::Interpreter;
|
||||
use std::env;
|
||||
use std::fs;
|
||||
use std::io::{self, Read};
|
||||
|
||||
fn main() {
|
||||
let src = match env::args().len() {
|
||||
1 => {
|
||||
// 无参数:从 stdin 读取
|
||||
let mut buf = String::new();
|
||||
if let Err(e) = io::stdin().read_to_string(&mut buf) {
|
||||
eprintln!("Failed to read stdin: {}", e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
buf
|
||||
}
|
||||
2 => {
|
||||
// 一个参数:作为文件路径
|
||||
let path = env::args().nth(1).unwrap();
|
||||
match fs::read_to_string(&path) {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
eprintln!("Failed to read file '{}': {}", path, e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
eprintln!("Usage: aster [script_file]");
|
||||
eprintln!(" aster - run from stdin");
|
||||
eprintln!(" aster <file> - run script file");
|
||||
std::process::exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
let tokens = Lexer::new(&src).tokenize();
|
||||
let mut parser = Parser::new(tokens);
|
||||
let stmts = parser.parse();
|
||||
|
||||
let mut interpreter = Interpreter::new();
|
||||
if let Err(e) = interpreter.interpret(stmts) {
|
||||
eprintln!("{}", e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user