Compare commits
3 Commits
5d01f1caf9
...
e295aebf56
Author | SHA1 | Date | |
---|---|---|---|
e295aebf56 | |||
f64babc546 | |||
7328467e64 |
@ -1,5 +1,5 @@
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
collections::{HashMap, VecDeque},
|
||||
fmt::{self, Display, Formatter},
|
||||
};
|
||||
|
||||
@ -7,6 +7,11 @@ use serde::{Deserialize, Serialize};
|
||||
|
||||
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)]
|
||||
pub struct Node {
|
||||
pub statement: Statement,
|
||||
|
@ -1,22 +1,67 @@
|
||||
use crate::{Node, Statement};
|
||||
/// Tools for analyzing an abstract syntax tree and catch errors before running the virtual
|
||||
/// 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;
|
||||
|
||||
pub fn analyze(abstract_tree: Vec<Node>) -> Result<(), AnalyzerError> {
|
||||
let analyzer = Analyzer::new(abstract_tree);
|
||||
use crate::{AbstractSyntaxTree, Identifier, Node, Statement, Type, Value};
|
||||
|
||||
/// 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()
|
||||
}
|
||||
|
||||
pub struct Analyzer {
|
||||
abstract_tree: Vec<Node>,
|
||||
/// Static analyzer that checks for potential runtime 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 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 Analyzer {
|
||||
pub fn new(abstract_tree: Vec<Node>) -> Self {
|
||||
Analyzer { abstract_tree }
|
||||
impl<'a> Analyzer<'a> {
|
||||
pub fn new(
|
||||
abstract_tree: &'a AbstractSyntaxTree,
|
||||
variables: &'a HashMap<Identifier, Value>,
|
||||
) -> Self {
|
||||
Self {
|
||||
abstract_tree,
|
||||
variables,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn analyze(&self) -> Result<(), AnalyzerError> {
|
||||
for node in &self.abstract_tree {
|
||||
for node in &self.abstract_tree.nodes {
|
||||
self.analyze_node(node)?;
|
||||
}
|
||||
|
||||
@ -26,6 +71,24 @@ impl Analyzer {
|
||||
fn analyze_node(&self, node: &Node) -> Result<(), AnalyzerError> {
|
||||
match &node.statement {
|
||||
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(right)?;
|
||||
}
|
||||
@ -55,12 +118,32 @@ impl Analyzer {
|
||||
}
|
||||
}
|
||||
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(right)?;
|
||||
}
|
||||
Statement::PropertyAccess(left, right) => {
|
||||
if let Statement::Identifier(_) = &left.statement {
|
||||
// Identifier is in the correct position
|
||||
if let Statement::Identifier(_) | Statement::Constant(_) | Statement::List(_) =
|
||||
&left.statement
|
||||
{
|
||||
// Left side is valid
|
||||
} else {
|
||||
return Err(AnalyzerError::ExpectedIdentifier {
|
||||
actual: left.as_ref().clone(),
|
||||
@ -79,6 +162,7 @@ impl Analyzer {
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum AnalyzerError {
|
||||
ExpectedIdentifier { actual: Node },
|
||||
ExpectedIntegerOrFloat { actual: Node },
|
||||
UnexpectedIdentifier { identifier: Node },
|
||||
}
|
||||
|
||||
@ -89,16 +173,71 @@ mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn assignment_expect_identifier() {
|
||||
let abstract_tree = vec![Node::new(
|
||||
Statement::Assign(
|
||||
Box::new(Node::new(Statement::Constant(Value::integer(1)), (0, 1))),
|
||||
Box::new(Node::new(Statement::Constant(Value::integer(2)), (1, 2))),
|
||||
),
|
||||
(0, 2),
|
||||
)];
|
||||
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);
|
||||
|
||||
let analyzer = Analyzer::new(abstract_tree);
|
||||
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]
|
||||
fn assignment_expect_identifier() {
|
||||
let abstract_tree = AbstractSyntaxTree {
|
||||
nodes: [Node::new(
|
||||
Statement::Assign(
|
||||
Box::new(Node::new(Statement::Constant(Value::integer(1)), (0, 1))),
|
||||
Box::new(Node::new(Statement::Constant(Value::integer(2)), (1, 2))),
|
||||
),
|
||||
(0, 2),
|
||||
)]
|
||||
.into(),
|
||||
};
|
||||
let variables = HashMap::new();
|
||||
let analyzer = Analyzer::new(&abstract_tree, &variables);
|
||||
|
||||
assert_eq!(
|
||||
analyzer.analyze(),
|
||||
@ -109,13 +248,16 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unexpected_identifier_simple() {
|
||||
let abstract_tree = vec![Node::new(
|
||||
Statement::Identifier(Identifier::new("x")),
|
||||
(0, 1),
|
||||
)];
|
||||
|
||||
let analyzer = Analyzer::new(abstract_tree);
|
||||
fn unexpected_identifier() {
|
||||
let abstract_tree = AbstractSyntaxTree {
|
||||
nodes: [Node::new(
|
||||
Statement::Identifier(Identifier::new("x")),
|
||||
(0, 1),
|
||||
)]
|
||||
.into(),
|
||||
};
|
||||
let variables = HashMap::new();
|
||||
let analyzer = Analyzer::new(&abstract_tree, &variables);
|
||||
|
||||
assert_eq!(
|
||||
analyzer.analyze(),
|
||||
@ -124,27 +266,4 @@ 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,6 +8,25 @@ use std::num::{ParseFloatError, ParseIntError};
|
||||
use crate::{Identifier, ReservedIdentifier, Span, Token};
|
||||
|
||||
/// 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> {
|
||||
let mut lexer = Lexer::new(input);
|
||||
let mut tokens = Vec::new();
|
||||
@ -28,6 +47,37 @@ pub fn lex(input: &str) -> Result<Vec<(Token, Span)>, LexError> {
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
/// 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> {
|
||||
source: &'a str,
|
||||
position: usize,
|
||||
|
@ -15,7 +15,7 @@ pub mod r#type;
|
||||
pub mod value;
|
||||
pub mod vm;
|
||||
|
||||
pub use abstract_tree::{Node, Statement};
|
||||
pub use abstract_tree::{AbstractSyntaxTree, Node, Statement};
|
||||
pub use analyzer::{analyze, Analyzer, AnalyzerError};
|
||||
pub use identifier::Identifier;
|
||||
pub use lex::{lex, LexError, Lexer};
|
||||
|
@ -1,11 +1,8 @@
|
||||
use std::collections::VecDeque;
|
||||
|
||||
use crate::{
|
||||
lex::{LexError, Lexer},
|
||||
Node, Span, Statement, Token, Value,
|
||||
};
|
||||
use crate::{AbstractSyntaxTree, 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 mut parser = Parser::new(lexer);
|
||||
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> {
|
||||
@ -227,7 +224,9 @@ mod tests {
|
||||
|
||||
assert_eq!(
|
||||
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,21 +236,23 @@ mod tests {
|
||||
|
||||
assert_eq!(
|
||||
parse(input),
|
||||
Ok([Node::new(
|
||||
Statement::PropertyAccess(
|
||||
Box::new(Node::new(
|
||||
Statement::List(vec![
|
||||
Node::new(Statement::Constant(Value::integer(1)), (1, 2)),
|
||||
Node::new(Statement::Constant(Value::integer(2)), (4, 5)),
|
||||
Node::new(Statement::Constant(Value::integer(3)), (7, 8)),
|
||||
]),
|
||||
(0, 9)
|
||||
)),
|
||||
Box::new(Node::new(Statement::Constant(Value::integer(0)), (10, 11))),
|
||||
),
|
||||
(0, 11),
|
||||
)]
|
||||
.into())
|
||||
Ok(AbstractSyntaxTree {
|
||||
nodes: [Node::new(
|
||||
Statement::PropertyAccess(
|
||||
Box::new(Node::new(
|
||||
Statement::List(vec![
|
||||
Node::new(Statement::Constant(Value::integer(1)), (1, 2)),
|
||||
Node::new(Statement::Constant(Value::integer(2)), (4, 5)),
|
||||
Node::new(Statement::Constant(Value::integer(3)), (7, 8)),
|
||||
]),
|
||||
(0, 9)
|
||||
)),
|
||||
Box::new(Node::new(Statement::Constant(Value::integer(0)), (10, 11))),
|
||||
),
|
||||
(0, 11),
|
||||
)]
|
||||
.into()
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
@ -261,20 +262,22 @@ mod tests {
|
||||
|
||||
assert_eq!(
|
||||
parse(input),
|
||||
Ok([Node::new(
|
||||
Statement::PropertyAccess(
|
||||
Box::new(Node::new(
|
||||
Statement::Identifier(Identifier::new("a")),
|
||||
(0, 1)
|
||||
)),
|
||||
Box::new(Node::new(
|
||||
Statement::Identifier(Identifier::new("b")),
|
||||
(2, 3)
|
||||
)),
|
||||
),
|
||||
(0, 3),
|
||||
)]
|
||||
.into())
|
||||
Ok(AbstractSyntaxTree {
|
||||
nodes: [Node::new(
|
||||
Statement::PropertyAccess(
|
||||
Box::new(Node::new(
|
||||
Statement::Identifier(Identifier::new("a")),
|
||||
(0, 1)
|
||||
)),
|
||||
Box::new(Node::new(
|
||||
Statement::Identifier(Identifier::new("b")),
|
||||
(2, 3)
|
||||
)),
|
||||
),
|
||||
(0, 3),
|
||||
)]
|
||||
.into()
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
@ -284,39 +287,44 @@ mod tests {
|
||||
|
||||
assert_eq!(
|
||||
parse(input),
|
||||
Ok([Node::new(
|
||||
Statement::List(vec![
|
||||
Node::new(Statement::Constant(Value::integer(1)), (1, 2)),
|
||||
Node::new(
|
||||
Statement::Add(
|
||||
Box::new(Node::new(Statement::Constant(Value::integer(1)), (4, 5))),
|
||||
Box::new(Node::new(Statement::Constant(Value::integer(1)), (8, 9))),
|
||||
Ok(AbstractSyntaxTree {
|
||||
nodes: [Node::new(
|
||||
Statement::List(vec![
|
||||
Node::new(Statement::Constant(Value::integer(1)), (1, 2)),
|
||||
Node::new(
|
||||
Statement::Add(
|
||||
Box::new(Node::new(Statement::Constant(Value::integer(1)), (4, 5))),
|
||||
Box::new(Node::new(Statement::Constant(Value::integer(1)), (8, 9))),
|
||||
),
|
||||
(4, 9),
|
||||
),
|
||||
(4, 9),
|
||||
),
|
||||
Node::new(
|
||||
Statement::Add(
|
||||
Box::new(Node::new(Statement::Constant(Value::integer(2)), (11, 12))),
|
||||
Box::new(Node::new(
|
||||
Statement::Multiply(
|
||||
Box::new(Node::new(
|
||||
Statement::Constant(Value::integer(4)),
|
||||
(16, 17)
|
||||
)),
|
||||
Box::new(Node::new(
|
||||
Statement::Constant(Value::integer(10)),
|
||||
(20, 22)
|
||||
)),
|
||||
),
|
||||
(15, 23),
|
||||
),),
|
||||
Node::new(
|
||||
Statement::Add(
|
||||
Box::new(Node::new(
|
||||
Statement::Constant(Value::integer(2)),
|
||||
(11, 12)
|
||||
)),
|
||||
Box::new(Node::new(
|
||||
Statement::Multiply(
|
||||
Box::new(Node::new(
|
||||
Statement::Constant(Value::integer(4)),
|
||||
(16, 17)
|
||||
)),
|
||||
Box::new(Node::new(
|
||||
Statement::Constant(Value::integer(10)),
|
||||
(20, 22)
|
||||
)),
|
||||
),
|
||||
(15, 23),
|
||||
),),
|
||||
),
|
||||
(11, 23),
|
||||
),
|
||||
(11, 23),
|
||||
),
|
||||
]),
|
||||
(0, 24),
|
||||
)]
|
||||
.into())
|
||||
]),
|
||||
(0, 24),
|
||||
)]
|
||||
.into()
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
@ -326,14 +334,16 @@ mod tests {
|
||||
|
||||
assert_eq!(
|
||||
parse(input),
|
||||
Ok([Node::new(
|
||||
Statement::List(vec![
|
||||
Node::new(Statement::Constant(Value::integer(1)), (1, 2)),
|
||||
Node::new(Statement::Constant(Value::integer(2)), (4, 5)),
|
||||
]),
|
||||
(0, 6),
|
||||
)]
|
||||
.into())
|
||||
Ok(AbstractSyntaxTree {
|
||||
nodes: [Node::new(
|
||||
Statement::List(vec![
|
||||
Node::new(Statement::Constant(Value::integer(1)), (1, 2)),
|
||||
Node::new(Statement::Constant(Value::integer(2)), (4, 5)),
|
||||
]),
|
||||
(0, 6),
|
||||
)]
|
||||
.into()
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
@ -343,7 +353,9 @@ mod tests {
|
||||
|
||||
assert_eq!(
|
||||
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!(
|
||||
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!(
|
||||
parse(input),
|
||||
Ok([Node::new(
|
||||
Statement::Add(
|
||||
Box::new(Node::new(Statement::Constant(Value::integer(1)), (0, 1))),
|
||||
Box::new(Node::new(Statement::Constant(Value::integer(2)), (4, 5))),
|
||||
),
|
||||
(0, 5),
|
||||
)]
|
||||
.into())
|
||||
Ok(AbstractSyntaxTree {
|
||||
nodes: [Node::new(
|
||||
Statement::Add(
|
||||
Box::new(Node::new(Statement::Constant(Value::integer(1)), (0, 1))),
|
||||
Box::new(Node::new(Statement::Constant(Value::integer(2)), (4, 5))),
|
||||
),
|
||||
(0, 5),
|
||||
)]
|
||||
.into()
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
@ -380,14 +396,16 @@ mod tests {
|
||||
|
||||
assert_eq!(
|
||||
parse(input),
|
||||
Ok([Node::new(
|
||||
Statement::Multiply(
|
||||
Box::new(Node::new(Statement::Constant(Value::integer(1)), (0, 1))),
|
||||
Box::new(Node::new(Statement::Constant(Value::integer(2)), (4, 5))),
|
||||
),
|
||||
(0, 5),
|
||||
)]
|
||||
.into())
|
||||
Ok(AbstractSyntaxTree {
|
||||
nodes: [Node::new(
|
||||
Statement::Multiply(
|
||||
Box::new(Node::new(Statement::Constant(Value::integer(1)), (0, 1))),
|
||||
Box::new(Node::new(Statement::Constant(Value::integer(2)), (4, 5))),
|
||||
),
|
||||
(0, 5),
|
||||
)]
|
||||
.into()
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
@ -397,20 +415,22 @@ mod tests {
|
||||
|
||||
assert_eq!(
|
||||
parse(input),
|
||||
Ok([Node::new(
|
||||
Statement::Add(
|
||||
Box::new(Node::new(Statement::Constant(Value::integer(1)), (0, 1))),
|
||||
Box::new(Node::new(
|
||||
Statement::Multiply(
|
||||
Box::new(Node::new(Statement::Constant(Value::integer(2)), (4, 5))),
|
||||
Box::new(Node::new(Statement::Constant(Value::integer(3)), (8, 9))),
|
||||
),
|
||||
(4, 9),
|
||||
)),
|
||||
),
|
||||
(0, 9),
|
||||
)]
|
||||
.into())
|
||||
Ok(AbstractSyntaxTree {
|
||||
nodes: [Node::new(
|
||||
Statement::Add(
|
||||
Box::new(Node::new(Statement::Constant(Value::integer(1)), (0, 1))),
|
||||
Box::new(Node::new(
|
||||
Statement::Multiply(
|
||||
Box::new(Node::new(Statement::Constant(Value::integer(2)), (4, 5))),
|
||||
Box::new(Node::new(Statement::Constant(Value::integer(3)), (8, 9))),
|
||||
),
|
||||
(4, 9),
|
||||
)),
|
||||
),
|
||||
(0, 9),
|
||||
)]
|
||||
.into()
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
@ -420,35 +440,37 @@ mod tests {
|
||||
|
||||
assert_eq!(
|
||||
parse(input),
|
||||
Ok([Node::new(
|
||||
Statement::Assign(
|
||||
Box::new(Node::new(
|
||||
Statement::Identifier(Identifier::new("a")),
|
||||
(0, 1)
|
||||
)),
|
||||
Box::new(Node::new(
|
||||
Statement::Add(
|
||||
Box::new(Node::new(Statement::Constant(Value::integer(1)), (4, 5))),
|
||||
Box::new(Node::new(
|
||||
Statement::Multiply(
|
||||
Box::new(Node::new(
|
||||
Statement::Constant(Value::integer(2)),
|
||||
(8, 9)
|
||||
)),
|
||||
Box::new(Node::new(
|
||||
Statement::Constant(Value::integer(3)),
|
||||
(12, 13)
|
||||
)),
|
||||
),
|
||||
(8, 13),
|
||||
)),
|
||||
),
|
||||
(4, 13),
|
||||
)),
|
||||
),
|
||||
(0, 13),
|
||||
)]
|
||||
.into())
|
||||
Ok(AbstractSyntaxTree {
|
||||
nodes: [Node::new(
|
||||
Statement::Assign(
|
||||
Box::new(Node::new(
|
||||
Statement::Identifier(Identifier::new("a")),
|
||||
(0, 1)
|
||||
)),
|
||||
Box::new(Node::new(
|
||||
Statement::Add(
|
||||
Box::new(Node::new(Statement::Constant(Value::integer(1)), (4, 5))),
|
||||
Box::new(Node::new(
|
||||
Statement::Multiply(
|
||||
Box::new(Node::new(
|
||||
Statement::Constant(Value::integer(2)),
|
||||
(8, 9)
|
||||
)),
|
||||
Box::new(Node::new(
|
||||
Statement::Constant(Value::integer(3)),
|
||||
(12, 13)
|
||||
)),
|
||||
),
|
||||
(8, 13),
|
||||
)),
|
||||
),
|
||||
(4, 13),
|
||||
)),
|
||||
),
|
||||
(0, 13),
|
||||
)]
|
||||
.into()
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -18,7 +18,7 @@ use std::{
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::identifier::Identifier;
|
||||
use crate::Identifier;
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
|
||||
pub struct TypeConflict {
|
||||
|
@ -1,7 +1,8 @@
|
||||
use std::collections::{HashMap, VecDeque};
|
||||
use std::collections::HashMap;
|
||||
|
||||
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(
|
||||
@ -9,18 +10,22 @@ pub fn run(
|
||||
variables: &mut HashMap<Identifier, Value>,
|
||||
) -> Result<Option<Value>, VmError> {
|
||||
let abstract_syntax_tree = parse(input)?;
|
||||
let analyzer = Analyzer::new(&abstract_syntax_tree, variables);
|
||||
|
||||
analyzer.analyze()?;
|
||||
|
||||
let mut vm = Vm::new(abstract_syntax_tree);
|
||||
|
||||
vm.run(variables)
|
||||
}
|
||||
|
||||
pub struct Vm {
|
||||
statement_nodes: VecDeque<Node>,
|
||||
abstract_tree: AbstractSyntaxTree,
|
||||
}
|
||||
|
||||
impl Vm {
|
||||
pub fn new(statement_nodes: VecDeque<Node>) -> Self {
|
||||
Vm { statement_nodes }
|
||||
pub fn new(abstract_tree: AbstractSyntaxTree) -> Self {
|
||||
Self { abstract_tree }
|
||||
}
|
||||
|
||||
pub fn run(
|
||||
@ -29,7 +34,7 @@ impl Vm {
|
||||
) -> Result<Option<Value>, VmError> {
|
||||
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)?;
|
||||
}
|
||||
|
||||
@ -166,6 +171,7 @@ impl Vm {
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum VmError {
|
||||
AnaylyzerError(AnalyzerError),
|
||||
ParseError(ParseError),
|
||||
ValueError(ValueError),
|
||||
|
||||
@ -178,6 +184,12 @@ pub enum VmError {
|
||||
ExpectedValue { position: Span },
|
||||
}
|
||||
|
||||
impl From<AnalyzerError> for VmError {
|
||||
fn from(error: AnalyzerError) -> Self {
|
||||
Self::AnaylyzerError(error)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ParseError> for VmError {
|
||||
fn from(error: ParseError) -> Self {
|
||||
Self::ParseError(error)
|
||||
|
Loading…
Reference in New Issue
Block a user