290 lines
6.6 KiB
Rust
290 lines
6.6 KiB
Rust
|
|
// This is an auto-generated file. Any changes made to this file may be overwritten.
|
||
|
|
// This file was created at: 2024-09-18 09:28:21
|
||
|
|
#![allow(dead_code)]
|
||
|
|
use std::fmt::Debug;
|
||
|
|
use std::any::Any;
|
||
|
|
|
||
|
|
use crate::token::Token;
|
||
|
|
|
||
|
|
pub trait ExprVisitor {
|
||
|
|
fn visit_binary_expr(&mut self, expr: &BinaryExpr) -> Result<(), Box<dyn std::error::Error>>;
|
||
|
|
fn visit_unary_expr(&mut self, expr: &UnaryExpr) -> Result<(), Box<dyn std::error::Error>>;
|
||
|
|
fn visit_call_expr(&mut self, expr: &CallExpr) -> Result<(), Box<dyn std::error::Error>>;
|
||
|
|
fn visit_get_expr(&mut self, expr: &GetExpr) -> Result<(), Box<dyn std::error::Error>>;
|
||
|
|
fn visit_primary_expr(&mut self, expr: &PrimaryExpr) -> Result<(), Box<dyn std::error::Error>>;
|
||
|
|
fn visit_function_expr(&mut self, expr: &FunctionExpr) -> Result<(), Box<dyn std::error::Error>>;
|
||
|
|
}
|
||
|
|
|
||
|
|
pub trait StmtVisitor {
|
||
|
|
fn visit_expr_stmt(&mut self, stmt: &ExprStmt) -> Result<(), Box<dyn std::error::Error>>;
|
||
|
|
fn visit_assign_stmt(&mut self, stmt: &AssignStmt) -> Result<(), Box<dyn std::error::Error>>;
|
||
|
|
fn visit_set_stmt(&mut self, stmt: &SetStmt) -> Result<(), Box<dyn std::error::Error>>;
|
||
|
|
fn visit_block_stmt(&mut self, stmt: &BlockStmt) -> Result<(), Box<dyn std::error::Error>>;
|
||
|
|
fn visit_return_stmt(&mut self, stmt: &ReturnStmt) -> Result<(), Box<dyn std::error::Error>>;
|
||
|
|
fn visit_if_stmt(&mut self, stmt: &IfStmt) -> Result<(), Box<dyn std::error::Error>>;
|
||
|
|
}
|
||
|
|
|
||
|
|
pub trait Expr: Debug + Any {
|
||
|
|
fn accept(&self, visitor: &mut dyn ExprVisitor) -> Result<(), Box<dyn std::error::Error>>;
|
||
|
|
fn as_any(self: Box<Self>) -> Box<dyn Any>;
|
||
|
|
fn as_any_ref(&self) -> &dyn Any;
|
||
|
|
}
|
||
|
|
|
||
|
|
pub type ExprP = Box<dyn Expr + 'static>;
|
||
|
|
|
||
|
|
#[derive(Debug)]
|
||
|
|
pub struct BinaryExpr {
|
||
|
|
pub lhs: ExprP,
|
||
|
|
pub op: Token,
|
||
|
|
pub rhs: ExprP,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl Expr for BinaryExpr {
|
||
|
|
fn accept(&self, visitor: &mut dyn ExprVisitor) -> Result<(), Box<dyn std::error::Error>>{
|
||
|
|
visitor.visit_binary_expr(self)
|
||
|
|
}
|
||
|
|
|
||
|
|
fn as_any(self: Box<Self>) -> Box<dyn Any> {
|
||
|
|
self
|
||
|
|
}
|
||
|
|
|
||
|
|
fn as_any_ref(&self) -> &dyn Any {
|
||
|
|
self
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(Debug)]
|
||
|
|
pub struct UnaryExpr {
|
||
|
|
pub op: Token,
|
||
|
|
pub expr: ExprP,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl Expr for UnaryExpr {
|
||
|
|
fn accept(&self, visitor: &mut dyn ExprVisitor) -> Result<(), Box<dyn std::error::Error>>{
|
||
|
|
visitor.visit_unary_expr(self)
|
||
|
|
}
|
||
|
|
|
||
|
|
fn as_any(self: Box<Self>) -> Box<dyn Any> {
|
||
|
|
self
|
||
|
|
}
|
||
|
|
|
||
|
|
fn as_any_ref(&self) -> &dyn Any {
|
||
|
|
self
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(Debug)]
|
||
|
|
pub struct CallExpr {
|
||
|
|
pub expr: ExprP,
|
||
|
|
pub args: Vec<ExprP>,
|
||
|
|
pub rparen: Token,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl Expr for CallExpr {
|
||
|
|
fn accept(&self, visitor: &mut dyn ExprVisitor) -> Result<(), Box<dyn std::error::Error>>{
|
||
|
|
visitor.visit_call_expr(self)
|
||
|
|
}
|
||
|
|
|
||
|
|
fn as_any(self: Box<Self>) -> Box<dyn Any> {
|
||
|
|
self
|
||
|
|
}
|
||
|
|
|
||
|
|
fn as_any_ref(&self) -> &dyn Any {
|
||
|
|
self
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(Debug)]
|
||
|
|
pub struct GetExpr {
|
||
|
|
pub expr: ExprP,
|
||
|
|
pub name: Token,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl Expr for GetExpr {
|
||
|
|
fn accept(&self, visitor: &mut dyn ExprVisitor) -> Result<(), Box<dyn std::error::Error>>{
|
||
|
|
visitor.visit_get_expr(self)
|
||
|
|
}
|
||
|
|
|
||
|
|
fn as_any(self: Box<Self>) -> Box<dyn Any> {
|
||
|
|
self
|
||
|
|
}
|
||
|
|
|
||
|
|
fn as_any_ref(&self) -> &dyn Any {
|
||
|
|
self
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(Debug)]
|
||
|
|
pub struct PrimaryExpr {
|
||
|
|
pub token: Token,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl Expr for PrimaryExpr {
|
||
|
|
fn accept(&self, visitor: &mut dyn ExprVisitor) -> Result<(), Box<dyn std::error::Error>>{
|
||
|
|
visitor.visit_primary_expr(self)
|
||
|
|
}
|
||
|
|
|
||
|
|
fn as_any(self: Box<Self>) -> Box<dyn Any> {
|
||
|
|
self
|
||
|
|
}
|
||
|
|
|
||
|
|
fn as_any_ref(&self) -> &dyn Any {
|
||
|
|
self
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(Debug)]
|
||
|
|
pub struct FunctionExpr {
|
||
|
|
pub lparen: Token,
|
||
|
|
pub params: Vec<(Token , Option<ExprP>)>,
|
||
|
|
pub return_type: Option<ExprP>,
|
||
|
|
pub body: Vec<StmtP>,
|
||
|
|
pub rbrace: Token,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl Expr for FunctionExpr {
|
||
|
|
fn accept(&self, visitor: &mut dyn ExprVisitor) -> Result<(), Box<dyn std::error::Error>>{
|
||
|
|
visitor.visit_function_expr(self)
|
||
|
|
}
|
||
|
|
|
||
|
|
fn as_any(self: Box<Self>) -> Box<dyn Any> {
|
||
|
|
self
|
||
|
|
}
|
||
|
|
|
||
|
|
fn as_any_ref(&self) -> &dyn Any {
|
||
|
|
self
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
pub trait Stmt: Debug + Any {
|
||
|
|
fn accept(&self, visitor: &mut dyn StmtVisitor) -> Result<(), Box<dyn std::error::Error>>;
|
||
|
|
fn as_any(self: Box<Self>) -> Box<dyn Any>;
|
||
|
|
fn as_any_ref(&self) -> &dyn Any;
|
||
|
|
}
|
||
|
|
|
||
|
|
pub type StmtP = Box<dyn Stmt + 'static>;
|
||
|
|
|
||
|
|
#[derive(Debug)]
|
||
|
|
pub struct ExprStmt {
|
||
|
|
pub expr: ExprP,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl Stmt for ExprStmt {
|
||
|
|
fn accept(&self, visitor: &mut dyn StmtVisitor) -> Result<(), Box<dyn std::error::Error>>{
|
||
|
|
visitor.visit_expr_stmt(self)
|
||
|
|
}
|
||
|
|
|
||
|
|
fn as_any(self: Box<Self>) -> Box<dyn Any> {
|
||
|
|
self
|
||
|
|
}
|
||
|
|
|
||
|
|
fn as_any_ref(&self) -> &dyn Any {
|
||
|
|
self
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(Debug)]
|
||
|
|
pub struct AssignStmt {
|
||
|
|
pub lhs: Token,
|
||
|
|
pub rhs: ExprP,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl Stmt for AssignStmt {
|
||
|
|
fn accept(&self, visitor: &mut dyn StmtVisitor) -> Result<(), Box<dyn std::error::Error>>{
|
||
|
|
visitor.visit_assign_stmt(self)
|
||
|
|
}
|
||
|
|
|
||
|
|
fn as_any(self: Box<Self>) -> Box<dyn Any> {
|
||
|
|
self
|
||
|
|
}
|
||
|
|
|
||
|
|
fn as_any_ref(&self) -> &dyn Any {
|
||
|
|
self
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(Debug)]
|
||
|
|
pub struct SetStmt {
|
||
|
|
pub expr: ExprP,
|
||
|
|
pub name: Token,
|
||
|
|
pub rhs: ExprP,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl Stmt for SetStmt {
|
||
|
|
fn accept(&self, visitor: &mut dyn StmtVisitor) -> Result<(), Box<dyn std::error::Error>>{
|
||
|
|
visitor.visit_set_stmt(self)
|
||
|
|
}
|
||
|
|
|
||
|
|
fn as_any(self: Box<Self>) -> Box<dyn Any> {
|
||
|
|
self
|
||
|
|
}
|
||
|
|
|
||
|
|
fn as_any_ref(&self) -> &dyn Any {
|
||
|
|
self
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(Debug)]
|
||
|
|
pub struct BlockStmt {
|
||
|
|
pub lbrace: Token,
|
||
|
|
pub stmts: Vec<StmtP>,
|
||
|
|
pub rbrace: Token,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl Stmt for BlockStmt {
|
||
|
|
fn accept(&self, visitor: &mut dyn StmtVisitor) -> Result<(), Box<dyn std::error::Error>>{
|
||
|
|
visitor.visit_block_stmt(self)
|
||
|
|
}
|
||
|
|
|
||
|
|
fn as_any(self: Box<Self>) -> Box<dyn Any> {
|
||
|
|
self
|
||
|
|
}
|
||
|
|
|
||
|
|
fn as_any_ref(&self) -> &dyn Any {
|
||
|
|
self
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(Debug)]
|
||
|
|
pub struct ReturnStmt {
|
||
|
|
pub return_kw: Token,
|
||
|
|
pub expr: Option<ExprP>,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl Stmt for ReturnStmt {
|
||
|
|
fn accept(&self, visitor: &mut dyn StmtVisitor) -> Result<(), Box<dyn std::error::Error>>{
|
||
|
|
visitor.visit_return_stmt(self)
|
||
|
|
}
|
||
|
|
|
||
|
|
fn as_any(self: Box<Self>) -> Box<dyn Any> {
|
||
|
|
self
|
||
|
|
}
|
||
|
|
|
||
|
|
fn as_any_ref(&self) -> &dyn Any {
|
||
|
|
self
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(Debug)]
|
||
|
|
pub struct IfStmt {
|
||
|
|
pub if_kw: Token,
|
||
|
|
pub condition: ExprP,
|
||
|
|
pub then_branch: BlockStmt,
|
||
|
|
pub else_branch: Vec<StmtP>,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl Stmt for IfStmt {
|
||
|
|
fn accept(&self, visitor: &mut dyn StmtVisitor) -> Result<(), Box<dyn std::error::Error>>{
|
||
|
|
visitor.visit_if_stmt(self)
|
||
|
|
}
|
||
|
|
|
||
|
|
fn as_any(self: Box<Self>) -> Box<dyn Any> {
|
||
|
|
self
|
||
|
|
}
|
||
|
|
|
||
|
|
fn as_any_ref(&self) -> &dyn Any {
|
||
|
|
self
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|