Skip to content
This repository has been archived by the owner on Jan 6, 2025. It is now read-only.

Commit

Permalink
feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
MiniaczQ authored and MiniaczQ committed May 13, 2022
1 parent 863238d commit cc01c9b
Show file tree
Hide file tree
Showing 13 changed files with 850 additions and 841 deletions.
844 changes: 422 additions & 422 deletions about.md

Large diffs are not rendered by default.

532 changes: 266 additions & 266 deletions grammar.md

Large diffs are not rendered by default.

36 changes: 18 additions & 18 deletions src/lexer/keywords.rs
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,
}
44 changes: 22 additions & 22 deletions src/lexer/macros.rs
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),
}
} };
}
2 changes: 1 addition & 1 deletion src/lexer/matchers/identifier_or_keyword.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn match_identifier_or_keyword(tb: &mut LexemBuilder) -> Option<Lexem> {
if can_begin(tb.peek()) {
let mut name = vec![tb.peek()];
tb.pop();
while can_continue(tb.peek()) {
while can_continue(tb.peek()) { // TODO maksymalna długość
name.push(tb.peek());
tb.pop();
}
Expand Down
12 changes: 6 additions & 6 deletions src/lexer/matchers/mod.rs
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;
16 changes: 12 additions & 4 deletions src/lexer/matchers/numerical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ use crate::{
/// Matches an integer or a float constant
pub fn match_numerical(tb: &mut LexemBuilder) -> Option<Lexem> {
if tb.peek().is_ascii_digit() {
let mut integer_part: i64 = tb.peek() as i64 - '0' as i64;
let mut integer_part: i64 = tb.peek() as i64 - '0' as i64; // TODO wyjąc konwertowanie
if tb.peek() != '0' {
tb.pop();
loop {
loop { // TODO użyć while
if tb.peek().is_ascii_digit() {
if let Some(new_integer_part) = integer_part.checked_mul(10) {
integer_part = new_integer_part;
integer_part += tb.peek() as i64 - '0' as i64;
integer_part += tb.peek() as i64 - '0' as i64; // TODO Sprawdzić czy nie ma przepełnienia
tb.pop();
} else {
eprintln!(
Expand Down Expand Up @@ -44,7 +44,7 @@ pub fn match_numerical(tb: &mut LexemBuilder) -> Option<Lexem> {

/// Matches a float constant
fn match_float(tb: &mut LexemBuilder, integer_part: i64) -> Option<Lexem> {
if tb.peek() == '.' {
if tb.peek() == '.' { // TODO odwrócić warunek
tb.pop();
if tb.peek().is_ascii_digit() {
let mut digits = 1;
Expand Down Expand Up @@ -128,6 +128,14 @@ mod tests {
);
}

#[test]
fn int_limit2() {
assert_eq!(
matcher("9_223_372_036_854_775_808"),
int_lexem(9223372036854775800, (1, 1), (1, 26))
);
}

#[test]
fn int_above_limit() {
assert_eq!(
Expand Down
4 changes: 2 additions & 2 deletions src/lexer/matchers/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ fn complete_string(tb: &mut LexemBuilder) -> Lexem {
'\\' => {
let pos = tb.get_here();
tb.pop();
match tb.peek() {
match tb.peek() { // Więcej znaków ucieczki np. z JSON-a
'\\' => content.push('\\'),
'"' => content.push('"'),
c => {
eprintln!(
eprintln!( // Zwracać błąd lepiej
"Unknown escape sequence `\\{}` inside string at {}.",
c, pos
)
Expand Down
58 changes: 29 additions & 29 deletions src/lexer/matchers/test_utils.rs
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(),
})
}
1 change: 1 addition & 0 deletions src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use self::{

pub struct Lexer {
pub scanner: CharScanner,
// TODO bufor na błędy
}

impl Lexer {
Expand Down
60 changes: 30 additions & 30 deletions src/lexer/operators.rs
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, // ,
}
4 changes: 2 additions & 2 deletions src/manual.txt
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
78 changes: 39 additions & 39 deletions src/position.rs
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 }
}
}

0 comments on commit cc01c9b

Please sign in to comment.