Skip to content

Commit b3adb15

Browse files
committed
(keywords) add: keywords functions
1 parent 1b58db7 commit b3adb15

File tree

11 files changed

+151
-44
lines changed

11 files changed

+151
-44
lines changed

src/lexer/types/keywords.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use core::fmt;
2+
13
macro_rules! impl_keywords {
24
($($pascal:ident $ktype:ident $str:expr ,)*) => {
35
#[derive(Debug, PartialEq, Eq)]
@@ -11,12 +13,6 @@ macro_rules! impl_keywords {
1113
$(Self::$pascal => KeywordType::$ktype,)*
1214
}
1315
}
14-
15-
pub const fn repr(&self) -> &str {
16-
match self {
17-
$(Self::$pascal => $str,)*
18-
}
19-
}
2016
}
2117

2218
impl TryFrom<&str> for Keyword {
@@ -28,6 +24,15 @@ macro_rules! impl_keywords {
2824
}
2925
}
3026
}
27+
28+
#[expect(clippy::min_ident_chars)]
29+
impl fmt::Display for Keyword {
30+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
31+
match self {
32+
$(Self::$pascal => $str.fmt(f),)*
33+
}
34+
}
35+
}
3136
};
3237
}
3338

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,5 @@ pub use crate::errors::api::{Location, Res};
6161
pub use crate::lexer::api::{display_tokens, lex_file, Number, TokenValue};
6262
#[expect(clippy::useless_attribute, clippy::pub_use)]
6363
pub use crate::parser::api::parse_tokens;
64+
65+
const EMPTY: &str = "\u{2205} ";

src/parser/keyword/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
extern crate alloc;
2-
mod types;
2+
pub mod types;
33

44
use alloc::vec::IntoIter;
55

src/parser/keyword/types/attributes.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
#![allow(clippy::arbitrary_source_item_ordering)]
1+
#![expect(clippy::arbitrary_source_item_ordering, reason = "macro used")]
2+
3+
use core::fmt;
24

35
use super::super::Ast;
46
use super::PushInNode;
7+
use crate::lexer::api::Keyword;
58

69
macro_rules! define_attribute_keywords {
710
($($name:ident: $($variant:ident)*,)*) => {
811

12+
#[derive(Debug, PartialEq, Eq)]
913
pub enum AttributeKeyword {
1014
$($name($name),)*
1115
}
@@ -28,20 +32,30 @@ macro_rules! define_attribute_keywords {
2832
$($($variant,)*)*
2933
}
3034

35+
$(
36+
#[derive(Debug, PartialEq, Eq)]
37+
pub enum $name {
38+
$($variant,)*
39+
}
40+
)*
3141

32-
$(pub enum $name {
33-
$($variant,)*
34-
})*
35-
42+
#[expect(clippy::min_ident_chars)]
43+
impl fmt::Display for AttributeKeyword {
44+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45+
match self {
46+
$($(Self::$name($name::$variant) => Keyword::$variant.fmt(f),)*)*
47+
}
48+
}
49+
}
3650

3751
};
3852
}
3953

4054
define_attribute_keywords!(
41-
Atomic: Bool Char Double Float Int Complex Decimal128 Decimal32 Decimal64 Imaginary BigInt Void,
55+
BasicDataType: Bool Char Double Float Int UComplex UDecimal128 UDecimal32 UDecimal64 UImaginary UBigInt Void,
4256
Modifiers: Signed Unsigned Long Short,
4357
Storage: Auto ThreadLocal Extern Static Register,
4458
Qualifiers: Const Constexpr Volatile Default,
45-
Complex: Typedef Struct Union Enum,
46-
SpecialAttributes: Alignas Inline Restrict Generic Noreturn Atomic,
59+
UserDefinedTypes: Typedef Struct Union Enum,
60+
SpecialAttributes: UAtomic Alignas Inline Restrict UGeneric UNoreturn,
4761
);

src/parser/keyword/types/controlflow.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ pub enum ControlFlowKeyword {
1717
}
1818

1919
impl ControlFlowKeyword {
20-
pub fn is_in_case_context(node: &Ast) -> bool {
21-
todo!()
20+
pub const fn is_in_case_context(node: &Ast) -> bool {
21+
// todo!()
22+
true //TODO
2223
}
2324
}
2425

src/parser/keyword/types/functions.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ use core::fmt;
22

33
use super::super::Ast;
44
use super::PushInNode;
5-
use crate::parser::tree::Literal;
5+
use crate::parser::tree::{Literal, Variable};
66

7+
#[derive(Debug, PartialEq, Eq)]
78
pub enum FunctionKeyword {
89
Alignof,
910
Sizeof,
@@ -16,7 +17,7 @@ pub enum FunctionKeyword {
1617

1718
impl PushInNode for FunctionKeyword {
1819
fn push_in_node(self, node: &mut Ast) -> Result<(), String> {
19-
node.push_block_as_leaf(Ast::Leaf(Literal::Variable(self.to_string())))
20+
node.push_block_as_leaf(Ast::Leaf(Literal::Variable(Variable::from_keyword(self))))
2021
}
2122
}
2223

src/parser/keyword/types/mod.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,21 +70,21 @@ impl From<(Keyword, bool)> for KeywordParsing {
7070
Keyword::Static => Self::Attr(Attr::from(UnsortedAttr::Static)),
7171
Keyword::Struct => Self::Attr(Attr::from(UnsortedAttr::Struct)),
7272
Keyword::Typedef => Self::Attr(Attr::from(UnsortedAttr::Typedef)),
73-
Keyword::UAtomic => Self::Attr(Attr::from(UnsortedAttr::Atomic)),
74-
Keyword::UBigInt => Self::Attr(Attr::from(UnsortedAttr::BigInt)),
73+
Keyword::UAtomic => Self::Attr(Attr::from(UnsortedAttr::UAtomic)),
74+
Keyword::UBigInt => Self::Attr(Attr::from(UnsortedAttr::UBigInt)),
7575
Keyword::Default => Self::Attr(Attr::from(UnsortedAttr::Default)),
7676
Keyword::Unsigned => Self::Attr(Attr::from(UnsortedAttr::Unsigned)),
7777
Keyword::Register => Self::Attr(Attr::from(UnsortedAttr::Register)),
7878
Keyword::Restrict => Self::Attr(Attr::from(UnsortedAttr::Restrict)),
7979
Keyword::Volatile => Self::Attr(Attr::from(UnsortedAttr::Volatile)),
80-
Keyword::UComplex => Self::Attr(Attr::from(UnsortedAttr::Complex)),
81-
Keyword::UGeneric => Self::Attr(Attr::from(UnsortedAttr::Generic)),
82-
Keyword::UNoreturn => Self::Attr(Attr::from(UnsortedAttr::Noreturn)),
80+
Keyword::UComplex => Self::Attr(Attr::from(UnsortedAttr::UComplex)),
81+
Keyword::UGeneric => Self::Attr(Attr::from(UnsortedAttr::UGeneric)),
82+
Keyword::UNoreturn => Self::Attr(Attr::from(UnsortedAttr::UNoreturn)),
8383
Keyword::Constexpr => Self::Attr(Attr::from(UnsortedAttr::Constexpr)),
84-
Keyword::UDecimal64 => Self::Attr(Attr::from(UnsortedAttr::Decimal64)),
85-
Keyword::UImaginary => Self::Attr(Attr::from(UnsortedAttr::Imaginary)),
86-
Keyword::UDecimal32 => Self::Attr(Attr::from(UnsortedAttr::Decimal32)),
87-
Keyword::UDecimal128 => Self::Attr(Attr::from(UnsortedAttr::Decimal128)),
84+
Keyword::UDecimal64 => Self::Attr(Attr::from(UnsortedAttr::UDecimal64)),
85+
Keyword::UImaginary => Self::Attr(Attr::from(UnsortedAttr::UImaginary)),
86+
Keyword::UDecimal32 => Self::Attr(Attr::from(UnsortedAttr::UDecimal32)),
87+
Keyword::UDecimal128 => Self::Attr(Attr::from(UnsortedAttr::UDecimal128)),
8888
Keyword::Alignas | Keyword::UAlignas => Self::Attr(Attr::from(UnsortedAttr::Alignas)),
8989
Keyword::Bool | Keyword::UBool => Self::Attr(Attr::from(UnsortedAttr::Bool)),
9090
Keyword::ThreadLocal | Keyword::UThreadLocal => {

src/parser/parse_content.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use super::state::{BlockState, ParsingState};
66
use super::symbols::handle_symbol;
77
use super::tree::blocks::Block;
88
use super::tree::node::Ast;
9-
use super::tree::Literal;
9+
use super::tree::{Literal, Variable};
1010
use crate::errors::api::{CompileError, Location, Res};
1111
use crate::lexer::api::{Token, TokenValue};
1212

@@ -65,9 +65,13 @@ pub fn parse_block(
6565
TokenValue::Char(ch) => {
6666
handle_literal(current, Literal::Char(ch), location, p_state, tokens)
6767
}
68-
TokenValue::Identifier(val) => {
69-
handle_literal(current, Literal::Variable(val), location, p_state, tokens)
70-
}
68+
TokenValue::Identifier(val) => handle_literal(
69+
current,
70+
Literal::Variable(Variable::from(val)),
71+
location,
72+
p_state,
73+
tokens,
74+
),
7175
TokenValue::Number(nb) => {
7276
handle_literal(current, Literal::Number(nb), location, p_state, tokens)
7377
}

src/parser/tree/mod.rs

Lines changed: 81 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@ mod conversions;
44
pub mod node;
55
mod traits;
66
pub mod unary;
7+
78
use core::fmt;
89

910
use node::Ast;
1011
use traits::{Associativity, Operator};
1112

13+
use super::keyword::types::attributes::AttributeKeyword;
14+
use super::keyword::types::functions::FunctionKeyword;
1215
use crate::lexer::api::Number;
16+
use crate::EMPTY;
1317

1418
#[derive(Debug, PartialEq)]
1519
pub struct CompoundLiteral {
@@ -43,8 +47,9 @@ impl Operator for CompoundLiteralOperator {
4347
pub struct FunctionCall {
4448
args: Vec<Ast>,
4549
full: bool,
46-
name: String,
50+
name: VariableName,
4751
op: FunctionOperator,
52+
return_attrs: Vec<AttributeKeyword>,
4853
}
4954

5055
#[expect(clippy::min_ident_chars)]
@@ -94,28 +99,28 @@ impl fmt::Display for ListInitialiser {
9499
}
95100
}
96101

97-
#[derive(Debug, PartialEq, Default)]
102+
#[derive(Debug, PartialEq)]
98103
pub enum Literal {
99104
Char(char),
100105
ConstantBool(bool),
101-
#[default]
102106
Empty,
103107
Nullptr,
104108
Number(Number),
105109
Str(String),
106-
Variable(String),
110+
Variable(Variable),
107111
}
108112

109113
#[expect(clippy::min_ident_chars)]
110114
impl fmt::Display for Literal {
111115
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
112116
match self {
113-
Self::Empty => "\u{2205} ".fmt(f),
117+
Self::Empty => EMPTY.fmt(f),
114118
Self::Nullptr => "NULL".fmt(f),
115119
Self::Char(val) => val.fmt(f),
120+
Self::Str(val) => val.fmt(f),
116121
Self::Number(val) => val.fmt(f),
117122
Self::ConstantBool(val) => val.fmt(f),
118-
Self::Variable(val) | Self::Str(val) => val.fmt(f),
123+
Self::Variable(val) => val.fmt(f),
119124
}
120125
}
121126
}
@@ -161,9 +166,78 @@ impl fmt::Display for TernaryOperator {
161166
}
162167
}
163168

169+
#[derive(Debug, PartialEq, Eq)]
170+
pub struct Variable {
171+
attrs: Vec<AttributeKeyword>,
172+
name: VariableName,
173+
}
174+
175+
impl Variable {
176+
pub const fn from_keyword(keyword: FunctionKeyword) -> Self {
177+
Self {
178+
name: VariableName::Keyword(keyword),
179+
attrs: vec![],
180+
}
181+
}
182+
}
183+
184+
impl From<String> for Variable {
185+
fn from(name: String) -> Self {
186+
Self {
187+
name: VariableName::UserDefined(name),
188+
attrs: vec![],
189+
}
190+
}
191+
}
192+
193+
#[expect(clippy::min_ident_chars)]
194+
impl fmt::Display for Variable {
195+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
196+
if self.attrs.is_empty() {
197+
self.name.fmt(f)
198+
} else {
199+
write!(
200+
f,
201+
"({} {})",
202+
self.attrs
203+
.iter()
204+
.map(|attr| format!("{attr}"))
205+
.collect::<Vec<_>>()
206+
.join(" "),
207+
self.name
208+
)
209+
}
210+
}
211+
}
212+
213+
#[derive(Debug, PartialEq, Eq, Default)]
214+
enum VariableName {
215+
#[default]
216+
Empty,
217+
Keyword(FunctionKeyword),
218+
UserDefined(String),
219+
}
220+
221+
impl From<&str> for VariableName {
222+
fn from(name: &str) -> Self {
223+
Self::UserDefined(name.to_owned())
224+
}
225+
}
226+
227+
#[expect(clippy::min_ident_chars)]
228+
impl fmt::Display for VariableName {
229+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
230+
match self {
231+
Self::Empty => EMPTY.fmt(f),
232+
Self::UserDefined(val) => val.fmt(f),
233+
Self::Keyword(val) => val.fmt(f),
234+
}
235+
}
236+
}
237+
164238
#[expect(clippy::borrowed_box)]
165239
fn repr_option_node(opt: Option<&Box<Ast>>) -> String {
166-
opt.map_or_else(|| "\u{2205} ".to_owned(), Box::<Ast>::to_string)
240+
opt.map_or_else(|| EMPTY.to_owned(), Box::<Ast>::to_string)
167241
}
168242

169243
fn repr_vec_node(vec: &[Ast]) -> String {

src/parser/tree/node.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ use super::blocks::Block;
66
use super::conversions::OperatorConversions;
77
use super::traits::{Associativity, IsComma, Operator as _};
88
use super::unary::Unary;
9-
use super::{FunctionCall, FunctionOperator, ListInitialiser, Literal, Ternary};
9+
use super::{
10+
FunctionCall, FunctionOperator, ListInitialiser, Literal, Ternary, Variable, VariableName
11+
};
12+
use crate::EMPTY;
1013

1114
/// Struct to represent the AST
1215
#[expect(clippy::arbitrary_source_item_ordering)]
@@ -493,9 +496,8 @@ impl Ast {
493496
//
494497
//
495498
// success
496-
Self::Leaf(Literal::Variable(var)) => {
497-
let name = mem::take(var);
498-
*self = Self::FunctionCall(FunctionCall { name, op: FunctionOperator, args: vec![], full: false }); true
499+
Self::Leaf(Literal::Variable(Variable{ name, attrs })) => {
500+
*self = Self::FunctionCall(FunctionCall { name: mem::replace(name, VariableName::from("")), return_attrs: mem::take(attrs), op: FunctionOperator, args: vec![], full: false }); true
499501
}
500502
//
501503
//
@@ -535,7 +537,7 @@ impl Ast {
535537
impl fmt::Display for Ast {
536538
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
537539
match self {
538-
Self::Empty => write!(f, "\u{2205} "),
540+
Self::Empty => EMPTY.fmt(f),
539541
Self::Binary(val) => val.fmt(f),
540542
Self::FunctionCall(val) => val.fmt(f),
541543
Self::Leaf(val) => val.fmt(f),

0 commit comments

Comments
 (0)