s2s2lua 0.1.0

Transpile Stranded II .s2s scripts to Lua for OpenStranded
Documentation
// s2s2lua – transpile Stranded II .s2s scripts to Lua for OpenStranded
// Copyright (C) 2025  s2s2lua contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

//! AST node types for parsed .s2s scripts.

/// A complete .s2s script.
#[derive(Debug, Clone)]
pub struct Script {
    pub statements: Vec<Stmt>,
}

/// A single statement.
#[derive(Debug, Clone)]
pub enum Stmt {
    /// An event handler: `on:Event { statements }`
    EventHandler {
        event: String,
        body: Vec<Stmt>,
    },
    /// An if/elseif/else chain.
    If {
        condition: Expr,
        then_body: Vec<Stmt>,
        else_ifs: Vec<ElseIfClause>,
        else_body: Vec<Stmt>,
    },
    /// A loop: `loop mode, arg { body }`
    Loop {
        mode: String,
        arg: String,
        body: Vec<Stmt>,
    },
    /// A simple command call ending with `;`.
    /// If `suppress` is true, it was prefixed with `@`.
    Command {
        name: String,
        args: Vec<Expr>,
        suppress: bool,
    },
    /// Variable assignment: `$var = expr;`
    Assign {
        var: String,
        value: Box<Expr>,
    },
    /// Compound assignment: `$var += expr;` or `$var -= expr;`
    CompoundAssign {
        var: String,
        op: AssignOp,
        value: Box<Expr>,
    },
    /// Increment or decrement: `$var++;` / `$var--;`
    IncDec {
        var: String,
        inc: bool, // true = ++, false = --
    },
    /// `exit;`
    Exit,
    /// `skip;`
    Skip,
    /// `skipevent;`
    SkipEvent,
    /// A comment (line or block) preserved verbatim for Lua output.
    Comment(String),
}

/// An elseif clause.
#[derive(Debug, Clone)]
pub struct ElseIfClause {
    pub condition: Expr,
    pub body: Vec<Stmt>,
}

/// Assignment operator for compound assignments.
#[derive(Debug, Clone)]
pub enum AssignOp {
    Add,
    Sub,
}

/// Arithmetic operator for binary expressions.
#[derive(Debug, Clone)]
pub enum ArithOp {
    Add,
    Sub,
    Mul,
    Div,
}

/// An expression: a value or a return-command.
#[derive(Debug, Clone)]
pub enum Expr {
    /// String literal (without surrounding quotes)
    String(String),
    /// Numeric literal (as text, to preserve formatting)
    Number(String),
    /// Variable reference
    Variable(String),
    /// Logical condition: left op right
    Condition {
        left: Box<Expr>,
        op: String,
        right: Box<Expr>,
    },
    /// Logical AND/OR/XOR: left op right
    Logical {
        left: Box<Expr>,
        op: LogicalOp,
        right: Box<Expr>,
    },
    /// A return-type command call used as an expression: `cmd(args)`
    CommandCall {
        name: String,
        args: Vec<Expr>,
    },
    /// Unary negation: `-expr`
    Negate(Box<Expr>),
    /// Binary arithmetic: `left + right`, `left - right`, `left * right`, `left / right`
    Arithmetic {
        left: Box<Expr>,
        op: ArithOp,
        right: Box<Expr>,
    },
}

/// Logical operator
#[derive(Debug, Clone)]
pub enum LogicalOp {
    And,
    Or,
    Xor,
}

impl Default for Script {
    fn default() -> Self {
        Self::new()
    }
}

impl Script {
    pub fn new() -> Self {
        Self { statements: Vec::new() }
    }
}