Commit a1dc7d75 authored by Sarah Spall's avatar Sarah Spall Committed by Vikram Narayanan

basic error checking, so non syntax errors can be caught, and execution can...

basic error checking, so non syntax errors can be caught, and execution can end after parsing, without crashing
parent fdf5cd29
......@@ -6,12 +6,15 @@ idl = parser/lcd_idl.cpp include/lcd_idl.h
CXXFLAGS = -g -Iinclude/
CXX = g++
objects = lcd_ast.o main.o lcd_idl.o scope.o header_gen.o
objects = lcd_ast.o main.o lcd_idl.o scope.o header_gen.o error.o
$(bin): $(objects)
$(CXX) $(LDFLAGS) -o $@ $^ $(LDLIBS)
main.o: main/main.cpp include/lcd_ast.h include/lcd_idl.h include/gen_visitor.h
main.o: main/main.cpp include/lcd_ast.h include/lcd_idl.h include/gen_visitor.h include/error.h
$(CXX) $(CXXFLAGS) -c -o $@ $(filter-out %.h,$^)
error.o: error/error.cpp include/error.h
$(CXX) $(CXXFLAGS) -c -o $@ $(filter-out %.h,$^)
header_gen.o: gen/header_gen.cpp include/gen_visitor.h
......
#include "error.h"
ErrorReport* ErrorReport::instance_ = 0;
ErrorReport* ErrorReport::instance()
{
if(!ErrorReport::instance_)
ErrorReport::instance_ = new ErrorReport();
return instance_;
}
ErrorReport::ErrorReport()
{
errors_ = false;
}
void ErrorReport::set_error()
{
this->errors_ = true;
}
bool ErrorReport::errors()
{
return errors_;
}
#ifndef ERROR_H
#define ERROR_H
#include <vector>
class ErrorReport
{
static ErrorReport* instance_;
ErrorReport();
bool errors_;
public:
static ErrorReport* instance();
bool errors();
void set_error();
};
#endif
#include "lcd_ast.h"
#include "lcd_idl.h"
#include "gen_visitor.h"
#include "error.h"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
int main(int argc, char ** argv)
{
if(argc != 5)
......@@ -17,19 +19,23 @@ int main(int argc, char ** argv)
{
char * file = argv[2];
File * tree = (File *) Parser::parse(std::string(file));
char* option = argv[1];
ErrorReport* er = ErrorReport::instance();
if(er->errors())
{
printf("There were errors during parsing\n");
// cleanup?
exit(0);
}
char* out_option = argv[3];
char * out_file = argv[4];
char* out_file = argv[4];
FILE* of = fopen(out_file, "w");
printf("outside if statements\n");
if(!of)
{
printf("Error: unable to open %s for writing\n", out_file);
// cleanup
exit(0);
}
if(!strcmp(option,"-header"))
if(!strcmp(argv[1],"-header"))
{
printf("in option\n");
HeaderVisitor* hv = new HeaderVisitor(of);
......@@ -37,19 +43,21 @@ int main(int argc, char ** argv)
tree->accept(hv);
printf("have finished hv\n");
}
else if(!strcmp(option,"-source"))
else if(!strcmp(argv[1],"-source"))
{
// TODO
}
else
{
printf("error unrecognized option: %s\n", option);
printf("error unrecognized option: %s\n", argv[1]);
}
return 0;
}
catch (const Parser::ParseException e)
{
printf("caught an exception\n");
// std::cout << e.getReason();
printf("caught a parser exception\n");
// printf("e is: %s\n", e.getReason().c_str());
exit(0);
}
}
......@@ -2,6 +2,7 @@ start-symbol: File
include: {{ #include "lcd_ast.h"
#include "error.h"
#include <vector>
#include <stdlib.h>
#include <string>
......@@ -29,12 +30,12 @@ code: {{
}
if(!err)
{ // error do something
printf("ERROR\n\n\n\n\n\n\n\n\n\n\n\n");
}
printf("herheherhehehehehr\n");
printf("num is: %d\n", val->num());
printf("herheherhehehehehr\n");
{
ErrorReport* er = ErrorReport::instance();
er->set_error();
printf("Error looking up type: %s\n", type);
return 0; // continue execution
}
return val;
}
......@@ -43,7 +44,9 @@ code: {{
FileScope *fs = current_file_scope();
int ret = fs->insert_symbol(sym, val);
if(!ret)
{ // error do something
{
ErrorReport* er = ErrorReport::instance();
er->set_error();
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment