From 8ea6b4be811e71f22b0d8b7b6a510e290d128263 Mon Sep 17 00:00:00 2001 From: Jeff Date: Tue, 21 May 2024 17:07:12 -0400 Subject: [PATCH] Begin implementing as expression --- dust-lang/src/abstract_tree/as.rs | 12 ++++++++++++ dust-lang/src/abstract_tree/expression.rs | 5 ++++- dust-lang/src/abstract_tree/mod.rs | 2 ++ dust-lang/src/lexer.rs | 3 +++ dust-lang/src/parser.rs | 7 +++++++ examples/fizzbuzz.ds | 2 +- 6 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 dust-lang/src/abstract_tree/as.rs diff --git a/dust-lang/src/abstract_tree/as.rs b/dust-lang/src/abstract_tree/as.rs new file mode 100644 index 0000000..7db6383 --- /dev/null +++ b/dust-lang/src/abstract_tree/as.rs @@ -0,0 +1,12 @@ +use super::{Expression, Type}; + +pub struct As { + expression: Expression, + r#type: Type, +} + +impl As { + pub fn new(expression: Expression, r#type: Type) -> Self { + Self { expression, r#type } + } +} diff --git a/dust-lang/src/abstract_tree/expression.rs b/dust-lang/src/abstract_tree/expression.rs index 9b58498..f00b44d 100644 --- a/dust-lang/src/abstract_tree/expression.rs +++ b/dust-lang/src/abstract_tree/expression.rs @@ -5,12 +5,13 @@ use crate::{ }; use super::{ - AbstractNode, Action, BuiltInFunctionCall, FunctionCall, ListIndex, Logic, MapIndex, Math, + AbstractNode, Action, As, BuiltInFunctionCall, FunctionCall, ListIndex, Logic, MapIndex, Math, SourcePosition, Type, ValueNode, WithPosition, }; #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)] pub enum Expression { + As(WithPosition>), BuiltInFunctionCall(WithPosition>), FunctionCall(WithPosition), Identifier(WithPosition), @@ -24,6 +25,7 @@ pub enum Expression { impl Expression { pub fn position(&self) -> SourcePosition { match self { + Expression::As(inner) => inner.position, Expression::FunctionCall(inner) => inner.position, Expression::Identifier(inner) => inner.position, Expression::MapIndex(inner) => inner.position, @@ -58,6 +60,7 @@ impl AbstractNode for Expression { Expression::BuiltInFunctionCall(built_in_function_call) => { built_in_function_call.item.expected_type(_context) } + Expression::As(_) => todo!(), } } diff --git a/dust-lang/src/abstract_tree/mod.rs b/dust-lang/src/abstract_tree/mod.rs index 12c7102..fcdb0ad 100644 --- a/dust-lang/src/abstract_tree/mod.rs +++ b/dust-lang/src/abstract_tree/mod.rs @@ -1,3 +1,4 @@ +pub mod r#as; pub mod assignment; pub mod async_block; pub mod block; @@ -32,6 +33,7 @@ pub use self::{ logic::Logic, map_index::MapIndex, math::Math, + r#as::As, r#loop::Loop, r#type::Type, r#while::While, diff --git a/dust-lang/src/lexer.rs b/dust-lang/src/lexer.rs index 2ecbd75..0812cbe 100644 --- a/dust-lang/src/lexer.rs +++ b/dust-lang/src/lexer.rs @@ -39,6 +39,7 @@ impl<'src> Display for Token<'src> { #[derive(Copy, Clone, Debug, PartialEq)] pub enum Keyword { Any, + As, Async, Bool, Break, @@ -65,6 +66,7 @@ impl Display for Keyword { fn fmt(&self, f: &mut Formatter) -> fmt::Result { match self { Keyword::Any => write!(f, "any"), + Keyword::As => write!(f, "as"), Keyword::Async => write!(f, "async"), Keyword::Bool => write!(f, "bool"), Keyword::Break => write!(f, "break"), @@ -256,6 +258,7 @@ pub fn lexer<'src>() -> impl Parser< let identifier_and_keyword = text::ident().map(|text: &str| match text { "any" => Token::Keyword(Keyword::Any), + "as" => Token::Keyword(Keyword::As), "async" => Token::Keyword(Keyword::Async), "bool" => Token::Keyword(Keyword::Bool), "break" => Token::Keyword(Keyword::Break), diff --git a/dust-lang/src/parser.rs b/dust-lang/src/parser.rs index 09dd003..48c2c3e 100644 --- a/dust-lang/src/parser.rs +++ b/dust-lang/src/parser.rs @@ -335,6 +335,12 @@ pub fn parser<'src>( just(Token::Control(Control::DoubleColon)), ); + let r#as = expression + .clone() + .then_ignore(just(Token::Keyword(Keyword::As))) + .then(r#type.clone()) + .map_with(|(expression, r#type), state| todo!()); + let atom = choice(( range.clone(), parsed_function.clone(), @@ -507,6 +513,7 @@ pub fn parser<'src>( choice(( logic_math_indexes_and_function_calls, + r#as, built_in_function_call, range, structure_instance, diff --git a/examples/fizzbuzz.ds b/examples/fizzbuzz.ds index cb79d50..cdbde9d 100644 --- a/examples/fizzbuzz.ds +++ b/examples/fizzbuzz.ds @@ -11,7 +11,7 @@ while count <= 15 { } else if divides_by_5 { 'buzz' } else { - string.new(count) + count as str } io.write_line(output)