-
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.
* feat: implement boolean tokenizer * feat: implement string parser * feat: implement unary operator tokenizer * feat: implement binary operator tokenizer * feat: implement if operator tokenizer * refactor: move decrypt process to tokenizer * feat: implement stringifier * feat: implement unary, binary parser
- Loading branch information
Showing
6 changed files
with
332 additions
and
20 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"files.associations": { | ||
".fantomasignore": "ignore", | ||
"*.html": "html", | ||
"*.blade.php": "html", | ||
"*.tsx": "typescriptreact", | ||
"*.bnf": "ebnf", | ||
"*.txt": "plaintext", | ||
"*.ts": "typescript", | ||
"あ": "plaintext", | ||
"ostream": "cpp" | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use icfpc2024::tokenizer::Tokenizer; | ||
use proconio::input; | ||
|
||
fn main() { | ||
input! { | ||
text: String, | ||
} | ||
let mut tokenizer = Tokenizer::new(&text); | ||
let result = tokenizer.tokenize(); | ||
|
||
for token in result { | ||
println!("{:?}", token); | ||
} | ||
} |
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,26 +1,145 @@ | ||
use crate::tokenizer::Token; | ||
|
||
const INTEGER_ASCII: &str = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; | ||
#[derive(Debug, PartialEq)] | ||
enum Node { | ||
IntegerLiteral(usize), | ||
StringLiteral(String), | ||
UnaryOperator((String, Box<Node>)), | ||
BinaryOperator((String, Box<Node>, Box<Node>)), | ||
} | ||
|
||
pub struct Parser<'a> { | ||
tokens: &'a [Token], | ||
position: usize, | ||
} | ||
|
||
impl<'a> Parser<'a> { | ||
pub fn new(tokens: &'a [Token]) -> Self { | ||
Self { | ||
tokens, | ||
position: 0, | ||
} | ||
} | ||
|
||
pub fn parse(&mut self) -> Node { | ||
self.parse_node() | ||
} | ||
|
||
fn parse_node(&mut self) -> Node { | ||
match self.tokens[self.position] { | ||
Token::Integer(value) => { | ||
self.position += 1; | ||
Node::IntegerLiteral(value) | ||
} | ||
Token::String(ref value) => { | ||
self.position += 1; | ||
Node::StringLiteral(value.clone()) | ||
} | ||
Token::UnaryOperator(_) => self.parse_unary(), | ||
Token::BinaryOperator(_) => self.parse_binary(), | ||
_ => panic!("Expected integer or unary operator"), | ||
} | ||
} | ||
|
||
fn parse_unary(&mut self) -> Node { | ||
let operator = match self.tokens[self.position] { | ||
Token::UnaryOperator(ref operator) => operator.clone(), | ||
_ => panic!("Expected unary operator"), | ||
}; | ||
self.position += 1; | ||
let operand = Box::new(self.parse_node()); | ||
Node::UnaryOperator((operator, operand)) | ||
} | ||
|
||
fn parse_integer(indicator: Token) -> usize { | ||
assert!(matches!(indicator, Token::Integer(_))); | ||
let mut result = 0; | ||
for c in indicator.to_string().chars() { | ||
let index = INTEGER_ASCII.find(c).unwrap(); | ||
result = result * INTEGER_ASCII.len() + index; | ||
fn parse_binary(&mut self) -> Node { | ||
let operator = match self.tokens[self.position] { | ||
Token::BinaryOperator(ref operator) => operator.clone(), | ||
_ => panic!("Expected binary operator"), | ||
}; | ||
self.position += 1; | ||
let left = Box::new(self.parse_node()); | ||
let right = Box::new(self.parse_node()); | ||
Node::BinaryOperator((operator, left, right)) | ||
} | ||
result | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_parse_integer() { | ||
assert_eq!(INTEGER_ASCII.len(), 94); | ||
let input = Token::Integer("/6".to_string()); | ||
let expected = 1337; | ||
assert_eq!(parse_integer(input), expected); | ||
fn test_parse_unary() { | ||
let tokens = vec![Token::UnaryOperator("-".to_string()), Token::Integer(3)]; | ||
let mut parser = Parser::new(&tokens); | ||
let node = parser.parse_unary(); | ||
assert_eq!( | ||
node, | ||
Node::UnaryOperator(("-".to_string(), Box::new(Node::IntegerLiteral(3)))) | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_parse_nested_unary() { | ||
let tokens = vec![ | ||
Token::UnaryOperator("-".to_string()), | ||
Token::UnaryOperator("-".to_string()), | ||
Token::Integer(3), | ||
]; | ||
let mut parser = Parser::new(&tokens); | ||
let node = parser.parse_unary(); | ||
assert_eq!( | ||
node, | ||
Node::UnaryOperator(( | ||
"-".to_string(), | ||
Box::new(Node::UnaryOperator(( | ||
"-".to_string(), | ||
Box::new(Node::IntegerLiteral(3)) | ||
))) | ||
)) | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_parse_binary() { | ||
let tokens = vec![ | ||
Token::BinaryOperator("+".to_string()), | ||
Token::Integer(3), | ||
Token::Integer(4), | ||
]; | ||
let mut parser = Parser::new(&tokens); | ||
let node = parser.parse_binary(); | ||
assert_eq!( | ||
node, | ||
Node::BinaryOperator(( | ||
"+".to_string(), | ||
Box::new(Node::IntegerLiteral(3)), | ||
Box::new(Node::IntegerLiteral(4)) | ||
)) | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_parse_nested_binary() { | ||
let tokens = vec![ | ||
Token::BinaryOperator("+".to_string()), | ||
Token::Integer(3), | ||
Token::BinaryOperator("+".to_string()), | ||
Token::Integer(4), | ||
Token::Integer(5), | ||
]; | ||
let mut parser = Parser::new(&tokens); | ||
let node = parser.parse_binary(); | ||
assert_eq!( | ||
node, | ||
Node::BinaryOperator(( | ||
"+".to_string(), | ||
Box::new(Node::IntegerLiteral(3)), | ||
Box::new(Node::BinaryOperator(( | ||
"+".to_string(), | ||
Box::new(Node::IntegerLiteral(4)), | ||
Box::new(Node::IntegerLiteral(5)) | ||
))) | ||
)) | ||
); | ||
} | ||
} |
Oops, something went wrong.