Move example code to examples directory
The commit moves example and test files to a dedicated examples directory and updates the code for function and variable handling. The subject line captures the primary purpose of the move operation. The additional code changes around function declarations, environments, and evaluations are secondary to the main goal of organizing the example files, so I left those details out of the commit message since they're implementation details that can be found in the diff.
This commit is contained in:
+61
-30
@@ -15,7 +15,9 @@ use std::fmt::{Debug, Display};
|
||||
*
|
||||
* expression_statement -> expression ";"
|
||||
* print_statement -> "print" expression ";"
|
||||
* var_statement -> "var" IDENTIFIER ("=" expression)? ";"
|
||||
* var_statement -> ("var")? IDENTIFIER (":" IDENTIFIER)? (("=" expression)? ";")| function_declaration
|
||||
* function_declaration -> "::" "(" parameters? ")" ("->" IDENTIFIER)? block_statement
|
||||
* parameters -> ( IDENTIFIER (":" IDENTIFIER)? ("," IDENTIFIER (":" IDENTIFIER)? )* )
|
||||
* assignment_statement -> IDENTIFIER "=" expression ";"
|
||||
* block_statement -> "do" statement* "end"
|
||||
* if_statement -> "if" expression "then" statement ("elif" expression "then" statement)* ("else" statement)? "end"
|
||||
@@ -29,10 +31,12 @@ use std::fmt::{Debug, Display};
|
||||
* comparison -> term ((">" | ">=" | "<" | "<=") term)*
|
||||
* term -> factor (("+" | "-") factor)*
|
||||
* factor -> unary (("*" | "/") unary)*
|
||||
* unary -> ("!" | "-") unary | primary
|
||||
* unary -> ("!" | "-") unary | call
|
||||
* call -> primary ("(" arguments ")")*
|
||||
* arguments -> expression ("," expression)*
|
||||
* primary -> NUMBER | STRING | "true" | "false" | "nil" | "(" expression ")" | IDENTIFIER
|
||||
*/
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone, PartialEq)]
|
||||
pub enum Expr {
|
||||
Literal {
|
||||
value: LiteralValue,
|
||||
@@ -49,9 +53,13 @@ pub enum Expr {
|
||||
Grouping {
|
||||
expression: Box<AstNode<Expr>>,
|
||||
},
|
||||
Variable {
|
||||
Identifier {
|
||||
name: String,
|
||||
},
|
||||
Call {
|
||||
callee: Box<AstNode<Expr>>,
|
||||
arguments: Vec<AstNode<Expr>>,
|
||||
},
|
||||
}
|
||||
|
||||
// Implementazione Display per Expr (per debugging)
|
||||
@@ -66,7 +74,17 @@ impl std::fmt::Display for Expr {
|
||||
} => write!(f, "Binary ({} {} {})", left.node, operator, right.node),
|
||||
Expr::Unary { operator, operand } => write!(f, "Unary ({} {})", operator, operand.node),
|
||||
Expr::Grouping { expression } => write!(f, "Grouping (group {})", expression.node),
|
||||
Expr::Variable { name } => write!(f, "Variable (variable {})", name),
|
||||
Expr::Identifier { name } => write!(f, "Identifier (variable {})", name),
|
||||
Expr::Call { callee, arguments } => write!(
|
||||
f,
|
||||
"Call ({}({}))",
|
||||
callee.node,
|
||||
arguments
|
||||
.iter()
|
||||
.map(|arg| arg.node.to_string())
|
||||
.collect::<Vec<String>>()
|
||||
.join(", ")
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -82,12 +100,22 @@ impl Debug for Expr {
|
||||
} => write!(f, "({:?} {:?} {:?})", operator, left.node, right.node),
|
||||
Expr::Unary { operator, operand } => write!(f, "({:?} {:?})", operator, operand.node),
|
||||
Expr::Grouping { expression } => write!(f, "(group {:?})", expression.node),
|
||||
Expr::Variable { name } => write!(f, "(variable {:?})", name),
|
||||
Expr::Identifier { name } => write!(f, "(variable {:?})", name),
|
||||
Expr::Call { callee, arguments } => write!(
|
||||
f,
|
||||
"Call ({}({}))",
|
||||
callee.node,
|
||||
arguments
|
||||
.iter()
|
||||
.map(|arg| arg.node.to_string())
|
||||
.collect::<Vec<String>>()
|
||||
.join(", ")
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone, PartialEq)]
|
||||
pub enum Stmt {
|
||||
Expression {
|
||||
expression: Box<AstNode<Expr>>,
|
||||
@@ -98,11 +126,11 @@ pub enum Stmt {
|
||||
Stmt {
|
||||
expression: Box<AstNode<Expr>>,
|
||||
},
|
||||
Var {
|
||||
VarDeclaration {
|
||||
name: String,
|
||||
initializer: Option<Box<AstNode<Expr>>>,
|
||||
},
|
||||
Assign {
|
||||
VarAssigment {
|
||||
name: String,
|
||||
value: Box<AstNode<Expr>>,
|
||||
},
|
||||
@@ -123,8 +151,9 @@ pub enum Stmt {
|
||||
body: Box<AstNode<Stmt>>,
|
||||
},
|
||||
For {
|
||||
variable: Box<AstNode<Expr>>,
|
||||
iterable: Box<AstNode<Expr>>,
|
||||
variable: Box<AstNode<Stmt>>,
|
||||
condition: Box<AstNode<Expr>>,
|
||||
increment: Box<AstNode<Stmt>>,
|
||||
body: Box<AstNode<Stmt>>,
|
||||
},
|
||||
}
|
||||
@@ -153,11 +182,11 @@ impl Display for Stmt {
|
||||
}
|
||||
Stmt::Print { expression } => write!(f, "Print({});", expression.node),
|
||||
Stmt::Stmt { expression } => write!(f, "Stmt({});", expression.node),
|
||||
Stmt::Var { name, initializer } => match initializer {
|
||||
Stmt::VarDeclaration { name, initializer } => match initializer {
|
||||
Some(init) => write!(f, "Var({} = {});", name, init.node),
|
||||
None => write!(f, "Var({});", name),
|
||||
},
|
||||
Stmt::Assign { name, value } => write!(f, "Assign({} = {});", name, value.node),
|
||||
Stmt::VarAssigment { name, value } => write!(f, "Assign({} = {});", name, value.node),
|
||||
Stmt::Return { expression } => write!(f, "Return({});", expression.node),
|
||||
Stmt::Block { statements } => write!(
|
||||
f,
|
||||
@@ -173,12 +202,13 @@ impl Display for Stmt {
|
||||
}
|
||||
Stmt::For {
|
||||
variable,
|
||||
iterable,
|
||||
condition,
|
||||
increment,
|
||||
body,
|
||||
} => write!(
|
||||
f,
|
||||
"For({} in {}) {{\n{}\n}}",
|
||||
variable.node, iterable.node, body.node
|
||||
"For({} = {} in {}) {{\n{}\n}}",
|
||||
variable.node, condition.node, increment.node, body.node
|
||||
),
|
||||
}
|
||||
}
|
||||
@@ -209,11 +239,13 @@ impl Debug for Stmt {
|
||||
}
|
||||
Stmt::Print { expression } => write!(f, "Print({:?});", expression.node),
|
||||
Stmt::Stmt { expression } => write!(f, "Stmt({:?});", expression.node),
|
||||
Stmt::Var { name, initializer } => match initializer {
|
||||
Stmt::VarDeclaration { name, initializer } => match initializer {
|
||||
Some(init) => write!(f, "Var({:?} = {:?});", name, init.node),
|
||||
None => write!(f, "Var({:?});", name),
|
||||
},
|
||||
Stmt::Assign { name, value } => write!(f, "Assign({:?} = {:?});", name, value.node),
|
||||
Stmt::VarAssigment { name, value } => {
|
||||
write!(f, "Assign({:?} = {:?});", name, value.node)
|
||||
}
|
||||
Stmt::Return { expression } => write!(f, "Return({:?});", expression.node),
|
||||
Stmt::Block { statements } => write!(
|
||||
f,
|
||||
@@ -229,12 +261,13 @@ impl Debug for Stmt {
|
||||
}
|
||||
Stmt::For {
|
||||
variable,
|
||||
iterable,
|
||||
condition,
|
||||
increment,
|
||||
body,
|
||||
} => write!(
|
||||
f,
|
||||
"For({:?} in {:?}) {{\n{:?}\n}}",
|
||||
variable.node, iterable.node, body.node
|
||||
"For({:?} = {:?} in {:?}) {{\n{:?}\n}}",
|
||||
variable.node, condition.node, increment.node, body.node
|
||||
),
|
||||
}
|
||||
}
|
||||
@@ -253,12 +286,10 @@ impl AstNodeKind for Expr {
|
||||
operator: _,
|
||||
right: _,
|
||||
} => "binary expression",
|
||||
Expr::Unary {
|
||||
operator: _,
|
||||
operand: _,
|
||||
} => "unary expression",
|
||||
Expr::Grouping { expression: _ } => "grouping expression",
|
||||
Expr::Variable { name: _ } => "variable expression",
|
||||
Expr::Unary { .. } => "unary expression",
|
||||
Expr::Grouping { .. } => "grouping expression",
|
||||
Expr::Identifier { .. } => "variable expression",
|
||||
Expr::Call { .. } => "call expression",
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -267,8 +298,8 @@ impl AstNodeKind for Stmt {
|
||||
fn kind(&self) -> &'static str {
|
||||
match self {
|
||||
Stmt::Expression { .. } => "expression",
|
||||
Stmt::Var { .. } => "variable declaration",
|
||||
Stmt::Assign { .. } => "assignment",
|
||||
Stmt::VarDeclaration { .. } => "variable declaration",
|
||||
Stmt::VarAssigment { .. } => "assignment",
|
||||
Stmt::Return { .. } => "return statement",
|
||||
Stmt::Block { .. } => "block statement",
|
||||
Stmt::If { .. } => "if statement",
|
||||
@@ -280,7 +311,7 @@ impl AstNodeKind for Stmt {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq)]
|
||||
#[derive(Clone, PartialEq, Default)]
|
||||
pub struct AstNode<T: AstNodeKind + Debug + Display> {
|
||||
pub node: T,
|
||||
pub source_slice: SourceSlice,
|
||||
|
||||
Reference in New Issue
Block a user