1
0

Add analysis step to run function

This commit is contained in:
Jeff 2024-08-07 11:38:08 -04:00
parent 5d01f1caf9
commit 7328467e64
6 changed files with 224 additions and 176 deletions

View File

@ -1,5 +1,5 @@
use std::{ use std::{
collections::HashMap, collections::{HashMap, VecDeque},
fmt::{self, Display, Formatter}, fmt::{self, Display, Formatter},
}; };
@ -7,6 +7,11 @@ use serde::{Deserialize, Serialize};
use crate::{Identifier, ReservedIdentifier, Span, Type, Value}; use crate::{Identifier, ReservedIdentifier, Span, Type, Value};
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct AbstractSyntaxTree {
pub nodes: VecDeque<Node>,
}
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)] #[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct Node { pub struct Node {
pub statement: Statement, pub statement: Statement,

View File

@ -1,22 +1,22 @@
use crate::{Node, Statement}; use crate::{AbstractSyntaxTree, Node, Statement};
pub fn analyze(abstract_tree: Vec<Node>) -> Result<(), AnalyzerError> { pub fn analyze(abstract_tree: &AbstractSyntaxTree) -> Result<(), AnalyzerError> {
let analyzer = Analyzer::new(abstract_tree); let analyzer = Analyzer::new(abstract_tree);
analyzer.analyze() analyzer.analyze()
} }
pub struct Analyzer { pub struct Analyzer<'a> {
abstract_tree: Vec<Node>, abstract_tree: &'a AbstractSyntaxTree,
} }
impl Analyzer { impl<'a> Analyzer<'a> {
pub fn new(abstract_tree: Vec<Node>) -> Self { pub fn new(abstract_tree: &'a AbstractSyntaxTree) -> Self {
Analyzer { abstract_tree } Analyzer { abstract_tree }
} }
pub fn analyze(&self) -> Result<(), AnalyzerError> { pub fn analyze(&self) -> Result<(), AnalyzerError> {
for node in &self.abstract_tree { for node in &self.abstract_tree.nodes {
self.analyze_node(node)?; self.analyze_node(node)?;
} }
@ -90,15 +90,18 @@ mod tests {
#[test] #[test]
fn assignment_expect_identifier() { fn assignment_expect_identifier() {
let abstract_tree = vec![Node::new( let abstract_tree = AbstractSyntaxTree {
nodes: [Node::new(
Statement::Assign( Statement::Assign(
Box::new(Node::new(Statement::Constant(Value::integer(1)), (0, 1))), Box::new(Node::new(Statement::Constant(Value::integer(1)), (0, 1))),
Box::new(Node::new(Statement::Constant(Value::integer(2)), (1, 2))), Box::new(Node::new(Statement::Constant(Value::integer(2)), (1, 2))),
), ),
(0, 2), (0, 2),
)]; )]
.into(),
};
let analyzer = Analyzer::new(abstract_tree); let analyzer = Analyzer::new(&abstract_tree);
assert_eq!( assert_eq!(
analyzer.analyze(), analyzer.analyze(),
@ -110,12 +113,15 @@ mod tests {
#[test] #[test]
fn unexpected_identifier_simple() { fn unexpected_identifier_simple() {
let abstract_tree = vec![Node::new( let abstract_tree = AbstractSyntaxTree {
nodes: [Node::new(
Statement::Identifier(Identifier::new("x")), Statement::Identifier(Identifier::new("x")),
(0, 1), (0, 1),
)]; )]
.into(),
};
let analyzer = Analyzer::new(abstract_tree); let analyzer = Analyzer::new(&abstract_tree);
assert_eq!( assert_eq!(
analyzer.analyze(), analyzer.analyze(),
@ -127,7 +133,8 @@ mod tests {
#[test] #[test]
fn unexpected_identifier_nested() { fn unexpected_identifier_nested() {
let abstract_tree = vec![Node::new( let abstract_tree = AbstractSyntaxTree {
nodes: [Node::new(
Statement::Add( Statement::Add(
Box::new(Node::new(Statement::Constant(Value::integer(1)), (0, 1))), Box::new(Node::new(Statement::Constant(Value::integer(1)), (0, 1))),
Box::new(Node::new( Box::new(Node::new(
@ -136,9 +143,11 @@ mod tests {
)), )),
), ),
(0, 1), (0, 1),
)]; )]
.into(),
};
let analyzer = Analyzer::new(abstract_tree); let analyzer = Analyzer::new(&abstract_tree);
assert_eq!( assert_eq!(
analyzer.analyze(), analyzer.analyze(),

View File

@ -15,7 +15,7 @@ pub mod r#type;
pub mod value; pub mod value;
pub mod vm; pub mod vm;
pub use abstract_tree::{Node, Statement}; pub use abstract_tree::{AbstractSyntaxTree, Node, Statement};
pub use analyzer::{analyze, Analyzer, AnalyzerError}; pub use analyzer::{analyze, Analyzer, AnalyzerError};
pub use identifier::Identifier; pub use identifier::Identifier;
pub use lex::{lex, LexError, Lexer}; pub use lex::{lex, LexError, Lexer};

View File

@ -1,11 +1,8 @@
use std::collections::VecDeque; use std::collections::VecDeque;
use crate::{ use crate::{AbstractSyntaxTree, LexError, Lexer, Node, Span, Statement, Token, Value};
lex::{LexError, Lexer},
Node, Span, Statement, Token, Value,
};
pub fn parse(input: &str) -> Result<VecDeque<Node>, ParseError> { pub fn parse(input: &str) -> Result<AbstractSyntaxTree, ParseError> {
let lexer = Lexer::new(input); let lexer = Lexer::new(input);
let mut parser = Parser::new(lexer); let mut parser = Parser::new(lexer);
let mut nodes = VecDeque::new(); let mut nodes = VecDeque::new();
@ -20,7 +17,7 @@ pub fn parse(input: &str) -> Result<VecDeque<Node>, ParseError> {
} }
} }
Ok(nodes) Ok(AbstractSyntaxTree { nodes })
} }
pub struct Parser<'src> { pub struct Parser<'src> {
@ -227,7 +224,9 @@ mod tests {
assert_eq!( assert_eq!(
parse(input), parse(input),
Ok([Node::new(Statement::Constant(Value::boolean(true)), (0, 4))].into()) Ok(AbstractSyntaxTree {
nodes: [Node::new(Statement::Constant(Value::boolean(true)), (0, 4))].into()
})
); );
} }
@ -237,7 +236,8 @@ mod tests {
assert_eq!( assert_eq!(
parse(input), parse(input),
Ok([Node::new( Ok(AbstractSyntaxTree {
nodes: [Node::new(
Statement::PropertyAccess( Statement::PropertyAccess(
Box::new(Node::new( Box::new(Node::new(
Statement::List(vec![ Statement::List(vec![
@ -251,7 +251,8 @@ mod tests {
), ),
(0, 11), (0, 11),
)] )]
.into()) .into()
})
); );
} }
@ -261,7 +262,8 @@ mod tests {
assert_eq!( assert_eq!(
parse(input), parse(input),
Ok([Node::new( Ok(AbstractSyntaxTree {
nodes: [Node::new(
Statement::PropertyAccess( Statement::PropertyAccess(
Box::new(Node::new( Box::new(Node::new(
Statement::Identifier(Identifier::new("a")), Statement::Identifier(Identifier::new("a")),
@ -274,7 +276,8 @@ mod tests {
), ),
(0, 3), (0, 3),
)] )]
.into()) .into()
})
); );
} }
@ -284,7 +287,8 @@ mod tests {
assert_eq!( assert_eq!(
parse(input), parse(input),
Ok([Node::new( Ok(AbstractSyntaxTree {
nodes: [Node::new(
Statement::List(vec![ Statement::List(vec![
Node::new(Statement::Constant(Value::integer(1)), (1, 2)), Node::new(Statement::Constant(Value::integer(1)), (1, 2)),
Node::new( Node::new(
@ -296,7 +300,10 @@ mod tests {
), ),
Node::new( Node::new(
Statement::Add( Statement::Add(
Box::new(Node::new(Statement::Constant(Value::integer(2)), (11, 12))), Box::new(Node::new(
Statement::Constant(Value::integer(2)),
(11, 12)
)),
Box::new(Node::new( Box::new(Node::new(
Statement::Multiply( Statement::Multiply(
Box::new(Node::new( Box::new(Node::new(
@ -316,7 +323,8 @@ mod tests {
]), ]),
(0, 24), (0, 24),
)] )]
.into()) .into()
})
); );
} }
@ -326,14 +334,16 @@ mod tests {
assert_eq!( assert_eq!(
parse(input), parse(input),
Ok([Node::new( Ok(AbstractSyntaxTree {
nodes: [Node::new(
Statement::List(vec![ Statement::List(vec![
Node::new(Statement::Constant(Value::integer(1)), (1, 2)), Node::new(Statement::Constant(Value::integer(1)), (1, 2)),
Node::new(Statement::Constant(Value::integer(2)), (4, 5)), Node::new(Statement::Constant(Value::integer(2)), (4, 5)),
]), ]),
(0, 6), (0, 6),
)] )]
.into()) .into()
})
); );
} }
@ -343,7 +353,9 @@ mod tests {
assert_eq!( assert_eq!(
parse(input), parse(input),
Ok([Node::new(Statement::List(vec![]), (0, 2))].into()) Ok(AbstractSyntaxTree {
nodes: [Node::new(Statement::List(vec![]), (0, 2))].into()
})
); );
} }
@ -353,7 +365,9 @@ mod tests {
assert_eq!( assert_eq!(
parse(input), parse(input),
Ok([Node::new(Statement::Constant(Value::float(42.0)), (0, 4))].into()) Ok(AbstractSyntaxTree {
nodes: [Node::new(Statement::Constant(Value::float(42.0)), (0, 4))].into()
})
); );
} }
@ -363,14 +377,16 @@ mod tests {
assert_eq!( assert_eq!(
parse(input), parse(input),
Ok([Node::new( Ok(AbstractSyntaxTree {
nodes: [Node::new(
Statement::Add( Statement::Add(
Box::new(Node::new(Statement::Constant(Value::integer(1)), (0, 1))), Box::new(Node::new(Statement::Constant(Value::integer(1)), (0, 1))),
Box::new(Node::new(Statement::Constant(Value::integer(2)), (4, 5))), Box::new(Node::new(Statement::Constant(Value::integer(2)), (4, 5))),
), ),
(0, 5), (0, 5),
)] )]
.into()) .into()
})
); );
} }
@ -380,14 +396,16 @@ mod tests {
assert_eq!( assert_eq!(
parse(input), parse(input),
Ok([Node::new( Ok(AbstractSyntaxTree {
nodes: [Node::new(
Statement::Multiply( Statement::Multiply(
Box::new(Node::new(Statement::Constant(Value::integer(1)), (0, 1))), Box::new(Node::new(Statement::Constant(Value::integer(1)), (0, 1))),
Box::new(Node::new(Statement::Constant(Value::integer(2)), (4, 5))), Box::new(Node::new(Statement::Constant(Value::integer(2)), (4, 5))),
), ),
(0, 5), (0, 5),
)] )]
.into()) .into()
})
); );
} }
@ -397,7 +415,8 @@ mod tests {
assert_eq!( assert_eq!(
parse(input), parse(input),
Ok([Node::new( Ok(AbstractSyntaxTree {
nodes: [Node::new(
Statement::Add( Statement::Add(
Box::new(Node::new(Statement::Constant(Value::integer(1)), (0, 1))), Box::new(Node::new(Statement::Constant(Value::integer(1)), (0, 1))),
Box::new(Node::new( Box::new(Node::new(
@ -410,7 +429,8 @@ mod tests {
), ),
(0, 9), (0, 9),
)] )]
.into()) .into()
})
); );
} }
@ -420,7 +440,8 @@ mod tests {
assert_eq!( assert_eq!(
parse(input), parse(input),
Ok([Node::new( Ok(AbstractSyntaxTree {
nodes: [Node::new(
Statement::Assign( Statement::Assign(
Box::new(Node::new( Box::new(Node::new(
Statement::Identifier(Identifier::new("a")), Statement::Identifier(Identifier::new("a")),
@ -448,7 +469,8 @@ mod tests {
), ),
(0, 13), (0, 13),
)] )]
.into()) .into()
})
); );
} }
} }

View File

@ -18,7 +18,7 @@ use std::{
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::identifier::Identifier; use crate::Identifier;
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct TypeConflict { pub struct TypeConflict {

View File

@ -1,7 +1,8 @@
use std::collections::{HashMap, VecDeque}; use std::collections::HashMap;
use crate::{ use crate::{
parse, Identifier, Node, ParseError, ReservedIdentifier, Span, Statement, Value, ValueError, parse, AbstractSyntaxTree, Analyzer, AnalyzerError, Identifier, Node, ParseError,
ReservedIdentifier, Span, Statement, Value, ValueError,
}; };
pub fn run( pub fn run(
@ -9,18 +10,22 @@ pub fn run(
variables: &mut HashMap<Identifier, Value>, variables: &mut HashMap<Identifier, Value>,
) -> Result<Option<Value>, VmError> { ) -> Result<Option<Value>, VmError> {
let abstract_syntax_tree = parse(input)?; let abstract_syntax_tree = parse(input)?;
let analyzer = Analyzer::new(&abstract_syntax_tree);
analyzer.analyze()?;
let mut vm = Vm::new(abstract_syntax_tree); let mut vm = Vm::new(abstract_syntax_tree);
vm.run(variables) vm.run(variables)
} }
pub struct Vm { pub struct Vm {
statement_nodes: VecDeque<Node>, abstract_tree: AbstractSyntaxTree,
} }
impl Vm { impl Vm {
pub fn new(statement_nodes: VecDeque<Node>) -> Self { pub fn new(abstract_tree: AbstractSyntaxTree) -> Self {
Vm { statement_nodes } Self { abstract_tree }
} }
pub fn run( pub fn run(
@ -29,7 +34,7 @@ impl Vm {
) -> Result<Option<Value>, VmError> { ) -> Result<Option<Value>, VmError> {
let mut previous_value = None; let mut previous_value = None;
while let Some(node) = self.statement_nodes.pop_front() { while let Some(node) = self.abstract_tree.nodes.pop_front() {
previous_value = self.run_node(node, variables)?; previous_value = self.run_node(node, variables)?;
} }
@ -166,6 +171,7 @@ impl Vm {
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub enum VmError { pub enum VmError {
AnaylyzerError(AnalyzerError),
ParseError(ParseError), ParseError(ParseError),
ValueError(ValueError), ValueError(ValueError),
@ -178,6 +184,12 @@ pub enum VmError {
ExpectedValue { position: Span }, ExpectedValue { position: Span },
} }
impl From<AnalyzerError> for VmError {
fn from(error: AnalyzerError) -> Self {
Self::AnaylyzerError(error)
}
}
impl From<ParseError> for VmError { impl From<ParseError> for VmError {
fn from(error: ParseError) -> Self { fn from(error: ParseError) -> Self {
Self::ParseError(error) Self::ParseError(error)