weirdnet-code-spaces 0.1.0

Weirdnet code spaces: math, Polars, Rust and typed Python sources compiled into plugins against the weirdnet-plugin SDK, content-addressed builds, ABI-checked loading, trust hashes and space line-ups, under an application profile.
Documentation
use proc_macro2::TokenStream;
use quote::quote;
use serde::{Deserialize, Serialize};

use crate::python_ast::{CodeGen, CodeGenContext, ExprType, Node, PythonOptions, SymbolTableScopes, impl_node_with_positions};

use super::Statement;

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct If {
    pub test: ExprType,
    pub body: Vec<Statement>,
    pub orelse: Vec<Statement>,
    pub lineno: Option<usize>,
    pub col_offset: Option<usize>,
    pub end_lineno: Option<usize>,
    pub end_col_offset: Option<usize>,
}

impl_node_with_positions!(If { lineno, col_offset, end_lineno, end_col_offset });

impl CodeGen for If {
    type Context = CodeGenContext;
    type Options = PythonOptions;
    type SymbolTable = SymbolTableScopes;

    fn find_symbols(self, symbols: Self::SymbolTable) -> Self::SymbolTable {
        let symbols = self.test.find_symbols(symbols);
        let symbols = self.body.into_iter().fold(symbols, |acc, stmt| stmt.find_symbols(acc));
        self.orelse.into_iter().fold(symbols, |acc, stmt| stmt.find_symbols(acc))
    }

    fn to_rust(self, ctx: Self::Context, options: Self::Options, symbols: Self::SymbolTable) -> Result<TokenStream, Box<dyn std::error::Error>> {
        // Regular if statement handling; the test is a condition position,
        // so Python truthiness applies.
        let test = crate::python_ast::condition_to_rust(&self.test, ctx.clone(), options.clone(), symbols.clone())?;

        let body_stmts: Result<Vec<_>, _> = self.body.into_iter().map(|stmt| stmt.to_rust(ctx.clone(), options.clone(), symbols.clone())).collect();
        let body_stmts = body_stmts?;

        if self.orelse.is_empty() {
            Ok(quote! {
                if #test {
                    #(#body_stmts;)*
                }
            })
        } else {
            let else_stmts: Result<Vec<_>, _> =
                self.orelse.into_iter().map(|stmt| stmt.to_rust(ctx.clone(), options.clone(), symbols.clone())).collect();
            let else_stmts = else_stmts?;

            Ok(quote! {
                if #test {
                    #(#body_stmts;)*
                } else {
                    #(#else_stmts;)*
                }
            })
        }
    }
}

#[cfg(python_ast_upstream_tests)]
mod tests {
    use super::*;
    use crate::python_ast::create_parse_test;

    create_parse_test!(test_simple_if, "if x > 5:\n    print('big')", "if_test.py");
    create_parse_test!(test_if_else, "if x > 5:\n    print('big')\nelse:\n    print('small')", "if_test.py");
    create_parse_test!(test_if_elif, "if x > 10:\n    print('huge')\nelif x > 5:\n    print('big')\nelse:\n    print('small')", "if_test.py");
}