Fix assignment parse

This commit is contained in:
JT 2021-07-17 17:28:25 +12:00
parent 0b8352049c
commit 6b0b8744c1

View File

@ -182,37 +182,37 @@ impl Expression {
} }
} }
pub fn as_block(self) -> Option<BlockId> { pub fn as_block(&self) -> Option<BlockId> {
match self.expr { match self.expr {
Expr::Block(block_id) => Some(block_id), Expr::Block(block_id) => Some(block_id),
_ => None, _ => None,
} }
} }
pub fn as_signature(self) -> Option<Signature> { pub fn as_signature(&self) -> Option<Signature> {
match self.expr { match &self.expr {
Expr::Signature(sig) => Some(sig), Expr::Signature(sig) => Some(sig.clone()),
_ => None, _ => None,
} }
} }
pub fn as_list(self) -> Option<Vec<Expression>> { pub fn as_list(&self) -> Option<Vec<Expression>> {
match self.expr { match &self.expr {
Expr::List(list) => Some(list), Expr::List(list) => Some(list.clone()),
_ => None, _ => None,
} }
} }
pub fn as_var(self) -> Option<VarId> { pub fn as_var(&self) -> Option<VarId> {
match self.expr { match self.expr {
Expr::Var(var_id) => Some(var_id), Expr::Var(var_id) => Some(var_id),
_ => None, _ => None,
} }
} }
pub fn as_string(self) -> Option<String> { pub fn as_string(&self) -> Option<String> {
match self.expr { match &self.expr {
Expr::String(string) => Some(string), Expr::String(string) => Some(string.clone()),
_ => None, _ => None,
} }
} }
@ -271,9 +271,6 @@ pub struct VarDecl {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum Statement { pub enum Statement {
Pipeline(Pipeline), Pipeline(Pipeline),
VarDecl(VarDecl),
Declaration(DeclId),
Import(Import),
Expression(Expression), Expression(Expression),
None, None,
} }
@ -494,7 +491,6 @@ impl ParserWorkingSet {
shape: SyntaxShape, shape: SyntaxShape,
) -> (Expression, Option<ParseError>) { ) -> (Expression, Option<ParseError>) {
let mut error = None; let mut error = None;
let arg_span = spans[*spans_idx];
match shape { match shape {
SyntaxShape::VarWithOptType => { SyntaxShape::VarWithOptType => {
@ -504,21 +500,24 @@ impl ParserWorkingSet {
(arg, error) (arg, error)
} }
SyntaxShape::RowCondition => { SyntaxShape::RowCondition => {
let (arg, err) = self.parse_row_condition(spans); let (arg, err) = self.parse_row_condition(&spans[*spans_idx..]);
error = error.or(err); error = error.or(err);
*spans_idx = spans.len(); *spans_idx = spans.len();
(arg, error) (arg, error)
} }
SyntaxShape::Expression => { SyntaxShape::Expression => {
let (arg, err) = self.parse_expression(spans); let (arg, err) = self.parse_expression(&spans[*spans_idx..]);
error = error.or(err); error = error.or(err);
*spans_idx = spans.len(); *spans_idx = spans.len();
(arg, error) (arg, error)
} }
SyntaxShape::Literal(literal) => { SyntaxShape::Literal(literal) => {
let arg_span = spans[*spans_idx];
let arg_contents = self.get_span_contents(arg_span); let arg_contents = self.get_span_contents(arg_span);
if arg_contents != literal { if arg_contents != literal {
// When keywords mismatch, this is a strong indicator of something going wrong. // When keywords mismatch, this is a strong indicator of something going wrong.
// We won't often override the current error, but as this is a strong indicator // We won't often override the current error, but as this is a strong indicator
@ -539,6 +538,8 @@ impl ParserWorkingSet {
} }
_ => { _ => {
// All other cases are single-span values // All other cases are single-span values
let arg_span = spans[*spans_idx];
let (arg, err) = self.parse_value(arg_span, shape); let (arg, err) = self.parse_value(arg_span, shape);
error = error.or(err); error = error.or(err);
@ -938,7 +939,6 @@ impl ParserWorkingSet {
Flag(Flag), Flag(Flag),
} }
println!("parse signature");
let bytes = self.get_span_contents(span); let bytes = self.get_span_contents(span);
let mut error = None; let mut error = None;
@ -1564,7 +1564,6 @@ impl ParserWorkingSet {
expr_stack.push(lhs); expr_stack.push(lhs);
while idx < spans.len() { while idx < spans.len() {
println!("idx: {}", idx);
let (op, err) = self.parse_operator(spans[idx]); let (op, err) = self.parse_operator(spans[idx]);
error = error.or(err); error = error.or(err);
@ -1675,7 +1674,7 @@ impl ParserWorkingSet {
if name == b"def" { if name == b"def" {
if let Some(decl_id) = self.find_decl(b"def") { if let Some(decl_id) = self.find_decl(b"def") {
let (mut call, call_span, err) = self.parse_internal_call(spans, decl_id); let (call, call_span, err) = self.parse_internal_call(spans, decl_id);
if err.is_some() { if err.is_some() {
return ( return (
@ -1686,20 +1685,13 @@ impl ParserWorkingSet {
err, err,
); );
} else { } else {
println!("{:?}", call); let name = call.positional[0]
let name = call
.positional
.remove(0)
.as_string() .as_string()
.expect("internal error: expected def name"); .expect("internal error: expected def name");
let mut signature = call let mut signature = call.positional[1]
.positional
.remove(0)
.as_signature() .as_signature()
.expect("internal error: expected param list"); .expect("internal error: expected param list");
let block_id = call let block_id = call.positional[2]
.positional
.remove(0)
.as_block() .as_block()
.expect("internal error: expected block"); .expect("internal error: expected block");
@ -1709,9 +1701,15 @@ impl ParserWorkingSet {
body: Some(block_id), body: Some(block_id),
}; };
let decl_id = self.add_decl(decl); self.add_decl(decl);
return (Statement::Declaration(decl_id), None); return (
Statement::Expression(Expression {
expr: Expr::Call(call),
span: call_span,
}),
None,
);
} }
} }
} }
@ -1732,24 +1730,15 @@ impl ParserWorkingSet {
if name == b"let" { if name == b"let" {
if let Some(decl_id) = self.find_decl(b"let") { if let Some(decl_id) = self.find_decl(b"let") {
let (mut call, call_span, err) = self.parse_internal_call(spans, decl_id); let (call, call_span, err) = self.parse_internal_call(spans, decl_id);
if err.is_some() { return (
return ( Statement::Expression(Expression {
Statement::Expression(Expression { expr: Expr::Call(call),
expr: Expr::Call(call), span: call_span,
span: call_span, }),
}), err,
err, );
);
} else if let Expression {
expr: Expr::Var(var_id),
..
} = call.positional[0]
{
let expression = call.positional.swap_remove(2);
return (Statement::VarDecl(VarDecl { var_id, expression }), None);
}
} }
} }
( (