Begin implementing as expression
This commit is contained in:
parent
7be9300f14
commit
8ea6b4be81
12
dust-lang/src/abstract_tree/as.rs
Normal file
12
dust-lang/src/abstract_tree/as.rs
Normal file
@ -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 }
|
||||||
|
}
|
||||||
|
}
|
@ -5,12 +5,13 @@ use crate::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
AbstractNode, Action, BuiltInFunctionCall, FunctionCall, ListIndex, Logic, MapIndex, Math,
|
AbstractNode, Action, As, BuiltInFunctionCall, FunctionCall, ListIndex, Logic, MapIndex, Math,
|
||||||
SourcePosition, Type, ValueNode, WithPosition,
|
SourcePosition, Type, ValueNode, WithPosition,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
|
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
|
||||||
pub enum Expression {
|
pub enum Expression {
|
||||||
|
As(WithPosition<Box<As>>),
|
||||||
BuiltInFunctionCall(WithPosition<Box<BuiltInFunctionCall>>),
|
BuiltInFunctionCall(WithPosition<Box<BuiltInFunctionCall>>),
|
||||||
FunctionCall(WithPosition<FunctionCall>),
|
FunctionCall(WithPosition<FunctionCall>),
|
||||||
Identifier(WithPosition<Identifier>),
|
Identifier(WithPosition<Identifier>),
|
||||||
@ -24,6 +25,7 @@ pub enum Expression {
|
|||||||
impl Expression {
|
impl Expression {
|
||||||
pub fn position(&self) -> SourcePosition {
|
pub fn position(&self) -> SourcePosition {
|
||||||
match self {
|
match self {
|
||||||
|
Expression::As(inner) => inner.position,
|
||||||
Expression::FunctionCall(inner) => inner.position,
|
Expression::FunctionCall(inner) => inner.position,
|
||||||
Expression::Identifier(inner) => inner.position,
|
Expression::Identifier(inner) => inner.position,
|
||||||
Expression::MapIndex(inner) => inner.position,
|
Expression::MapIndex(inner) => inner.position,
|
||||||
@ -58,6 +60,7 @@ impl AbstractNode for Expression {
|
|||||||
Expression::BuiltInFunctionCall(built_in_function_call) => {
|
Expression::BuiltInFunctionCall(built_in_function_call) => {
|
||||||
built_in_function_call.item.expected_type(_context)
|
built_in_function_call.item.expected_type(_context)
|
||||||
}
|
}
|
||||||
|
Expression::As(_) => todo!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
pub mod r#as;
|
||||||
pub mod assignment;
|
pub mod assignment;
|
||||||
pub mod async_block;
|
pub mod async_block;
|
||||||
pub mod block;
|
pub mod block;
|
||||||
@ -32,6 +33,7 @@ pub use self::{
|
|||||||
logic::Logic,
|
logic::Logic,
|
||||||
map_index::MapIndex,
|
map_index::MapIndex,
|
||||||
math::Math,
|
math::Math,
|
||||||
|
r#as::As,
|
||||||
r#loop::Loop,
|
r#loop::Loop,
|
||||||
r#type::Type,
|
r#type::Type,
|
||||||
r#while::While,
|
r#while::While,
|
||||||
|
@ -39,6 +39,7 @@ impl<'src> Display for Token<'src> {
|
|||||||
#[derive(Copy, Clone, Debug, PartialEq)]
|
#[derive(Copy, Clone, Debug, PartialEq)]
|
||||||
pub enum Keyword {
|
pub enum Keyword {
|
||||||
Any,
|
Any,
|
||||||
|
As,
|
||||||
Async,
|
Async,
|
||||||
Bool,
|
Bool,
|
||||||
Break,
|
Break,
|
||||||
@ -65,6 +66,7 @@ impl Display for Keyword {
|
|||||||
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Keyword::Any => write!(f, "any"),
|
Keyword::Any => write!(f, "any"),
|
||||||
|
Keyword::As => write!(f, "as"),
|
||||||
Keyword::Async => write!(f, "async"),
|
Keyword::Async => write!(f, "async"),
|
||||||
Keyword::Bool => write!(f, "bool"),
|
Keyword::Bool => write!(f, "bool"),
|
||||||
Keyword::Break => write!(f, "break"),
|
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 {
|
let identifier_and_keyword = text::ident().map(|text: &str| match text {
|
||||||
"any" => Token::Keyword(Keyword::Any),
|
"any" => Token::Keyword(Keyword::Any),
|
||||||
|
"as" => Token::Keyword(Keyword::As),
|
||||||
"async" => Token::Keyword(Keyword::Async),
|
"async" => Token::Keyword(Keyword::Async),
|
||||||
"bool" => Token::Keyword(Keyword::Bool),
|
"bool" => Token::Keyword(Keyword::Bool),
|
||||||
"break" => Token::Keyword(Keyword::Break),
|
"break" => Token::Keyword(Keyword::Break),
|
||||||
|
@ -335,6 +335,12 @@ pub fn parser<'src>(
|
|||||||
just(Token::Control(Control::DoubleColon)),
|
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((
|
let atom = choice((
|
||||||
range.clone(),
|
range.clone(),
|
||||||
parsed_function.clone(),
|
parsed_function.clone(),
|
||||||
@ -507,6 +513,7 @@ pub fn parser<'src>(
|
|||||||
|
|
||||||
choice((
|
choice((
|
||||||
logic_math_indexes_and_function_calls,
|
logic_math_indexes_and_function_calls,
|
||||||
|
r#as,
|
||||||
built_in_function_call,
|
built_in_function_call,
|
||||||
range,
|
range,
|
||||||
structure_instance,
|
structure_instance,
|
||||||
|
@ -11,7 +11,7 @@ while count <= 15 {
|
|||||||
} else if divides_by_5 {
|
} else if divides_by_5 {
|
||||||
'buzz'
|
'buzz'
|
||||||
} else {
|
} else {
|
||||||
string.new(count)
|
count as str
|
||||||
}
|
}
|
||||||
|
|
||||||
io.write_line(output)
|
io.write_line(output)
|
||||||
|
Loading…
Reference in New Issue
Block a user