jackfiled
783c5b3e3e
All checks were successful
Run Unit Tests / Run-Unit-Tests (push) Successful in -33s
420 lines
6.9 KiB
Rust
420 lines
6.9 KiB
Rust
use nom::InputLength;
|
|
use rustic_sysy::parser::compiler_unit_parser;
|
|
use rustic_sysy::tokenizer::lexical_parser;
|
|
|
|
fn parse_grammar(input: &'static str) {
|
|
let (_, tokens) = lexical_parser(input).unwrap();
|
|
let (remaining, node) = compiler_unit_parser((&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];
|
|
}");
|
|
}
|
|
|
|
#[test]
|
|
fn operator_priority_test() {
|
|
parse_grammar(r"//test the priority of add and mul
|
|
int main(){
|
|
int a, b, c, d;
|
|
a = 10;
|
|
b = 4;
|
|
c = 2;
|
|
d = 2;
|
|
return c + a * b - d;
|
|
}");
|
|
|
|
parse_grammar(r"//test the priority of add and mul
|
|
int main(){
|
|
int a, b, c, d;
|
|
a = 10;
|
|
b = 4;
|
|
c = 2;
|
|
d = 2;
|
|
return (c + a) * (b - d);
|
|
}");
|
|
|
|
parse_grammar(r"//test the priority of unary operator and binary operator
|
|
int main(){
|
|
int a, b;
|
|
a = 10;
|
|
b = 30;
|
|
return a - -5 + b + -5;
|
|
}");
|
|
}
|
|
|
|
#[test]
|
|
fn statement_test() {
|
|
parse_grammar(r"int k;
|
|
const int n = 10;
|
|
int main () {
|
|
int i = 0;
|
|
k = 1;
|
|
while (i <= n - 1) {
|
|
i = i + 1;
|
|
k + 1;
|
|
k = k + k;
|
|
}
|
|
putint(k);
|
|
return k;
|
|
}");
|
|
|
|
parse_grammar(r"// test if-else-if
|
|
int ifElseIf() {
|
|
int a;
|
|
a = 5;
|
|
int b;
|
|
b = 10;
|
|
if(a == 6 || b == 0xb) {
|
|
return a;
|
|
}
|
|
else {
|
|
if (b == 10 && a == 1)
|
|
a = 25;
|
|
else if (b == 10 && a == -5)
|
|
a = a + 15;
|
|
else
|
|
a = -+a;
|
|
}
|
|
|
|
return a;
|
|
}
|
|
|
|
int main(){
|
|
putint(ifElseIf());
|
|
return 0;
|
|
}");
|
|
|
|
parse_grammar(r"// test if-if-else
|
|
int ififElse() {
|
|
int a;
|
|
a = 5;
|
|
int b;
|
|
b = 10;
|
|
if(a == 5)
|
|
if (b == 10)
|
|
a = 25;
|
|
else
|
|
a = a + 15;
|
|
|
|
return (a);
|
|
}
|
|
|
|
int main(){
|
|
return (ififElse());
|
|
}");
|
|
|
|
parse_grammar(r"// test if-{if-else}
|
|
int if_ifElse_() {
|
|
int a;
|
|
a = 5;
|
|
int b;
|
|
b = 10;
|
|
if(a == 5){
|
|
if (b == 10)
|
|
a = 25;
|
|
else
|
|
a = a + 15;
|
|
}
|
|
return (a);
|
|
}
|
|
|
|
int main(){
|
|
return (if_ifElse_());
|
|
}");
|
|
|
|
parse_grammar(r"// 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);
|
|
}
|
|
|
|
|
|
int main(){
|
|
return (whileIf());
|
|
}");
|
|
|
|
parse_grammar(r"int deepWhileBr(int a, int b) {
|
|
int c;
|
|
c = a + b;
|
|
while (c < 75) {
|
|
int d;
|
|
d = 42;
|
|
if (c < 100) {
|
|
c = c + d;
|
|
if (c > 99) {
|
|
int e;
|
|
e = d * 2;
|
|
if (1 == 1) {
|
|
c = e * 2;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return (c);
|
|
}
|
|
|
|
int main() {
|
|
int p;
|
|
p = 2;
|
|
return deepWhileBr(p, p);
|
|
}");
|
|
}
|
|
|
|
#[test]
|
|
fn dijkstra_test() {
|
|
parse_grammar(r"const int INF = 65535;
|
|
int e[16][16];
|
|
int book[16];
|
|
int dis[16];
|
|
int n, m;
|
|
int v1, v2, w;
|
|
|
|
void Dijkstra()
|
|
{
|
|
int i, j;
|
|
|
|
i = 1;
|
|
while (i <= n) {
|
|
dis[i] = e[1][i];
|
|
book[i] = 0;
|
|
i = i + 1;
|
|
}
|
|
book[1] = 1;
|
|
|
|
i = 1;
|
|
while (i <= n - 1) {
|
|
int min_num = INF;
|
|
int min_index = 0;
|
|
int k = 1;
|
|
while (k <= n) {
|
|
if (min_num > dis[k] && book[k] == 0) {
|
|
min_num = dis[k];
|
|
min_index = k;
|
|
}
|
|
k = k + 1;
|
|
}
|
|
book[min_index] = 1;
|
|
int j = 1;
|
|
while (j <= n) {
|
|
if (e[min_index][j] < INF) {
|
|
if (dis[j] > dis[min_index] + e[min_index][j]) {
|
|
dis[j] = dis[min_index] + e[min_index][j];
|
|
}
|
|
}
|
|
j = j + 1;
|
|
}
|
|
i = i + 1;
|
|
}
|
|
}
|
|
|
|
int main()
|
|
{
|
|
int i;
|
|
n = getint();
|
|
m = getint();
|
|
|
|
i = 1;
|
|
while (i <= n) {
|
|
int j = 1;
|
|
while (j <= n) {
|
|
if (i == j)
|
|
e[i][j] = 0;
|
|
else
|
|
e[i][j] = INF;
|
|
j = j + 1;
|
|
}
|
|
i = i + 1;
|
|
}
|
|
|
|
i = 1;
|
|
while (i <= m) {
|
|
int u = getint(), v = getint();
|
|
e[u][v] = getint();
|
|
i = i + 1;
|
|
}
|
|
|
|
Dijkstra();
|
|
|
|
i = 1;
|
|
while (i <= n) {
|
|
putint(dis[i]);
|
|
putch(32);
|
|
i = i + 1;
|
|
}
|
|
putch(10);
|
|
return 0;
|
|
}");
|
|
}
|
|
|
|
#[test]
|
|
fn sort_test() {
|
|
parse_grammar(r"int n;
|
|
int bubblesort(int arr[])
|
|
{
|
|
int i;
|
|
int j;
|
|
i =0;
|
|
while(i < n-1){
|
|
// Last i elements are already in place
|
|
j = 0;
|
|
while(j < n-i-1){
|
|
if (arr[j] > arr[j+1]) {
|
|
// swap(&arr[j], &arr[j+1]);
|
|
int tmp;
|
|
tmp = arr[j+1];
|
|
arr[j+1] = arr[j];
|
|
arr[j] = tmp;
|
|
}
|
|
j = j + 1;
|
|
}
|
|
i = i + 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int main(){
|
|
n = 10;
|
|
int a[10];
|
|
a[0]=4;a[1]=3;a[2]=9;a[3]=2;a[4]=0;
|
|
a[5]=1;a[6]=6;a[7]=5;a[8]=7;a[9]=8;
|
|
int i;
|
|
i = bubblesort(a);
|
|
while (i < n) {
|
|
int tmp;
|
|
tmp = a[i];
|
|
putint(tmp);
|
|
tmp = 10;
|
|
putch(tmp);
|
|
i = i + 1;
|
|
}
|
|
return 0;
|
|
}");
|
|
|
|
parse_grammar(r"int n;
|
|
int insertsort(int a[])
|
|
{
|
|
int i;
|
|
i = 1;
|
|
while(i<n)
|
|
{
|
|
int temp;
|
|
temp=a[i];
|
|
int j;
|
|
j=i-1;
|
|
while(j>-1&&temp<a[j])
|
|
{
|
|
a[j+1]=a[j];
|
|
j = j - 1;
|
|
}
|
|
a[j+1]=temp;
|
|
i = i + 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int main(){
|
|
n = 10;
|
|
int a[10];
|
|
a[0]=4;a[1]=3;a[2]=9;a[3]=2;a[4]=0;
|
|
a[5]=1;a[6]=6;a[7]=5;a[8]=7;a[9]=8;
|
|
int i;
|
|
i = insertsort(a);
|
|
while (i < n) {
|
|
int tmp;
|
|
tmp = a[i];
|
|
putint(tmp);
|
|
tmp = 10;
|
|
putch(tmp);
|
|
i = i + 1;
|
|
}
|
|
return 0;
|
|
}");
|
|
}
|
|
|
|
|
|
|
|
|