rustic-sysy/tests/grammar_tests.rs
jackfiled 4de4eec758
Some checks failed
Run Unit Tests / Run-Unit-Tests (push) Failing after 31s
add: grammar tests for all sysy files.
fix: stack overflow when dropping long BinaryOperators.
2024-11-04 21:03:10 +08:00

97 lines
1.8 KiB
Rust

use nom::InputLength;
use rustic_sysy::parser::parse_compiler_unit;
use rustic_sysy::tokenizer::lexical_parser;
fn parse_grammar(input: &str) {
let (_, tokens) = lexical_parser(input).unwrap();
let (remaining, node) = parse_compiler_unit((&tokens).into()).unwrap();
dbg!(node);
assert_eq!(0, remaining.input_len());
}
#[test]
fn main_test() {
parse_grammar(
r"int main(){
return 3;
}",
);
}
#[test]
fn value_definition_test() {
parse_grammar(
r"//test domain of global var define and local define
int a = 3;
int b = 5;
int main(){
int a = 5;
return a + b;
}",
);
parse_grammar(
r"//test local var define
int main(){
int a, b0, _c;
a = 1;
b0 = 2;
_c = 3;
return b0 + _c;
}",
);
parse_grammar(
r"//test const gloal var define
const int a = 10, b = 5;
int main(){
return b;
}",
);
parse_grammar(
r"//test const local var define
int main(){
const int a = 10, b = 5;
return b;
}",
);
}
#[test]
fn array_definition_test() {
parse_grammar(
r"int a[10][10];
int main(){
return 0;
}",
);
parse_grammar(
r"//test array define
int main(){
int a[4][2] = {};
int b[4][2] = {1, 2, 3, 4, 5, 6, 7, 8};
int c[4][2] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
int d[4][2] = {1, 2, {3}, {5}, 7 , 8};
int e[4][2] = {{d[2][1], c[2][1]}, {3, 4}, {5, 6}, {7, 8}};
return e[3][1] + e[0][0] + e[0][1] + a[2][0];
}",
);
parse_grammar(
r"int main(){
const int a[4][2] = {{1, 2}, {3, 4}, {}, 7};
const int N = 3;
int b[4][2] = {};
int c[4][2] = {1, 2, 3, 4, 5, 6, 7, 8};
int d[N + 1][2] = {1, 2, {3}, {5}, a[3][0], 8};
int e[4][2][1] = {{d[2][1], {c[2][1]}}, {3, 4}, {5, 6}, {7, 8}};
return e[3][1][0] + e[0][0][0] + e[0][1][0] + d[3][0];
}",
);
}