This repository has been archived by the owner on Jan 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
MiniaczQ
authored and
MiniaczQ
committed
May 13, 2022
1 parent
863238d
commit cc01c9b
Showing
13 changed files
with
850 additions
and
841 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
/// Possible keywords | ||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
pub enum Keyword { | ||
Int, | ||
Float, | ||
Bool, | ||
String, | ||
Let, | ||
Fn, | ||
Return, | ||
While, | ||
For, | ||
In, | ||
If, | ||
Else, | ||
True, | ||
False, | ||
} | ||
/// Possible keywords | ||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
pub enum Keyword { | ||
Int, | ||
Float, | ||
Bool, | ||
String, | ||
Let, | ||
Fn, | ||
Return, | ||
While, | ||
For, | ||
In, | ||
If, | ||
Else, | ||
True, | ||
False, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,22 @@ | ||
#[macro_export] | ||
/// Shorthand for branching character matching | ||
/// | ||
/// First two arguments are `TokenBuilder` and explression to be returned in default case | ||
/// | ||
/// Each pair of arguments after that are a character constant and expression to be returned in that case | ||
macro_rules! char_match { | ||
($token_builder: expr, $default: expr) => { { | ||
$token_builder.pop(); | ||
Some($default) | ||
} }; | ||
($token_builder: expr, $default: expr, $($pattern: literal, $operator: expr), +) => { { | ||
$token_builder.pop(); | ||
match $token_builder.peek() { | ||
$($pattern => { | ||
$token_builder.pop(); | ||
Some($operator) | ||
},)* | ||
_ => Some($default), | ||
} | ||
} }; | ||
} | ||
#[macro_export] | ||
/// Shorthand for branching character matching | ||
/// | ||
/// First two arguments are `TokenBuilder` and explression to be returned in default case | ||
/// | ||
/// Each pair of arguments after that are a character constant and expression to be returned in that case | ||
macro_rules! char_match { | ||
($token_builder: expr, $default: expr) => { { | ||
$token_builder.pop(); | ||
Some($default) | ||
} }; | ||
($token_builder: expr, $default: expr, $($pattern: literal, $operator: expr), +) => { { | ||
$token_builder.pop(); | ||
match $token_builder.peek() { | ||
$($pattern => { | ||
$token_builder.pop(); | ||
Some($operator) | ||
},)* | ||
_ => Some($default), | ||
} | ||
} }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
pub mod comment; | ||
pub mod identifier_or_keyword; | ||
pub mod numerical; | ||
pub mod operator; | ||
pub mod string; | ||
mod test_utils; | ||
pub mod comment; | ||
pub mod identifier_or_keyword; | ||
pub mod numerical; | ||
pub mod operator; | ||
pub mod string; | ||
mod test_utils; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,29 @@ | ||
use std::io::BufReader; | ||
|
||
use crate::lexer::{ | ||
char_scanner::CharScanner, | ||
lexem::{Lexem, LexemBuilder, LexemType}, | ||
}; | ||
|
||
#[allow(dead_code)] | ||
pub fn matcher_with( | ||
matcher: fn(&mut LexemBuilder) -> Option<Lexem>, | ||
string: &'static str, | ||
) -> Option<Lexem> { | ||
let scanner = &mut CharScanner::new(BufReader::new(string.as_bytes())); | ||
let lb = &mut LexemBuilder::new(scanner); | ||
matcher(lb) | ||
} | ||
|
||
#[allow(dead_code)] | ||
pub fn lexem_with( | ||
lexem_type: LexemType, | ||
start: (usize, usize), | ||
stop: (usize, usize), | ||
) -> Option<Lexem> { | ||
Some(Lexem { | ||
lexem_type, | ||
start: start.into(), | ||
stop: stop.into(), | ||
}) | ||
} | ||
use std::io::BufReader; | ||
|
||
use crate::lexer::{ | ||
char_scanner::CharScanner, | ||
lexem::{Lexem, LexemBuilder, LexemType}, | ||
}; | ||
|
||
#[allow(dead_code)] | ||
pub fn matcher_with( | ||
matcher: fn(&mut LexemBuilder) -> Option<Lexem>, | ||
string: &'static str, | ||
) -> Option<Lexem> { | ||
let scanner = &mut CharScanner::new(BufReader::new(string.as_bytes())); | ||
let lb = &mut LexemBuilder::new(scanner); | ||
matcher(lb) | ||
} | ||
|
||
#[allow(dead_code)] | ||
pub fn lexem_with( | ||
lexem_type: LexemType, | ||
start: (usize, usize), | ||
stop: (usize, usize), | ||
) -> Option<Lexem> { | ||
Some(Lexem { | ||
lexem_type, | ||
start: start.into(), | ||
stop: stop.into(), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ use self::{ | |
|
||
pub struct Lexer { | ||
pub scanner: CharScanner, | ||
// TODO bufor na błędy | ||
} | ||
|
||
impl Lexer { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,30 @@ | ||
/// Possible operators | ||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
pub enum Operator { | ||
Plus, // + | ||
Minus, // - | ||
Asterisk, // * | ||
Slash, // / | ||
Modulo, // % | ||
ExclamationMark, // ! | ||
And, // & | ||
Or, // | | ||
Unequal, // != | ||
DoubleEqual, // == | ||
Greater, // > | ||
GreaterEqual, // >= | ||
Lesser, // < | ||
LesserEqual, // <= | ||
OpenRoundBracket, // ( | ||
CloseRoundBracket, // ) | ||
OpenSquareBracket, // [ | ||
CloseSquareBracket, // ] | ||
OpenCurlyBracket, // { | ||
CloseCurlyBracket, // } | ||
Colon, // : | ||
DoubleColon, // :: | ||
Equal, // = | ||
Arrow, // -> | ||
Semicolon, // ; | ||
Split, // , | ||
} | ||
/// Possible operators | ||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
pub enum Operator { | ||
Plus, // + | ||
Minus, // - | ||
Asterisk, // * | ||
Slash, // / | ||
Modulo, // % | ||
ExclamationMark, // ! | ||
And, // & | ||
Or, // | | ||
Unequal, // != | ||
DoubleEqual, // == | ||
Greater, // > | ||
GreaterEqual, // >= | ||
Lesser, // < | ||
LesserEqual, // <= | ||
OpenRoundBracket, // ( | ||
CloseRoundBracket, // ) | ||
OpenSquareBracket, // [ | ||
CloseSquareBracket, // ] | ||
OpenCurlyBracket, // { | ||
CloseCurlyBracket, // } | ||
Colon, // : | ||
DoubleColon, // :: | ||
Equal, // = | ||
Arrow, // -> | ||
Semicolon, // ; | ||
Split, // , | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
Instruction manual. | ||
-f/--file <file path> - Interpret a file | ||
Instruction manual. | ||
-f/--file <file path> - Interpret a file | ||
-i/--interactive - Interpret standard input |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,39 @@ | ||
use std::fmt::Display; | ||
|
||
/// Position of a token | ||
#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
pub struct Position { | ||
row: usize, | ||
col: usize, | ||
} | ||
|
||
impl Position { | ||
/// Forwards the column | ||
pub fn next_col(&mut self) { | ||
self.col += 1; | ||
} | ||
|
||
/// Forwards the line and resets the column | ||
pub fn next_line(&mut self) { | ||
self.row += 1; | ||
self.col = 1; | ||
} | ||
} | ||
|
||
impl Default for Position { | ||
fn default() -> Self { | ||
Self { row: 1, col: 1 } | ||
} | ||
} | ||
|
||
impl Display for Position { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
f.write_fmt(format_args!("Ln {}, Col {}", self.row, self.col)) | ||
} | ||
} | ||
|
||
impl From<(usize, usize)> for Position { | ||
fn from((row, col): (usize, usize)) -> Self { | ||
Self { row, col } | ||
} | ||
} | ||
use std::fmt::Display; | ||
|
||
/// Position of a token | ||
#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
pub struct Position { | ||
row: usize, | ||
col: usize, | ||
} | ||
|
||
impl Position { | ||
/// Forwards the column | ||
pub fn next_col(&mut self) { | ||
self.col += 1; | ||
} | ||
|
||
/// Forwards the line and resets the column | ||
pub fn next_line(&mut self) { | ||
self.row += 1; | ||
self.col = 1; | ||
} | ||
} | ||
|
||
impl Default for Position { | ||
fn default() -> Self { | ||
Self { row: 1, col: 1 } | ||
} | ||
} | ||
|
||
impl Display for Position { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
f.write_fmt(format_args!("Ln {}, Col {}", self.row, self.col)) | ||
} | ||
} | ||
|
||
impl From<(usize, usize)> for Position { | ||
fn from((row, col): (usize, usize)) -> Self { | ||
Self { row, col } | ||
} | ||
} |