2024-08-27 18:08:18 +08:00
|
|
|
use rustic_sysy::tokenizer::{lexical_parser, LexicalTokenType};
|
|
|
|
use rustic_sysy::tokenizer::LexicalTokenType::{ConstInteger, Delimiter, Identifier, Keyword, Operator};
|
|
|
|
|
|
|
|
fn validate_tokens(input: &'static str, tokens: Vec<LexicalTokenType>) {
|
|
|
|
let (_, actual_tokens) = lexical_parser(input).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(tokens.len(), actual_tokens.len());
|
|
|
|
|
|
|
|
for (actual, except) in actual_tokens.iter().zip(tokens.iter()) {
|
|
|
|
assert_eq!(&actual.token_type, except,
|
|
|
|
"The literal value of actual token is {}", actual.literal_value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn main_test() {
|
2024-09-11 18:11:06 +08:00
|
|
|
validate_tokens("int main() { return 0; }", vec![
|
2024-08-27 18:08:18 +08:00
|
|
|
Keyword,
|
|
|
|
Identifier,
|
|
|
|
Delimiter,
|
2024-09-11 18:11:06 +08:00
|
|
|
Delimiter,
|
|
|
|
Delimiter,
|
2024-08-27 18:08:18 +08:00
|
|
|
Keyword,
|
|
|
|
ConstInteger(0),
|
|
|
|
Delimiter,
|
|
|
|
Delimiter
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2024-09-11 18:11:06 +08:00
|
|
|
#[test]
|
|
|
|
fn number_test() {
|
|
|
|
validate_tokens("123", vec![
|
|
|
|
ConstInteger(123)
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
2024-08-27 18:08:18 +08:00
|
|
|
#[test]
|
|
|
|
fn hexadecimal_test() {
|
|
|
|
validate_tokens("// test hexadecimal define
|
|
|
|
int main(){
|
|
|
|
int a;
|
|
|
|
a = 0xf;
|
|
|
|
return a;
|
|
|
|
}", vec![
|
|
|
|
Keyword,
|
|
|
|
Identifier,
|
|
|
|
Delimiter,
|
|
|
|
Delimiter,
|
|
|
|
Delimiter,
|
|
|
|
Keyword,
|
|
|
|
Identifier,
|
|
|
|
Delimiter,
|
|
|
|
Identifier,
|
|
|
|
Operator,
|
|
|
|
ConstInteger(15),
|
|
|
|
Delimiter,
|
|
|
|
Keyword,
|
|
|
|
Identifier,
|
|
|
|
Delimiter,
|
|
|
|
Delimiter
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn while_and_if_test() {
|
|
|
|
validate_tokens("
|
|
|
|
// test while-if
|
|
|
|
int whileIf() {
|
|
|
|
int a;
|
|
|
|
a = 0;
|
|
|
|
int b;
|
|
|
|
b = 0;
|
|
|
|
while (a < 100) {
|
|
|
|
if (a == 5) {
|
|
|
|
b = 25;
|
|
|
|
}
|
|
|
|
else if (a == 10) {
|
|
|
|
b = 42;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
b = a * 2;
|
|
|
|
}
|
|
|
|
a = a + 1;
|
|
|
|
}
|
|
|
|
return (b);
|
|
|
|
}", vec![
|
|
|
|
// int whileIf() {
|
|
|
|
Keyword,
|
|
|
|
Identifier,
|
|
|
|
Delimiter,
|
|
|
|
Delimiter,
|
|
|
|
Delimiter,
|
|
|
|
// int a;
|
|
|
|
Keyword,
|
|
|
|
Identifier,
|
|
|
|
Delimiter,
|
|
|
|
// a = 0;
|
|
|
|
Identifier,
|
|
|
|
Operator,
|
|
|
|
ConstInteger(0),
|
|
|
|
Delimiter,
|
|
|
|
// int b;
|
|
|
|
Keyword,
|
|
|
|
Identifier,
|
|
|
|
Delimiter,
|
|
|
|
// b = 0;
|
|
|
|
Identifier,
|
|
|
|
Operator,
|
|
|
|
ConstInteger(0),
|
|
|
|
Delimiter,
|
|
|
|
// while (a < 100) {
|
|
|
|
Keyword,
|
|
|
|
Delimiter,
|
|
|
|
Identifier,
|
|
|
|
Operator,
|
|
|
|
ConstInteger(100),
|
|
|
|
Delimiter,
|
|
|
|
Delimiter,
|
|
|
|
// if (a == 5) {
|
|
|
|
Keyword,
|
|
|
|
Delimiter,
|
|
|
|
Identifier,
|
|
|
|
Operator,
|
|
|
|
ConstInteger(5),
|
|
|
|
Delimiter,
|
|
|
|
Delimiter,
|
|
|
|
// b = 25;
|
|
|
|
Identifier,
|
|
|
|
Operator,
|
|
|
|
ConstInteger(25),
|
|
|
|
Delimiter,
|
|
|
|
// }
|
|
|
|
Delimiter,
|
|
|
|
// else if (a == 10) {
|
|
|
|
Keyword,
|
|
|
|
Keyword,
|
|
|
|
Delimiter,
|
|
|
|
Identifier,
|
|
|
|
Operator,
|
|
|
|
ConstInteger(10),
|
|
|
|
Delimiter,
|
|
|
|
Delimiter,
|
|
|
|
// b = 42;
|
|
|
|
Identifier,
|
|
|
|
Operator,
|
|
|
|
ConstInteger(42),
|
|
|
|
Delimiter,
|
|
|
|
// }
|
|
|
|
Delimiter,
|
|
|
|
// else {
|
|
|
|
Keyword,
|
|
|
|
Delimiter,
|
|
|
|
// b = a * 2;
|
|
|
|
Identifier,
|
|
|
|
Operator,
|
|
|
|
Identifier,
|
|
|
|
Operator,
|
|
|
|
ConstInteger(2),
|
|
|
|
Delimiter,
|
|
|
|
// }
|
|
|
|
Delimiter,
|
|
|
|
// a = a + 1;
|
|
|
|
Identifier,
|
|
|
|
Operator,
|
|
|
|
Identifier,
|
|
|
|
Operator,
|
|
|
|
ConstInteger(1),
|
|
|
|
Delimiter,
|
|
|
|
// }
|
|
|
|
Delimiter,
|
|
|
|
// return (b);
|
|
|
|
Keyword,
|
|
|
|
Delimiter,
|
|
|
|
Identifier,
|
|
|
|
Delimiter,
|
|
|
|
Delimiter,
|
|
|
|
// }
|
|
|
|
Delimiter
|
|
|
|
]);
|
|
|
|
}
|