Starting types

This commit is contained in:
greg 2017-09-11 15:42:49 -07:00
parent 3f1de5f60d
commit 117e0e38a8

View File

@ -285,13 +285,15 @@ statement := expression | declaration
declaration := type_declaration | func_declaration declaration := type_declaration | func_declaration
type_declaration := TYPE type_declaration := TYPE identifier
func_declaration := FN func_declaration := FN
expression := primary expression := primary
primary := literal primary := literal
literal := TRUE | FALSE | number_literal | str_literal literal := TRUE | FALSE | number_literal | str_literal
identifier := IDENTIFIER
// a float_literal can still be assigned to an int in type-checking // a float_literal can still be assigned to an int in type-checking
number_literal := int_literal | float_literal number_literal := int_literal | float_literal
int_literal = (HEX_SIGIL | BIN_SIGIL) digits int_literal = (HEX_SIGIL | BIN_SIGIL) digits
@ -336,8 +338,8 @@ impl Parser {
macro_rules! expect { macro_rules! expect {
($self:expr, $token_type:pat, $message:expr) => { ($self:expr, $token_type:pat, $message:expr) => {
match $self.peek() { match $self.peek() {
Some($token_type) => {$self.next();}, $token_type => $self.next(),
_ => return ParseError { msg: $message.to_string() }, _ => return Err(ParseError { msg: $message.to_string() }),
} }
} }
} }
@ -354,7 +356,12 @@ pub enum Statement {
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum Declaration { pub enum Declaration {
FuncDecl, FuncDecl,
TypeDecl TypeDecl(Rc<String>, TypeBody)
}
#[derive(Debug, PartialEq)]
pub enum TypeBody {
TypeBody
} }
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
@ -390,7 +397,9 @@ impl Parser {
} }
fn type_declaration(&mut self) -> ParseResult<Declaration> { fn type_declaration(&mut self) -> ParseResult<Declaration> {
unimplemented!() expect!(self, Keyword(Type), "Expected 'type'");
let name = self.identifier()?;
Ok(Declaration::TypeDecl(name, TypeBody::TypeBody))
} }
fn func_declaration(&mut self) -> ParseResult<Declaration> { fn func_declaration(&mut self) -> ParseResult<Declaration> {
@ -405,6 +414,13 @@ impl Parser {
self.literal() self.literal()
} }
fn identifier(&mut self) -> ParseResult<Rc<String>> {
match self.next() {
Identifier(s) => Ok(s),
p => ParseError::new(&format!("Expected an identifier, got {:?}", p)),
}
}
fn literal(&mut self) -> ParseResult<Expression> { fn literal(&mut self) -> ParseResult<Expression> {
match self.peek() { match self.peek() {
DigitGroup(_) | HexNumberSigil | BinNumberSigil | Period => self.number_literal(), DigitGroup(_) | HexNumberSigil | BinNumberSigil | Period => self.number_literal(),