[Parser]
update declaration parser to parse array types
@@ -61,21 +61,12 @@ fn parse_generic_decl_statement(parser: &mut Parser) -> (Ident, Option<Type>, Ex
(name.into(), typ, expr)
}
-pub(crate) fn parse_decl_type(parser: &mut Parser) -> Type {
- // Currently we only support the default types and integers.
- // If we get into this function, then the user is specifying a type
- match &parser.curr_token {
- Token::Keyword(Keyword::Witness) => Type::Witness,
- Token::Keyword(Keyword::Public) => Type::Public,
- Token::IntType(int_type) => int_type.into(),
- _ => unimplemented!("This type is currently not supported"),
- }
-}
pub(crate) fn parse_let_statement(parser: &mut Parser) -> Box<LetStatement> {
let (name, typ, expr) = parse_generic_decl_statement(parser);
+
let stmt = LetStatement {
identifier: name,
- r#type: Type::Error, //XXX: Haven't implemented this yet for general structs
+ r#type: typ.unwrap_or(Type::Error), //XXX: Haven't implemented this yet for general structs, we only parse arrays using this
expression: expr,
};
Box::new(stmt)
@@ -124,3 +115,54 @@ pub(crate) fn parse_public_statement(parser: &mut Parser) -> Box<PublicStatement
};
Box::new(stmt)
}
+
+pub(crate) fn parse_decl_type(parser: &mut Parser) -> Type {
+ // Currently we only support the default types and integers.
+ // If we get into this function, then the user is specifying a type
+ match &parser.curr_token {
+ Token::Keyword(Keyword::Witness) => Type::Witness,
+ Token::Keyword(Keyword::Public) => Type::Public,
+ Token::IntType(int_type) => int_type.into(),
+ Token::LeftBracket => parse_array_type(parser),
+ k => unimplemented!("This type is currently not supported, {}", k),
+ }
+}
+
+fn parse_array_type(parser: &mut Parser) -> Type {
+ // Expression is of the form [3]Type
+
+ // Current token is '['
+ //
+ // Next token should be an Integer
+ if !parser.peek_check_int() {
+ panic!("Expected an Int")
+ };
+ let array_len = match parser.curr_token {
+ Token::Int(integer) => integer,
+ _ => panic!("User error: Expected an Integer for the array length"),
+ };
+
+ if array_len < 0 {
+ panic!("Cannot have a negative array size, [-k]Type is disallowed")
+ }
+ let array_len = array_len as u128;
+
+ if !parser.peek_check_variant_advance(Token::RightBracket) {
+ panic!(
+ "expected a right bracket after integer, got {}",
+ parser.peek_token
+ )
+ }
+
+ // Skip Right bracket
+ parser.advance_tokens();
+
+ // Disallow [4][3]Witness ie Matrices
+ if parser.peek_token == Token::LeftBracket {
+ panic!("Currently Multi-dimensional arrays are not supported")
+ }
+
+ let array_type = parse_decl_type(parser);
+
+ Type::Array(array_len, Box::new(array_type))
+}
