Skip to content

Commit 1b58db7

Browse files
committed
(keywords) add: logic
1 parent 144d213 commit 1b58db7

File tree

6 files changed

+102
-29
lines changed

6 files changed

+102
-29
lines changed

src/parser/keyword/mod.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ mod types;
33

44
use alloc::vec::IntoIter;
55

6-
use types::KeywordParsing;
6+
use types::controlflow::ControlFlowKeyword;
7+
use types::{KeywordParsing, PushInNode as _};
78

9+
use super::parse_content::parse_block;
810
use super::state::ParsingState;
911
use super::tree::node::Ast;
1012
use crate::errors::api::CompileError;
@@ -14,18 +16,15 @@ use crate::Location;
1416
#[allow(clippy::todo, reason = "not yet implemented")]
1517
pub fn handle_keyword(
1618
keyword: Keyword,
17-
_current: &mut Ast,
18-
_p_state: &mut ParsingState,
19-
_tokens: &mut IntoIter<Token>,
20-
_location: Location,
19+
current: &mut Ast,
20+
p_state: &mut ParsingState,
21+
tokens: &mut IntoIter<Token>,
22+
location: Location,
2123
) -> Result<(), CompileError> {
22-
let case_context = true; // node.is_in_case_context();
23-
match KeywordParsing::from((keyword, case_context)) {
24-
KeywordParsing::Nullptr => todo!(),
25-
KeywordParsing::False => todo!(),
26-
KeywordParsing::True => todo!(),
27-
KeywordParsing::CtrlFlow(_) => todo!(),
28-
KeywordParsing::Attr(_) => todo!(),
29-
KeywordParsing::Func(_) => todo!(),
30-
}
24+
let case_context = ControlFlowKeyword::is_in_case_context(current);
25+
let parsed_keyword = KeywordParsing::from((keyword, case_context));
26+
parsed_keyword
27+
.push_in_node(current)
28+
.map_err(|msg| location.into_error(msg))?;
29+
parse_block(tokens, p_state, current)
3130
}

src/parser/keyword/types/attributes.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
#![allow(clippy::arbitrary_source_item_ordering)]
22

3+
use super::super::Ast;
4+
use super::PushInNode;
5+
36
macro_rules! define_attribute_keywords {
47
($($name:ident: $($variant:ident)*,)*) => {
58

69
pub enum AttributeKeyword {
710
$($name($name),)*
811
}
912

10-
1113
impl From<UnsortedAttributeKeyword> for AttributeKeyword {
1214
fn from(value: UnsortedAttributeKeyword) -> Self {
1315
match value {
@@ -16,6 +18,12 @@ macro_rules! define_attribute_keywords {
1618
}
1719
}
1820

21+
impl PushInNode for AttributeKeyword {
22+
fn push_in_node(self, node: &mut Ast) -> Result<(), String> {
23+
todo!()
24+
}
25+
}
26+
1927
pub enum UnsortedAttributeKeyword {
2028
$($($variant,)*)*
2129
}

src/parser/keyword/types/controlflow.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
use super::super::Ast;
2+
use super::PushInNode;
3+
14
pub enum ControlFlowKeyword {
25
Break,
36
Case,
@@ -12,3 +15,15 @@ pub enum ControlFlowKeyword {
1215
Switch,
1316
While,
1417
}
18+
19+
impl ControlFlowKeyword {
20+
pub fn is_in_case_context(node: &Ast) -> bool {
21+
todo!()
22+
}
23+
}
24+
25+
impl PushInNode for ControlFlowKeyword {
26+
fn push_in_node(self, node: &mut Ast) -> Result<(), String> {
27+
todo!()
28+
}
29+
}

src/parser/keyword/types/functions.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
use core::fmt;
2+
3+
use super::super::Ast;
4+
use super::PushInNode;
5+
use crate::parser::tree::Literal;
6+
17
pub enum FunctionKeyword {
28
Alignof,
39
Sizeof,
@@ -7,3 +13,24 @@ pub enum FunctionKeyword {
713
UAlignof,
814
UThreadLocal,
915
}
16+
17+
impl PushInNode for FunctionKeyword {
18+
fn push_in_node(self, node: &mut Ast) -> Result<(), String> {
19+
node.push_block_as_leaf(Ast::Leaf(Literal::Variable(self.to_string())))
20+
}
21+
}
22+
23+
#[expect(clippy::min_ident_chars)]
24+
impl fmt::Display for FunctionKeyword {
25+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26+
match self {
27+
Self::Alignof => "alignof".fmt(f),
28+
Self::Sizeof => "sizeof".fmt(f),
29+
Self::StaticAssert => "static_assert".fmt(f),
30+
Self::Typeof => "typeof".fmt(f),
31+
Self::TypeofUnqual => "typeof_unqual".fmt(f),
32+
Self::UAlignof => "u_alignof".fmt(f),
33+
Self::UThreadLocal => "u_thread_local".fmt(f),
34+
}
35+
}
36+
}

src/parser/keyword/types/mod.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1-
#![allow(dead_code, reason = "not yet implemented")]
1+
#![allow(
2+
dead_code,
3+
unused_variables,
4+
clippy::todo,
5+
reason = "not yet implemented"
6+
)]
27

3-
mod attributes;
4-
mod controlflow;
5-
mod functions;
8+
pub mod attributes;
9+
pub mod controlflow;
10+
pub mod functions;
611

712
use attributes::{AttributeKeyword as Attr, UnsortedAttributeKeyword as UnsortedAttr};
813
use controlflow::ControlFlowKeyword as CtrlFlow;
914
use functions::FunctionKeyword as Func;
1015

16+
use super::super::tree::node::Ast;
1117
use crate::lexer::api::Keyword;
18+
use crate::parser::tree::Literal;
1219

1320
pub enum KeywordParsing {
1421
Attr(Attr),
@@ -86,3 +93,20 @@ impl From<(Keyword, bool)> for KeywordParsing {
8693
}
8794
}
8895
}
96+
97+
impl PushInNode for KeywordParsing {
98+
fn push_in_node(self, node: &mut Ast) -> Result<(), String> {
99+
match self {
100+
Self::Func(func) => func.push_in_node(node),
101+
Self::Attr(attr) => attr.push_in_node(node),
102+
Self::CtrlFlow(ctrl) => ctrl.push_in_node(node),
103+
Self::Nullptr => node.push_block_as_leaf(Ast::Leaf(Literal::Nullptr)),
104+
Self::True => node.push_block_as_leaf(Ast::Leaf(Literal::ConstantBool(true))),
105+
Self::False => node.push_block_as_leaf(Ast::Leaf(Literal::ConstantBool(false))),
106+
}
107+
}
108+
}
109+
110+
pub trait PushInNode {
111+
fn push_in_node(self, node: &mut Ast) -> Result<(), String>;
112+
}

src/parser/tree/mod.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,10 @@ impl fmt::Display for ListInitialiser {
9797
#[derive(Debug, PartialEq, Default)]
9898
pub enum Literal {
9999
Char(char),
100+
ConstantBool(bool),
100101
#[default]
101102
Empty,
103+
Nullptr,
102104
Number(Number),
103105
Str(String),
104106
Variable(String),
@@ -107,16 +109,14 @@ pub enum Literal {
107109
#[expect(clippy::min_ident_chars)]
108110
impl fmt::Display for Literal {
109111
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
110-
write!(
111-
f,
112-
"{}",
113-
match self {
114-
Self::Empty => "\u{2205} ".to_owned(),
115-
Self::Variable(val) | Self::Str(val) => val.to_string(),
116-
Self::Char(val) => val.to_string(),
117-
Self::Number(val) => format!("{val}"),
118-
}
119-
)
112+
match self {
113+
Self::Empty => "\u{2205} ".fmt(f),
114+
Self::Nullptr => "NULL".fmt(f),
115+
Self::Char(val) => val.fmt(f),
116+
Self::Number(val) => val.fmt(f),
117+
Self::ConstantBool(val) => val.fmt(f),
118+
Self::Variable(val) | Self::Str(val) => val.fmt(f),
119+
}
120120
}
121121
}
122122

0 commit comments

Comments
 (0)