Begin adding "new" expressions

This commit is contained in:
Jeff 2024-01-23 16:01:21 -05:00
parent 42ec57bf82
commit 6c997c837d
3 changed files with 43 additions and 3 deletions

View File

@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};
use crate::{ use crate::{
value_node::ValueNode, AbstractTree, Error, Format, FunctionCall, Identifier, Index, Logic, value_node::ValueNode, AbstractTree, Error, Format, FunctionCall, Identifier, Index, Logic,
Map, Math, Result, SyntaxNode, Type, Value, Yield, Map, Math, New, Result, SyntaxNode, Type, Value, Yield,
}; };
/// Abstract representation of an expression statement. /// Abstract representation of an expression statement.
@ -19,6 +19,7 @@ pub enum Expression {
Logic(Box<Logic>), Logic(Box<Logic>),
FunctionCall(Box<FunctionCall>), FunctionCall(Box<FunctionCall>),
Yield(Box<Yield>), Yield(Box<Yield>),
New(New),
} }
impl AbstractTree for Expression { impl AbstractTree for Expression {
@ -43,9 +44,10 @@ impl AbstractTree for Expression {
child, source, context, child, source, context,
)?)), )?)),
"yield" => Expression::Yield(Box::new(Yield::from_syntax(child, source, context)?)), "yield" => Expression::Yield(Box::new(Yield::from_syntax(child, source, context)?)),
"new" => Expression::New(New::from_syntax(child, source, context)?),
_ => { _ => {
return Err(Error::UnexpectedSyntaxNode { return Err(Error::UnexpectedSyntaxNode {
expected: "value_node, identifier, index, math, logic, function_call or yield" expected: "value, identifier, index, math, logic, function call, new or ->"
.to_string(), .to_string(),
actual: child.kind().to_string(), actual: child.kind().to_string(),
location: child.start_position(), location: child.start_position(),
@ -66,6 +68,7 @@ impl AbstractTree for Expression {
Expression::FunctionCall(function_call) => function_call.check_type(_source, _context), Expression::FunctionCall(function_call) => function_call.check_type(_source, _context),
Expression::Index(index) => index.check_type(_source, _context), Expression::Index(index) => index.check_type(_source, _context),
Expression::Yield(r#yield) => r#yield.check_type(_source, _context), Expression::Yield(r#yield) => r#yield.check_type(_source, _context),
Expression::New(_) => todo!(),
} }
} }
@ -78,6 +81,7 @@ impl AbstractTree for Expression {
Expression::FunctionCall(function_call) => function_call.run(source, context), Expression::FunctionCall(function_call) => function_call.run(source, context),
Expression::Index(index) => index.run(source, context), Expression::Index(index) => index.run(source, context),
Expression::Yield(r#yield) => r#yield.run(source, context), Expression::Yield(r#yield) => r#yield.run(source, context),
Expression::New(_) => todo!(),
} }
} }
@ -90,6 +94,7 @@ impl AbstractTree for Expression {
Expression::FunctionCall(function_call) => function_call.expected_type(context), Expression::FunctionCall(function_call) => function_call.expected_type(context),
Expression::Index(index) => index.expected_type(context), Expression::Index(index) => index.expected_type(context),
Expression::Yield(r#yield) => r#yield.expected_type(context), Expression::Yield(r#yield) => r#yield.expected_type(context),
Expression::New(_) => todo!(),
} }
} }
} }
@ -104,6 +109,7 @@ impl Format for Expression {
Expression::FunctionCall(function_call) => function_call.format(output, indent_level), Expression::FunctionCall(function_call) => function_call.format(output, indent_level),
Expression::Index(index) => index.format(output, indent_level), Expression::Index(index) => index.format(output, indent_level),
Expression::Yield(r#yield) => r#yield.format(output, indent_level), Expression::Yield(r#yield) => r#yield.format(output, indent_level),
Expression::New(_) => todo!(),
} }
} }
} }

View File

@ -25,6 +25,7 @@ pub mod logic_operator;
pub mod r#match; pub mod r#match;
pub mod math; pub mod math;
pub mod math_operator; pub mod math_operator;
pub mod new;
pub mod statement; pub mod statement;
pub mod r#type; pub mod r#type;
pub mod type_specification; pub mod type_specification;
@ -36,7 +37,7 @@ pub use {
assignment::*, assignment_operator::*, block::*, built_in_value::*, expression::*, assignment::*, assignment_operator::*, block::*, built_in_value::*, expression::*,
function_call::*, function_expression::*, function_node::*, identifier::*, if_else::*, function_call::*, function_expression::*, function_node::*, identifier::*, if_else::*,
index::*, index_assignment::IndexAssignment, index_expression::*, logic::*, logic_operator::*, index::*, index_assignment::IndexAssignment, index_expression::*, logic::*, logic_operator::*,
math::*, math_operator::*, r#for::*, r#match::*, r#type::*, r#while::*, r#yield::*, math::*, math_operator::*, new::*, r#for::*, r#match::*, r#type::*, r#while::*, r#yield::*,
statement::*, type_specification::*, value_node::*, statement::*, type_specification::*, value_node::*,
}; };

33
src/abstract_tree/new.rs Normal file
View File

@ -0,0 +1,33 @@
use serde::{Deserialize, Serialize};
use crate::{AbstractTree, Format, Identifier, Type, TypeSpecification, Value, ValueNode};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct New {
identifier: Identifier,
properties: Vec<(Identifier, ValueNode, Option<TypeSpecification>)>,
}
impl AbstractTree for New {
fn from_syntax(
node: tree_sitter::Node,
source: &str,
context: &crate::Map,
) -> crate::Result<Self> {
todo!()
}
fn run(&self, source: &str, context: &crate::Map) -> crate::Result<Value> {
todo!()
}
fn expected_type(&self, context: &crate::Map) -> crate::Result<Type> {
todo!()
}
}
impl Format for New {
fn format(&self, output: &mut String, indent_level: u8) {
todo!()
}
}