Skip to content

Commit

Permalink
Simple proof of concept version.
Browse files Browse the repository at this point in the history
  • Loading branch information
bas committed May 17, 2012
1 parent 8bb857c commit 66d6a88
Show file tree
Hide file tree
Showing 3 changed files with 313 additions and 0 deletions.
98 changes: 98 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@

ASSN = 4
CLASS= cs143
CLASSDIR= /usr/class/cs143/cool
LIB= -lfl
AR= gar
ARCHIVE_NEW= -cr
RANLIB= gar -qs

SRC= semant.cc semant.h cool-tree.h cool-tree.handcode.h good.cl bad.cl README
CSRC= semant-phase.cc symtab_example.cc handle_flags.cc ast-lex.cc ast-parse.cc utilities.cc stringtab.cc dumptype.cc tree.cc cool-tree.cc dotdump.cc
TSRC= mycoolc mysemant cool-tree.aps
CGEN=
HGEN=
LIBS= lexer parser cgen
CFIL= semant.cc ${CSRC} ${CGEN}
LSRC= Makefile
OBJS= ${CFIL:.cc=.o}
OUTPUT= good.output bad.output


CPPINCLUDE= -I. -I${CLASSDIR}/include/PA${ASSN} -I${CLASSDIR}/src/PA${ASSN}

FFLAGS = -d8 -ocool-lex.cc
BFLAGS = -d -v -y -b cool --debug -p cool_yy
ASTBFLAGS = -d -v -y -b ast --debug -p ast_yy

CC=g++
CFLAGS=-g -Wall -Wno-unused -Wno-write-strings ${CPPINCLUDE} -DDEBUG
FLEX=flex ${FFLAGS}
BISON= bison ${BFLAGS}
DEPEND = ${CC} -MM ${CPPINCLUDE}

all: semant

source: ${SRC} lsource

lsource: ${LSRC}

${OUTPUT}: semant
@rm -f ${OUTPUT}
./mysemant good.cl >good.output 2>&1
-./mysemant bad.cl >bad.output 2>&1

compile: semant change-prot

change-prot:
@-chmod 660 ${SRC} ${OUTPUT}

SEMANT_OBJS := ${filter-out symtab_example.o,${OBJS}}

semant: ${SEMANT_OBJS}
${CC} ${CFLAGS} ${SEMANT_OBJS} ${LIB} -o semant

symtab_example: symtab_example.cc
${CC} ${CFLAGS} symtab_example.cc ${LIB} -o symtab_example

.cc.o:
${CC} ${CFLAGS} -c $<

dotest: semant good.cl bad.cl
@echo "\nRunning semantic checker on good.cl\n"
-./mysemant good.cl
@echo "\nRunning semantic checker on bad.cl\n"
-./mysemant bad.cl

${LIBS}:
${CLASSDIR}/etc/link-object ${ASSN} $@

# These dependencies allow you to get the starting files for
# the assignment. They will not overwrite a file you already have.

${SRC} :
-${CLASSDIR}/etc/copy-skel ${ASSN} ${SRC}

${LSRC} :
-${CLASSDIR}/etc/link-shared ${ASSN} ${LSRC}

${TSRC} ${CSRC}:
-ln -s ${CLASSDIR}/src/PA${ASSN}/$@ $@

${HSRC}:
-ln -s ${CLASSDIR}/include/PA${ASSN}/$@ $@

submit-clean: ${OUTPUT}
-rm -f *.s core ${OBJS} ${CGEN} ${HGEN} lexer *~ parser cgen semant symtab_example

clean :
-rm -f ${OUTPUT} *.s core ${OBJS} semant cgen symtab_example parser lexer *~ *.a *.o

clean-compile:
@-rm -f core ${OBJS} ${LSRC}

%.d: %.cc ${SRC}
${SHELL} -ec '${DEPEND} $< | sed '\''s/\($*\.o\)[ :]*/\1 $@ : /g'\'' > $@'

-include ${CFIL:.cc=.d}

112 changes: 112 additions & 0 deletions cool-tree.handcode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
//
// The following include files must come first.

#ifndef COOL_TREE_HANDCODE_H
#define COOL_TREE_HANDCODE_H

#include <iostream>
#include <fstream>
#include "tree.h"
#include "cool.h"
#include "stringtab.h"
#define yylineno curr_lineno;
extern int yylineno;

inline Boolean copy_Boolean(Boolean b) {return b; }
inline void assert_Boolean(Boolean) {}
inline void dump_Boolean(ostream& stream, int padding, Boolean b)
{ stream << pad(padding) << (int) b << "\n"; }

void dump_Symbol(ostream& stream, int padding, Symbol b);
void assert_Symbol(Symbol b);
Symbol copy_Symbol(Symbol b);

class Program_class;
typedef Program_class *Program;
class Class__class;
typedef Class__class *Class_;
class Feature_class;
typedef Feature_class *Feature;
class Formal_class;
typedef Formal_class *Formal;
class Expression_class;
typedef Expression_class *Expression;
class Case_class;
typedef Case_class *Case;

typedef list_node<Class_> Classes_class;
typedef Classes_class *Classes;
typedef list_node<Feature> Features_class;
typedef Features_class *Features;
typedef list_node<Formal> Formals_class;
typedef Formals_class *Formals;
typedef list_node<Expression> Expressions_class;
typedef Expressions_class *Expressions;
typedef list_node<Case> Cases_class;
typedef Cases_class *Cases;

#define Program_EXTRAS \
virtual void semant() = 0; \
virtual void dump_with_types(ostream&, int) = 0; \
virtual void dump_to_dot(std::ofstream&, int) = 0;



#define program_EXTRAS \
void semant(); \
void dump_with_types(ostream&, int); \
void dump_to_dot(std::ofstream&, int);

#define Class__EXTRAS \
virtual Symbol get_filename() = 0; \
virtual void dump_with_types(ostream&,int) = 0; \
virtual void dump_to_dot(std::ofstream&, int) = 0;


#define class__EXTRAS \
Symbol get_filename() { return filename; } \
void dump_with_types(ostream&,int); \
void dump_to_dot(std::ofstream&, int);


#define Feature_EXTRAS \
virtual void dump_with_types(ostream&,int) = 0; \
virtual void dump_to_dot(std::ofstream&, int) = 0;


#define Feature_SHARED_EXTRAS \
void dump_with_types(ostream&,int); \
void dump_to_dot(std::ofstream&, int);


#define Formal_EXTRAS \
virtual void dump_with_types(ostream&,int) = 0; \
virtual void dump_to_dot(std::ofstream&, int) = 0;

#define formal_EXTRAS \
void dump_with_types(ostream&,int); \
void dump_to_dot(std::ofstream&, int);

#define Case_EXTRAS \
virtual void dump_with_types(ostream& ,int) = 0; \
virtual void dump_to_dot(std::ofstream&, int) = 0;

#define branch_EXTRAS \
void dump_with_types(ostream& ,int); \
void dump_to_dot(std::ofstream&, int);


#define Expression_EXTRAS \
Symbol type; \
Symbol get_type() { return type; } \
Expression set_type(Symbol s) { type = s; return this; } \
virtual void dump_with_types(ostream&,int) = 0; \
void dump_type(ostream&, int); \
Expression_class() { type = (Symbol) NULL; } \
void dump_to_dot(std::ofstream&, int); \

#define Expression_SHARED_EXTRAS \
void dump_with_types(ostream&,int); \
void dump_to_dot(std::ofstream&, int);

#endif
103 changes: 103 additions & 0 deletions dotdump.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#include "cool.h"
#include "tree.h"
#include "cool-tree.h"
#include "utilities.h"

#define PROGRAM_NODE "program"

static int classname = 0;
static int featurename = 1000;
static int formalname = 2000;
static int branchname = 3000;
static int exprname = 4000;

int exprcaller;

void Expression_class::dump_to_dot(std::ofstream& f, int n) {

f << exprcaller << "->" << exprname << ";" << std::endl;
// if (type) {
f << exprname << " [label=\" notype \"];" << std::endl;
// }
// else
// {
// f << exprname << " [label=\"type: " << type << "\"];" << std::endl;
// }
exprname++;
}

void program_class::dump_to_dot(std::ofstream& f, int n) {
f << "digraph AST {" << std::endl;
for(int i = classes->first(); classes->more(i); i = classes->next(i)) {
classes->nth(i)->dump_to_dot(f, i);
classname++;
}
f << "}" << std::endl;
}

/* n is the class index */
void class__class::dump_to_dot(std::ofstream& f, int n) {
// program->classIndex
f << "\t" << PROGRAM_NODE << "->" << classname << ";" << std::endl;
f << "\t" << classname << " [label= \"" << name << "\"];" << std::endl;

for(int i = features->first(); features->more(i); i = features->next(i)) {
features->nth(i)->dump_to_dot(f, i);
featurename++;
}
}

void method_class::dump_to_dot(std::ofstream& f, int n) {
f << "\t" << classname << "->" << featurename << ";" << std::endl;
f << "\t" << featurename << " [label= \" method: " << name << ":" << return_type << "\"];" << std::endl;
f << "\t" << featurename << "->" << formalname << ";" << std::endl;
f << "\t" << formalname << " [label= \" params: " ;
for(int i = formals->first(); formals->more(i); i = formals->next(i)) {
formals->nth(i)->dump_to_dot(f, i);
}
f << "\"];" << std::endl;
formalname++;
exprcaller = featurename;
expr->dump_to_dot(f,-1);
}

void attr_class::dump_to_dot(std::ofstream& f, int n) {
f << "\t" << classname << "->" << featurename << ";" << std::endl;
f << "\t" << featurename << " [label= \" attr: " << name << ":" << type_decl << "\"];" << std::endl;
exprcaller = featurename;
init->dump_to_dot(f,-1);
}

void formal_class::dump_to_dot(std::ofstream& f, int n) {
f << name << ":" << type_decl << ", ";
}

void block_class::dump_to_dot(std::ofstream& f, int n) {
int tmp = exprcaller;
f << "\t" << exprcaller << "->" << exprname << ":" << std::endl;
f << "\t" << exprcaller << " [label= \" block\"];" << std::endl;
exprcaller = exprname;
exprname++;
for(int i = body->first(); body->more(i); i = body->next(i))
body->nth(i)->dump_to_dot(f, -1);
exprcaller = tmp;
}

void branch_class::dump_to_dot(std::ofstream& f, int n) {
;
}

void assign_class::dump_to_dot(std::ofstream& f, int n) {
int tmp = exprcaller;
f << "\t" << exprcaller << "->" << exprname << ";" << std::endl;
f << "\t" << exprname << " [label= \" assign: " << name << "\"];" << std::endl;
exprcaller = exprname;
expr->dump_to_dot(f,n);
exprcaller = tmp;
exprname++;
}

void no_expr_class::dump_to_dot(std::ofstream& f, int n) {
f << "\t" << exprcaller << "->" << exprname << ";" << std::endl;
f << "\t" << exprname << " [label= \" no_expr\"];" << std::endl;
}

0 comments on commit 66d6a88

Please sign in to comment.