From 235384fb85737777e98c4f6a33fedf022c7c140d Mon Sep 17 00:00:00 2001 From: cflmy Date: Tue, 11 Aug 2026 17:29:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=80=E5=8F=91=E5=9F=BA=E6=9C=AC=E7=9A=84?= =?UTF-8?q?=E9=AA=A8=E6=9E=B6=EF=BC=8C=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86?= =?UTF-8?q?=E7=9A=84=E7=BB=93=E6=9E=84=E4=BD=93=E5=92=8C=E6=A1=86=E6=9E=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + Cargo.lock | 7 +++++ Cargo.toml | 10 ++++++ src/error.rs | 40 ++++++++++++++++++++++++ src/marqdo.rs | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 143 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/error.rs create mode 100644 src/marqdo.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..546dbd1 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "marqdo" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..2addaf6 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "marqdo" +version = "0.1.0" +edition = "2024" + +[dependencies] + +[[bin]] +name = "marqdo" +path = "src/marqdo.rs" diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..3c18941 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,40 @@ +//解释器检测到错误的处理模块 + +//! 错误报告模块,集中管理解释器所有语法错误输出 +// use std::io; +// use std::io::Write; + +//建立独立抽象ErrorReport结构体 +//错误报告器,保存错误状态并提供错误报告接口 +#[derive(Debug, Default)] +pub struct ErrorReport { + had_error: bool, +} + +impl ErrorReport { + //创建新的错误报告器 + pub fn new() -> Self { + Self { had_error: false } + } + + //对外公开的错误接口,指定行号和错误信息 + pub fn error(&mut self, line: usize, message: &str) { + self.report(line, "", message); + } + + //内部私有函数,输出错误信息,标记错误状态为true + fn report(&mut self, line: usize, location: &str, message: &str) { + eprintln!("[line {}] Error{}: {}", line, location, message); + self.had_error = true; + } + + //获取错误状态 + pub fn has_error(&self) -> bool { + self.had_error + } + + //重置错误状态 + pub fn clear(&mut self) { + self.had_error = false; + } +} \ No newline at end of file diff --git a/src/marqdo.rs b/src/marqdo.rs new file mode 100644 index 0000000..b56cefd --- /dev/null +++ b/src/marqdo.rs @@ -0,0 +1,85 @@ +//使用rust开发marqdo语言的解释器 + +//导入库 +use std::env; //用于获取命令行参数 +use std::fs; //用于读取文件 +// use std::io; //用于读取标准输入和输出 +// use std::io::BufReader; //用于读取缓冲区 + +//导入错误报告模块 +mod error; +use error::ErrorReport; + +fn main() { + //创建新的错误报告器 + let mut error_report = ErrorReport::new(); + //获取命令行参数 + let args: Vec = env::args().collect(); + + //解析命令行参数 + if args.len() > 3 { + eprintln!("Usage: marqdo "); + std::process::exit(1); + } + else if args.len() == 3 { + let use_mode = &args[1]; + let file_path = &args[2]; + //传入模式选择函数,根据不同的模式选择不同的函数执行 + mode_selector(use_mode, file_path, &mut error_report); + } + else if args.len() == 2 { + let file_path = &args[1]; + //默认为run模式,自动进行文件执行 + run_file(file_path, &mut error_report); + } + else { + //无参数输入,默认进入交互模式 + run_prompt(&mut error_report); + } +} + +//模式选择函数 +fn mode_selector(use_mode: &str, file_path: &str, error_report: &mut ErrorReport) { + //根据不同的模式选择不同的函数执行 + match use_mode { + "run" => run_file(file_path, error_report), + _ => {eprintln!("Invalid use mode: {}", use_mode); + std::process::exit(2); + } + } +} + +//读取marqdo文件并运行marqdo脚本文件 +fn run_file(file_path: &str, error_report: &mut ErrorReport) { + match fs::read_to_string(file_path) { + Ok(source) => { + run(&source, error_report); + } + Err(e) => { + eprintln!("Error reading file: {}", e); + std::process::exit(3); + } + } +} + +//交互式模式 +fn run_prompt(error_report: &mut ErrorReport) { + //提示交互式模式正在开发中 + println!("Interactive mode is not available yet."); + error_report.error(0, "Interactive mode is not available yet."); + //清除错误报告 + error_report.clear(); + std::process::exit(4); +} + +//运行marqdo脚本 +fn run(source: &str, error_report: &mut ErrorReport) { + //提示正在开发中 + println!("Running script is not available yet."); + //输出source + println!("Source: {}", source); + //输出错误报告 + println!("Error report: {}", error_report.has_error()); + //退出程序 + std::process::exit(5); +} \ No newline at end of file