robinpath 0.2.0

RobinPath - A lightweight, fast scripting language interpreter for automation and data processing
Documentation
use super::operators::{BinaryOp, UnaryOp};
use super::span::Span;
use super::stmt::Stmt;
use crate::value::AttributePathSegment;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ObjectProperty {
    pub key: String,
    pub value: Expr,
    pub span: Span,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Expr {
    /// Literal value: number, string, bool, null
    Literal {
        value: LiteralValue,
        span: Span,
    },
    /// Variable reference: $var, $obj.prop, $arr[0]
    Variable {
        name: String,
        path: Vec<AttributePathSegment>,
        span: Span,
    },
    /// Last value: $
    LastValue {
        span: Span,
    },
    /// Binary operation: a + b, a == b, etc.
    Binary {
        op: BinaryOp,
        left: Box<Expr>,
        right: Box<Expr>,
        span: Span,
    },
    /// Unary operation: not x, -x
    Unary {
        op: UnaryOp,
        argument: Box<Expr>,
        span: Span,
    },
    /// Function/command call used as expression: math.add(5, 3)
    Call {
        callee: String,
        args: Vec<Expr>,
        span: Span,
    },
    /// Object literal: { key: value, ... }
    ObjectLiteral {
        properties: Vec<ObjectProperty>,
        span: Span,
    },
    /// Array literal: [a, b, c]
    ArrayLiteral {
        elements: Vec<Expr>,
        span: Span,
    },
    /// Subexpression: $( ... )
    Subexpression {
        body: Vec<Stmt>,
        span: Span,
    },
    /// Template string: `hello ${name}`
    TemplateLiteral {
        raw: String,
        span: Span,
    },
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum LiteralValue {
    Null,
    Bool(bool),
    Number(f64),
    String(String),
}

impl Expr {
    pub fn span(&self) -> &Span {
        match self {
            Expr::Literal { span, .. }
            | Expr::Variable { span, .. }
            | Expr::LastValue { span }
            | Expr::Binary { span, .. }
            | Expr::Unary { span, .. }
            | Expr::Call { span, .. }
            | Expr::ObjectLiteral { span, .. }
            | Expr::ArrayLiteral { span, .. }
            | Expr::Subexpression { span, .. }
            | Expr::TemplateLiteral { span, .. } => span,
        }
    }
}