1
0

Implement closed and open statement

This commit is contained in:
Jeff 2023-09-29 00:16:51 -04:00
parent 4983975e59
commit cdf79f8763
2 changed files with 19 additions and 12 deletions

View File

@ -109,7 +109,8 @@ impl Item {
Ok(Item::Comment(value_string.to_string())) Ok(Item::Comment(value_string.to_string()))
} else if node.kind() == "statement" { } else if node.kind() == "statement" {
Ok(Item::Statement(Statement::new(node, source)?)) let child = node.child(0).unwrap();
Ok(Item::Statement(Statement::new(child, source)?))
} else { } else {
Err(Error::UnexpectedSourceNode { Err(Error::UnexpectedSourceNode {
expected: "comment or statement", expected: "comment or statement",
@ -122,20 +123,20 @@ impl Item {
#[derive(Debug)] #[derive(Debug)]
enum Statement { enum Statement {
Closed(Expression), Closed(Expression),
Open(Expression),
} }
impl Statement { impl Statement {
fn new(node: Node, source: &str) -> Result<Self> { fn new(node: Node, source: &str) -> Result<Self> {
if node.kind() == "statement" { let child = node.child(0).unwrap();
Ok(Statement::Closed(Expression::new(
node.child(0).unwrap(), match node.kind() {
source, "closed_statement" => Ok(Statement::Closed(Expression::new(child, source)?)),
)?)) "open_statement" => Ok(Self::Open(Expression::new(child, source)?)),
} else { _ => Err(Error::UnexpectedSourceNode {
Err(Error::UnexpectedSourceNode { expected: "closed_statement or open_statement",
expected: "statement",
actual: node.kind(), actual: node.kind(),
}) }),
} }
} }
@ -146,7 +147,12 @@ impl Statement {
source: &str, source: &str,
) -> Result<Value> { ) -> Result<Value> {
match self { match self {
Statement::Closed(expression) => expression.run(context, &mut cursor, source), Statement::Closed(expression) => {
expression.run(context, &mut cursor, source)?;
Ok(Value::Empty)
}
Statement::Open(expression) => expression.run(context, &mut cursor, source),
} }
} }
} }

View File

@ -66,6 +66,7 @@ impl Value {
Ok(Value::Integer(raw)) Ok(Value::Integer(raw))
} }
"string" => Ok(Value::String(value_snippet.to_string())),
_ => Err(Error::UnexpectedSourceNode { _ => Err(Error::UnexpectedSourceNode {
expected: "raw value", expected: "raw value",
actual: child.kind(), actual: child.kind(),
@ -252,7 +253,7 @@ impl Add for Value {
Ok(Value::String(concatenated)) Ok(Value::String(concatenated))
} }
(Value::String(string), other) | (other, Value::String(string)) => { (Value::String(_), other) | (other, Value::String(_)) => {
Err(Error::ExpectedString { actual: other }) Err(Error::ExpectedString { actual: other })
} }
(Value::Float(left), Value::Float(right)) => { (Value::Float(left), Value::Float(right)) => {