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
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)* | ε
// a float_literal can still be assigned to an int in type-checking
@ -408,6 +408,10 @@ pub enum Expression {
Call {
name: Rc<String>,
params: Vec<Expression>,
},
Index {
indexee: Box<Expression>,
indexers: Vec<Expression>,
}
}
@ -548,9 +552,12 @@ impl Parser {
unimplemented!()
},
LSquareBracket => {
let bracket = self.index_expr()?;
unimplemented!()
},
let indexers = self.index_expr()?;
Ok(Expression::Index {
indexee: Box::new(Expression::Variable(identifier)),
indexers: indexers,
})
}
_ => Ok(Expression::Variable(identifier))
}
}
@ -559,8 +566,21 @@ impl Parser {
unimplemented!()
}
fn index_expr(&mut self) -> ParseResult<Expression> {
unimplemented!()
fn index_expr(&mut self) -> ParseResult<Vec<Expression>> {
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>> {
@ -715,8 +735,13 @@ mod parse_tests {
fn parsing_identifiers() {
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(
//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]
fn parsing_functions() {
parse_test!("fn oi()", AST(vec![Declaration(FuncDecl { name: rc!(oi), params: vec![] })]));

View File

@ -78,7 +78,7 @@ fn main() {
#what if type A = B meant that you could had to create A's with A(B), but when you used A's the interface was exactly like B's?
# maybe introduce a 'newtype' keyword for this
#declaring types of all stripes
type MyData = { a: i32, b: String }
type MyType = MyType