fixed parser but syntax kinda janky now

This commit is contained in:
TriMill 2022-09-22 14:03:52 -04:00
parent d9b1f08bbf
commit 7ec8742e17
2 changed files with 7 additions and 5 deletions

View file

@ -7,6 +7,5 @@ edition = "2021"
num-complex = "0.4.2" num-complex = "0.4.2"
num-rational = "0.4.1" num-rational = "0.4.1"
num-traits = "0.2.15" num-traits = "0.2.15"
strum_macros = "0.24.3"
strum = { version = "0.24.1", features = ["derive"] } strum = { version = "0.24.1", features = ["derive"] }
paste = "1.0.9" paste = "1.0.9"

View file

@ -449,7 +449,7 @@ impl Parser {
match self.peek().ty { match self.peek().ty {
TokenType::LParen => expr = self.fncall_inner(expr)?, TokenType::LParen => expr = self.fncall_inner(expr)?,
TokenType::LBrack => expr = self.arrindex_inner(expr)?, TokenType::LBrack => expr = self.arrindex_inner(expr)?,
TokenType::LBrace => expr = self.structinit_inner(expr)?, TokenType::Colon => expr = self.structinit_inner(expr)?,
_ => return Ok(expr) _ => return Ok(expr)
} }
} }
@ -474,11 +474,14 @@ impl Parser {
Ok(Expr::Index { lhs: Box::new(expr), index: Box::new(index), pos: lbrack.pos }) Ok(Expr::Index { lhs: Box::new(expr), index: Box::new(index), pos: lbrack.pos })
} }
// struct initialization: A { b } // struct initialization: A: { b }
fn structinit_inner(&mut self, expr: Expr) -> Result<Expr, ParserError> { fn structinit_inner(&mut self, expr: Expr) -> Result<Expr, ParserError> {
let lbrace = self.next(); let colon = self.next();
if !self.expect(TokenType::LBrace).0 {
return Err(self.mk_error("Expected left brace in struct initialization"))
}
let args = self.commalist(TokenType::RBrace, Self::assignment)?; let args = self.commalist(TokenType::RBrace, Self::assignment)?;
Ok(Expr::StructInit { ty: Box::new(expr), args, pos: lbrace.pos }) Ok(Expr::StructInit { ty: Box::new(expr), args, pos: colon.pos })
} }
// key-value pairs for maps // key-value pairs for maps