Skip to content

Commit 3c3f597

Browse files
committed
Add project files
1 parent dce57fc commit 3c3f597

File tree

478 files changed

+12269
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

478 files changed

+12269
-0
lines changed

Makefile

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
CC=g++
2+
CPPFLAGS+=-std=c++11 -W -Wall -g -Wno-unused-parameter
3+
CPPFLAGS+=-I include
4+
5+
OBJECTS += src/helperfunctions.o src/main.o src/compiler_flexer.yy.o src/compiler_parser.tab.o
6+
OBJECTS += src/impl/FuncDeclCall.o src/impl/Function.o
7+
OBJECTS += src/impl/Program.o
8+
OBJECTS += src/impl/Sequence.o
9+
10+
11+
all: bin/tester bin/c_compiler
12+
13+
src/test.yy.cpp: src/test.flex
14+
flex -o src/test.yy.cpp src/test.flex
15+
16+
bin/tester: src/test.yy.cpp
17+
mkdir -p bin
18+
$(CC) src/test.yy.cpp -o bin/tester
19+
20+
run_tester:
21+
bin/tester
22+
23+
src/compiler_flexer.yy.cpp: src/compiler_flexer.flex src/compiler_parser.tab.hpp
24+
flex -o src/compiler_flexer.yy.cpp src/compiler_flexer.flex
25+
26+
src/compiler_parser.tab.cpp src/compiler_parser.tab.hpp: src/compiler_parser.y
27+
bison -v -d src/compiler_parser.y -o src/compiler_parser.tab.cpp
28+
29+
src/impl/%.o: src/impl/%.cpp
30+
$(CC) $(CPPFLAGS) -c $< -o $@
31+
32+
bin/c_compiler: $(OBJECTS)
33+
mkdir -p bin
34+
$(CC) $(CPPFLAGS) -o bin/c_compiler $^
35+
36+
run_compiler:
37+
bin/c_compiler
38+
39+
clean:
40+
rm src/*.o src/impl/*.o bin/* src/*.tab.cpp src/*.yy.cpp src/*.tab.hpp src/*.output

c_translator_formative.sh

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/bin/bash
2+
3+
if [[ "$1" != "" ]] ; then
4+
compiler="$1"
5+
else
6+
compiler="bin/c_compiler"
7+
fi
8+
9+
have_compiler=0
10+
if [[ ! -f bin/c_compiler ]] ; then
11+
>&2 echo "Warning : cannot find compiler at path ${compiler}. Only checking C reference against python reference."
12+
have_compiler=1
13+
fi
14+
15+
input_dir="translator_tests/tests"
16+
17+
working="tmp/formative"
18+
mkdir -p ${working}
19+
20+
for i in ${input_dir}/*.c ; do
21+
base=$(echo $i | sed -E -e "s|${input_dir}/([^.]+)[.]c|\1|g");
22+
23+
# Compile the reference C version
24+
gcc $i -o $working/$base
25+
26+
# Run the reference C version
27+
$working/$base
28+
REF_C_OUT=$?
29+
30+
# Run the reference python version
31+
#python3 ${input_dir}/$base.py
32+
#REF_P_OUT=$?
33+
34+
if [[ ${have_compiler} -eq 0 ]] ; then
35+
36+
# Create the DUT python version by invoking the compiler with translation flags
37+
$compiler --translate $i -o ${working}/$base-got.py
38+
39+
# Run the DUT python version
40+
python3 ${working}/$base-got.py
41+
GOT_P_OUT=$?
42+
fi
43+
44+
#if [[ $REF_C_OUT -ne $REF_P_OUT ]] ; then
45+
# echo "$base, REF_FAIL, Expected ${REF_C_OUT}, got ${REF_P_OUT}"
46+
if [[ ${have_compiler} -ne 0 ]] ; then
47+
echo "$base, Fail, No C compiler/translator"
48+
elif [[ $REF_C_OUT -ne $GOT_P_OUT ]] ; then
49+
echo "$base, Fail, Expected ${REF_C_OUT}, got ${GOT_P_OUT}"
50+
else
51+
echo "$base, Pass"
52+
fi
53+
done

compiler_test.sh

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/bin/bash
2+
3+
if [[ "$1" != "" ]] ; then
4+
compiler="$1"
5+
else
6+
compiler="bin/c_compiler"
7+
fi
8+
9+
have_compiler=0
10+
if [[ ! -f bin/c_compiler ]] ; then
11+
>&2 echo "Warning : cannot find compiler at path ${compiler}. Only checking C reference against python reference."
12+
have_compiler=1
13+
fi
14+
15+
echo "Enter Name of Subdirectory in compiler_tests you wish to test:"
16+
17+
read varname
18+
19+
input_dir="compiler_tests/${varname}"
20+
21+
working="tmp/compile"
22+
mkdir -p ${working}
23+
24+
for i in ${input_dir}/*.c ; do
25+
base=$(echo $i | sed -E -e "s|${input_dir}/([^.]+)[.]c|\1|g");
26+
27+
if [[ -f "${input_dir}/${base}_driver.c" ]] ; then
28+
29+
echo "${input_dir}/${base}_driver.c"
30+
if [[ ${have_compiler} -eq 0 ]] ; then
31+
32+
# Create the executable by invoking the compiler with -S flags
33+
$compiler -S ${input_dir}/${base}.c -o ${working}/${base}.s
34+
mips-linux-gnu-gcc -mfp32 -o ${working}/${base}.o -c ${working}/${base}.s
35+
mips-linux-gnu-gcc -mfp32 -static -o ${working}/${base} ${working}/${base}.o ${input_dir}/${base}_driver.c
36+
37+
# Run the executable
38+
qemu-mips ${working}/${base}
39+
GOT_MIP_OUT=$?
40+
fi
41+
42+
if [[ ${have_compiler} -ne 0 ]] ; then
43+
echo "$base, Fail, No C compiler/translator"
44+
elif [[ $GOT_MIP_OUT -ne 0 ]] ; then
45+
echo "$base, Fail, Expected 0, got ${GOT_MIP_OUT}"
46+
else
47+
echo "$base, Pass"
48+
fi
49+
fi
50+
done

compiler_tests/array/assign_global.c

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
int x[8];
2+
3+
int f()
4+
{
5+
int i = 2;
6+
x[i + 2] = i*2*2*2;
7+
return x[4];
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
int f();
2+
3+
int main()
4+
{
5+
return !(f()==16);
6+
}

compiler_tests/array/compare.c

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
3+
char cr[6] = { 'a' , '\\' , '3' , 'G', 'm', ')' };
4+
5+
char compare(char* c, int size)
6+
{
7+
if(size != 6)
8+
{
9+
return 0;
10+
}
11+
int i = 0;
12+
while(i < 6)
13+
{
14+
if(c[i] != cr[i])
15+
{
16+
return 0;
17+
}
18+
i++;
19+
}
20+
return 1;
21+
}

compiler_tests/array/compare_driver.c

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "stdio.h"
2+
3+
int compare(char* c, int size);
4+
5+
int main()
6+
{
7+
char c1[3] = {'a' , '\\' , '3'};
8+
char c2[6] = {'a', 'b', 'c', 'd', 'e', 'f'};
9+
char c3[6] = {'a' , '\\' , '3' , 'G', 'm', ')' };
10+
char c4[6] = {'a' , '\\' , '3' , 'G', 'm', '(' };
11+
char c5[6];
12+
c5[0] = 'a';
13+
c5[1] = '\\';
14+
c5[2] = '3';
15+
c5[3] = 'G';
16+
c5[4] = 'm';
17+
c5[5] = ')';
18+
if(compare(c1, 3) != 0) { printf("1\n"); return 1; }
19+
if(compare(c2, 6) != 0) { printf("2\n"); return 2; }
20+
if(compare(c3, 6) != 1) { printf("3\n"); return 3; }
21+
if(compare(c4, 6) != 0) { printf("4\n"); return 4; }
22+
if(compare(c5, 6) != 1) { printf("5\n"); return 5; }
23+
return 0;
24+
}

compiler_tests/array/declare_global.c

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
int x[8];
2+
3+
int f()
4+
{
5+
return 11;
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
int f();
3+
4+
int main()
5+
{
6+
return !(f()==11);
7+
}

compiler_tests/array/declare_local.c

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
int f()
2+
{
3+
int y;
4+
int x[8];
5+
y=13;
6+
return y;
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
int f();
3+
4+
int main()
5+
{
6+
return !(f()==13);
7+
}

compiler_tests/array/find_smallest.c

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
int smallest(int* a, int size)
2+
{
3+
int min = a[0];
4+
for(int i = 0; i < size; i++)
5+
{
6+
if(a[i] < min)
7+
{ min = a[i]; }
8+
}
9+
return min;
10+
}
11+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
int smallest(int* a, int size);
2+
3+
int main()
4+
{
5+
int arr[10] = { 23, 12, 25, 13, 0, 2, -3, -4, 10, -1};
6+
return !(smallest(arr, 10) == -4);
7+
}

compiler_tests/array/global_init.c

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
int x[8] = {23, 21, 12, 14, 76, 34, 32, 1+2+2};
2+
3+
int f()
4+
{
5+
return x[1] + x[4] + x[7];
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
int f();
2+
3+
int main()
4+
{
5+
return !(f()==102);
6+
}

compiler_tests/array/index_constant.c

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
int f()
2+
{
3+
int x[8];
4+
x[0]=23;
5+
return x[0];
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
int f();
3+
4+
int main()
5+
{
6+
return !(f()==23);
7+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
int f()
2+
{
3+
int i;
4+
int x[8];
5+
int acc;
6+
for(i=8; i<16; i++){
7+
x[i-8]=i;
8+
}
9+
acc=0;
10+
for(i=0; i<8; i++){
11+
acc=acc+x[i+0];
12+
}
13+
return acc;
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
int f();
3+
4+
int main()
5+
{
6+
return !(f()==92);
7+
}

compiler_tests/array/index_variable.c

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
int f()
2+
{
3+
int i;
4+
int x[8];
5+
for(i=0; i<8; i++){
6+
x[i]=i;
7+
}
8+
return x[4];
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
int f();
3+
4+
int main()
5+
{
6+
return !(f()==4);
7+
}

compiler_tests/array/local_init.c

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
int f()
2+
{
3+
int a[5] = {1+2, 2*3, 4/2, 5%3, 8};
4+
a[2] += 1;
5+
return a[0] + a[2] + a[3];
6+
}
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
int f();
2+
3+
int main()
4+
{
5+
return !(f()==8);
6+
}

compiler_tests/array/sort.c

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
double dr[4] = {1.23, 1.22, 2.5, -0.9};
2+
3+
int sort()
4+
{
5+
for(int j = 0; j < 3; j++)
6+
{
7+
for(int i = 0; i < 3; i++)
8+
{
9+
if(dr[i] > dr[i+1])
10+
{
11+
double t = dr[i];
12+
dr[i] = dr[i+1];
13+
dr[i+1] = t;
14+
}
15+
}
16+
}
17+
int ab = (dr[0] == -0.9)&&(dr[1] == 1.22)&&(dr[2] == 1.23)&&(dr[3] == 2.5);
18+
return ab;
19+
}

compiler_tests/array/sort_driver.c

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
int sort();
2+
3+
4+
int main()
5+
{
6+
return !(sort() == 1);
7+
}

compiler_tests/array/summate.c

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
float fr[6] = { 1.23f, 3.4F, 2.0f, 2.34f + 6.7F, 1.2f*1.3f, 0.45F};
2+
3+
float summate()
4+
{
5+
float total = 0.0f;
6+
float hei[6];
7+
for(int i = 0; i < 6; i+=1)
8+
hei[i] = fr[i];
9+
10+
for(unsigned j = 0u; j < 6; j++)
11+
{
12+
total = total + hei[j];
13+
}
14+
return total;
15+
}
16+

0 commit comments

Comments
 (0)