Skip to content

Commit b20f389

Browse files
committed
(parser) add: array subscripts
1 parent 6dfa48b commit b20f389

File tree

6 files changed

+41
-47
lines changed

6 files changed

+41
-47
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
*.lock
44
*.old
55
*.old.*
6-
!.github
6+
!.github
7+
debug.rs

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ repository = "https://www.github.com/t-webber/ccompiler"
88

99
[dependencies]
1010

11-
[[bin]]
12-
name = "lib"
13-
path = "src/lib.rs"
11+
# [[bin]]
12+
# name = "lib"
13+
# path = "src/lib.rs"
1414

1515

1616
[features]

src/lib.rs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -64,26 +64,3 @@ pub mod prelude {
6464
pub use crate::lexer::lex_file;
6565
pub use crate::parser::parse_tokens;
6666
}
67-
68-
use std::fs;
69-
70-
use errors::location::Location;
71-
use lexer::lex_file;
72-
use parser::parse_tokens;
73-
74-
#[expect(clippy::panic, clippy::print_stdout, clippy::use_debug)]
75-
fn main() {
76-
let path = ".files/test.c";
77-
let content: &str = &fs::read_to_string(path).unwrap_or_else(|_| {
78-
panic!(
79-
"The provided path is incorrect. No file found at {}.",
80-
&path
81-
)
82-
});
83-
let files: &[(String, &str)] = &[(path.to_owned(), content)];
84-
let mut location = Location::from(path);
85-
let tokens = lex_file(content, &mut location).unwrap_or_display(files, "lexer");
86-
println!("Tokens = \n{:?}", &tokens);
87-
let ast = parse_tokens(tokens).unwrap_or_display(files, "parser");
88-
println!("Ast = \n{}", &ast);
89-
}

src/parser/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ fn parse_block(
4949
TokenValue::Symbol(symbol) => {
5050
handle_symbol(&symbol, current, p_state, tokens, location)
5151
}
52-
TokenValue::Keyword(_) => todo!(),
52+
TokenValue::Keyword(key) => todo!("{key:?}"),
5353
}
5454
})
5555
}

src/parser/state.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#[derive(Default)]
22
pub struct ParsingState {
3-
pub parenthesis: usize,
4-
pub brackets: usize,
5-
pub braces: usize,
63
pub ternary: usize,
74
pub wanting_colon: bool,
5+
pub closing_bracket: bool,
86
}

src/parser/symbols.rs

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ fn safe_decr(counter: &mut usize, ch: char) -> Result<TodoState, String> {
1414
*counter = counter
1515
.checked_sub(1)
1616
.ok_or_else(|| format!("Mismactched closing '{ch}'"))?;
17-
Ok(TodoState::CloseParens)
17+
Ok(TodoState::CloseBlock)
1818
}
1919

2020
fn handle_double_binary(
@@ -39,18 +39,18 @@ fn handle_double_unary(
3939
// TODO: check for nested
4040
enum TodoState {
4141
None,
42-
OpenParens,
43-
CloseParens,
44-
SemiColon,
42+
OpenBlock,
4543
CloseBlock,
44+
OpenBracket,
45+
CloseBracket,
4646
}
4747

4848
use {BinaryOperator as BOp, UnaryOperator as UOp};
4949

5050
fn handle_one_symbol(
5151
symbol: &Symbol,
5252
current: &mut Node,
53-
p_state: &mut ParsingState,
53+
p_state: &ParsingState,
5454
) -> Result<TodoState, String> {
5555
#[allow(clippy::enum_glob_use)]
5656
use Symbol::*;
@@ -104,9 +104,7 @@ fn handle_one_symbol(
104104
Interrogation => current.push_op(TernaryOperator)?,
105105

106106
Colon => current.handle_colon()?,
107-
//
108-
SemiColon => return Ok(TodoState::SemiColon),
109-
// parenthesis
107+
// braces & blocks
110108
BraceOpen if *current == Node::Empty => *current = Node::Block(vec![]),
111109
BraceOpen => {
112110
current.push_block_as_leaf(Node::ListInitialiser(ListInitialiser::default()))?;
@@ -116,12 +114,10 @@ fn handle_one_symbol(
116114
return Ok(TodoState::CloseBlock);
117115
}
118116
}
119-
BracketOpen | BracketClose => todo!(),
120-
ParenthesisOpen => {
121-
p_state.parenthesis += 1;
122-
return Ok(TodoState::OpenParens);
123-
}
124-
ParenthesisClose => return safe_decr(&mut p_state.parenthesis, ')'),
117+
BracketOpen => return Ok(TodoState::OpenBracket),
118+
BracketClose => return Ok(TodoState::CloseBracket),
119+
ParenthesisOpen => return Ok(TodoState::OpenBlock),
120+
SemiColon | ParenthesisClose => return Ok(TodoState::CloseBlock),
125121
}
126122
Ok(TodoState::None)
127123
}
@@ -135,7 +131,7 @@ pub fn handle_symbol(
135131
) -> Result<(), CompileError> {
136132
// TODO: i can't believe this works
137133
match handle_one_symbol(symbol, current, p_state).map_err(|err| to_error!(location, "{err}"))? {
138-
TodoState::OpenParens => {
134+
TodoState::OpenBlock => {
139135
let mut parenthesized_block = Node::Empty;
140136
parse_block(tokens, p_state, &mut parenthesized_block)?;
141137
current
@@ -144,6 +140,28 @@ pub fn handle_symbol(
144140
parse_block(tokens, p_state, current)
145141
}
146142
TodoState::None => parse_block(tokens, p_state, current),
147-
TodoState::CloseParens | TodoState::CloseBlock | TodoState::SemiColon => Ok(()),
143+
TodoState::CloseBlock => Ok(()),
144+
TodoState::OpenBracket => {
145+
let mut bracket_node = Node::Empty;
146+
parse_block(tokens, p_state, &mut bracket_node)?;
147+
if p_state.closing_bracket {
148+
current
149+
.push_op(BOp::ArraySubscript)
150+
.map_err(|err| to_error!(location, "{err}"))?;
151+
current
152+
.push_block_as_leaf(bracket_node)
153+
.map_err(|err| to_error!(location, "{err}"))?;
154+
parse_block(tokens, p_state, current)
155+
} else {
156+
Err(to_error!(
157+
location,
158+
"Expected expression found block, as argument of an array subscript."
159+
))
160+
}
161+
}
162+
TodoState::CloseBracket => {
163+
p_state.closing_bracket = true;
164+
Ok(())
165+
}
148166
}
149167
}

0 commit comments

Comments
 (0)