Skip to content

Commit 7374c8c

Browse files
authored
pre processor rewrite (#118)
Adds: - CONCAT(LEFT, RIGHT) macro to join tokens - Allow putting parenthesis around simple (non-function) like macros
2 parents 0b56f65 + 443f22d commit 7374c8c

File tree

7 files changed

+574
-218
lines changed

7 files changed

+574
-218
lines changed

Cargo.lock

Lines changed: 104 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ base64 = "0.22.1"
1111
clap = { version = "4.5.20", features = ["derive"] }
1212
clap_complete_command = "0.6.1"
1313
clap_derive = "4.5.18"
14+
color-debug = "0.1.1"
1415
colored = "2.1.0"
1516
csscolorparser = "0.7.0"
1617
directories = "6.0.0"

src/diagnostic/diagnostic_kind.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ pub enum DiagnosticKind {
5252
func: SmolStr,
5353
given: usize,
5454
},
55+
MacroArgsCountMismatch {
56+
expected: usize,
57+
given: usize,
58+
},
5559
CommandFailed {
5660
stderr: Vec<u8>,
5761
},
@@ -150,6 +154,12 @@ impl DiagnosticKind {
150154
given
151155
)
152156
}
157+
DiagnosticKind::MacroArgsCountMismatch { expected, given } => {
158+
format!(
159+
"macro expects {} arguments, but {} were given",
160+
expected, given
161+
)
162+
}
153163
DiagnosticKind::CommandFailed { .. } => "command failed".to_string(),
154164
DiagnosticKind::TypeMismatch { expected, given } => {
155165
format!("type mismatch: expected {}, but got {}", expected, given)
@@ -225,6 +235,7 @@ impl From<&DiagnosticKind> for Level {
225235
| DiagnosticKind::ReprArgsCountMismatch { .. }
226236
| DiagnosticKind::ProcArgsCountMismatch { .. }
227237
| DiagnosticKind::FuncArgsCountMismatch { .. }
238+
| DiagnosticKind::MacroArgsCountMismatch { .. }
228239
| DiagnosticKind::CommandFailed { .. }
229240
| DiagnosticKind::TypeMismatch { .. }
230241
| DiagnosticKind::NotStruct

src/lexer/token.rs

Lines changed: 126 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
use std::fmt::{
2+
self,
3+
Display,
4+
};
5+
16
use logos::Logos;
27

38
use super::literal::*;
49
use crate::misc::SmolStr;
510

6-
#[derive(Debug, Logos, Clone)]
11+
#[derive(Debug, Logos, Clone, PartialEq)]
712
#[logos(skip r"[ \r\t\f]+")]
813
#[logos(skip r"#[^\n]*\n")]
914
pub enum Token {
@@ -234,3 +239,123 @@ pub enum Token {
234239
#[token("var")]
235240
Var,
236241
}
242+
243+
impl Display for Token {
244+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
245+
match self {
246+
Token::Name(name) => write!(f, "name{}", name),
247+
Token::Define => write!(f, "%define"),
248+
Token::Undef => write!(f, "%undef"),
249+
Token::Newline => write!(f, "\n"),
250+
Token::Backslash => write!(f, "\\"),
251+
Token::Arg(name) => write!(f, "${}", name),
252+
Token::Bin(value) => write!(f, "bin{}", value),
253+
Token::Oct(value) => write!(f, "oct{}", value),
254+
Token::Int(value) => write!(f, "int{}", value),
255+
Token::Hex(value) => write!(f, "hex{}", value),
256+
Token::Float(value) => write!(f, "float{}", value),
257+
Token::Str(value) => write!(f, "str{}", value),
258+
Token::Cmd(value) => write!(f, "cmd{}", value),
259+
Token::Costumes => write!(f, "costumes"),
260+
Token::Sounds => write!(f, "sounds"),
261+
Token::Local => write!(f, "local"),
262+
Token::Proc => write!(f, "proc"),
263+
Token::Func => write!(f, "func"),
264+
Token::Return => write!(f, "return"),
265+
Token::NoWarp => write!(f, "nowarp"),
266+
Token::On => write!(f, "on"),
267+
Token::OnFlag => write!(f, "onflag"),
268+
Token::OnKey => write!(f, "onkey"),
269+
Token::OnClick => write!(f, "onclick"),
270+
Token::OnBackdrop => write!(f, "onbackdrop"),
271+
Token::OnLoudness => write!(f, "onloudness"),
272+
Token::OnTimer => write!(f, "ontimer"),
273+
Token::OnClone => write!(f, "onclone"),
274+
Token::If => write!(f, "if"),
275+
Token::Else => write!(f, "else"),
276+
Token::Elif => write!(f, "elif"),
277+
Token::Until => write!(f, "until"),
278+
Token::Forever => write!(f, "forever"),
279+
Token::Repeat => write!(f, "repeat"),
280+
Token::Comma => write!(f, ","),
281+
Token::LParen => write!(f, "("),
282+
Token::RParen => write!(f, ")"),
283+
Token::LBrace => write!(f, "{{"),
284+
Token::RBrace => write!(f, "}}"),
285+
Token::Assign => write!(f, "="),
286+
Token::Eq => write!(f, "=="),
287+
Token::Increment => write!(f, "++"),
288+
Token::Decrement => write!(f, "--"),
289+
Token::AssignAdd => write!(f, "+="),
290+
Token::AssignSubtract => write!(f, "-="),
291+
Token::AssignMultiply => write!(f, "*="),
292+
Token::AssignDivide => write!(f, "/="),
293+
Token::AssignFloorDiv => write!(f, "//="),
294+
Token::AssignModulo => write!(f, "%="),
295+
Token::AssignJoin => write!(f, "&="),
296+
Token::LBracket => write!(f, "["),
297+
Token::RBracket => write!(f, "]"),
298+
Token::Dot => write!(f, "."),
299+
Token::Ne => write!(f, "!="),
300+
Token::Lt => write!(f, "<"),
301+
Token::Gt => write!(f, ">"),
302+
Token::Le => write!(f, "<="),
303+
Token::Ge => write!(f, ">="),
304+
Token::Not => write!(f, "not"),
305+
Token::And => write!(f, "and"),
306+
Token::Or => write!(f, "or"),
307+
Token::In => write!(f, "in"),
308+
Token::Amp => write!(f, "&"),
309+
Token::Plus => write!(f, "+"),
310+
Token::Minus => write!(f, "-"),
311+
Token::Star => write!(f, "*"),
312+
Token::Slash => write!(f, "/"),
313+
Token::FloorDiv => write!(f, "//"),
314+
Token::Percent => write!(f, "%"),
315+
Token::Semicolon => write!(f, ";"),
316+
Token::Colon => write!(f, ":"),
317+
Token::Length => write!(f, "length"),
318+
Token::Round => write!(f, "round"),
319+
Token::Abs => write!(f, "abs"),
320+
Token::Floor => write!(f, "floor"),
321+
Token::Ceil => write!(f, "ceil"),
322+
Token::Sqrt => write!(f, "sqrt"),
323+
Token::Sin => write!(f, "sin"),
324+
Token::Cos => write!(f, "cos"),
325+
Token::Tan => write!(f, "tan"),
326+
Token::Asin => write!(f, "asin"),
327+
Token::Acos => write!(f, "acos"),
328+
Token::Atan => write!(f, "atan"),
329+
Token::Ln => write!(f, "ln"),
330+
Token::Log => write!(f, "log"),
331+
Token::Antiln => write!(f, "antiln"),
332+
Token::Antilog => write!(f, "antilog"),
333+
Token::Show => write!(f, "show"),
334+
Token::Hide => write!(f, "hide"),
335+
Token::Add => write!(f, "add"),
336+
Token::To => write!(f, "to"),
337+
Token::Delete => write!(f, "delete"),
338+
Token::Insert => write!(f, "insert"),
339+
Token::At => write!(f, "at"),
340+
Token::Of => write!(f, "of"),
341+
Token::As => write!(f, "as"),
342+
Token::Enum => write!(f, "enum"),
343+
Token::Struct => write!(f, "struct"),
344+
Token::True => write!(f, "true"),
345+
Token::False => write!(f, "false"),
346+
Token::List => write!(f, "list"),
347+
Token::Cloud => write!(f, "cloud"),
348+
Token::Pipe => write!(f, "|>"),
349+
Token::SetX => write!(f, "set_x"),
350+
Token::SetY => write!(f, "set_y"),
351+
Token::SetSize => write!(f, "set_size"),
352+
Token::PointInDirection => write!(f, "point_in_direction"),
353+
Token::SetVolume => write!(f, "set_volume"),
354+
Token::SetRotationStyleLeftRight => write!(f, "set_rotation_style_left_right"),
355+
Token::SetRotationStyleAllAround => write!(f, "set_rotation_style_all_around"),
356+
Token::SetRotationStyleDoNotRotate => write!(f, "set_rotation_style_do_not_rotate"),
357+
Token::SetLayerOrder => write!(f, "set_layer_order"),
358+
Token::Var => write!(f, "var"),
359+
}
360+
}
361+
}

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use std::{
2121
use colored::Colorize;
2222

2323
fn main() -> ExitCode {
24+
// unsafe { color_debug::enable() };
2425
pretty_env_logger::init();
2526
std::panic::set_hook(Box::new(|info| {
2627
eprintln!(

src/parser.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
ast::Sprite,
66
diagnostic::Diagnostic,
77
lexer::adaptor,
8-
pre_processor::pre_processor,
8+
pre_processor::PreProcessor,
99
translation_unit::TranslationUnit,
1010
};
1111

@@ -17,7 +17,7 @@ pub fn parse(translation_unit: &TranslationUnit) -> Result<Sprite, Diagnostic> {
1717
let token = token?;
1818
tokens.push(token);
1919
}
20-
pre_processor(&mut tokens)?;
20+
PreProcessor::apply(&mut tokens)?;
2121
let parser = SpriteParser::new();
2222
let mut sprite = Sprite::default();
2323
parser.parse(&mut sprite, tokens)?;

0 commit comments

Comments
 (0)