1
0

Improve AST ergonomics

This commit is contained in:
Jeff 2024-08-17 12:15:47 -04:00
parent 0b64afccb1
commit 0fd19a623d
4 changed files with 352 additions and 355 deletions

View File

@ -32,10 +32,10 @@ pub enum Expression {
} }
impl Expression { impl Expression {
pub fn map(pairs: Vec<(Node<Identifier>, Expression)>, position: Span) -> Self { pub fn map<T: Into<Vec<(Node<Identifier>, Expression)>>>(pairs: T, position: Span) -> Self {
Self::Map(Node::new( Self::Map(Node::new(
Box::new(MapExpression { Box::new(MapExpression {
pairs: pairs.into_iter().collect(), pairs: pairs.into(),
}), }),
position, position,
)) ))
@ -214,24 +214,40 @@ impl Expression {
Self::Struct(Node::new(Box::new(struct_expression), position)) Self::Struct(Node::new(Box::new(struct_expression), position))
} }
pub fn identifier(identifier: Identifier, position: Span) -> Self { pub fn identifier<T: ToString>(to_string: T, position: Span) -> Self {
Self::Identifier(Node::new(identifier, position)) Self::Identifier(Node::new(Identifier::new(to_string), position))
} }
pub fn list(list_expression: ListExpression, position: Span) -> Self { pub fn list<T: Into<Vec<Expression>>>(expressions: T, position: Span) -> Self {
Self::List(Node::new(Box::new(list_expression), position)) Self::List(Node::new(
Box::new(ListExpression::Ordered(expressions.into())),
position,
))
} }
pub fn list_index(list_index: ListIndexExpression, position: Span) -> Self { pub fn auto_fill_list(repeat: Expression, length: Expression, position: Span) -> Self {
Self::ListIndex(Node::new(Box::new(list_index), position)) Self::List(Node::new(
Box::new(ListExpression::AutoFill {
repeat_operand: repeat,
length_operand: length,
}),
position,
))
}
pub fn list_index(list: Expression, index: Expression, position: Span) -> Self {
Self::ListIndex(Node::new(
Box::new(ListIndexExpression { list, index }),
position,
))
} }
pub fn r#if(r#if: IfExpression, position: Span) -> Self { pub fn r#if(r#if: IfExpression, position: Span) -> Self {
Self::If(Node::new(Box::new(r#if), position)) Self::If(Node::new(Box::new(r#if), position))
} }
pub fn literal(literal: LiteralExpression, position: Span) -> Self { pub fn literal<T: Into<LiteralExpression>>(into_literal: T, position: Span) -> Self {
Self::Literal(Node::new(Box::new(literal), position)) Self::Literal(Node::new(Box::new(into_literal.into()), position))
} }
pub fn has_block(&self) -> bool { pub fn has_block(&self) -> bool {
@ -325,9 +341,12 @@ impl Expression {
} }
} }
Expression::Literal(literal_expression) => match literal_expression.inner.as_ref() { Expression::Literal(literal_expression) => match literal_expression.inner.as_ref() {
LiteralExpression::Boolean(_) => Some(Type::Boolean), LiteralExpression::Primitive(primitive_value) => match primitive_value {
LiteralExpression::Float(_) => Some(Type::Float), PrimitiveValueExpression::Boolean(_) => Some(Type::Boolean),
LiteralExpression::Integer(_) => Some(Type::Integer), PrimitiveValueExpression::Character(_) => Some(Type::Character),
PrimitiveValueExpression::Integer(_) => Some(Type::Integer),
PrimitiveValueExpression::Float(_) => Some(Type::Float),
},
LiteralExpression::String(_) => Some(Type::String), LiteralExpression::String(_) => Some(Type::String),
}, },
Expression::Loop(loop_expression) => match loop_expression.inner.as_ref() { Expression::Loop(loop_expression) => match loop_expression.inner.as_ref() {
@ -361,7 +380,7 @@ impl Expression {
Expression::Range(range_expression) => { Expression::Range(range_expression) => {
let start = match range_expression.inner.as_ref() { let start = match range_expression.inner.as_ref() {
RangeExpression::Exclusive { start, .. } => start, RangeExpression::Exclusive { start, .. } => start,
RangeExpression::Inclusive { start, end } => start, RangeExpression::Inclusive { start, .. } => start,
}; };
let start_type = start.return_type(context)?; let start_type = start.return_type(context)?;
let rangeable_type = match start_type { let rangeable_type = match start_type {
@ -578,20 +597,114 @@ impl Display for ListExpression {
} }
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum LiteralExpression { pub enum PrimitiveValueExpression {
Boolean(bool), Boolean(bool),
Character(char),
Float(f64), Float(f64),
Integer(i64), Integer(i64),
String(String), }
impl Display for PrimitiveValueExpression {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
PrimitiveValueExpression::Boolean(boolean) => write!(f, "{boolean}"),
PrimitiveValueExpression::Character(character) => write!(f, "{character}"),
PrimitiveValueExpression::Float(float) => write!(f, "{float}"),
PrimitiveValueExpression::Integer(integer) => write!(f, "{integer}"),
}
}
}
impl Eq for PrimitiveValueExpression {}
impl PartialOrd for PrimitiveValueExpression {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for PrimitiveValueExpression {
fn cmp(&self, other: &Self) -> Ordering {
match (self, other) {
(PrimitiveValueExpression::Boolean(left), PrimitiveValueExpression::Boolean(right)) => {
left.cmp(right)
}
(PrimitiveValueExpression::Boolean(_), _) => Ordering::Greater,
(
PrimitiveValueExpression::Character(left),
PrimitiveValueExpression::Character(right),
) => left.cmp(right),
(PrimitiveValueExpression::Character(_), _) => Ordering::Greater,
(PrimitiveValueExpression::Float(left), PrimitiveValueExpression::Float(right)) => {
left.to_bits().cmp(&right.to_bits())
}
(PrimitiveValueExpression::Float(_), _) => Ordering::Greater,
(PrimitiveValueExpression::Integer(left), PrimitiveValueExpression::Integer(right)) => {
left.cmp(right)
}
(PrimitiveValueExpression::Integer(_), _) => Ordering::Greater,
}
}
}
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct RawStringExpression(pub String);
impl Display for RawStringExpression {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<i64> for LiteralExpression {
fn from(value: i64) -> Self {
LiteralExpression::Primitive(PrimitiveValueExpression::Integer(value))
}
}
impl From<String> for LiteralExpression {
fn from(value: String) -> Self {
LiteralExpression::String(RawStringExpression(value))
}
}
impl From<&str> for LiteralExpression {
fn from(value: &str) -> Self {
LiteralExpression::String(RawStringExpression(value.to_string()))
}
}
impl From<f64> for LiteralExpression {
fn from(value: f64) -> Self {
LiteralExpression::Primitive(PrimitiveValueExpression::Float(value))
}
}
impl From<bool> for LiteralExpression {
fn from(value: bool) -> Self {
LiteralExpression::Primitive(PrimitiveValueExpression::Boolean(value))
}
}
impl From<char> for LiteralExpression {
fn from(value: char) -> Self {
LiteralExpression::Primitive(PrimitiveValueExpression::Character(value))
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum LiteralExpression {
Primitive(PrimitiveValueExpression),
String(RawStringExpression),
} }
impl Display for LiteralExpression { impl Display for LiteralExpression {
fn fmt(&self, f: &mut Formatter) -> fmt::Result { fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self { match self {
LiteralExpression::Boolean(boolean) => write!(f, "{}", boolean), LiteralExpression::Primitive(primitive) => {
LiteralExpression::Float(float) => write!(f, "{}", float), write!(f, "{primitive}")
LiteralExpression::Integer(integer) => write!(f, "{}", integer), }
LiteralExpression::String(string) => write!(f, "{}", string), LiteralExpression::String(string) => write!(f, "{string}"),
} }
} }
} }
@ -607,17 +720,12 @@ impl PartialOrd for LiteralExpression {
impl Ord for LiteralExpression { impl Ord for LiteralExpression {
fn cmp(&self, other: &Self) -> Ordering { fn cmp(&self, other: &Self) -> Ordering {
match (self, other) { match (self, other) {
(LiteralExpression::Boolean(left), LiteralExpression::Boolean(right)) => { (LiteralExpression::Primitive(left), LiteralExpression::Primitive(right)) => {
left.cmp(right)
}
(LiteralExpression::Float(left), LiteralExpression::Float(right)) => {
left.to_bits().cmp(&right.to_bits())
}
(LiteralExpression::Integer(left), LiteralExpression::Integer(right)) => {
left.cmp(right) left.cmp(right)
} }
(LiteralExpression::Primitive(_), _) => Ordering::Greater,
(LiteralExpression::String(left), LiteralExpression::String(right)) => left.cmp(right), (LiteralExpression::String(left), LiteralExpression::String(right)) => left.cmp(right),
_ => unreachable!(), (LiteralExpression::String(_), _) => Ordering::Greater,
} }
} }
} }
@ -726,6 +834,28 @@ pub enum MathOperator {
Modulo, Modulo,
} }
impl MathOperator {
pub fn add(position: Span) -> Node<Self> {
Node::new(MathOperator::Add, position)
}
pub fn subtract(position: Span) -> Node<Self> {
Node::new(MathOperator::Subtract, position)
}
pub fn multiply(position: Span) -> Node<Self> {
Node::new(MathOperator::Multiply, position)
}
pub fn divide(position: Span) -> Node<Self> {
Node::new(MathOperator::Divide, position)
}
pub fn modulo(position: Span) -> Node<Self> {
Node::new(MathOperator::Modulo, position)
}
}
impl Display for MathOperator { impl Display for MathOperator {
fn fmt(&self, f: &mut Formatter) -> fmt::Result { fn fmt(&self, f: &mut Formatter) -> fmt::Result {
let operator = match self { let operator = match self {

View File

@ -418,27 +418,23 @@ impl<'src> Parser<'src> {
Token::Boolean(text) => { Token::Boolean(text) => {
self.next_token()?; self.next_token()?;
let boolean = text.parse().map_err(|error| ParseError::Boolean { let boolean = text.parse::<bool>().map_err(|error| ParseError::Boolean {
error, error,
position: start_position, position: start_position,
})?; })?;
let statement = let statement = Expression::literal(boolean, start_position);
Expression::literal(LiteralExpression::Boolean(boolean), start_position);
Ok(statement) Ok(statement)
} }
Token::Float(text) => { Token::Float(text) => {
self.next_token()?; self.next_token()?;
let float = text.parse().map_err(|error| ParseError::Float { let float = text.parse::<f64>().map_err(|error| ParseError::Float {
error, error,
position: start_position, position: start_position,
})?; })?;
Ok(Expression::literal( Ok(Expression::literal(float, start_position))
LiteralExpression::Float(float),
start_position,
))
} }
Token::Identifier(text) => { Token::Identifier(text) => {
self.next_token()?; self.next_token()?;
@ -502,10 +498,7 @@ impl<'src> Parser<'src> {
position: start_position, position: start_position,
})?; })?;
Ok(Expression::literal( Ok(Expression::literal(integer, start_position))
LiteralExpression::Integer(integer),
start_position,
))
} }
Token::If => { Token::If => {
self.next_token()?; self.next_token()?;
@ -518,10 +511,7 @@ impl<'src> Parser<'src> {
Token::String(text) => { Token::String(text) => {
self.next_token()?; self.next_token()?;
Ok(Expression::literal( Ok(Expression::literal(text.to_string(), start_position))
LiteralExpression::String(text.to_string()),
start_position,
))
} }
Token::LeftCurlyBrace => { Token::LeftCurlyBrace => {
let block_node = self.parse_block()?; let block_node = self.parse_block()?;
@ -555,10 +545,7 @@ impl<'src> Parser<'src> {
self.next_token()?; self.next_token()?;
return Ok(Expression::list( return Ok(Expression::list(Vec::new(), position));
ListExpression::Ordered(Vec::new()),
position,
));
} }
let first_expression = self.parse_expression(0)?; let first_expression = self.parse_expression(0)?;
@ -573,11 +560,9 @@ impl<'src> Parser<'src> {
self.next_token()?; self.next_token()?;
return Ok(Expression::list( return Ok(Expression::auto_fill_list(
ListExpression::AutoFill { first_expression,
length_operand: first_expression, repeat_operand,
repeat_operand,
},
position, position,
)); ));
} else { } else {
@ -597,10 +582,7 @@ impl<'src> Parser<'src> {
self.next_token()?; self.next_token()?;
return Ok(Expression::list( return Ok(Expression::list(expressions, position));
ListExpression::Ordered(expressions),
position,
));
} }
if let Token::Comma = self.current_token { if let Token::Comma = self.current_token {
@ -899,7 +881,7 @@ impl<'src> Parser<'src> {
let position = (left.position().0, operator_end); let position = (left.position().0, operator_end);
Expression::list_index(ListIndexExpression { list: left, index }, position) Expression::list_index(left, index, position)
} }
_ => { _ => {
return Err(ParseError::ExpectedTokenMultiple { return Err(ParseError::ExpectedTokenMultiple {
@ -1184,18 +1166,15 @@ mod tests {
vec![ vec![
( (
Node::new(Identifier::new("x"), (6, 7)), Node::new(Identifier::new("x"), (6, 7)),
Expression::literal( Expression::literal("1".to_string(), (10, 13)),
LiteralExpression::String("1".to_string()),
(10, 13)
),
), ),
( (
Node::new(Identifier::new("y"), (15, 16)), Node::new(Identifier::new("y"), (15, 16)),
Expression::literal(LiteralExpression::Integer(2), (19, 20)), Expression::literal(2, (19, 20)),
), ),
( (
Node::new(Identifier::new("z"), (22, 23)), Node::new(Identifier::new("z"), (22, 23)),
Expression::literal(LiteralExpression::Float(3.0), (26, 29)), Expression::literal(3.0, (26, 29)),
), ),
], ],
(0, 31), (0, 31),
@ -1216,7 +1195,7 @@ mod tests {
Statement::Let(Node::new( Statement::Let(Node::new(
LetStatement::LetMut { LetStatement::LetMut {
identifier: Node::new(Identifier::new("x"), (8, 9)), identifier: Node::new(Identifier::new("x"), (8, 9)),
value: Expression::literal(LiteralExpression::Integer(0), (12, 13)), value: Expression::literal(0, (12, 13)),
}, },
(0, 14), (0, 14),
)), )),
@ -1225,7 +1204,7 @@ mod tests {
Expression::comparison( Expression::comparison(
Expression::identifier(Identifier::new("x"), (21, 22)), Expression::identifier(Identifier::new("x"), (21, 22)),
Node::new(ComparisonOperator::LessThan, (23, 24)), Node::new(ComparisonOperator::LessThan, (23, 24)),
Expression::literal(LiteralExpression::Integer(10), (25, 27)), Expression::literal(10, (25, 27)),
(21, 27), (21, 27),
), ),
Node::new( Node::new(
@ -1233,10 +1212,7 @@ mod tests {
Expression::compound_assignment( Expression::compound_assignment(
Expression::identifier(Identifier::new("x"), (30, 31)), Expression::identifier(Identifier::new("x"), (30, 31)),
Node::new(MathOperator::Add, (32, 34)), Node::new(MathOperator::Add, (32, 34)),
Expression::literal( Expression::literal(1, (35, 36)),
LiteralExpression::Integer(1),
(35, 36)
),
(30, 36), (30, 36),
), ),
)]), )]),
@ -1263,7 +1239,7 @@ mod tests {
statements: [Statement::Let(Node::new( statements: [Statement::Let(Node::new(
LetStatement::Let { LetStatement::Let {
identifier: Node::new(Identifier::new("x"), (4, 5)), identifier: Node::new(Identifier::new("x"), (4, 5)),
value: Expression::literal(LiteralExpression::Integer(42), (8, 10)), value: Expression::literal(42, (8, 10)),
}, },
(0, 11), (0, 11),
))] ))]
@ -1282,7 +1258,7 @@ mod tests {
statements: [Statement::Let(Node::new( statements: [Statement::Let(Node::new(
LetStatement::LetMut { LetStatement::LetMut {
identifier: Node::new(Identifier::new("x"), (8, 9)), identifier: Node::new(Identifier::new("x"), (8, 9)),
value: Expression::literal(LiteralExpression::Boolean(false), (12, 17)), value: Expression::literal(false, (12, 17)),
}, },
(0, 18), (0, 18),
))] ))]
@ -1308,10 +1284,7 @@ mod tests {
Identifier::new("x"), Identifier::new("x"),
(8, 9) (8, 9)
), ),
value: Expression::literal( value: Expression::literal(42, (12, 14)),
LiteralExpression::Integer(42),
(12, 14)
),
}, },
(8, 14) (8, 14)
), ),
@ -1323,10 +1296,7 @@ mod tests {
Identifier::new("y"), Identifier::new("y"),
(16, 17) (16, 17)
), ),
value: Expression::literal( value: Expression::literal(4.0, (20, 23)),
LiteralExpression::Float(4.0),
(20, 23)
),
}, },
(16, 23) (16, 23)
)) ))
@ -1351,11 +1321,8 @@ mod tests {
Expression::call( Expression::call(
Expression::identifier(Identifier::new("Foo"), (0, 3)), Expression::identifier(Identifier::new("Foo"), (0, 3)),
vec![ vec![
Expression::literal(LiteralExpression::Integer(42), (4, 6)), Expression::literal(42, (4, 6)),
Expression::literal( Expression::literal("bar".to_string(), (8, 13)),
LiteralExpression::String("bar".to_string()),
(8, 13)
),
], ],
(0, 15) (0, 15)
), ),
@ -1369,11 +1336,6 @@ mod tests {
#[test] #[test]
fn fields_struct_instantiation() { fn fields_struct_instantiation() {
let source = "Foo { a: 42, b: 4.0 }"; let source = "Foo { a: 42, b: 4.0 }";
let mut tree = AbstractSyntaxTree::new();
if parse_into(source, &mut tree).is_err() {
println!("{:?}", tree);
}
assert_eq!( assert_eq!(
parse(source), parse(source),
@ -1384,11 +1346,11 @@ mod tests {
fields: vec![ fields: vec![
( (
Node::new(Identifier::new("a"), (6, 7)), Node::new(Identifier::new("a"), (6, 7)),
Expression::literal(LiteralExpression::Integer(42), (9, 11)), Expression::literal(42, (9, 11)),
), ),
( (
Node::new(Identifier::new("b"), (13, 14)), Node::new(Identifier::new("b"), (13, 14)),
Expression::literal(LiteralExpression::Float(4.0), (16, 19)) Expression::literal(4.0, (16, 19))
) )
] ]
}, },
@ -1435,8 +1397,8 @@ mod tests {
Statement::Expression(Expression::call( Statement::Expression(Expression::call(
Expression::identifier(Identifier::new("Foo"), (0, 3)), Expression::identifier(Identifier::new("Foo"), (0, 3)),
vec![ vec![
Expression::literal(LiteralExpression::Integer(1), (4, 5)), Expression::literal(1, (4, 5)),
Expression::literal(LiteralExpression::Float(2.0), (7, 10)) Expression::literal(2.0, (7, 10))
], ],
(0, 11) (0, 11)
)) ))
@ -1490,31 +1452,21 @@ mod tests {
parse(source), parse(source),
Ok(AbstractSyntaxTree::with_statements([ Ok(AbstractSyntaxTree::with_statements([
Statement::Expression(Expression::list_index( Statement::Expression(Expression::list_index(
ListIndexExpression { Expression::list_index(
list: Expression::list_index( Expression::list(
ListIndexExpression { [
list: Expression::list( Expression::literal(1, (1, 2)),
ListExpression::Ordered(vec![ Expression::list([Expression::literal(2, (5, 6))], (4, 7)),
Expression::literal(LiteralExpression::Integer(1), (1, 2)), Expression::literal(3, (9, 10)),
Expression::list( ],
ListExpression::Ordered(vec![Expression::literal( (0, 11)
LiteralExpression::Integer(2),
(5, 6)
)]),
(4, 7)
),
Expression::literal(LiteralExpression::Integer(3), (9, 10)),
]),
(0, 11)
),
index: Expression::literal(LiteralExpression::Integer(1), (12, 13)),
},
(0, 14)
), ),
index: Expression::literal(LiteralExpression::Integer(0), (15, 16)), Expression::literal(1, (12, 13)),
}, (0, 14)
),
Expression::literal(0, (15, 16)),
(0, 17) (0, 17)
)) ),)
])) ]))
); );
} }
@ -1527,8 +1479,8 @@ mod tests {
parse(source), parse(source),
Ok(AbstractSyntaxTree::with_statements([ Ok(AbstractSyntaxTree::with_statements([
Statement::Expression(Expression::exclusive_range( Statement::Expression(Expression::exclusive_range(
Expression::literal(LiteralExpression::Integer(0), (0, 1)), Expression::literal(0, (0, 1)),
Expression::literal(LiteralExpression::Integer(42), (3, 5)), Expression::literal(42, (3, 5)),
(0, 5) (0, 5)
)) ))
])) ]))
@ -1546,7 +1498,7 @@ mod tests {
Expression::operator( Expression::operator(
OperatorExpression::Assignment { OperatorExpression::Assignment {
assignee: Expression::identifier(Identifier::new("a"), (0, 1)), assignee: Expression::identifier(Identifier::new("a"), (0, 1)),
value: Expression::literal(LiteralExpression::Integer(1), (4, 5)), value: Expression::literal(1, (4, 5)),
}, },
(0, 5) (0, 5)
), ),
@ -1574,9 +1526,9 @@ mod tests {
OperatorExpression::Negation(Expression::grouped( OperatorExpression::Negation(Expression::grouped(
Expression::operator( Expression::operator(
OperatorExpression::Math { OperatorExpression::Math {
left: Expression::literal(LiteralExpression::Integer(1), (2, 3)), left: Expression::literal(1, (2, 3)),
operator: Node::new(MathOperator::Add, (4, 5)), operator: Node::new(MathOperator::Add, (4, 5)),
right: Expression::literal(LiteralExpression::Integer(1), (6, 7)), right: Expression::literal(1, (6, 7)),
}, },
(2, 7) (2, 7)
), ),
@ -1599,9 +1551,9 @@ mod tests {
OperatorExpression::Not(Expression::grouped( OperatorExpression::Not(Expression::grouped(
Expression::operator( Expression::operator(
OperatorExpression::Comparison { OperatorExpression::Comparison {
left: Expression::literal(LiteralExpression::Integer(1), (2, 3)), left: Expression::literal(1, (2, 3)),
operator: Node::new(ComparisonOperator::GreaterThan, (4, 5)), operator: Node::new(ComparisonOperator::GreaterThan, (4, 5)),
right: Expression::literal(LiteralExpression::Integer(42), (6, 8)), right: Expression::literal(42, (6, 8)),
}, },
(2, 8) (2, 8)
), ),
@ -1624,7 +1576,7 @@ mod tests {
Expression::operator( Expression::operator(
OperatorExpression::Assignment { OperatorExpression::Assignment {
assignee: Expression::identifier(Identifier::new("a"), (0, 1)), assignee: Expression::identifier(Identifier::new("a"), (0, 1)),
value: Expression::literal(LiteralExpression::Boolean(false), (4, 9)), value: Expression::literal(false, (4, 9)),
}, },
(0, 9) (0, 9)
), ),
@ -1756,10 +1708,7 @@ mod tests {
OperatorExpression::Comparison { OperatorExpression::Comparison {
left: Expression::identifier(Identifier::new("x"), (6, 7)), left: Expression::identifier(Identifier::new("x"), (6, 7)),
operator: Node::new(ComparisonOperator::LessThan, (8, 9)), operator: Node::new(ComparisonOperator::LessThan, (8, 9)),
right: Expression::literal( right: Expression::literal(10, (10, 12)),
LiteralExpression::Integer(10),
(10, 12)
),
}, },
(6, 12) (6, 12)
), ),
@ -1772,10 +1721,7 @@ mod tests {
(15, 16) (15, 16)
), ),
operator: Node::new(MathOperator::Add, (17, 19)), operator: Node::new(MathOperator::Add, (17, 19)),
modifier: Expression::literal( modifier: Expression::literal(1, (20, 21)),
LiteralExpression::Integer(1),
(20, 21)
),
}, },
(15, 21) (15, 21)
) )
@ -1801,7 +1747,7 @@ mod tests {
OperatorExpression::CompoundAssignment { OperatorExpression::CompoundAssignment {
assignee: Expression::identifier(Identifier::new("a"), (0, 1)), assignee: Expression::identifier(Identifier::new("a"), (0, 1)),
operator: Node::new(MathOperator::Add, (2, 4)), operator: Node::new(MathOperator::Add, (2, 4)),
modifier: Expression::literal(LiteralExpression::Integer(1), (5, 6)), modifier: Expression::literal(1, (5, 6)),
}, },
(0, 6) (0, 6)
)) ))
@ -1818,9 +1764,9 @@ mod tests {
Ok(AbstractSyntaxTree::with_statements([ Ok(AbstractSyntaxTree::with_statements([
Statement::Expression(Expression::operator( Statement::Expression(Expression::operator(
OperatorExpression::Logic { OperatorExpression::Logic {
left: Expression::literal(LiteralExpression::Boolean(true), (0, 4)), left: Expression::literal(true, (0, 4)),
operator: Node::new(LogicOperator::Or, (5, 7)), operator: Node::new(LogicOperator::Or, (5, 7)),
right: Expression::literal(LiteralExpression::Boolean(false), (8, 13)), right: Expression::literal(false, (8, 13)),
}, },
(0, 13) (0, 13)
)) ))
@ -1839,9 +1785,9 @@ mod tests {
Expression::block( Expression::block(
BlockExpression::Sync(vec![Statement::Expression(Expression::operator( BlockExpression::Sync(vec![Statement::Expression(Expression::operator(
OperatorExpression::Math { OperatorExpression::Math {
left: Expression::literal(LiteralExpression::Integer(40), (2, 4)), left: Expression::literal(40, (2, 4)),
operator: Node::new(MathOperator::Add, (5, 6)), operator: Node::new(MathOperator::Add, (5, 6)),
right: Expression::literal(LiteralExpression::Integer(2), (7, 8)), right: Expression::literal(2, (7, 8)),
}, },
(2, 8) (2, 8)
))]), ))]),
@ -1864,50 +1810,26 @@ mod tests {
Expression::block( Expression::block(
BlockExpression::Sync(vec![ BlockExpression::Sync(vec![
Statement::ExpressionNullified(Node::new( Statement::ExpressionNullified(Node::new(
Expression::operator( Expression::assignment(
OperatorExpression::Assignment { Expression::identifier("foo", (2, 5)),
assignee: Expression::identifier( Expression::literal(42, (8, 10)),
Identifier::new("foo"),
(2, 5)
),
value: Expression::literal(
LiteralExpression::Integer(42),
(8, 10)
),
},
(2, 10) (2, 10)
), ),
(2, 11) (2, 11)
),), )),
Statement::ExpressionNullified(Node::new( Statement::ExpressionNullified(Node::new(
Expression::operator( Expression::assignment(
OperatorExpression::Assignment { Expression::identifier("bar", (12, 15)),
assignee: Expression::identifier( Expression::literal(42, (18, 20)),
Identifier::new("bar"),
(12, 15)
),
value: Expression::literal(
LiteralExpression::Integer(42),
(18, 20)
),
},
(12, 20) (12, 20)
), ),
(12, 21) (12, 21)
),),
Statement::Expression(Expression::operator(
OperatorExpression::Assignment {
assignee: Expression::identifier(
Identifier::new("baz"),
(22, 25)
),
value: Expression::literal(
LiteralExpression::String("42".to_string()),
(28, 32)
),
},
(22, 32)
)), )),
Statement::Expression(Expression::assignment(
Expression::identifier("baz", (22, 25)),
Expression::literal("42", (28, 32)),
(22, 32)
))
]), ]),
(0, 34) (0, 34)
), ),
@ -1924,13 +1846,11 @@ mod tests {
assert_eq!( assert_eq!(
parse(source), parse(source),
Ok(AbstractSyntaxTree::with_statements([ Ok(AbstractSyntaxTree::with_statements([
Statement::Expression(Expression::operator( Statement::Expression(Expression::comparison(
OperatorExpression::Comparison { Expression::literal(42, (0, 2)),
left: Expression::literal(LiteralExpression::Integer(42), (0, 2)), Node::new(ComparisonOperator::Equal, (3, 5)),
operator: Node::new(ComparisonOperator::Equal, (3, 5)), Expression::literal(42, (6, 8)),
right: Expression::literal(LiteralExpression::Integer(42), (6, 8)), (0, 8)
},
(0, 8),
)) ))
])) ]))
); );
@ -1943,13 +1863,11 @@ mod tests {
assert_eq!( assert_eq!(
parse(source), parse(source),
Ok(AbstractSyntaxTree::with_statements([ Ok(AbstractSyntaxTree::with_statements([
Statement::Expression(Expression::operator( Statement::Expression(Expression::comparison(
OperatorExpression::Comparison { Expression::literal(1, (0, 1)),
left: Expression::literal(LiteralExpression::Integer(1), (0, 1)), Node::new(ComparisonOperator::LessThan, (2, 3)),
operator: Node::new(ComparisonOperator::LessThan, (2, 3)), Expression::literal(2, (4, 5)),
right: Expression::literal(LiteralExpression::Integer(2), (4, 5)), (0, 5)
},
(0, 5),
)) ))
])) ]))
); );
@ -1962,13 +1880,11 @@ mod tests {
assert_eq!( assert_eq!(
parse(source), parse(source),
Ok(AbstractSyntaxTree::with_statements([ Ok(AbstractSyntaxTree::with_statements([
Statement::Expression(Expression::operator( Statement::Expression(Expression::comparison(
OperatorExpression::Comparison { Expression::literal(1, (0, 1)),
left: Expression::literal(LiteralExpression::Integer(1), (0, 1)), Node::new(ComparisonOperator::LessThanOrEqual, (2, 4)),
operator: Node::new(ComparisonOperator::LessThanOrEqual, (2, 4)), Expression::literal(2, (5, 6)),
right: Expression::literal(LiteralExpression::Integer(2), (5, 6)), (0, 6)
},
(0, 6),
)) ))
])) ]))
); );
@ -1981,13 +1897,11 @@ mod tests {
assert_eq!( assert_eq!(
parse(source), parse(source),
Ok(AbstractSyntaxTree::with_statements([ Ok(AbstractSyntaxTree::with_statements([
Statement::Expression(Expression::operator( Statement::Expression(Expression::comparison(
OperatorExpression::Comparison { Expression::literal(1, (0, 1)),
left: Expression::literal(LiteralExpression::Integer(1), (0, 1)), Node::new(ComparisonOperator::GreaterThanOrEqual, (2, 4)),
operator: Node::new(ComparisonOperator::GreaterThanOrEqual, (2, 4)), Expression::literal(2, (5, 6)),
right: Expression::literal(LiteralExpression::Integer(2), (5, 6)), (0, 6)
},
(0, 6),
)) ))
])) ]))
); );
@ -2000,13 +1914,11 @@ mod tests {
assert_eq!( assert_eq!(
parse(source), parse(source),
Ok(AbstractSyntaxTree::with_statements([ Ok(AbstractSyntaxTree::with_statements([
Statement::Expression(Expression::operator( Statement::Expression(Expression::comparison(
OperatorExpression::Comparison { Expression::literal(1, (0, 1)),
left: Expression::literal(LiteralExpression::Integer(1), (0, 1)), Node::new(ComparisonOperator::GreaterThan, (2, 3)),
operator: Node::new(ComparisonOperator::GreaterThan, (2, 3)), Expression::literal(2, (4, 5)),
right: Expression::literal(LiteralExpression::Integer(2), (4, 5)), (0, 5)
},
(0, 5),
)) ))
])) ]))
); );
@ -2019,13 +1931,11 @@ mod tests {
assert_eq!( assert_eq!(
parse(source), parse(source),
Ok(AbstractSyntaxTree::with_statements([ Ok(AbstractSyntaxTree::with_statements([
Statement::Expression(Expression::operator( Statement::Expression(Expression::math(
OperatorExpression::Math { Expression::literal(-1, (0, 2)),
left: Expression::literal(LiteralExpression::Integer(-1), (0, 2)), MathOperator::subtract((3, 4)),
operator: Node::new(MathOperator::Subtract, (3, 4)), Expression::literal(-2, (5, 7)),
right: Expression::literal(LiteralExpression::Integer(-2), (5, 7)), (0, 7)
},
(0, 7),
)) ))
])) ]))
); );
@ -2038,13 +1948,11 @@ mod tests {
assert_eq!( assert_eq!(
parse(source), parse(source),
Ok(AbstractSyntaxTree::with_statements([ Ok(AbstractSyntaxTree::with_statements([
Statement::Expression(Expression::operator( Statement::Expression(Expression::math(
OperatorExpression::Math { Expression::literal(42, (0, 2)),
left: Expression::literal(LiteralExpression::Integer(42), (0, 2)), MathOperator::modulo((3, 4)),
operator: Node::new(MathOperator::Modulo, (3, 4)), Expression::literal(2, (5, 6)),
right: Expression::literal(LiteralExpression::Integer(2), (5, 6)), (0, 6)
},
(0, 6),
)) ))
])) ]))
); );
@ -2057,13 +1965,11 @@ mod tests {
assert_eq!( assert_eq!(
parse(source), parse(source),
Ok(AbstractSyntaxTree::with_statements([ Ok(AbstractSyntaxTree::with_statements([
Statement::Expression(Expression::operator( Statement::Expression(Expression::math(
OperatorExpression::Math { Expression::literal(42, (0, 2)),
left: Expression::literal(LiteralExpression::Integer(42), (0, 2)), MathOperator::divide((3, 4)),
operator: Node::new(MathOperator::Divide, (3, 4)), Expression::literal(2, (5, 6)),
right: Expression::literal(LiteralExpression::Integer(2), (5, 6)), (0, 6)
},
(0, 6),
)) ))
])) ]))
); );
@ -2076,18 +1982,10 @@ mod tests {
assert_eq!( assert_eq!(
parse(source), parse(source),
Ok(AbstractSyntaxTree::with_statements([ Ok(AbstractSyntaxTree::with_statements([
Statement::Expression(Expression::operator( Statement::Expression(Expression::math(
OperatorExpression::Math { Expression::literal("Hello, ", (0, 9)),
left: Expression::literal( MathOperator::add((10, 11)),
LiteralExpression::String("Hello, ".to_string()), Expression::literal("World!", (12, 20)),
(0, 9)
),
operator: Node::new(MathOperator::Add, (10, 11)),
right: Expression::literal(
LiteralExpression::String("World!".to_string()),
(12, 20)
)
},
(0, 20) (0, 20)
)) ))
])) ]))
@ -2101,10 +1999,7 @@ mod tests {
assert_eq!( assert_eq!(
parse(source), parse(source),
Ok(AbstractSyntaxTree::with_statements([ Ok(AbstractSyntaxTree::with_statements([
Statement::Expression(Expression::literal( Statement::Expression(Expression::literal("Hello, World!", (0, 15)))
LiteralExpression::String("Hello, World!".to_string()),
(0, 15)
))
])) ]))
); );
} }
@ -2116,10 +2011,7 @@ mod tests {
assert_eq!( assert_eq!(
parse(source), parse(source),
Ok(AbstractSyntaxTree::with_statements([ Ok(AbstractSyntaxTree::with_statements([
Statement::Expression(Expression::literal( Statement::Expression(Expression::literal(true, (0, 4)))
LiteralExpression::Boolean(true),
(0, 4)
))
])) ]))
); );
} }
@ -2132,17 +2024,15 @@ mod tests {
parse(source), parse(source),
Ok(AbstractSyntaxTree::with_statements([ Ok(AbstractSyntaxTree::with_statements([
Statement::Expression(Expression::list_index( Statement::Expression(Expression::list_index(
ListIndexExpression { Expression::list(
list: Expression::list( [
ListExpression::Ordered(vec![ Expression::literal(1, (1, 2)),
Expression::literal(LiteralExpression::Integer(1), (1, 2)), Expression::literal(2, (4, 5)),
Expression::literal(LiteralExpression::Integer(2), (4, 5)), Expression::literal(3, (7, 8)),
Expression::literal(LiteralExpression::Integer(3), (7, 8)), ],
]), (0, 9)
(0, 9) ),
), Expression::literal(0, (10, 11)),
index: Expression::literal(LiteralExpression::Integer(0), (10, 11)),
},
(0, 12) (0, 12)
)) ))
])) ]))
@ -2173,41 +2063,29 @@ mod tests {
parse(source), parse(source),
Ok(AbstractSyntaxTree::with_statements([ Ok(AbstractSyntaxTree::with_statements([
Statement::Expression(Expression::list( Statement::Expression(Expression::list(
ListExpression::Ordered(vec![ [
Expression::literal(LiteralExpression::Integer(1), (1, 2)), Expression::literal(1, (1, 2)),
Expression::operator( Expression::math(
OperatorExpression::Math { Expression::literal(1, (4, 5)),
left: Expression::literal(LiteralExpression::Integer(1), (4, 5)), MathOperator::add((6, 7)),
operator: Node::new(MathOperator::Add, (6, 7)), Expression::literal(1, (8, 9)),
right: Expression::literal(LiteralExpression::Integer(1), (8, 9)),
},
(4, 9) (4, 9)
), ),
Expression::operator( Expression::math(
OperatorExpression::Math { Expression::literal(2, (11, 12)),
left: Expression::literal(LiteralExpression::Integer(2), (11, 12)), Node::new(MathOperator::Add, (13, 14)),
operator: Node::new(MathOperator::Add, (13, 14)), Expression::grouped(
right: Expression::grouped( Expression::math(
Expression::operator( Expression::literal(4, (16, 17)),
OperatorExpression::Math { MathOperator::multiply((18, 19)),
left: Expression::literal( Expression::literal(10, (20, 22)),
LiteralExpression::Integer(4), (16, 22)
(16, 17)
),
operator: Node::new(MathOperator::Multiply, (18, 19)),
right: Expression::literal(
LiteralExpression::Integer(10),
(20, 22)
),
},
(16, 22)
),
(15, 23)
), ),
}, (15, 23)
),
(11, 23) (11, 23)
) ),
]), ],
(0, 24) (0, 24)
)) ))
])) ]))
@ -2222,10 +2100,10 @@ mod tests {
parse(source), parse(source),
Ok(AbstractSyntaxTree::with_statements([ Ok(AbstractSyntaxTree::with_statements([
Statement::Expression(Expression::list( Statement::Expression(Expression::list(
ListExpression::Ordered(vec![ [
Expression::literal(LiteralExpression::Integer(1), (1, 2)), Expression::literal(1, (1, 2)),
Expression::literal(LiteralExpression::Integer(2), (4, 5)) Expression::literal(2, (4, 5))
]), ],
(0, 6) (0, 6)
)) ))
])) ]))
@ -2239,7 +2117,7 @@ mod tests {
assert_eq!( assert_eq!(
parse(source), parse(source),
Ok(AbstractSyntaxTree::with_statements([ Ok(AbstractSyntaxTree::with_statements([
Statement::Expression(Expression::list(ListExpression::Ordered(vec![]), (0, 2))) Statement::Expression(Expression::list(vec![], (0, 2)))
])) ]))
); );
} }
@ -2251,7 +2129,7 @@ mod tests {
assert_eq!( assert_eq!(
parse(source), parse(source),
Ok(AbstractSyntaxTree::with_statements([ Ok(AbstractSyntaxTree::with_statements([
Statement::Expression(Expression::literal(LiteralExpression::Float(42.0), (0, 4))) Statement::Expression(Expression::literal(42.0, (0, 4)))
])) ]))
); );
} }
@ -2263,12 +2141,10 @@ mod tests {
assert_eq!( assert_eq!(
parse(source), parse(source),
Ok(AbstractSyntaxTree::with_statements([ Ok(AbstractSyntaxTree::with_statements([
Statement::Expression(Expression::operator( Statement::Expression(Expression::math(
OperatorExpression::Math { Expression::literal(1, (0, 1)),
left: Expression::literal(LiteralExpression::Integer(1), (0, 1)), MathOperator::add((2, 3)),
operator: Node::new(MathOperator::Add, (2, 3)), Expression::literal(2, (4, 5)),
right: Expression::literal(LiteralExpression::Integer(2), (4, 5))
},
(0, 5) (0, 5)
)) ))
])) ]))
@ -2282,14 +2158,12 @@ mod tests {
assert_eq!( assert_eq!(
parse(source), parse(source),
Ok(AbstractSyntaxTree::with_statements([ Ok(AbstractSyntaxTree::with_statements([
Statement::Expression(Expression::operator( Statement::Expression(Expression::math(
OperatorExpression::Math { Expression::literal(1, (0, 1)),
left: Expression::literal(LiteralExpression::Integer(1), (0, 1)), MathOperator::multiply((2, 3)),
operator: Node::new(MathOperator::Multiply, (2, 3)), Expression::literal(2, (4, 5)),
right: Expression::literal(LiteralExpression::Integer(2), (4, 5))
},
(0, 5) (0, 5)
)) ),)
])) ]))
); );
} }
@ -2301,21 +2175,17 @@ mod tests {
assert_eq!( assert_eq!(
parse(source), parse(source),
Ok(AbstractSyntaxTree::with_statements([ Ok(AbstractSyntaxTree::with_statements([
Statement::Expression(Expression::operator( Statement::Expression(Expression::math(
OperatorExpression::Math { Expression::literal(1, (0, 1)),
left: Expression::literal(LiteralExpression::Integer(1), (0, 1)), MathOperator::add((2, 3)),
operator: Node::new(MathOperator::Add, (2, 3)), Expression::math(
right: Expression::operator( Expression::literal(2, (4, 5)),
OperatorExpression::Math { MathOperator::multiply((6, 7)),
left: Expression::literal(LiteralExpression::Integer(2), (4, 5)), Expression::literal(3, (8, 9)),
operator: Node::new(MathOperator::Multiply, (6, 7)), (4, 9)
right: Expression::literal(LiteralExpression::Integer(3), (8, 9)) ),
},
(4, 9)
)
},
(0, 9) (0, 9)
)) )),
])) ]))
); );
} }
@ -2327,31 +2197,19 @@ mod tests {
assert_eq!( assert_eq!(
parse(source), parse(source),
Ok(AbstractSyntaxTree::with_statements([ Ok(AbstractSyntaxTree::with_statements([
Statement::Expression(Expression::operator( Statement::Expression(Expression::assignment(
OperatorExpression::Assignment { Expression::identifier("a", (0, 1)),
assignee: Expression::identifier(Identifier::new("a"), (0, 1)), Expression::math(
value: Expression::operator( Expression::literal(1, (4, 5)),
OperatorExpression::Math { MathOperator::add((6, 7)),
left: Expression::literal(LiteralExpression::Integer(1), (4, 5)), Expression::math(
operator: Node::new(MathOperator::Add, (6, 7)), Expression::literal(2, (8, 9)),
right: Expression::operator( MathOperator::multiply((10, 11)),
OperatorExpression::Math { Expression::literal(3, (12, 13)),
left: Expression::literal( (8, 13)
LiteralExpression::Integer(2), ),
(8, 9) (4, 13)
), ),
operator: Node::new(MathOperator::Multiply, (10, 11)),
right: Expression::literal(
LiteralExpression::Integer(3),
(12, 13)
)
},
(8, 13)
)
},
(4, 13)
)
},
(0, 13) (0, 13)
)) ))
])) ]))

View File

@ -3,6 +3,10 @@ use std::fmt::{self, Display, Formatter};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
pub struct Raw<'src> {
data: &'src str,
}
/// Source code token. /// Source code token.
#[derive(Debug, Serialize, Deserialize, PartialEq)] #[derive(Debug, Serialize, Deserialize, PartialEq)]
pub enum Token<'src> { pub enum Token<'src> {

View File

@ -7,6 +7,7 @@
use std::{ use std::{
collections::HashMap, collections::HashMap,
fmt::{self, Display, Formatter}, fmt::{self, Display, Formatter},
primitive,
sync::{Arc, Mutex}, sync::{Arc, Mutex},
}; };
@ -17,7 +18,8 @@ use crate::{
AbstractSyntaxTree, BlockExpression, CallExpression, ComparisonOperator, ElseExpression, AbstractSyntaxTree, BlockExpression, CallExpression, ComparisonOperator, ElseExpression,
FieldAccessExpression, IfExpression, LetStatement, ListExpression, ListIndexExpression, FieldAccessExpression, IfExpression, LetStatement, ListExpression, ListIndexExpression,
LiteralExpression, LogicOperator, LoopExpression, MapExpression, MathOperator, Node, LiteralExpression, LogicOperator, LoopExpression, MapExpression, MathOperator, Node,
OperatorExpression, RangeExpression, Span, Statement, StructDefinition, StructExpression, OperatorExpression, PrimitiveValueExpression, RangeExpression, RawStringExpression, Span,
Statement, StructDefinition, StructExpression,
}, },
parse, Analyzer, BuiltInFunctionError, Context, DustError, Expression, FieldsStructType, parse, Analyzer, BuiltInFunctionError, Context, DustError, Expression, FieldsStructType,
Identifier, ParseError, Struct, StructType, TupleType, Type, Value, ValueError, Identifier, ParseError, Struct, StructType, TupleType, Type, Value, ValueError,
@ -617,10 +619,13 @@ impl Vm {
fn run_literal(&self, literal: LiteralExpression) -> Result<Evaluation, RuntimeError> { fn run_literal(&self, literal: LiteralExpression) -> Result<Evaluation, RuntimeError> {
let value = match literal { let value = match literal {
LiteralExpression::Boolean(boolean) => Value::Boolean(boolean), LiteralExpression::String(RawStringExpression(string)) => Value::String(string),
LiteralExpression::Float(float) => Value::Float(float), LiteralExpression::Primitive(primitive_expression) => match primitive_expression {
LiteralExpression::Integer(integer) => Value::Integer(integer), PrimitiveValueExpression::Boolean(boolean) => Value::Boolean(boolean),
LiteralExpression::String(string) => Value::String(string), PrimitiveValueExpression::Character(character) => Value::Character(character),
PrimitiveValueExpression::Integer(integer) => Value::Integer(integer),
PrimitiveValueExpression::Float(float) => Value::Float(float),
},
}; };
Ok(Evaluation::Return(Some(value))) Ok(Evaluation::Return(Some(value)))