Index expressions

This only partially works
This commit is contained in:
greg 2017-09-14 00:11:52 -07:00
parent 5871bf68de
commit dfc89e5060
2 changed files with 32 additions and 7 deletions

View File

@ -307,7 +307,7 @@ identifier_expr := call_expr | index_expr | IDENTIFIER
literal := «true» | «false» | number_literal | str_literal literal := «true» | «false» | number_literal | str_literal
call_expr := IDENTIFIER «(» expr_list «)» //TODO maybe make this optional? or no, have a bare identifier meant to be used as method taken care of in eval call_expr := IDENTIFIER «(» expr_list «)» //TODO maybe make this optional? or no, have a bare identifier meant to be used as method taken care of in eval
index_expr := «(» (expression («,» (expression)* | ε) «)» index_expr := «[» (expression («,» (expression)* | ε) «]»
expr_list := expression («,» expression)* | ε expr_list := expression («,» expression)* | ε
// 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
@ -408,6 +408,10 @@ pub enum Expression {
Call { Call {
name: Rc<String>, name: Rc<String>,
params: Vec<Expression>, params: Vec<Expression>,
},
Index {
indexee: Box<Expression>,
indexers: Vec<Expression>,
} }
} }
@ -548,9 +552,12 @@ impl Parser {
unimplemented!() unimplemented!()
}, },
LSquareBracket => { LSquareBracket => {
let bracket = self.index_expr()?; let indexers = self.index_expr()?;
unimplemented!() Ok(Expression::Index {
}, indexee: Box::new(Expression::Variable(identifier)),
indexers: indexers,
})
}
_ => Ok(Expression::Variable(identifier)) _ => Ok(Expression::Variable(identifier))
} }
} }
@ -559,8 +566,21 @@ impl Parser {
unimplemented!() unimplemented!()
} }
fn index_expr(&mut self) -> ParseResult<Expression> { fn index_expr(&mut self) -> ParseResult<Vec<Expression>> {
unimplemented!() expect!(self, LSquareBracket, "Expected '['");
let mut exprs = Vec::new();
loop {
if let RSquareBracket = self.peek() {
break;
}
exprs.push(self.expression()?);
match self.peek() {
Comma => { self.next(); }
_ => break,
};
}
expect!(self, RSquareBracket, "Expected ']'");
Ok(exprs)
} }
fn identifier(&mut self) -> ParseResult<Rc<String>> { fn identifier(&mut self) -> ParseResult<Rc<String>> {
@ -715,8 +735,13 @@ mod parse_tests {
fn parsing_identifiers() { fn parsing_identifiers() {
parse_test!("a", AST(vec![Expression(var!("a"))])); parse_test!("a", AST(vec![Expression(var!("a"))]));
parse_test!("a + b", AST(vec![Expression(binexp!(op!("+"), var!("a"), var!("b")))])); parse_test!("a + b", AST(vec![Expression(binexp!(op!("+"), var!("a"), var!("b")))]));
//parse_test!("a[b]", AST(vec![Expression(
//parse_test!("a[]", <- TODO THIS NEEDS TO FAIL
//parse_test!(damn()[a] ,<- TODO needs to succeed
parse_test!("a[b,c]", AST(vec![Expression(Index { indexee: Box::new(var!("a")), indexers: vec![var!("b"), var!("c")]} )]));
} }
#[test] #[test]
fn parsing_functions() { fn parsing_functions() {
parse_test!("fn oi()", AST(vec![Declaration(FuncDecl { name: rc!(oi), params: vec![] })])); parse_test!("fn oi()", AST(vec![Declaration(FuncDecl { name: rc!(oi), params: vec![] })]));