From 477b98d8d271dafdbf4b02137a9068c70637c466 Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Sat, 27 Sep 2025 20:35:59 +0800 Subject: feat: ast --- src/ast.rs | 53 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 5 deletions(-) (limited to 'src/ast.rs') diff --git a/src/ast.rs b/src/ast.rs index 9777d4e..a87b6a2 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -1,17 +1,18 @@ -use crate::parser::{self, Expr, ExprT, Scope}; +use crate::parser::{Expr, ExprT, Metadata, Scope}; +#[derive(Clone, Debug)] pub enum DepthExpr<'a> { Decision { cond: Box>, t: Scope, - else_branch: Option>>, + else_branch: Option>>>, }, IO(&'a str), Process(&'a str), } -fn parse(vec: Vec) -> Vec { - let exprs = Vec::new(); +pub fn parse<'a, 'b>(vec: &'b Vec>) -> Vec> { + let mut exprs: Vec> = Vec::new(); let mut it = vec .iter() @@ -19,8 +20,50 @@ fn parse(vec: Vec) -> Vec { while let Some(e) = it.next() { if let ExprT::Decision((cond, scope)) = e.expr { - let cond = parser::parse(cond); + let cond = DepthExpr::Process(cond); + for e in it.clone() { + if e.meta == Some(Metadata::EndScope) { + break; + } + } + + exprs.push(DepthExpr::Decision { + cond: Box::new(cond), + t: scope, + else_branch: Some(Box::new(parse(&entire_scopes( + it.clone() + .cloned(), + )))), + }); + } + } + + exprs +} + +fn entire_scopes<'a>(it: impl Iterator>) -> Vec> { + let mut exprs: Vec> = Vec::new(); + let mut scope_count: u32 = 1; // assume we already entered the scope + + for i in it { + if scope_count == 0 { + break; + } + if let Some(meta) = i.meta { + match meta { + Metadata::StartScope => { + scope_count = scope_count + .checked_add(1) + .expect("Scopes nested too deep") + } + Metadata::EndScope => { + scope_count = scope_count + .checked_sub(1) + .expect("Closed not opened scope") + } + } } + exprs.push(i); } exprs -- cgit v1.2.3