Skip to content

Commit 9aabc87

Browse files
committed
refactor(parser/parse_type): change array type syntax
Place brackets before the type, eg. `[8]u8`
1 parent 2c4d9a2 commit 9aabc87

File tree

1 file changed

+33
-59
lines changed

1 file changed

+33
-59
lines changed

src/parser/mod.rs

Lines changed: 33 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,11 @@ impl<'a, 'src, T: Iterator<Item = Result<Token, Span>>> Parser<'a, 'src, T> {
198198
_ => todo!("Don't know what error to return yet"),
199199
};
200200
self.expect(&Token::Colon)?;
201-
let mut type_ = self.parse_type()?;
202-
self.array_type(&mut type_)?;
201+
let ty = self.parse_type()?;
203202

204203
match fields.iter().find(|(field_name, _)| field_name == &name) {
205204
Some(_) => todo!("Don't know yet what error to return"),
206-
None => fields.push((name, type_)),
205+
None => fields.push((name, ty)),
207206
};
208207

209208
if !self.cur_token_is(&Token::RBrace) {
@@ -273,26 +272,33 @@ impl<'a, 'src, T: Iterator<Item = Result<Token, Span>>> Parser<'a, 'src, T> {
273272
}
274273

275274
fn parse_type(&mut self) -> Result<Ty, Error> {
276-
let mut n = 0;
277-
while self.cur_token_is(&Token::Asterisk) {
278-
self.expect(&Token::Asterisk)?;
279-
n += 1;
280-
}
275+
Ok(match self.next_token()?.unwrap() {
276+
Token::Asterisk => Ty::Ptr(Box::new(self.parse_type()?)),
277+
Token::LBracket => match self.next_token()?.unwrap() {
278+
Token::Integer(int) => {
279+
let length: usize = str::parse(&int).unwrap();
280+
self.expect(&Token::RBracket)?;
281281

282-
let mut base = match self.next_token()?.unwrap() {
283-
Token::U8 => Ok(Ty::UInt(UintTy::U8)),
284-
Token::U16 => Ok(Ty::UInt(UintTy::U16)),
285-
Token::U32 => Ok(Ty::UInt(UintTy::U32)),
286-
Token::U64 => Ok(Ty::UInt(UintTy::U64)),
287-
Token::I8 => Ok(Ty::Int(IntTy::I8)),
288-
Token::I16 => Ok(Ty::Int(IntTy::I16)),
289-
Token::I32 => Ok(Ty::Int(IntTy::I32)),
290-
Token::I64 => Ok(Ty::Int(IntTy::I64)),
291-
Token::Usize => Ok(Ty::UInt(UintTy::Usize)),
292-
Token::Isize => Ok(Ty::Int(IntTy::Isize)),
293-
Token::Bool => Ok(Ty::Bool),
294-
Token::Void => Ok(Ty::Void),
295-
Token::Ident(ident) => Ok(Ty::Ident(ident)),
282+
Ty::Array {
283+
ty: Box::new(self.parse_type()?),
284+
len: length,
285+
}
286+
}
287+
token => panic!("Expected integer, got {token}"),
288+
},
289+
Token::U8 => Ty::UInt(UintTy::U8),
290+
Token::U16 => Ty::UInt(UintTy::U16),
291+
Token::U32 => Ty::UInt(UintTy::U32),
292+
Token::U64 => Ty::UInt(UintTy::U64),
293+
Token::I8 => Ty::Int(IntTy::I8),
294+
Token::I16 => Ty::Int(IntTy::I16),
295+
Token::I32 => Ty::Int(IntTy::I32),
296+
Token::I64 => Ty::Int(IntTy::I64),
297+
Token::Usize => Ty::UInt(UintTy::Usize),
298+
Token::Isize => Ty::Int(IntTy::Isize),
299+
Token::Bool => Ty::Bool,
300+
Token::Void => Ty::Void,
301+
Token::Ident(ident) => Ty::Ident(ident),
296302
Token::Fn => {
297303
self.expect(&Token::LParen)?;
298304

@@ -309,17 +315,10 @@ impl<'a, 'src, T: Iterator<Item = Result<Token, Span>>> Parser<'a, 'src, T> {
309315
self.expect(&Token::RParen)?;
310316
self.expect(&Token::Arrow)?;
311317

312-
Ok(Ty::Fn(params, Box::new(self.parse_type()?)))
318+
Ty::Fn(params, Box::new(self.parse_type()?))
313319
}
314-
token => Err(Error::ParseType(token)),
315-
}?;
316-
317-
while n > 0 {
318-
base = Ty::Ptr(Box::new(base));
319-
n -= 1;
320-
}
321-
322-
Ok(base)
320+
token => return Err(Error::ParseType(token)),
321+
})
323322
}
324323

325324
fn parse_return(&mut self) -> Result<Stmt, Error> {
@@ -403,27 +402,6 @@ impl<'a, 'src, T: Iterator<Item = Result<Token, Span>>> Parser<'a, 'src, T> {
403402
}))
404403
}
405404

406-
fn array_type(&mut self, type_: &mut Ty) -> Result<(), Error> {
407-
if self.cur_token_is(&Token::LBracket) {
408-
self.expect(&Token::LBracket)?;
409-
410-
match self.next_token()?.unwrap() {
411-
Token::Integer(int) => {
412-
let length: usize = str::parse(&int).unwrap();
413-
self.expect(&Token::RBracket)?;
414-
415-
*type_ = Ty::Array {
416-
ty: Box::new(type_.clone()),
417-
len: length,
418-
};
419-
}
420-
token => panic!("Expected integer, got {token}"),
421-
}
422-
}
423-
424-
Ok(())
425-
}
426-
427405
fn local(&mut self) -> Result<Stmt, Error> {
428406
self.expect(&Token::Let)?;
429407

@@ -436,10 +414,7 @@ impl<'a, 'src, T: Iterator<Item = Result<Token, Span>>> Parser<'a, 'src, T> {
436414
let ty = if self.cur_token_is(&Token::Colon) {
437415
self.expect(&Token::Colon)?;
438416

439-
let mut ty = self.parse_type()?;
440-
self.array_type(&mut ty)?;
441-
442-
ty
417+
self.parse_type()?
443418
} else {
444419
Ty::Infer
445420
};
@@ -472,8 +447,7 @@ impl<'a, 'src, T: Iterator<Item = Result<Token, Span>>> Parser<'a, 'src, T> {
472447
};
473448
self.expect(&Token::Colon)?;
474449

475-
let mut ty = self.parse_type()?;
476-
self.array_type(&mut ty)?;
450+
let ty = self.parse_type()?;
477451

478452
let expr = if self.cur_token_is(&Token::Assign) {
479453
self.expect(&Token::Assign)?;

0 commit comments

Comments
 (0)