Begin implementing functions
This commit is contained in:
parent
b7288ceed8
commit
56fbbdee0b
@ -6,11 +6,11 @@ use crate::{
|
||||
use super::{AbstractTree, Action, Identifier, Statement, Type};
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
|
||||
pub struct Assignment<'src> {
|
||||
pub struct Assignment {
|
||||
identifier: Identifier,
|
||||
r#type: Option<Type>,
|
||||
operator: AssignmentOperator,
|
||||
statement: Box<Statement<'src>>,
|
||||
statement: Box<Statement>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
|
||||
@ -20,12 +20,12 @@ pub enum AssignmentOperator {
|
||||
SubAssign,
|
||||
}
|
||||
|
||||
impl<'src> Assignment<'src> {
|
||||
impl Assignment {
|
||||
pub fn new(
|
||||
identifier: Identifier,
|
||||
r#type: Option<Type>,
|
||||
operator: AssignmentOperator,
|
||||
statement: Statement<'src>,
|
||||
statement: Statement,
|
||||
) -> Self {
|
||||
Self {
|
||||
identifier,
|
||||
@ -36,7 +36,7 @@ impl<'src> Assignment<'src> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'src> AbstractTree for Assignment<'src> {
|
||||
impl AbstractTree for Assignment {
|
||||
fn expected_type(&self, _context: &Context) -> Result<Type, ValidationError> {
|
||||
todo!()
|
||||
}
|
||||
|
@ -7,17 +7,17 @@ use crate::{
|
||||
use super::{AbstractTree, Action, Statement, Type};
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
|
||||
pub struct Block<'src> {
|
||||
statements: Vec<Statement<'src>>,
|
||||
pub struct Block {
|
||||
statements: Vec<Statement>,
|
||||
}
|
||||
|
||||
impl<'src> Block<'src> {
|
||||
pub fn new(statements: Vec<Statement<'src>>) -> Self {
|
||||
impl Block {
|
||||
pub fn new(statements: Vec<Statement>) -> Self {
|
||||
Self { statements }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'src> AbstractTree for Block<'src> {
|
||||
impl AbstractTree for Block {
|
||||
fn expected_type(&self, _context: &Context) -> Result<Type, ValidationError> {
|
||||
let final_statement = self.statements.last().unwrap();
|
||||
|
||||
@ -70,7 +70,7 @@ mod tests {
|
||||
#[test]
|
||||
fn expected_type_returns_type_of_final_statement() {
|
||||
let block = Block::new(vec![
|
||||
Statement::Expression(Expression::Value(ValueNode::String("42"))),
|
||||
Statement::Expression(Expression::Value(ValueNode::String("42".to_string()))),
|
||||
Statement::Expression(Expression::Value(ValueNode::Integer(42))),
|
||||
]);
|
||||
|
||||
|
@ -6,15 +6,15 @@ use crate::{
|
||||
use super::{AbstractTree, Action, Identifier, Index, Logic, Math, Type, ValueNode};
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
|
||||
pub enum Expression<'src> {
|
||||
pub enum Expression {
|
||||
Identifier(Identifier),
|
||||
Index(Box<Index<'src>>),
|
||||
Logic(Box<Logic<'src>>),
|
||||
Math(Box<Math<'src>>),
|
||||
Value(ValueNode<'src>),
|
||||
Index(Box<Index>),
|
||||
Logic(Box<Logic>),
|
||||
Math(Box<Math>),
|
||||
Value(ValueNode),
|
||||
}
|
||||
|
||||
impl<'src> AbstractTree for Expression<'src> {
|
||||
impl AbstractTree for Expression {
|
||||
fn expected_type(&self, _context: &Context) -> Result<Type, ValidationError> {
|
||||
match self {
|
||||
Expression::Identifier(identifier) => identifier.expected_type(_context),
|
||||
|
@ -6,17 +6,17 @@ use crate::{
|
||||
use super::{AbstractTree, Action, Expression, Statement, Type};
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
|
||||
pub struct IfElse<'src> {
|
||||
if_expression: Expression<'src>,
|
||||
if_statement: Box<Statement<'src>>,
|
||||
else_statement: Option<Box<Statement<'src>>>,
|
||||
pub struct IfElse {
|
||||
if_expression: Expression,
|
||||
if_statement: Box<Statement>,
|
||||
else_statement: Option<Box<Statement>>,
|
||||
}
|
||||
|
||||
impl<'src> IfElse<'src> {
|
||||
impl IfElse {
|
||||
pub fn new(
|
||||
if_expression: Expression<'src>,
|
||||
if_statement: Statement<'src>,
|
||||
else_statement: Option<Statement<'src>>,
|
||||
if_expression: Expression,
|
||||
if_statement: Statement,
|
||||
else_statement: Option<Statement>,
|
||||
) -> Self {
|
||||
Self {
|
||||
if_expression,
|
||||
@ -26,7 +26,7 @@ impl<'src> IfElse<'src> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'src> AbstractTree for IfElse<'src> {
|
||||
impl AbstractTree for IfElse {
|
||||
fn expected_type(&self, _context: &Context) -> Result<Type, ValidationError> {
|
||||
Ok(Type::None)
|
||||
}
|
||||
@ -71,7 +71,7 @@ mod tests {
|
||||
assert_eq!(
|
||||
IfElse::new(
|
||||
Expression::Value(ValueNode::Boolean(true)),
|
||||
Statement::Expression(Expression::Value(ValueNode::String("foo"))),
|
||||
Statement::Expression(Expression::Value(ValueNode::String("foo".to_string()))),
|
||||
None
|
||||
)
|
||||
.run(&Context::new()),
|
||||
@ -84,9 +84,9 @@ mod tests {
|
||||
assert_eq!(
|
||||
IfElse::new(
|
||||
Expression::Value(ValueNode::Boolean(false)),
|
||||
Statement::Expression(Expression::Value(ValueNode::String("foo"))),
|
||||
Statement::Expression(Expression::Value(ValueNode::String("foo".to_string()))),
|
||||
Some(Statement::Expression(Expression::Value(ValueNode::String(
|
||||
"bar"
|
||||
"bar".to_string()
|
||||
))))
|
||||
)
|
||||
.run(&Context::new()),
|
||||
|
@ -7,18 +7,18 @@ use crate::{
|
||||
use super::{AbstractTree, Action, Expression, Type, ValueNode};
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
|
||||
pub struct Index<'src> {
|
||||
left: Expression<'src>,
|
||||
right: Expression<'src>,
|
||||
pub struct Index {
|
||||
left: Expression,
|
||||
right: Expression,
|
||||
}
|
||||
|
||||
impl<'src> Index<'src> {
|
||||
pub fn new(left: Expression<'src>, right: Expression<'src>) -> Self {
|
||||
impl Index {
|
||||
pub fn new(left: Expression, right: Expression) -> Self {
|
||||
Self { left, right }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'src> AbstractTree for Index<'src> {
|
||||
impl AbstractTree for Index {
|
||||
fn expected_type(&self, _context: &Context) -> Result<Type, ValidationError> {
|
||||
let left_type = self.left.expected_type(_context)?;
|
||||
|
||||
|
@ -7,19 +7,19 @@ use crate::{
|
||||
use super::{AbstractTree, Action, Expression, Type};
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
|
||||
pub enum Logic<'src> {
|
||||
Equal(Expression<'src>, Expression<'src>),
|
||||
NotEqual(Expression<'src>, Expression<'src>),
|
||||
Greater(Expression<'src>, Expression<'src>),
|
||||
Less(Expression<'src>, Expression<'src>),
|
||||
GreaterOrEqual(Expression<'src>, Expression<'src>),
|
||||
LessOrEqual(Expression<'src>, Expression<'src>),
|
||||
And(Expression<'src>, Expression<'src>),
|
||||
Or(Expression<'src>, Expression<'src>),
|
||||
Not(Expression<'src>),
|
||||
pub enum Logic {
|
||||
Equal(Expression, Expression),
|
||||
NotEqual(Expression, Expression),
|
||||
Greater(Expression, Expression),
|
||||
Less(Expression, Expression),
|
||||
GreaterOrEqual(Expression, Expression),
|
||||
LessOrEqual(Expression, Expression),
|
||||
And(Expression, Expression),
|
||||
Or(Expression, Expression),
|
||||
Not(Expression),
|
||||
}
|
||||
|
||||
impl<'src> AbstractTree for Logic<'src> {
|
||||
impl AbstractTree for Logic {
|
||||
fn expected_type(&self, _context: &Context) -> Result<Type, ValidationError> {
|
||||
Ok(Type::Boolean)
|
||||
}
|
||||
|
@ -6,17 +6,17 @@ use crate::{
|
||||
use super::{AbstractTree, Action, Statement, Type};
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
|
||||
pub struct Loop<'src> {
|
||||
statements: Vec<Statement<'src>>,
|
||||
pub struct Loop {
|
||||
statements: Vec<Statement>,
|
||||
}
|
||||
|
||||
impl<'src> Loop<'src> {
|
||||
pub fn new(statements: Vec<Statement<'src>>) -> Self {
|
||||
impl Loop {
|
||||
pub fn new(statements: Vec<Statement>) -> Self {
|
||||
Self { statements }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'src> AbstractTree for Loop<'src> {
|
||||
impl AbstractTree for Loop {
|
||||
fn expected_type(&self, _context: &Context) -> Result<Type, ValidationError> {
|
||||
Ok(Type::None)
|
||||
}
|
||||
|
@ -8,15 +8,15 @@ use crate::{
|
||||
use super::{AbstractTree, Action, Expression, Type};
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
|
||||
pub enum Math<'src> {
|
||||
Add(Expression<'src>, Expression<'src>),
|
||||
Subtract(Expression<'src>, Expression<'src>),
|
||||
Multiply(Expression<'src>, Expression<'src>),
|
||||
Divide(Expression<'src>, Expression<'src>),
|
||||
Modulo(Expression<'src>, Expression<'src>),
|
||||
pub enum Math {
|
||||
Add(Expression, Expression),
|
||||
Subtract(Expression, Expression),
|
||||
Multiply(Expression, Expression),
|
||||
Divide(Expression, Expression),
|
||||
Modulo(Expression, Expression),
|
||||
}
|
||||
|
||||
impl<'src> AbstractTree for Math<'src> {
|
||||
impl AbstractTree for Math {
|
||||
fn expected_type(&self, _context: &Context) -> Result<Type, ValidationError> {
|
||||
match self {
|
||||
Math::Add(left, _)
|
||||
|
@ -6,16 +6,16 @@ use crate::{
|
||||
use super::{AbstractTree, Action, Assignment, Block, Expression, IfElse, Loop, Type};
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
|
||||
pub enum Statement<'src> {
|
||||
Assignment(Assignment<'src>),
|
||||
Block(Block<'src>),
|
||||
Break(Expression<'src>),
|
||||
Expression(Expression<'src>),
|
||||
IfElse(IfElse<'src>),
|
||||
Loop(Loop<'src>),
|
||||
pub enum Statement {
|
||||
Assignment(Assignment),
|
||||
Block(Block),
|
||||
Break(Expression),
|
||||
Expression(Expression),
|
||||
IfElse(IfElse),
|
||||
Loop(Loop),
|
||||
}
|
||||
|
||||
impl<'src> AbstractTree for Statement<'src> {
|
||||
impl AbstractTree for Statement {
|
||||
fn expected_type(&self, _context: &Context) -> Result<Type, ValidationError> {
|
||||
match self {
|
||||
Statement::Assignment(assignment) => assignment.expected_type(_context),
|
||||
|
@ -6,21 +6,26 @@ use crate::{
|
||||
Value,
|
||||
};
|
||||
|
||||
use super::{AbstractTree, Action, Expression, Identifier, Type};
|
||||
use super::{AbstractTree, Action, Expression, Identifier, Statement, Type};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum ValueNode<'src> {
|
||||
pub enum ValueNode {
|
||||
Boolean(bool),
|
||||
Float(f64),
|
||||
Integer(i64),
|
||||
List(Vec<Expression<'src>>),
|
||||
Map(Vec<(Identifier, Option<Type>, Expression<'src>)>),
|
||||
List(Vec<Expression>),
|
||||
Map(Vec<(Identifier, Option<Type>, Expression)>),
|
||||
Range(Range<i64>),
|
||||
String(&'src str),
|
||||
String(String),
|
||||
Enum(Identifier, Identifier),
|
||||
Function {
|
||||
parameters: Vec<(Identifier, Type)>,
|
||||
return_type: Type,
|
||||
body: Box<Statement>,
|
||||
},
|
||||
}
|
||||
|
||||
impl<'src> AbstractTree for ValueNode<'src> {
|
||||
impl AbstractTree for ValueNode {
|
||||
fn expected_type(&self, _context: &Context) -> Result<Type, ValidationError> {
|
||||
let r#type = match self {
|
||||
ValueNode::Boolean(_) => Type::Boolean,
|
||||
@ -39,6 +44,7 @@ impl<'src> AbstractTree for ValueNode<'src> {
|
||||
ValueNode::Range(_) => Type::Range,
|
||||
ValueNode::String(_) => Type::String,
|
||||
ValueNode::Enum(name, _) => Type::Custom(name.clone()),
|
||||
ValueNode::Function { .. } => todo!(),
|
||||
};
|
||||
|
||||
Ok(r#type)
|
||||
@ -84,21 +90,26 @@ impl<'src> AbstractTree for ValueNode<'src> {
|
||||
Value::r#enum(name, variant)
|
||||
}
|
||||
}
|
||||
ValueNode::Function {
|
||||
parameters,
|
||||
return_type,
|
||||
body,
|
||||
} => Value::function(parameters, return_type, *body),
|
||||
};
|
||||
|
||||
Ok(Action::Return(value))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'src> Eq for ValueNode<'src> {}
|
||||
impl Eq for ValueNode {}
|
||||
|
||||
impl<'src> PartialOrd for ValueNode<'src> {
|
||||
impl PartialOrd for ValueNode {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'src> Ord for ValueNode<'src> {
|
||||
impl Ord for ValueNode {
|
||||
fn cmp(&self, other: &Self) -> Ordering {
|
||||
use ValueNode::*;
|
||||
|
||||
@ -135,6 +146,33 @@ impl<'src> Ord for ValueNode<'src> {
|
||||
}
|
||||
}
|
||||
(Enum(_, _), _) => Ordering::Greater,
|
||||
(
|
||||
Function {
|
||||
parameters: left_parameters,
|
||||
return_type: left_return,
|
||||
body: left_body,
|
||||
},
|
||||
Function {
|
||||
parameters: right_parameters,
|
||||
return_type: right_return,
|
||||
body: right_body,
|
||||
},
|
||||
) => {
|
||||
let parameter_cmp = left_parameters.cmp(right_parameters);
|
||||
|
||||
if parameter_cmp.is_eq() {
|
||||
let return_cmp = left_return.cmp(right_return);
|
||||
|
||||
if return_cmp.is_eq() {
|
||||
left_body.cmp(right_body)
|
||||
} else {
|
||||
return_cmp
|
||||
}
|
||||
} else {
|
||||
parameter_cmp
|
||||
}
|
||||
}
|
||||
(Function { .. }, _) => Ordering::Greater,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ pub type DustParser<'src> = Boxed<
|
||||
'src,
|
||||
'src,
|
||||
ParserInput<'src>,
|
||||
Vec<(Statement<'src>, SimpleSpan)>,
|
||||
Vec<(Statement, SimpleSpan)>,
|
||||
extra::Err<Rich<'src, Token<'src>, SimpleSpan>>,
|
||||
>;
|
||||
|
||||
@ -21,7 +21,7 @@ pub type ParserInput<'src> =
|
||||
|
||||
pub fn parse<'src>(
|
||||
tokens: &'src [(Token<'src>, SimpleSpan)],
|
||||
) -> Result<Vec<(Statement<'src>, SimpleSpan)>, Vec<Error>> {
|
||||
) -> Result<Vec<(Statement, SimpleSpan)>, Vec<Error>> {
|
||||
parser()
|
||||
.parse(tokens.spanned((tokens.len()..tokens.len()).into()))
|
||||
.into_result()
|
||||
@ -85,7 +85,7 @@ pub fn parser<'src>() -> DustParser<'src> {
|
||||
Token::Boolean(boolean) => ValueNode::Boolean(boolean),
|
||||
Token::Integer(integer) => ValueNode::Integer(integer),
|
||||
Token::Float(float) => ValueNode::Float(float),
|
||||
Token::String(string) => ValueNode::String(string),
|
||||
Token::String(string) => ValueNode::String(string.to_string()),
|
||||
}
|
||||
.map(|value| Expression::Value(value))
|
||||
.boxed();
|
||||
@ -221,6 +221,7 @@ pub fn parser<'src>() -> DustParser<'src> {
|
||||
.map(|expression| Statement::Break(expression));
|
||||
|
||||
let assignment = identifier
|
||||
.clone()
|
||||
.then(type_specification.clone().or_not())
|
||||
.then(choice((
|
||||
just(Token::Operator(Operator::Assign)).to(AssignmentOperator::Assign),
|
||||
@ -269,6 +270,25 @@ pub fn parser<'src>() -> DustParser<'src> {
|
||||
})
|
||||
.boxed();
|
||||
|
||||
let function = identifier
|
||||
.clone()
|
||||
.then(type_specification.clone())
|
||||
.separated_by(just(Token::Control(Control::Comma)))
|
||||
.collect::<Vec<(Identifier, Type)>>()
|
||||
.delimited_by(
|
||||
just(Token::Control(Control::ParenOpen)),
|
||||
just(Token::Control(Control::ParenClose)),
|
||||
)
|
||||
.then(type_specification)
|
||||
.then(statement.clone())
|
||||
.map(|((parameters, return_type), body)| {
|
||||
Statement::Expression(Expression::Value(ValueNode::Function {
|
||||
parameters,
|
||||
return_type,
|
||||
body: Box::new(body),
|
||||
}))
|
||||
});
|
||||
|
||||
choice((
|
||||
assignment,
|
||||
expression_statement,
|
||||
@ -276,6 +296,7 @@ pub fn parser<'src>() -> DustParser<'src> {
|
||||
block,
|
||||
r#loop,
|
||||
if_else,
|
||||
function,
|
||||
))
|
||||
.then_ignore(just(Token::Control(Control::Semicolon)).or_not())
|
||||
});
|
||||
@ -293,13 +314,27 @@ mod tests {
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn function() {
|
||||
assert_eq!(
|
||||
parse(&lex("(x: int): int x ").unwrap()).unwrap()[0].0,
|
||||
Statement::Expression(Expression::Value(ValueNode::Function {
|
||||
parameters: vec![(Identifier::new("x"), Type::Integer)],
|
||||
return_type: Type::Integer,
|
||||
body: Box::new(Statement::Expression(Expression::Identifier(
|
||||
Identifier::new("x")
|
||||
)))
|
||||
}))
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn r#if() {
|
||||
assert_eq!(
|
||||
parse(&lex("if true 'foo'").unwrap()).unwrap()[0].0,
|
||||
Statement::IfElse(IfElse::new(
|
||||
Expression::Value(ValueNode::Boolean(true)),
|
||||
Statement::Expression(Expression::Value(ValueNode::String("foo"))),
|
||||
Statement::Expression(Expression::Value(ValueNode::String("foo".to_string()))),
|
||||
None
|
||||
))
|
||||
)
|
||||
@ -311,9 +346,9 @@ mod tests {
|
||||
parse(&lex("if true 'foo' else 'bar'").unwrap()).unwrap()[0].0,
|
||||
Statement::IfElse(IfElse::new(
|
||||
Expression::Value(ValueNode::Boolean(true)),
|
||||
Statement::Expression(Expression::Value(ValueNode::String("foo"))),
|
||||
Statement::Expression(Expression::Value(ValueNode::String("foo".to_string()))),
|
||||
Some(Statement::Expression(Expression::Value(ValueNode::String(
|
||||
"bar"
|
||||
"bar".to_string()
|
||||
))))
|
||||
))
|
||||
)
|
||||
@ -326,7 +361,7 @@ mod tests {
|
||||
Statement::Expression(Expression::Value(ValueNode::Map(vec![(
|
||||
Identifier::new("foo"),
|
||||
None,
|
||||
Expression::Value(ValueNode::String("bar"))
|
||||
Expression::Value(ValueNode::String("bar".to_string()))
|
||||
)])))
|
||||
);
|
||||
assert_eq!(
|
||||
@ -521,7 +556,7 @@ mod tests {
|
||||
AssignmentOperator::Assign,
|
||||
Statement::Expression(Expression::Value(ValueNode::List(vec![
|
||||
Expression::Value(ValueNode::Integer(42)),
|
||||
Expression::Value(ValueNode::String("foo"))
|
||||
Expression::Value(ValueNode::String("foo".to_string()))
|
||||
])))
|
||||
)),
|
||||
);
|
||||
@ -596,8 +631,8 @@ mod tests {
|
||||
parse(&lex("[42, 'foo', 'bar', [1, 2, 3,]]").unwrap()).unwrap()[0].0,
|
||||
Statement::Expression(Expression::Value(ValueNode::List(vec![
|
||||
Expression::Value(ValueNode::Integer(42)),
|
||||
Expression::Value(ValueNode::String("foo")),
|
||||
Expression::Value(ValueNode::String("bar")),
|
||||
Expression::Value(ValueNode::String("foo".to_string())),
|
||||
Expression::Value(ValueNode::String("bar".to_string())),
|
||||
Expression::Value(ValueNode::List(vec![
|
||||
Expression::Value(ValueNode::Integer(1)),
|
||||
Expression::Value(ValueNode::Integer(2)),
|
||||
@ -751,15 +786,15 @@ mod tests {
|
||||
fn double_quoted_string() {
|
||||
assert_eq!(
|
||||
parse(&lex("\"\"").unwrap()).unwrap()[0].0,
|
||||
Statement::Expression(Expression::Value(ValueNode::String("")))
|
||||
Statement::Expression(Expression::Value(ValueNode::String("".to_string())))
|
||||
);
|
||||
assert_eq!(
|
||||
parse(&lex("\"42\"").unwrap()).unwrap()[0].0,
|
||||
Statement::Expression(Expression::Value(ValueNode::String("42")))
|
||||
Statement::Expression(Expression::Value(ValueNode::String("42".to_string())))
|
||||
);
|
||||
assert_eq!(
|
||||
parse(&lex("\"foobar\"").unwrap()).unwrap()[0].0,
|
||||
Statement::Expression(Expression::Value(ValueNode::String("foobar")))
|
||||
Statement::Expression(Expression::Value(ValueNode::String("foobar".to_string())))
|
||||
);
|
||||
}
|
||||
|
||||
@ -767,15 +802,15 @@ mod tests {
|
||||
fn single_quoted_string() {
|
||||
assert_eq!(
|
||||
parse(&lex("''").unwrap()).unwrap()[0].0,
|
||||
Statement::Expression(Expression::Value(ValueNode::String("")))
|
||||
Statement::Expression(Expression::Value(ValueNode::String("".to_string())))
|
||||
);
|
||||
assert_eq!(
|
||||
parse(&lex("'42'").unwrap()).unwrap()[0].0,
|
||||
Statement::Expression(Expression::Value(ValueNode::String("42")))
|
||||
Statement::Expression(Expression::Value(ValueNode::String("42".to_string())))
|
||||
);
|
||||
assert_eq!(
|
||||
parse(&lex("'foobar'").unwrap()).unwrap()[0].0,
|
||||
Statement::Expression(Expression::Value(ValueNode::String("foobar")))
|
||||
Statement::Expression(Expression::Value(ValueNode::String("foobar".to_string())))
|
||||
);
|
||||
}
|
||||
|
||||
@ -783,15 +818,15 @@ mod tests {
|
||||
fn grave_quoted_string() {
|
||||
assert_eq!(
|
||||
parse(&lex("``").unwrap()).unwrap()[0].0,
|
||||
Statement::Expression(Expression::Value(ValueNode::String("")))
|
||||
Statement::Expression(Expression::Value(ValueNode::String("".to_string())))
|
||||
);
|
||||
assert_eq!(
|
||||
parse(&lex("`42`").unwrap()).unwrap()[0].0,
|
||||
Statement::Expression(Expression::Value(ValueNode::String("42")))
|
||||
Statement::Expression(Expression::Value(ValueNode::String("42".to_string())))
|
||||
);
|
||||
assert_eq!(
|
||||
parse(&lex("`foobar`").unwrap()).unwrap()[0].0,
|
||||
Statement::Expression(Expression::Value(ValueNode::String("foobar")))
|
||||
Statement::Expression(Expression::Value(ValueNode::String("foobar".to_string())))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
56
src/value.rs
56
src/value.rs
@ -13,7 +13,7 @@ use stanza::{
|
||||
};
|
||||
|
||||
use crate::{
|
||||
abstract_tree::{Identifier, Type},
|
||||
abstract_tree::{Identifier, Statement, Type},
|
||||
error::ValidationError,
|
||||
};
|
||||
|
||||
@ -72,6 +72,18 @@ impl Value {
|
||||
Value(Arc::new(ValueInner::Enum(name, variant)))
|
||||
}
|
||||
|
||||
pub fn function(
|
||||
parameters: Vec<(Identifier, Type)>,
|
||||
return_type: Type,
|
||||
body: Statement,
|
||||
) -> Self {
|
||||
Value(Arc::new(ValueInner::Function(Function {
|
||||
parameters,
|
||||
return_type,
|
||||
body,
|
||||
})))
|
||||
}
|
||||
|
||||
pub fn r#type(&self) -> Type {
|
||||
match self.0.as_ref() {
|
||||
ValueInner::Boolean(_) => Type::Boolean,
|
||||
@ -90,6 +102,7 @@ impl Value {
|
||||
ValueInner::Range(_) => Type::Range,
|
||||
ValueInner::String(_) => Type::String,
|
||||
ValueInner::Enum(name, _) => Type::Custom(name.clone()),
|
||||
ValueInner::Function(_) => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
@ -152,17 +165,15 @@ impl Value {
|
||||
|
||||
impl Display for Value {
|
||||
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
||||
use ValueInner::*;
|
||||
|
||||
fn create_table() -> Table {
|
||||
Table::with_styles(Styles::default().with(HAlign::Centred).with(MinWidth(3)))
|
||||
}
|
||||
|
||||
match self.inner().as_ref() {
|
||||
Boolean(boolean) => write!(f, "{boolean}"),
|
||||
Float(float) => write!(f, "{float}"),
|
||||
Integer(integer) => write!(f, "{integer}"),
|
||||
List(list) => {
|
||||
ValueInner::Boolean(boolean) => write!(f, "{boolean}"),
|
||||
ValueInner::Float(float) => write!(f, "{float}"),
|
||||
ValueInner::Integer(integer) => write!(f, "{integer}"),
|
||||
ValueInner::List(list) => {
|
||||
let mut table = create_table();
|
||||
|
||||
for value in list {
|
||||
@ -171,7 +182,7 @@ impl Display for Value {
|
||||
|
||||
write!(f, "{}", Console::default().render(&table))
|
||||
}
|
||||
Map(map) => {
|
||||
ValueInner::Map(map) => {
|
||||
let mut table = create_table();
|
||||
|
||||
for (identifier, value) in map {
|
||||
@ -180,10 +191,23 @@ impl Display for Value {
|
||||
|
||||
write!(f, "{}", Console::default().render(&table))
|
||||
}
|
||||
Range(_) => todo!(),
|
||||
String(string) => write!(f, "{string}"),
|
||||
ValueInner::Range(_) => todo!(),
|
||||
ValueInner::String(string) => write!(f, "{string}"),
|
||||
|
||||
Enum(_, _) => todo!(),
|
||||
ValueInner::Enum(_, _) => todo!(),
|
||||
ValueInner::Function(Function {
|
||||
parameters,
|
||||
return_type,
|
||||
body,
|
||||
}) => {
|
||||
write!(f, "(")?;
|
||||
|
||||
for (identifier, r#type) in parameters {
|
||||
write!(f, "{identifier}: {}", r#type)?;
|
||||
}
|
||||
|
||||
write!(f, "): {return_type} {body:?}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -206,6 +230,7 @@ impl Ord for Value {
|
||||
pub enum ValueInner {
|
||||
Boolean(bool),
|
||||
Float(f64),
|
||||
Function(Function),
|
||||
Integer(i64),
|
||||
List(Vec<Value>),
|
||||
Map(BTreeMap<Identifier, Value>),
|
||||
@ -259,6 +284,15 @@ impl Ord for ValueInner {
|
||||
}
|
||||
}
|
||||
(Enum(..), _) => Ordering::Greater,
|
||||
(Function(left), Function(right)) => left.cmp(right),
|
||||
(Function(_), _) => Ordering::Greater,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
|
||||
pub struct Function {
|
||||
parameters: Vec<(Identifier, Type)>,
|
||||
return_type: Type,
|
||||
body: Statement,
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user