From cda0203242b82189a1b5a6b6c7d663518605850c Mon Sep 17 00:00:00 2001 From: Jeff Date: Wed, 7 Aug 2024 15:47:37 -0400 Subject: [PATCH] Replace spans with a generic type --- dust-lang/src/abstract_tree.rs | 38 ++++++++++++------------ dust-lang/src/analyzer.rs | 26 ++++++++-------- dust-lang/src/parse.rs | 32 ++++++++++---------- dust-lang/src/value.rs | 2 +- dust-lang/src/vm.rs | 54 +++++++++++++++++----------------- 5 files changed, 76 insertions(+), 76 deletions(-) diff --git a/dust-lang/src/abstract_tree.rs b/dust-lang/src/abstract_tree.rs index f27709f..87f8307 100644 --- a/dust-lang/src/abstract_tree.rs +++ b/dust-lang/src/abstract_tree.rs @@ -5,45 +5,45 @@ use std::{ use serde::{Deserialize, Serialize}; -use crate::{Identifier, ReservedIdentifier, Span, Type, Value}; +use crate::{Identifier, ReservedIdentifier, Type, Value}; #[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)] -pub struct AbstractSyntaxTree { - pub nodes: VecDeque, +pub struct AbstractSyntaxTree

{ + pub nodes: VecDeque>, } #[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)] -pub struct Node { - pub statement: Statement, - pub span: Span, +pub struct Node

{ + pub statement: Statement

, + pub position: P, } -impl Node { - pub fn new(operation: Statement, span: Span) -> Self { +impl

Node

{ + pub fn new(operation: Statement

, position: P) -> Self { Self { statement: operation, - span, + position, } } } -impl Display for Node { +impl

Display for Node

{ fn fmt(&self, f: &mut Formatter) -> fmt::Result { write!(f, "{}", self.statement) } } #[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)] -pub enum Statement { +pub enum Statement

{ // Top-level statements - Assign(Box, Box), + Assign(Box>, Box>), // Expressions - Add(Box, Box), - BuiltInValue(Box), - PropertyAccess(Box, Box), - List(Vec), - Multiply(Box, Box), + Add(Box>, Box>), + BuiltInValue(Box>), + PropertyAccess(Box>, Box>), + List(Vec>), + Multiply(Box>, Box>), // Hard-coded values Constant(Value), @@ -51,7 +51,7 @@ pub enum Statement { ReservedIdentifier(ReservedIdentifier), } -impl Statement { +impl

Statement

{ pub fn expected_type(&self, variables: &HashMap) -> Option { match self { Statement::Add(left, _) => left.statement.expected_type(variables), @@ -72,7 +72,7 @@ impl Statement { } } -impl Display for Statement { +impl

Display for Statement

{ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Statement::Assign(left, right) => write!(f, "{left} = {right}"), diff --git a/dust-lang/src/analyzer.rs b/dust-lang/src/analyzer.rs index a8acf07..73eccc4 100644 --- a/dust-lang/src/analyzer.rs +++ b/dust-lang/src/analyzer.rs @@ -22,10 +22,10 @@ use crate::{AbstractSyntaxTree, Identifier, Node, Statement, Type, Value}; /// /// assert!(result.is_err()); /// ``` -pub fn analyze( - abstract_tree: &AbstractSyntaxTree, +pub fn analyze( + abstract_tree: &AbstractSyntaxTree

, variables: &HashMap, -) -> Result<(), AnalyzerError> { +) -> Result<(), AnalyzerError

> { let analyzer = Analyzer::new(abstract_tree, variables); analyzer.analyze() @@ -44,14 +44,14 @@ pub fn analyze( /// let result = analyzer.analyze(); /// /// assert!(result.is_err()); -pub struct Analyzer<'a> { - abstract_tree: &'a AbstractSyntaxTree, +pub struct Analyzer<'a, P> { + abstract_tree: &'a AbstractSyntaxTree

, variables: &'a HashMap, } -impl<'a> Analyzer<'a> { +impl<'a, P: Clone> Analyzer<'a, P> { pub fn new( - abstract_tree: &'a AbstractSyntaxTree, + abstract_tree: &'a AbstractSyntaxTree

, variables: &'a HashMap, ) -> Self { Self { @@ -60,7 +60,7 @@ impl<'a> Analyzer<'a> { } } - pub fn analyze(&self) -> Result<(), AnalyzerError> { + pub fn analyze(&self) -> Result<(), AnalyzerError

> { for node in &self.abstract_tree.nodes { self.analyze_node(node)?; } @@ -68,7 +68,7 @@ impl<'a> Analyzer<'a> { Ok(()) } - fn analyze_node(&self, node: &Node) -> Result<(), AnalyzerError> { + fn analyze_node(&self, node: &Node

) -> Result<(), AnalyzerError

> { match &node.statement { Statement::Add(left, right) => { if let Some(Type::Integer) | Some(Type::Float) = @@ -160,10 +160,10 @@ impl<'a> Analyzer<'a> { } #[derive(Clone, Debug, PartialEq)] -pub enum AnalyzerError { - ExpectedIdentifier { actual: Node }, - ExpectedIntegerOrFloat { actual: Node }, - UnexpectedIdentifier { identifier: Node }, +pub enum AnalyzerError

{ + ExpectedIdentifier { actual: Node

}, + ExpectedIntegerOrFloat { actual: Node

}, + UnexpectedIdentifier { identifier: Node

}, } #[cfg(test)] diff --git a/dust-lang/src/parse.rs b/dust-lang/src/parse.rs index 17c6ed5..89a4823 100644 --- a/dust-lang/src/parse.rs +++ b/dust-lang/src/parse.rs @@ -23,20 +23,20 @@ use crate::{AbstractSyntaxTree, LexError, Lexer, Node, Span, Statement, Token, V /// statement: Statement::Assign( /// Box::new(Node { /// statement: Statement::Identifier("x".into()), -/// span: (0, 1), +/// position: (0, 1), /// }), /// Box::new(Node { /// statement: Statement::Constant(Value::integer(42)), -/// span: (4, 6), +/// position: (4, 6), /// }) /// ), -/// span: (0, 6), +/// position: (0, 6), /// } /// ].into(), /// }), /// ); /// ``` -pub fn parse(input: &str) -> Result { +pub fn parse(input: &str) -> Result, ParseError> { let lexer = Lexer::new(input); let mut parser = Parser::new(lexer); let mut nodes = VecDeque::new(); @@ -77,19 +77,19 @@ pub fn parse(input: &str) -> Result { /// /// assert_eq!( /// nodes, -/// Into::>::into([ +/// Into::>>::into([ /// Node { /// statement: Statement::Assign( /// Box::new(Node { /// statement: Statement::Identifier("x".into()), -/// span: (0, 1), +/// position: (0, 1), /// }), /// Box::new(Node { /// statement: Statement::Constant(Value::integer(42)), -/// span: (4, 6), +/// position: (4, 6), /// }) /// ), -/// span: (0, 6), +/// position: (0, 6), /// } /// ]), /// ); @@ -107,7 +107,7 @@ impl<'src> Parser<'src> { Parser { lexer, current } } - pub fn parse(&mut self) -> Result { + pub fn parse(&mut self) -> Result, ParseError> { self.parse_node(0) } @@ -121,9 +121,9 @@ impl<'src> Parser<'src> { Ok(()) } - fn parse_node(&mut self, precedence: u8) -> Result { + fn parse_node(&mut self, precedence: u8) -> Result, ParseError> { let left_node = self.parse_primary()?; - let left_start = left_node.span.0; + let left_start = left_node.position.0; if precedence < self.current_precedence() { match &self.current { @@ -131,7 +131,7 @@ impl<'src> Parser<'src> { self.next_token()?; let right_node = self.parse_node(self.current_precedence())?; - let right_end = right_node.span.1; + let right_end = right_node.position.1; return Ok(Node::new( Statement::Add(Box::new(left_node), Box::new(right_node)), @@ -142,7 +142,7 @@ impl<'src> Parser<'src> { self.next_token()?; let right_node = self.parse_node(self.current_precedence())?; - let right_end = right_node.span.1; + let right_end = right_node.position.1; return Ok(Node::new( Statement::Multiply(Box::new(left_node), Box::new(right_node)), @@ -153,7 +153,7 @@ impl<'src> Parser<'src> { self.next_token()?; let right_node = self.parse_node(self.current_precedence())?; - let right_end = right_node.span.1; + let right_end = right_node.position.1; return Ok(Node::new( Statement::Assign(Box::new(left_node), Box::new(right_node)), @@ -164,7 +164,7 @@ impl<'src> Parser<'src> { self.next_token()?; let right_node = self.parse_node(self.current_precedence())?; - let right_end = right_node.span.1; + let right_end = right_node.position.1; return Ok(Node::new( Statement::PropertyAccess(Box::new(left_node), Box::new(right_node)), @@ -178,7 +178,7 @@ impl<'src> Parser<'src> { Ok(left_node) } - fn parse_primary(&mut self) -> Result { + fn parse_primary(&mut self) -> Result, ParseError> { match self.current.clone() { (Token::Boolean(boolean), span) => { self.next_token()?; diff --git a/dust-lang/src/value.rs b/dust-lang/src/value.rs index 3588f74..21083bd 100644 --- a/dust-lang/src/value.rs +++ b/dust-lang/src/value.rs @@ -539,7 +539,7 @@ pub struct Function { pub name: Identifier, pub type_parameters: Option>, pub value_parameters: Option>, - pub body: Vec, + pub body: Vec>, } impl Function { diff --git a/dust-lang/src/vm.rs b/dust-lang/src/vm.rs index fea2c8d..dafbc96 100644 --- a/dust-lang/src/vm.rs +++ b/dust-lang/src/vm.rs @@ -8,7 +8,7 @@ use crate::{ pub fn run( input: &str, variables: &mut HashMap, -) -> Result, VmError> { +) -> Result, VmError> { let abstract_syntax_tree = parse(input)?; let analyzer = Analyzer::new(&abstract_syntax_tree, variables); @@ -19,19 +19,19 @@ pub fn run( vm.run(variables) } -pub struct Vm { - abstract_tree: AbstractSyntaxTree, +pub struct Vm

{ + abstract_tree: AbstractSyntaxTree

, } -impl Vm { - pub fn new(abstract_tree: AbstractSyntaxTree) -> Self { +impl Vm

{ + pub fn new(abstract_tree: AbstractSyntaxTree

) -> Self { Self { abstract_tree } } pub fn run( &mut self, variables: &mut HashMap, - ) -> Result, VmError> { + ) -> Result, VmError

> { let mut previous_value = None; while let Some(node) = self.abstract_tree.nodes.pop_front() { @@ -43,16 +43,16 @@ impl Vm { fn run_node( &self, - node: Node, + node: Node

, variables: &mut HashMap, - ) -> Result, VmError> { + ) -> Result, VmError

> { match node.statement { Statement::BuiltInValue(node) => self.run_node(*node, variables), Statement::Constant(value) => Ok(Some(value.clone())), Statement::Identifier(_) => Ok(None), Statement::ReservedIdentifier(_) => Ok(None), Statement::Add(left, right) => { - let left_span = left.span; + let left_span = left.position; let left = if let Some(value) = self.run_node(*left, variables)? { value } else { @@ -60,7 +60,7 @@ impl Vm { position: left_span, }); }; - let right_span = right.span; + let right_span = right.position; let right = if let Some(value) = self.run_node(*right, variables)? { value } else { @@ -77,10 +77,10 @@ impl Vm { identifier } else { return Err(VmError::ExpectedIdentifier { - position: left.span, + position: left.position, }); }; - let right_span = right.span; + let right_span = right.position; let value = if let Some(value) = self.run_node(*right, variables)? { value } else { @@ -97,20 +97,20 @@ impl Vm { let values = nodes .into_iter() .map(|node| { - let span = node.span; + let span = node.position; if let Some(value) = self.run_node(node, variables)? { Ok(value) } else { Err(VmError::ExpectedValue { position: span }) } }) - .collect::, VmError>>()?; + .collect::, VmError

>>()?; Ok(Some(Value::list(values))) } Statement::Multiply(_, _) => todo!(), Statement::PropertyAccess(left, right) => { - let left_span = left.span; + let left_span = left.position; let left = if let Some(value) = self.run_node(*left, variables)? { value } else { @@ -118,7 +118,7 @@ impl Vm { position: left_span, }); }; - let right_span = right.span; + let right_span = right.position; if let Statement::ReservedIdentifier(reserved) = &right.statement { match reserved { @@ -170,33 +170,33 @@ impl Vm { } #[derive(Clone, Debug, PartialEq)] -pub enum VmError { - AnaylyzerError(AnalyzerError), +pub enum VmError

{ + AnaylyzerError(AnalyzerError

), ParseError(ParseError), ValueError(ValueError), // Anaylsis Failures // These should be prevented by running the analyzer before the VM - ExpectedIdentifier { position: Span }, - ExpectedIdentifierOrInteger { position: Span }, - ExpectedInteger { position: Span }, - ExpectedList { position: Span }, - ExpectedValue { position: Span }, + ExpectedIdentifier { position: P }, + ExpectedIdentifierOrInteger { position: P }, + ExpectedInteger { position: P }, + ExpectedList { position: P }, + ExpectedValue { position: P }, } -impl From for VmError { - fn from(error: AnalyzerError) -> Self { +impl

From> for VmError

{ + fn from(error: AnalyzerError

) -> Self { Self::AnaylyzerError(error) } } -impl From for VmError { +impl

From for VmError

{ fn from(error: ParseError) -> Self { Self::ParseError(error) } } -impl From for VmError { +impl

From for VmError

{ fn from(error: ValueError) -> Self { Self::ValueError(error) }