From e4bd0a51d6152c745652cc3419dd092e74e5b5e3 Mon Sep 17 00:00:00 2001 From: Jeff Date: Tue, 23 Jan 2024 17:03:35 -0500 Subject: [PATCH] Continue implementing type definition type --- src/abstract_tree/expression.rs | 74 ++++++++++++++++----------------- src/abstract_tree/new.rs | 21 ++++++---- src/value/type_definition.rs | 4 +- 3 files changed, 54 insertions(+), 45 deletions(-) diff --git a/src/abstract_tree/expression.rs b/src/abstract_tree/expression.rs index 4774f18..a6ac1f4 100644 --- a/src/abstract_tree/expression.rs +++ b/src/abstract_tree/expression.rs @@ -23,7 +23,7 @@ pub enum Expression { } impl AbstractTree for Expression { - fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> Result { + fn from_syntax(node: SyntaxNode, source: &str, _context: &Map) -> Result { Error::expect_syntax_node(source, "expression", node)?; let child = if node.child(0).unwrap().is_named() { @@ -33,18 +33,18 @@ impl AbstractTree for Expression { }; let expression = match child.kind() { - "value" => Expression::Value(ValueNode::from_syntax(child, source, context)?), + "value" => Expression::Value(ValueNode::from_syntax(child, source, _context)?), "identifier" => { - Expression::Identifier(Identifier::from_syntax(child, source, context)?) + Expression::Identifier(Identifier::from_syntax(child, source, _context)?) } - "index" => Expression::Index(Box::new(Index::from_syntax(child, source, context)?)), - "math" => Expression::Math(Box::new(Math::from_syntax(child, source, context)?)), - "logic" => Expression::Logic(Box::new(Logic::from_syntax(child, source, context)?)), + "index" => Expression::Index(Box::new(Index::from_syntax(child, source, _context)?)), + "math" => Expression::Math(Box::new(Math::from_syntax(child, source, _context)?)), + "logic" => Expression::Logic(Box::new(Logic::from_syntax(child, source, _context)?)), "function_call" => Expression::FunctionCall(Box::new(FunctionCall::from_syntax( - child, source, context, + child, source, _context, )?)), - "yield" => Expression::Yield(Box::new(Yield::from_syntax(child, source, context)?)), - "new" => Expression::New(New::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 { expected: "value, identifier, index, math, logic, function call, new or ->" @@ -68,48 +68,48 @@ impl AbstractTree for Expression { Expression::FunctionCall(function_call) => function_call.check_type(_source, _context), Expression::Index(index) => index.check_type(_source, _context), Expression::Yield(r#yield) => r#yield.check_type(_source, _context), - Expression::New(_) => todo!(), + Expression::New(new) => new.check_type(_source, _context), } } - fn run(&self, source: &str, context: &Map) -> Result { + fn run(&self, _source: &str, _context: &Map) -> Result { match self { - Expression::Value(value_node) => value_node.run(source, context), - Expression::Identifier(identifier) => identifier.run(source, context), - Expression::Math(math) => math.run(source, context), - Expression::Logic(logic) => logic.run(source, context), - Expression::FunctionCall(function_call) => function_call.run(source, context), - Expression::Index(index) => index.run(source, context), - Expression::Yield(r#yield) => r#yield.run(source, context), - Expression::New(_) => todo!(), + Expression::Value(value_node) => value_node.run(_source, _context), + Expression::Identifier(identifier) => identifier.run(_source, _context), + Expression::Math(math) => math.run(_source, _context), + Expression::Logic(logic) => logic.run(_source, _context), + Expression::FunctionCall(function_call) => function_call.run(_source, _context), + Expression::Index(index) => index.run(_source, _context), + Expression::Yield(r#yield) => r#yield.run(_source, _context), + Expression::New(new) => new.run(_source, _context), } } - fn expected_type(&self, context: &Map) -> Result { + fn expected_type(&self, _context: &Map) -> Result { match self { - Expression::Value(value_node) => value_node.expected_type(context), - Expression::Identifier(identifier) => identifier.expected_type(context), - Expression::Math(math) => math.expected_type(context), - Expression::Logic(logic) => logic.expected_type(context), - Expression::FunctionCall(function_call) => function_call.expected_type(context), - Expression::Index(index) => index.expected_type(context), - Expression::Yield(r#yield) => r#yield.expected_type(context), - Expression::New(_) => todo!(), + Expression::Value(value_node) => value_node.expected_type(_context), + Expression::Identifier(identifier) => identifier.expected_type(_context), + Expression::Math(math) => math.expected_type(_context), + Expression::Logic(logic) => logic.expected_type(_context), + Expression::FunctionCall(function_call) => function_call.expected_type(_context), + Expression::Index(index) => index.expected_type(_context), + Expression::Yield(r#yield) => r#yield.expected_type(_context), + Expression::New(new) => new.expected_type(_context), } } } impl Format for Expression { - fn format(&self, output: &mut String, indent_level: u8) { + fn format(&self, _output: &mut String, _indent_level: u8) { match self { - Expression::Value(value_node) => value_node.format(output, indent_level), - Expression::Identifier(identifier) => identifier.format(output, indent_level), - Expression::Math(math) => math.format(output, indent_level), - Expression::Logic(logic) => logic.format(output, indent_level), - Expression::FunctionCall(function_call) => function_call.format(output, indent_level), - Expression::Index(index) => index.format(output, indent_level), - Expression::Yield(r#yield) => r#yield.format(output, indent_level), - Expression::New(_) => todo!(), + Expression::Value(value_node) => value_node.format(_output, _indent_level), + Expression::Identifier(identifier) => identifier.format(_output, _indent_level), + Expression::Math(math) => math.format(_output, _indent_level), + Expression::Logic(logic) => logic.format(_output, _indent_level), + Expression::FunctionCall(function_call) => function_call.format(_output, _indent_level), + Expression::Index(index) => index.format(_output, _indent_level), + Expression::Yield(r#yield) => r#yield.format(_output, _indent_level), + Expression::New(new) => new.format(_output, _indent_level), } } } diff --git a/src/abstract_tree/new.rs b/src/abstract_tree/new.rs index ae16300..cc4d496 100644 --- a/src/abstract_tree/new.rs +++ b/src/abstract_tree/new.rs @@ -1,6 +1,9 @@ use serde::{Deserialize, Serialize}; +use tree_sitter::Node; -use crate::{AbstractTree, Format, Identifier, Type, TypeSpecification, Value, ValueNode}; +use crate::{ + AbstractTree, Format, Identifier, Map, Result, Type, TypeSpecification, Value, ValueNode, +}; #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)] pub struct New { @@ -9,12 +12,16 @@ pub struct New { } impl AbstractTree for New { - fn from_syntax( - node: tree_sitter::Node, - source: &str, - context: &crate::Map, - ) -> crate::Result { - todo!() + fn from_syntax(node: Node, source: &str, context: &Map) -> Result { + let identifier_node = node.child(1).unwrap(); + let identifier = Identifier::from_syntax(identifier_node, source, context)?; + + let mut properties = Vec::new(); + + Ok(New { + identifier, + properties, + }) } fn run(&self, source: &str, context: &crate::Map) -> crate::Result { diff --git a/src/value/type_definition.rs b/src/value/type_definition.rs index b1a14ed..fc72d29 100644 --- a/src/value/type_definition.rs +++ b/src/value/type_definition.rs @@ -11,6 +11,8 @@ pub enum TypeDefintion { impl Display for TypeDefintion { fn fmt(&self, f: &mut Formatter) -> fmt::Result { - write!(f, "{self}") + match self { + TypeDefintion::Structure(structure) => write!(f, "{structure}"), + } } }