Compare commits
No commits in common. "a46e5dd3653face7979fd231caea11ad52b9d92b" and "f2823b6236859e53a2716c5fa14ba92c065f12b2" have entirely different histories.
a46e5dd365
...
f2823b6236
@ -23,8 +23,8 @@ use crate::{
|
|||||||
/// # use dust_lang::*;
|
/// # use dust_lang::*;
|
||||||
/// let input = "x = 1 + false";
|
/// let input = "x = 1 + false";
|
||||||
/// let abstract_tree = parse(input).unwrap();
|
/// let abstract_tree = parse(input).unwrap();
|
||||||
/// let mut context = Context::new();
|
/// let variables = HashMap::new();
|
||||||
/// let result = analyze(&abstract_tree, &mut context);
|
/// let result = analyze(&abstract_tree, &variables);
|
||||||
///
|
///
|
||||||
/// assert!(result.is_err());
|
/// assert!(result.is_err());
|
||||||
/// ```
|
/// ```
|
||||||
@ -45,8 +45,8 @@ pub fn analyze(
|
|||||||
/// # use dust_lang::*;
|
/// # use dust_lang::*;
|
||||||
/// let input = "x = 1 + false";
|
/// let input = "x = 1 + false";
|
||||||
/// let abstract_tree = parse(input).unwrap();
|
/// let abstract_tree = parse(input).unwrap();
|
||||||
/// let mut context = Context::new();
|
/// let variables = HashMap::new();
|
||||||
/// let mut analyzer = Analyzer::new(&abstract_tree, &mut context);
|
/// let analyzer = Analyzer::new(&abstract_tree, &variables);
|
||||||
/// let result = analyzer.analyze();
|
/// let result = analyzer.analyze();
|
||||||
///
|
///
|
||||||
/// assert!(result.is_err());
|
/// assert!(result.is_err());
|
||||||
|
@ -21,31 +21,28 @@ use crate::{
|
|||||||
/// # Examples
|
/// # Examples
|
||||||
/// ```
|
/// ```
|
||||||
/// # use dust_lang::*;
|
/// # use dust_lang::*;
|
||||||
/// let tree = parse("x = 42").unwrap();
|
/// let input = "x = 42";
|
||||||
|
/// let result = parse(input);
|
||||||
///
|
///
|
||||||
/// assert_eq!(
|
/// assert_eq!(
|
||||||
/// tree,
|
/// result,
|
||||||
/// AbstractSyntaxTree {
|
/// Ok(AbstractSyntaxTree {
|
||||||
/// nodes: [
|
/// nodes: [
|
||||||
/// Node::new(
|
/// Node {
|
||||||
/// Statement::BinaryOperation {
|
/// inner: Statement::Assignment {
|
||||||
/// left: Box::new(Node::new(
|
/// identifier: Node {
|
||||||
/// Statement::Identifier(Identifier::new("x")),
|
/// inner: Identifier::new("x"),
|
||||||
/// (0, 1),
|
/// position: (0, 1),
|
||||||
/// )),
|
/// },
|
||||||
/// operator: Node::new(
|
/// value_node: Box::new(Node {
|
||||||
/// BinaryOperator::Assign,
|
/// inner: Statement::Constant(Value::integer(42)),
|
||||||
/// (2, 3)
|
/// position: (4, 6),
|
||||||
/// ),
|
/// })
|
||||||
/// right: Box::new(Node::new(
|
|
||||||
/// Statement::Constant(Value::integer(42)),
|
|
||||||
/// (4, 6),
|
|
||||||
/// ))
|
|
||||||
/// },
|
/// },
|
||||||
/// (0, 6),
|
/// position: (0, 6),
|
||||||
/// )
|
/// }
|
||||||
/// ].into(),
|
/// ].into(),
|
||||||
/// },
|
/// }),
|
||||||
/// );
|
/// );
|
||||||
/// ```
|
/// ```
|
||||||
pub fn parse(input: &str) -> Result<AbstractSyntaxTree, ParseError> {
|
pub fn parse(input: &str) -> Result<AbstractSyntaxTree, ParseError> {
|
||||||
@ -90,23 +87,19 @@ pub fn parse(input: &str) -> Result<AbstractSyntaxTree, ParseError> {
|
|||||||
/// assert_eq!(
|
/// assert_eq!(
|
||||||
/// nodes,
|
/// nodes,
|
||||||
/// Into::<VecDeque<Node<Statement>>>::into([
|
/// Into::<VecDeque<Node<Statement>>>::into([
|
||||||
/// Node::new(
|
/// Node {
|
||||||
/// Statement::BinaryOperation {
|
/// inner: Statement::Assignment {
|
||||||
/// left: Box::new(Node::new(
|
/// identifier: Node {
|
||||||
/// Statement::Identifier(Identifier::new("x")),
|
/// inner: Identifier::new("x"),
|
||||||
/// (0, 1),
|
/// position: (0, 1),
|
||||||
/// )),
|
/// },
|
||||||
/// operator: Node::new(
|
/// value_node: Box::new(Node {
|
||||||
/// BinaryOperator::Assign,
|
/// inner: Statement::Constant(Value::integer(42)),
|
||||||
/// (2, 3),
|
/// position: (4, 6),
|
||||||
/// ),
|
/// }),
|
||||||
/// right: Box::new(Node::new(
|
|
||||||
/// Statement::Constant(Value::integer(42)),
|
|
||||||
/// (4, 6),
|
|
||||||
/// )),
|
|
||||||
/// },
|
/// },
|
||||||
/// (0, 6),
|
/// position: (0, 6),
|
||||||
/// )
|
/// }
|
||||||
/// ]),
|
/// ]),
|
||||||
/// );
|
/// );
|
||||||
/// ```
|
/// ```
|
||||||
@ -238,23 +231,13 @@ impl<'src> Parser<'src> {
|
|||||||
|
|
||||||
let next_node = self.parse_statement(0)?;
|
let next_node = self.parse_statement(0)?;
|
||||||
|
|
||||||
// If the new statement is already a block, add the next node to it
|
|
||||||
if statement
|
|
||||||
.as_ref()
|
|
||||||
.is_some_and(|statement| matches!(statement, Statement::Block(_)))
|
|
||||||
{
|
|
||||||
if let Statement::Block(block) =
|
|
||||||
statement.get_or_insert_with(|| Statement::Block(Vec::new()))
|
|
||||||
{
|
|
||||||
block.push(next_node);
|
|
||||||
}
|
|
||||||
// If the next node is an assignment, this might be a map
|
// If the next node is an assignment, this might be a map
|
||||||
} else if let Statement::BinaryOperation {
|
if let Statement::BinaryOperation {
|
||||||
left,
|
left,
|
||||||
operator:
|
operator:
|
||||||
Node {
|
Node {
|
||||||
inner: BinaryOperator::Assign,
|
inner: BinaryOperator::Assign,
|
||||||
position: operator_position,
|
..
|
||||||
},
|
},
|
||||||
right,
|
right,
|
||||||
} = next_node.inner
|
} = next_node.inner
|
||||||
@ -273,28 +256,10 @@ impl<'src> Parser<'src> {
|
|||||||
map_properties.push((*left, *right));
|
map_properties.push((*left, *right));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Allow commas after properties
|
// Ignore commas after properties
|
||||||
if let Token::Comma = self.current.0 {
|
if let Token::Comma = self.current.0 {
|
||||||
self.next_token()?;
|
self.next_token()?;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// Otherwise, the new statement is a block
|
|
||||||
if let Statement::Block(statements) =
|
|
||||||
statement.get_or_insert_with(|| Statement::Block(Vec::new()))
|
|
||||||
{
|
|
||||||
// Add the statement to the block
|
|
||||||
statements.push(Node::new(
|
|
||||||
Statement::BinaryOperation {
|
|
||||||
left,
|
|
||||||
operator: Node::new(
|
|
||||||
BinaryOperator::Assign,
|
|
||||||
operator_position,
|
|
||||||
),
|
|
||||||
right,
|
|
||||||
},
|
|
||||||
next_node.position,
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// Otherwise, the new statement is a block
|
// Otherwise, the new statement is a block
|
||||||
} else if let Statement::Block(statements) =
|
} else if let Statement::Block(statements) =
|
||||||
@ -690,13 +655,13 @@ mod tests {
|
|||||||
Statement::Identifier(Identifier::new("foo")),
|
Statement::Identifier(Identifier::new("foo")),
|
||||||
(2, 5)
|
(2, 5)
|
||||||
)),
|
)),
|
||||||
operator: Node::new(BinaryOperator::Assign, (6, 7)),
|
operator: Node::new(BinaryOperator::Assign, (6, 8)),
|
||||||
right: Box::new(Node::new(
|
right: Box::new(Node::new(
|
||||||
Statement::Constant(Value::integer(42)),
|
Statement::Constant(Value::integer(42)),
|
||||||
(8, 10)
|
(9, 11)
|
||||||
)),
|
)),
|
||||||
},
|
},
|
||||||
(2, 10)
|
(2, 11)
|
||||||
),)),
|
),)),
|
||||||
(2, 15)
|
(2, 15)
|
||||||
),
|
),
|
||||||
@ -705,12 +670,12 @@ mod tests {
|
|||||||
Statement::BinaryOperation {
|
Statement::BinaryOperation {
|
||||||
left: Box::new(Node::new(
|
left: Box::new(Node::new(
|
||||||
Statement::Identifier(Identifier::new("bar")),
|
Statement::Identifier(Identifier::new("bar")),
|
||||||
(12, 15)
|
(16, 19)
|
||||||
)),
|
)),
|
||||||
operator: Node::new(BinaryOperator::Assign, (16, 17)),
|
operator: Node::new(BinaryOperator::Assign, (20, 22)),
|
||||||
right: Box::new(Node::new(
|
right: Box::new(Node::new(
|
||||||
Statement::Constant(Value::integer(42)),
|
Statement::Constant(Value::integer(42)),
|
||||||
(18, 20)
|
(23, 25)
|
||||||
)),
|
)),
|
||||||
},
|
},
|
||||||
(12, 20)
|
(12, 20)
|
||||||
@ -721,12 +686,12 @@ mod tests {
|
|||||||
Statement::BinaryOperation {
|
Statement::BinaryOperation {
|
||||||
left: Box::new(Node::new(
|
left: Box::new(Node::new(
|
||||||
Statement::Identifier(Identifier::new("baz")),
|
Statement::Identifier(Identifier::new("baz")),
|
||||||
(22, 25)
|
(26, 29)
|
||||||
)),
|
)),
|
||||||
operator: Node::new(BinaryOperator::Assign, (26, 27)),
|
operator: Node::new(BinaryOperator::Assign, (30, 32)),
|
||||||
right: Box::new(Node::new(
|
right: Box::new(Node::new(
|
||||||
Statement::Constant(Value::string("42")),
|
Statement::Constant(Value::string("42")),
|
||||||
(28, 32)
|
(22, 32)
|
||||||
)),
|
)),
|
||||||
},
|
},
|
||||||
(22, 32)
|
(22, 32)
|
||||||
|
@ -43,11 +43,11 @@ use crate::{identifier::Identifier, AbstractSyntaxTree, Context, Type, Vm, VmErr
|
|||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// # use std::collections::HashMap;
|
/// # use std::collections::HashMap;
|
||||||
/// # use dust_lang::*;
|
/// # use dust_lang::{Type, Value};
|
||||||
/// let context = Context::new();
|
/// let variables = HashMap::new();
|
||||||
/// let value = Value::integer(42);
|
/// let value = Value::integer(42);
|
||||||
///
|
///
|
||||||
/// assert_eq!(value.r#type(&context), Type::Integer);
|
/// assert_eq!(value.r#type(&variables), Type::Integer);
|
||||||
/// ```
|
/// ```
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub struct Value(Arc<ValueInner>);
|
pub struct Value(Arc<ValueInner>);
|
||||||
|
Loading…
Reference in New Issue
Block a user