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:
@@ -0,0 +1,84 @@
|
||||
use crate::ast::stmt::Stmt;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Expr {
|
||||
/// 字面量:number / string / bool / nil
|
||||
Literal(Literal),
|
||||
|
||||
/// 变量引用
|
||||
Variable(String),
|
||||
|
||||
/// 赋值表达式:a = b
|
||||
Assign {
|
||||
name: String,
|
||||
value: Box<Expr>,
|
||||
},
|
||||
|
||||
/// 一元运算:!expr / -expr
|
||||
Unary {
|
||||
op: UnaryOp,
|
||||
right: Box<Expr>,
|
||||
},
|
||||
|
||||
/// 二元运算:a + b
|
||||
Binary {
|
||||
left: Box<Expr>,
|
||||
op: BinaryOp,
|
||||
right: Box<Expr>,
|
||||
},
|
||||
|
||||
/// 逻辑运算:&& ||
|
||||
Logical {
|
||||
left: Box<Expr>,
|
||||
op: LogicalOp,
|
||||
right: Box<Expr>,
|
||||
},
|
||||
|
||||
/// 函数调用
|
||||
Call {
|
||||
callee: Box<Expr>,
|
||||
arguments: Vec<Expr>,
|
||||
},
|
||||
|
||||
/// 匿名函数(为闭包预留)
|
||||
Lambda {
|
||||
params: Vec<String>,
|
||||
body: Vec<Stmt>,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Literal {
|
||||
Number(f64),
|
||||
String(String),
|
||||
Bool(bool),
|
||||
Nil,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum UnaryOp {
|
||||
Negate, // -
|
||||
Not, // !
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum BinaryOp {
|
||||
Add,
|
||||
Sub,
|
||||
Mul,
|
||||
Div,
|
||||
|
||||
Greater,
|
||||
GreaterEqual,
|
||||
Less,
|
||||
LessEqual,
|
||||
|
||||
Equal,
|
||||
NotEqual,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum LogicalOp {
|
||||
And,
|
||||
Or,
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
pub mod expr;
|
||||
pub mod stmt;
|
||||
|
||||
pub use expr::*;
|
||||
pub use stmt::*;
|
||||
@@ -0,0 +1,39 @@
|
||||
use crate::ast::expr::Expr;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Stmt {
|
||||
/// let x = expr;
|
||||
Let {
|
||||
name: String,
|
||||
initializer: Expr,
|
||||
},
|
||||
|
||||
/// 表达式语句:expr;
|
||||
ExprStmt(Expr),
|
||||
|
||||
/// 代码块:{ ... }
|
||||
Block(Vec<Stmt>),
|
||||
|
||||
/// if 语句
|
||||
If {
|
||||
condition: Expr,
|
||||
then_branch: Box<Stmt>,
|
||||
else_branch: Option<Box<Stmt>>,
|
||||
},
|
||||
|
||||
/// while 循环
|
||||
While {
|
||||
condition: Expr,
|
||||
body: Box<Stmt>,
|
||||
},
|
||||
|
||||
/// 函数声明
|
||||
Function {
|
||||
name: String,
|
||||
params: Vec<String>,
|
||||
body: Vec<Stmt>,
|
||||
},
|
||||
|
||||
/// return expr?;
|
||||
Return(Option<Expr>),
|
||||
}
|
||||
Reference in New Issue
Block a user