From f1386f2d7b63c8a215b0d5c8c3fc5bc6c167aa0e Mon Sep 17 00:00:00 2001 From: Jonas Maier <> Date: Sat, 7 Mar 2026 09:26:37 +0100 Subject: hook up fundecl and variable assignment parsing to ast --- src/parse.rs | 47 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/parse.rs b/src/parse.rs index cbc37bf..b1721be 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -27,22 +27,24 @@ pub trait Expander { #[derive(Debug, Clone)] pub enum Ast { - VarAssign(VarAssign), + FunDecl(FunDecl), + VarAssign(VarAssign), Pipes(Pipes), } impl Ast { pub fn expand(self, e: &mut E) -> Res, E::Error> { match self { - Ast::VarAssign(_) => todo!(), + Ast::VarAssign(va) => Ok(Ast::VarAssign(va.expand(e)?)), Ast::Pipes(pipes) => Ok(Ast::Pipes(pipes.expand(e)?)), + Ast::FunDecl(fd) => Ok(Ast::FunDecl(fd.expand(e)?)), } } } #[derive(Debug, Clone)] pub struct FunBody { - pub body: Ast, + pub body: Box>, } impl Parse for FunBody { @@ -58,7 +60,7 @@ impl Parse for FunBody { } b.adv(); - let body = Ast::parse(b)?; + let body = Box::new(Ast::parse(b)?); if b.is_empty() { if b.is_completion() { Ok(Self { body }) @@ -81,7 +83,6 @@ pub struct FunDecl { impl Parse for FunDecl { fn parse(b: &mut Cursor<'_>) -> Result { - b.spaces(); if !b.buf.starts_with(b"fun ") && !b.buf.starts_with(b"fun\t") { return Err(ParseError::NotAFunDecl); } @@ -93,6 +94,15 @@ impl Parse for FunDecl { } } +impl FunDecl { + fn expand(self, e: &mut E) -> Res, E::Error> { + Ok(FunDecl { + name: self.name.expand(e)?, + body: self.body, + }) + } +} + #[derive(Debug, Clone)] pub struct VarDecl { pub name: S::Str, @@ -128,6 +138,15 @@ impl Parse for VarAssign { } } +impl VarAssign { + fn expand(self, e: &mut E) -> Res, E::Error> { + Ok(VarAssign { + var: self.var.expand(e)?, + val: self.val.expand(e)?, + }) + } +} + #[derive(Debug, Clone)] pub struct Pipes { pub cmds: Vec>, @@ -550,6 +569,24 @@ impl<'a> Cursor<'a> { impl Parse for Ast { fn parse(b: &mut Cursor<'_>) -> Result { + b.spaces(); + + let orig_len = b.buf.len(); + let x = VarAssign::parse(b); + if let Ok(va) = x { + return Ok(Self::VarAssign(va)); + } else if b.buf.len() != orig_len { + x?; + } + + let orig_len = b.buf.len(); + let x = FunDecl::parse(b); + if let Ok(fd) = x { + return Ok(Self::FunDecl(fd)); + } else if b.buf.len() != orig_len { + x?; + } + Ok(Self::Pipes(b.parse()?)) } } -- cgit v1.2.3