Skip to content

Commit 400733f

Browse files
author
Gabrijel Boduljak
committed
added compilers project
0 parents  commit 400733f

File tree

921 files changed

+167874
-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.

921 files changed

+167874
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.o
2+
*.d
3+
!/assignments/*/*/.out

assignments/PA1/Makefile

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
ASSN = 1
3+
CLASS= cs143
4+
CLASSDIR= ../..
5+
SRC= stack.cl README
6+
LSRC= Makefile atoi.cl stack.test
7+
8+
CC=gcc
9+
CFLAGS=-g
10+
11+
.c.o:
12+
${CC} ${CFLAGS} -c $<
13+
14+
compile: stack.s
15+
16+
stack.s: stack.cl atoi.cl
17+
${CLASSDIR}/bin/coolc stack.cl atoi.cl
18+
19+
test: compile
20+
@echo stack.test
21+
${CLASSDIR}/bin/spim -file stack.s < stack.test
22+
23+
clean :
24+
rm -f *.s core *~

assignments/PA1/README

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
README file for Programming Assignment 1
3+
========================================
4+
5+
Your directory should now contain the following files:
6+
7+
Makefile
8+
README
9+
atoi.cl -> [cool root]/assignments/PA1/atoi.cl
10+
stack.cl
11+
stack.test -> [cool root]/assignments/PA1/stack.test
12+
13+
The Makefile contains targets for compiling and running your
14+
program, as well as handing it in.
15+
16+
The README contains this info. Part of the assignment is to
17+
answer the questions at the end of this README file.
18+
Just edit this file.
19+
20+
atoi.cl is an implementation in Cool of the string to integer
21+
conversion function known from the C language.
22+
23+
stack.cl is the skeleton file which you should fill in with
24+
your program.
25+
26+
stack.test is a short test input to the stack machine.
27+
28+
29+
The symlinked files (see "man ln") are that way to emphasize
30+
that they are read-only.
31+
32+
33+
Instructions
34+
------------
35+
36+
To compile and run your program, type:
37+
38+
% gmake test
39+
40+
Try it now -- it should work, and print "Nothing implemented"
41+
(among a few other things).
42+
43+
44+
To simply compile your program, type
45+
46+
% gmake compile
47+
48+
49+
Instructions for turning in the assignment will be posted on the
50+
course web page.
51+
52+
GOOD LUCK!
53+
54+
---8<------8<------8<------8<---cut here---8<------8<------8<------8<---
55+
56+
Questions on PA1
57+
----------------
58+
59+
1. Describe your implementation of the stack machine in a single short
60+
paragraph.
61+
62+
63+
2. List 3 things that you like about the Cool programming language.
64+
65+
66+
3. List 3 things you DON'T like about Cool.
67+

assignments/PA1/atoi.cl

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
(*
2+
The class A2I provides integer-to-string and string-to-integer
3+
conversion routines. To use these routines, either inherit them
4+
in the class where needed, have a dummy variable bound to
5+
something of type A2I, or simpl write (new A2I).method(argument).
6+
*)
7+
8+
9+
(*
10+
c2i Converts a 1-character string to an integer. Aborts
11+
if the string is not "0" through "9"
12+
*)
13+
class A2I {
14+
15+
c2i(char : String) : Int {
16+
if char = "0" then 0 else
17+
if char = "1" then 1 else
18+
if char = "2" then 2 else
19+
if char = "3" then 3 else
20+
if char = "4" then 4 else
21+
if char = "5" then 5 else
22+
if char = "6" then 6 else
23+
if char = "7" then 7 else
24+
if char = "8" then 8 else
25+
if char = "9" then 9 else
26+
{ abort(); 0; } -- the 0 is needed to satisfy the typchecker
27+
fi fi fi fi fi fi fi fi fi fi
28+
};
29+
30+
(*
31+
i2c is the inverse of c2i.
32+
*)
33+
i2c(i : Int) : String {
34+
if i = 0 then "0" else
35+
if i = 1 then "1" else
36+
if i = 2 then "2" else
37+
if i = 3 then "3" else
38+
if i = 4 then "4" else
39+
if i = 5 then "5" else
40+
if i = 6 then "6" else
41+
if i = 7 then "7" else
42+
if i = 8 then "8" else
43+
if i = 9 then "9" else
44+
{ abort(); ""; } -- the "" is needed to satisfy the typchecker
45+
fi fi fi fi fi fi fi fi fi fi
46+
};
47+
48+
(*
49+
a2i converts an ASCII string into an integer. The empty string
50+
is converted to 0. Signed and unsigned strings are handled. The
51+
method aborts if the string does not represent an integer. Very
52+
long strings of digits produce strange answers because of arithmetic
53+
overflow.
54+
55+
*)
56+
a2i(s : String) : Int {
57+
if s.length() = 0 then 0 else
58+
if s.substr(0,1) = "-" then ~a2i_aux(s.substr(1,s.length()-1)) else
59+
if s.substr(0,1) = "+" then a2i_aux(s.substr(1,s.length()-1)) else
60+
a2i_aux(s)
61+
fi fi fi
62+
};
63+
64+
(*
65+
a2i_aux converts the usigned portion of the string. As a programming
66+
example, this method is written iteratively.
67+
*)
68+
a2i_aux(s : String) : Int {
69+
(let int : Int <- 0 in
70+
{
71+
(let j : Int <- s.length() in
72+
(let i : Int <- 0 in
73+
while i < j loop
74+
{
75+
int <- int * 10 + c2i(s.substr(i,1));
76+
i <- i + 1;
77+
}
78+
pool
79+
)
80+
);
81+
int;
82+
}
83+
)
84+
};
85+
86+
(*
87+
i2a converts an integer to a string. Positive and negative
88+
numbers are handled correctly.
89+
*)
90+
i2a(i : Int) : String {
91+
if i = 0 then "0" else
92+
if 0 < i then i2a_aux(i) else
93+
"-".concat(i2a_aux(i * ~1))
94+
fi fi
95+
};
96+
97+
(*
98+
i2a_aux is an example using recursion.
99+
*)
100+
i2a_aux(i : Int) : String {
101+
if i = 0 then "" else
102+
(let next : Int <- i / 10 in
103+
i2a_aux(next).concat(i2c(i - next * 10))
104+
)
105+
fi
106+
};
107+
108+
};

assignments/PA1/stack.cl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
(*
2+
* CS164 Fall 94
3+
*
4+
* Programming Assignment 1
5+
* Implementation of a simple stack machine.
6+
*
7+
* Skeleton file
8+
*)
9+
10+
class Main inherits IO {
11+
12+
main() : Object {
13+
out_string("Nothing implemented\n")
14+
};
15+
16+
};

assignments/PA1/stack.test

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
e
2+
e
3+
1
4+
+
5+
2
6+
s
7+
d
8+
e
9+
e
10+
d
11+
e
12+
+
13+
1
14+
s
15+
s
16+
s
17+
d
18+
e
19+
e
20+
s
21+
e
22+
e
23+
e
24+
d
25+
x

assignments/PA2/Makefile

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
2+
ASSN = 2
3+
CLASS= cs143
4+
CLASSDIR= ../..
5+
LIB=
6+
7+
SRC= cool.flex test.cl README
8+
CSRC= lextest.cc utilities.cc stringtab.cc handle_flags.cc
9+
TSRC= mycoolc
10+
HSRC=
11+
CGEN= cool-lex.cc
12+
HGEN=
13+
LIBS= parser semant cgen
14+
CFIL= ${CSRC} ${CGEN}
15+
LSRC= Makefile
16+
OBJS= ${CFIL:.cc=.o}
17+
OUTPUT= test.output
18+
19+
CPPINCLUDE= -I. -I${CLASSDIR}/include/PA${ASSN} -I${CLASSDIR}/src/PA${ASSN}
20+
21+
22+
FFLAGS= -d -ocool-lex.cc
23+
24+
CC=g++
25+
CFLAGS= -g -Wall -Wno-unused -Wno-write-strings ${CPPINCLUDE}
26+
FLEX=flex ${FFLAGS}
27+
DEPEND = ${CC} -MM ${CPPINCLUDE}
28+
29+
30+
source : ${SRC} ${TSRC} ${LSRC} ${LIBS} lsource
31+
32+
lsource: ${LSRC}
33+
34+
${OUTPUT}: lexer test.cl
35+
@rm -f test.output
36+
-./lexer test.cl >test.output 2>&1
37+
38+
lexer: ${OBJS}
39+
${CC} ${CFLAGS} ${OBJS} ${LIB} -o lexer
40+
41+
.cc.o:
42+
${CC} ${CFLAGS} -c $<
43+
44+
cool-lex.cc: cool.flex
45+
${FLEX} cool.flex
46+
47+
dotest: lexer test.cl
48+
./lexer test.cl
49+
50+
${LIBS}:
51+
${CLASSDIR}/etc/link-object ${ASSN} $@
52+
53+
${TSRC} ${CSRC}:
54+
-ln -s ${CLASSDIR}/src/PA${ASSN}/$@ $@
55+
56+
${HSRC}:
57+
-ln -s ${CLASSDIR}/include/PA${ASSN}/$@ $@
58+
59+
clean :
60+
-rm -f ${OUTPUT} *.s core ${OBJS} lexer cool-lex.cc *~ parser cgen semant
61+
62+
clean-compile:
63+
@-rm -f core ${OBJS} cool-lex.cc ${LSRC}
64+
65+
%.d: %.cc ${SRC} ${LSRC}
66+
${SHELL} -ec '${DEPEND} $< | sed '\''s/\($*\.o\)[ :]*/\1 $@ : /g'\'' > $@'
67+
68+
-include ${CFIL:.cc=.d}
69+
70+

0 commit comments

Comments
 (0)