From 117e0e38a886e776b310f8b77d58919c91f792a4 Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 11 Sep 2017 15:42:49 -0700 Subject: [PATCH] Starting types --- src/schala_lang/parsing.rs | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/schala_lang/parsing.rs b/src/schala_lang/parsing.rs index 9d2c426..63a0e8d 100644 --- a/src/schala_lang/parsing.rs +++ b/src/schala_lang/parsing.rs @@ -285,13 +285,15 @@ statement := expression | declaration declaration := type_declaration | func_declaration -type_declaration := TYPE +type_declaration := TYPE identifier func_declaration := FN expression := primary primary := literal literal := TRUE | FALSE | number_literal | str_literal +identifier := IDENTIFIER + // a float_literal can still be assigned to an int in type-checking number_literal := int_literal | float_literal int_literal = (HEX_SIGIL | BIN_SIGIL) digits @@ -336,8 +338,8 @@ impl Parser { macro_rules! expect { ($self:expr, $token_type:pat, $message:expr) => { match $self.peek() { - Some($token_type) => {$self.next();}, - _ => return ParseError { msg: $message.to_string() }, + $token_type => $self.next(), + _ => return Err(ParseError { msg: $message.to_string() }), } } } @@ -354,7 +356,12 @@ pub enum Statement { #[derive(Debug, PartialEq)] pub enum Declaration { FuncDecl, - TypeDecl + TypeDecl(Rc, TypeBody) +} + +#[derive(Debug, PartialEq)] +pub enum TypeBody { + TypeBody } #[derive(Debug, PartialEq)] @@ -390,7 +397,9 @@ impl Parser { } fn type_declaration(&mut self) -> ParseResult { - unimplemented!() + expect!(self, Keyword(Type), "Expected 'type'"); + let name = self.identifier()?; + Ok(Declaration::TypeDecl(name, TypeBody::TypeBody)) } fn func_declaration(&mut self) -> ParseResult { @@ -405,6 +414,13 @@ impl Parser { self.literal() } + fn identifier(&mut self) -> ParseResult> { + match self.next() { + Identifier(s) => Ok(s), + p => ParseError::new(&format!("Expected an identifier, got {:?}", p)), + } + } + fn literal(&mut self) -> ParseResult { match self.peek() { DigitGroup(_) | HexNumberSigil | BinNumberSigil | Period => self.number_literal(),