Compare commits
No commits in common. "e295aebf5677b5fe5febeb8486bf55a88b97120f" and "5d01f1caf900e9666efda25fbe35911154596f0c" have entirely different histories.
e295aebf56
...
5d01f1caf9
@ -1,5 +1,5 @@
|
|||||||
use std::{
|
use std::{
|
||||||
collections::{HashMap, VecDeque},
|
collections::HashMap,
|
||||||
fmt::{self, Display, Formatter},
|
fmt::{self, Display, Formatter},
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -7,11 +7,6 @@ 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,
|
||||||
|
@ -1,67 +1,22 @@
|
|||||||
/// Tools for analyzing an abstract syntax tree and catch errors before running the virtual
|
use crate::{Node, Statement};
|
||||||
/// machine.
|
|
||||||
///
|
|
||||||
/// This module provides to anlysis options, both of which borrow an abstract syntax tree and a
|
|
||||||
/// hash map of variables:
|
|
||||||
/// - `analyze` convenience function
|
|
||||||
/// - `Analyzer` struct
|
|
||||||
use std::collections::HashMap;
|
|
||||||
|
|
||||||
use crate::{AbstractSyntaxTree, Identifier, Node, Statement, Type, Value};
|
pub fn analyze(abstract_tree: Vec<Node>) -> Result<(), AnalyzerError> {
|
||||||
|
let analyzer = Analyzer::new(abstract_tree);
|
||||||
/// Analyzes the abstract syntax tree for errors.
|
|
||||||
///
|
|
||||||
/// # Examples
|
|
||||||
/// ```
|
|
||||||
/// # use std::collections::HashMap;
|
|
||||||
/// # use dust_lang::*;
|
|
||||||
/// let input = "x = 1 + false";
|
|
||||||
/// let abstract_tree = parse(input).unwrap();
|
|
||||||
/// let variables = HashMap::new();
|
|
||||||
/// let result = analyze(&abstract_tree, &variables);
|
|
||||||
///
|
|
||||||
/// assert!(result.is_err());
|
|
||||||
/// ```
|
|
||||||
pub fn analyze(
|
|
||||||
abstract_tree: &AbstractSyntaxTree,
|
|
||||||
variables: &HashMap<Identifier, Value>,
|
|
||||||
) -> Result<(), AnalyzerError> {
|
|
||||||
let analyzer = Analyzer::new(abstract_tree, variables);
|
|
||||||
|
|
||||||
analyzer.analyze()
|
analyzer.analyze()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Static analyzer that checks for potential runtime errors.
|
pub struct Analyzer {
|
||||||
///
|
abstract_tree: Vec<Node>,
|
||||||
/// # Examples
|
|
||||||
/// ```
|
|
||||||
/// # use std::collections::HashMap;
|
|
||||||
/// # use dust_lang::*;
|
|
||||||
/// let input = "x = 1 + false";
|
|
||||||
/// let abstract_tree = parse(input).unwrap();
|
|
||||||
/// let variables = HashMap::new();
|
|
||||||
/// let analyzer = Analyzer::new(&abstract_tree, &variables);
|
|
||||||
/// let result = analyzer.analyze();
|
|
||||||
///
|
|
||||||
/// assert!(result.is_err());
|
|
||||||
pub struct Analyzer<'a> {
|
|
||||||
abstract_tree: &'a AbstractSyntaxTree,
|
|
||||||
variables: &'a HashMap<Identifier, Value>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Analyzer<'a> {
|
impl Analyzer {
|
||||||
pub fn new(
|
pub fn new(abstract_tree: Vec<Node>) -> Self {
|
||||||
abstract_tree: &'a AbstractSyntaxTree,
|
Analyzer { abstract_tree }
|
||||||
variables: &'a HashMap<Identifier, Value>,
|
|
||||||
) -> Self {
|
|
||||||
Self {
|
|
||||||
abstract_tree,
|
|
||||||
variables,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn analyze(&self) -> Result<(), AnalyzerError> {
|
pub fn analyze(&self) -> Result<(), AnalyzerError> {
|
||||||
for node in &self.abstract_tree.nodes {
|
for node in &self.abstract_tree {
|
||||||
self.analyze_node(node)?;
|
self.analyze_node(node)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,24 +26,6 @@ impl<'a> Analyzer<'a> {
|
|||||||
fn analyze_node(&self, node: &Node) -> Result<(), AnalyzerError> {
|
fn analyze_node(&self, node: &Node) -> Result<(), AnalyzerError> {
|
||||||
match &node.statement {
|
match &node.statement {
|
||||||
Statement::Add(left, right) => {
|
Statement::Add(left, right) => {
|
||||||
if let Some(Type::Integer) | Some(Type::Float) =
|
|
||||||
left.statement.expected_type(self.variables)
|
|
||||||
{
|
|
||||||
} else {
|
|
||||||
return Err(AnalyzerError::ExpectedIntegerOrFloat {
|
|
||||||
actual: left.as_ref().clone(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(Type::Integer) | Some(Type::Float) =
|
|
||||||
right.statement.expected_type(self.variables)
|
|
||||||
{
|
|
||||||
} else {
|
|
||||||
return Err(AnalyzerError::ExpectedIntegerOrFloat {
|
|
||||||
actual: right.as_ref().clone(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
self.analyze_node(left)?;
|
self.analyze_node(left)?;
|
||||||
self.analyze_node(right)?;
|
self.analyze_node(right)?;
|
||||||
}
|
}
|
||||||
@ -118,32 +55,12 @@ impl<'a> Analyzer<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Statement::Multiply(left, right) => {
|
Statement::Multiply(left, right) => {
|
||||||
if let Some(Type::Integer) | Some(Type::Float) =
|
|
||||||
left.statement.expected_type(self.variables)
|
|
||||||
{
|
|
||||||
} else {
|
|
||||||
return Err(AnalyzerError::ExpectedIntegerOrFloat {
|
|
||||||
actual: left.as_ref().clone(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(Type::Integer) | Some(Type::Float) =
|
|
||||||
right.statement.expected_type(self.variables)
|
|
||||||
{
|
|
||||||
} else {
|
|
||||||
return Err(AnalyzerError::ExpectedIntegerOrFloat {
|
|
||||||
actual: right.as_ref().clone(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
self.analyze_node(left)?;
|
self.analyze_node(left)?;
|
||||||
self.analyze_node(right)?;
|
self.analyze_node(right)?;
|
||||||
}
|
}
|
||||||
Statement::PropertyAccess(left, right) => {
|
Statement::PropertyAccess(left, right) => {
|
||||||
if let Statement::Identifier(_) | Statement::Constant(_) | Statement::List(_) =
|
if let Statement::Identifier(_) = &left.statement {
|
||||||
&left.statement
|
// Identifier is in the correct position
|
||||||
{
|
|
||||||
// Left side is valid
|
|
||||||
} else {
|
} else {
|
||||||
return Err(AnalyzerError::ExpectedIdentifier {
|
return Err(AnalyzerError::ExpectedIdentifier {
|
||||||
actual: left.as_ref().clone(),
|
actual: left.as_ref().clone(),
|
||||||
@ -162,7 +79,6 @@ impl<'a> Analyzer<'a> {
|
|||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub enum AnalyzerError {
|
pub enum AnalyzerError {
|
||||||
ExpectedIdentifier { actual: Node },
|
ExpectedIdentifier { actual: Node },
|
||||||
ExpectedIntegerOrFloat { actual: Node },
|
|
||||||
UnexpectedIdentifier { identifier: Node },
|
UnexpectedIdentifier { identifier: Node },
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -172,72 +88,17 @@ mod tests {
|
|||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn multiply_expect_integer_or_float() {
|
|
||||||
let abstract_tree = AbstractSyntaxTree {
|
|
||||||
nodes: [Node::new(
|
|
||||||
Statement::Multiply(
|
|
||||||
Box::new(Node::new(Statement::Constant(Value::integer(1)), (0, 1))),
|
|
||||||
Box::new(Node::new(
|
|
||||||
Statement::Constant(Value::boolean(false)),
|
|
||||||
(1, 2),
|
|
||||||
)),
|
|
||||||
),
|
|
||||||
(0, 2),
|
|
||||||
)]
|
|
||||||
.into(),
|
|
||||||
};
|
|
||||||
let variables = HashMap::new();
|
|
||||||
let analyzer = Analyzer::new(&abstract_tree, &variables);
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
analyzer.analyze(),
|
|
||||||
Err(AnalyzerError::ExpectedIntegerOrFloat {
|
|
||||||
actual: Node::new(Statement::Constant(Value::boolean(false)), (1, 2))
|
|
||||||
})
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn add_expect_integer_or_float() {
|
|
||||||
let abstract_tree = AbstractSyntaxTree {
|
|
||||||
nodes: [Node::new(
|
|
||||||
Statement::Add(
|
|
||||||
Box::new(Node::new(Statement::Constant(Value::integer(1)), (0, 1))),
|
|
||||||
Box::new(Node::new(
|
|
||||||
Statement::Constant(Value::boolean(false)),
|
|
||||||
(1, 2),
|
|
||||||
)),
|
|
||||||
),
|
|
||||||
(0, 2),
|
|
||||||
)]
|
|
||||||
.into(),
|
|
||||||
};
|
|
||||||
let variables = HashMap::new();
|
|
||||||
let analyzer = Analyzer::new(&abstract_tree, &variables);
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
analyzer.analyze(),
|
|
||||||
Err(AnalyzerError::ExpectedIntegerOrFloat {
|
|
||||||
actual: Node::new(Statement::Constant(Value::boolean(false)), (1, 2))
|
|
||||||
})
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn assignment_expect_identifier() {
|
fn assignment_expect_identifier() {
|
||||||
let abstract_tree = AbstractSyntaxTree {
|
let abstract_tree = vec![Node::new(
|
||||||
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 variables = HashMap::new();
|
|
||||||
let analyzer = Analyzer::new(&abstract_tree, &variables);
|
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
analyzer.analyze(),
|
analyzer.analyze(),
|
||||||
@ -248,16 +109,13 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn unexpected_identifier() {
|
fn unexpected_identifier_simple() {
|
||||||
let abstract_tree = AbstractSyntaxTree {
|
let abstract_tree = vec![Node::new(
|
||||||
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 variables = HashMap::new();
|
|
||||||
let analyzer = Analyzer::new(&abstract_tree, &variables);
|
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
analyzer.analyze(),
|
analyzer.analyze(),
|
||||||
@ -266,4 +124,27 @@ mod tests {
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn unexpected_identifier_nested() {
|
||||||
|
let abstract_tree = vec![Node::new(
|
||||||
|
Statement::Add(
|
||||||
|
Box::new(Node::new(Statement::Constant(Value::integer(1)), (0, 1))),
|
||||||
|
Box::new(Node::new(
|
||||||
|
Statement::Identifier(Identifier::new("x")),
|
||||||
|
(1, 2),
|
||||||
|
)),
|
||||||
|
),
|
||||||
|
(0, 1),
|
||||||
|
)];
|
||||||
|
|
||||||
|
let analyzer = Analyzer::new(abstract_tree);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
analyzer.analyze(),
|
||||||
|
Err(AnalyzerError::UnexpectedIdentifier {
|
||||||
|
identifier: Node::new(Statement::Identifier(Identifier::new("x")), (1, 2))
|
||||||
|
})
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,25 +8,6 @@ use std::num::{ParseFloatError, ParseIntError};
|
|||||||
use crate::{Identifier, ReservedIdentifier, Span, Token};
|
use crate::{Identifier, ReservedIdentifier, Span, Token};
|
||||||
|
|
||||||
/// Lex the input and return a vector of tokens and their positions.
|
/// Lex the input and return a vector of tokens and their positions.
|
||||||
///
|
|
||||||
/// # Examples
|
|
||||||
/// ```
|
|
||||||
/// # use dust_lang::*;
|
|
||||||
/// let input = "x = 1 + 2";
|
|
||||||
/// let tokens = lex(input).unwrap();
|
|
||||||
///
|
|
||||||
/// assert_eq!(
|
|
||||||
/// tokens,
|
|
||||||
/// [
|
|
||||||
/// (Token::Identifier(Identifier::new("x")), (0, 1)),
|
|
||||||
/// (Token::Equal, (2, 3)),
|
|
||||||
/// (Token::Integer(1), (4, 5)),
|
|
||||||
/// (Token::Plus, (6, 7)),
|
|
||||||
/// (Token::Integer(2), (8, 9)),
|
|
||||||
/// (Token::Eof, (9, 9)),
|
|
||||||
/// ]
|
|
||||||
/// );
|
|
||||||
/// ```
|
|
||||||
pub fn lex(input: &str) -> Result<Vec<(Token, Span)>, LexError> {
|
pub fn lex(input: &str) -> Result<Vec<(Token, Span)>, LexError> {
|
||||||
let mut lexer = Lexer::new(input);
|
let mut lexer = Lexer::new(input);
|
||||||
let mut tokens = Vec::new();
|
let mut tokens = Vec::new();
|
||||||
@ -47,37 +28,6 @@ pub fn lex(input: &str) -> Result<Vec<(Token, Span)>, LexError> {
|
|||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
/// Low-level tool for lexing a single token at a time.
|
/// Low-level tool for lexing a single token at a time.
|
||||||
///
|
|
||||||
/// # Examples
|
|
||||||
/// ```
|
|
||||||
/// # use dust_lang::*;
|
|
||||||
/// let input = "x = 1 + 2";
|
|
||||||
/// let mut lexer = Lexer::new(input);
|
|
||||||
/// let mut tokens = Vec::new();
|
|
||||||
///
|
|
||||||
/// loop {
|
|
||||||
/// let (token, span) = lexer.next_token().unwrap();
|
|
||||||
/// let is_eof = matches!(token, Token::Eof);
|
|
||||||
///
|
|
||||||
/// tokens.push((token, span));
|
|
||||||
///
|
|
||||||
/// if is_eof {
|
|
||||||
/// break;
|
|
||||||
/// }
|
|
||||||
/// }
|
|
||||||
///
|
|
||||||
/// assert_eq!(
|
|
||||||
/// tokens,
|
|
||||||
/// [
|
|
||||||
/// (Token::Identifier(Identifier::new("x")), (0, 1)),
|
|
||||||
/// (Token::Equal, (2, 3)),
|
|
||||||
/// (Token::Integer(1), (4, 5)),
|
|
||||||
/// (Token::Plus, (6, 7)),
|
|
||||||
/// (Token::Integer(2), (8, 9)),
|
|
||||||
/// (Token::Eof, (9, 9)),
|
|
||||||
/// ]
|
|
||||||
/// )
|
|
||||||
/// ```
|
|
||||||
pub struct Lexer<'a> {
|
pub struct Lexer<'a> {
|
||||||
source: &'a str,
|
source: &'a str,
|
||||||
position: usize,
|
position: usize,
|
||||||
|
@ -15,7 +15,7 @@ pub mod r#type;
|
|||||||
pub mod value;
|
pub mod value;
|
||||||
pub mod vm;
|
pub mod vm;
|
||||||
|
|
||||||
pub use abstract_tree::{AbstractSyntaxTree, Node, Statement};
|
pub use abstract_tree::{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};
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
|
|
||||||
use crate::{AbstractSyntaxTree, LexError, Lexer, Node, Span, Statement, Token, Value};
|
use crate::{
|
||||||
|
lex::{LexError, Lexer},
|
||||||
|
Node, Span, Statement, Token, Value,
|
||||||
|
};
|
||||||
|
|
||||||
pub fn parse(input: &str) -> Result<AbstractSyntaxTree, ParseError> {
|
pub fn parse(input: &str) -> Result<VecDeque<Node>, 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();
|
||||||
@ -17,7 +20,7 @@ pub fn parse(input: &str) -> Result<AbstractSyntaxTree, ParseError> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(AbstractSyntaxTree { nodes })
|
Ok(nodes)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Parser<'src> {
|
pub struct Parser<'src> {
|
||||||
@ -224,9 +227,7 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
parse(input),
|
parse(input),
|
||||||
Ok(AbstractSyntaxTree {
|
Ok([Node::new(Statement::Constant(Value::boolean(true)), (0, 4))].into())
|
||||||
nodes: [Node::new(Statement::Constant(Value::boolean(true)), (0, 4))].into()
|
|
||||||
})
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -236,8 +237,7 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
parse(input),
|
parse(input),
|
||||||
Ok(AbstractSyntaxTree {
|
Ok([Node::new(
|
||||||
nodes: [Node::new(
|
|
||||||
Statement::PropertyAccess(
|
Statement::PropertyAccess(
|
||||||
Box::new(Node::new(
|
Box::new(Node::new(
|
||||||
Statement::List(vec![
|
Statement::List(vec![
|
||||||
@ -251,8 +251,7 @@ mod tests {
|
|||||||
),
|
),
|
||||||
(0, 11),
|
(0, 11),
|
||||||
)]
|
)]
|
||||||
.into()
|
.into())
|
||||||
})
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -262,8 +261,7 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
parse(input),
|
parse(input),
|
||||||
Ok(AbstractSyntaxTree {
|
Ok([Node::new(
|
||||||
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")),
|
||||||
@ -276,8 +274,7 @@ mod tests {
|
|||||||
),
|
),
|
||||||
(0, 3),
|
(0, 3),
|
||||||
)]
|
)]
|
||||||
.into()
|
.into())
|
||||||
})
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -287,8 +284,7 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
parse(input),
|
parse(input),
|
||||||
Ok(AbstractSyntaxTree {
|
Ok([Node::new(
|
||||||
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(
|
||||||
@ -300,10 +296,7 @@ mod tests {
|
|||||||
),
|
),
|
||||||
Node::new(
|
Node::new(
|
||||||
Statement::Add(
|
Statement::Add(
|
||||||
Box::new(Node::new(
|
Box::new(Node::new(Statement::Constant(Value::integer(2)), (11, 12))),
|
||||||
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(
|
||||||
@ -323,8 +316,7 @@ mod tests {
|
|||||||
]),
|
]),
|
||||||
(0, 24),
|
(0, 24),
|
||||||
)]
|
)]
|
||||||
.into()
|
.into())
|
||||||
})
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -334,16 +326,14 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
parse(input),
|
parse(input),
|
||||||
Ok(AbstractSyntaxTree {
|
Ok([Node::new(
|
||||||
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())
|
||||||
})
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -353,9 +343,7 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
parse(input),
|
parse(input),
|
||||||
Ok(AbstractSyntaxTree {
|
Ok([Node::new(Statement::List(vec![]), (0, 2))].into())
|
||||||
nodes: [Node::new(Statement::List(vec![]), (0, 2))].into()
|
|
||||||
})
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -365,9 +353,7 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
parse(input),
|
parse(input),
|
||||||
Ok(AbstractSyntaxTree {
|
Ok([Node::new(Statement::Constant(Value::float(42.0)), (0, 4))].into())
|
||||||
nodes: [Node::new(Statement::Constant(Value::float(42.0)), (0, 4))].into()
|
|
||||||
})
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -377,16 +363,14 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
parse(input),
|
parse(input),
|
||||||
Ok(AbstractSyntaxTree {
|
Ok([Node::new(
|
||||||
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())
|
||||||
})
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -396,16 +380,14 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
parse(input),
|
parse(input),
|
||||||
Ok(AbstractSyntaxTree {
|
Ok([Node::new(
|
||||||
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())
|
||||||
})
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -415,8 +397,7 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
parse(input),
|
parse(input),
|
||||||
Ok(AbstractSyntaxTree {
|
Ok([Node::new(
|
||||||
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(
|
||||||
@ -429,8 +410,7 @@ mod tests {
|
|||||||
),
|
),
|
||||||
(0, 9),
|
(0, 9),
|
||||||
)]
|
)]
|
||||||
.into()
|
.into())
|
||||||
})
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -440,8 +420,7 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
parse(input),
|
parse(input),
|
||||||
Ok(AbstractSyntaxTree {
|
Ok([Node::new(
|
||||||
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")),
|
||||||
@ -469,8 +448,7 @@ mod tests {
|
|||||||
),
|
),
|
||||||
(0, 13),
|
(0, 13),
|
||||||
)]
|
)]
|
||||||
.into()
|
.into())
|
||||||
})
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,7 @@ use std::{
|
|||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::Identifier;
|
use crate::identifier::Identifier;
|
||||||
|
|
||||||
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
|
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
|
||||||
pub struct TypeConflict {
|
pub struct TypeConflict {
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::{HashMap, VecDeque};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
parse, AbstractSyntaxTree, Analyzer, AnalyzerError, Identifier, Node, ParseError,
|
parse, Identifier, Node, ParseError, ReservedIdentifier, Span, Statement, Value, ValueError,
|
||||||
ReservedIdentifier, Span, Statement, Value, ValueError,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn run(
|
pub fn run(
|
||||||
@ -10,22 +9,18 @@ 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, variables);
|
|
||||||
|
|
||||||
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 {
|
||||||
abstract_tree: AbstractSyntaxTree,
|
statement_nodes: VecDeque<Node>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Vm {
|
impl Vm {
|
||||||
pub fn new(abstract_tree: AbstractSyntaxTree) -> Self {
|
pub fn new(statement_nodes: VecDeque<Node>) -> Self {
|
||||||
Self { abstract_tree }
|
Vm { statement_nodes }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run(
|
pub fn run(
|
||||||
@ -34,7 +29,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.abstract_tree.nodes.pop_front() {
|
while let Some(node) = self.statement_nodes.pop_front() {
|
||||||
previous_value = self.run_node(node, variables)?;
|
previous_value = self.run_node(node, variables)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -171,7 +166,6 @@ 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),
|
||||||
|
|
||||||
@ -184,12 +178,6 @@ 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)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user