Skip to content

Commit

Permalink
Assorted fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Donn committed Oct 28, 2018
1 parent a8a7bc2 commit 9099129
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 33 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ phi
*.vcd

*.exe
*.stackdump
*.stackdump

./*.phi
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "Submodules/dff3c8a487c73c042efa086a8fc68bd3"]
path = Submodules/dff3c8a487c73c042efa086a8fc68bd3
url = https://gist.github.com/donn/dff3c8a487c73c042efa086a8fc68bd3
[submodule "Submodules/termcolor"]
path = Submodules/termcolor
url = https://github.com/ikalnytskyi/termcolor
1 change: 1 addition & 0 deletions Examples/d_ff.phi
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module dFlipFlop(
clock: Input,
reset: Input
) {
Wire[31..0] v = x;
Register[31..0] store;

@ posedge clock | negedge reset {
Expand Down
5 changes: 1 addition & 4 deletions Grammar/phi.l
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,11 @@
return token::KEYWORD_NEGEDGE;
}

(@[_A-Za-z][_A-Za-z0-9]*) {
return token::ANNOTATION;
}
[_A-Za-z][_A-Za-z0-9]* {
yylval->text = strdup(yytext);
return token::IDENTIFIER;
}
[0-9]+[bodx][0-9zx]+ {
[0-9]+[bodx][A-F0-9zx]+ {
yylval->text = strdup(yytext);
return token::FW_NUMERIC;
}
Expand Down
11 changes: 5 additions & 6 deletions Grammar/phi.yy
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
%token KEYWORD_OUTPUT
%token KEYWORD_POSEDGE
%token KEYWORD_NEGEDGE
%token ANNOTATION

%token NUMERIC
%token FW_NUMERIC
Expand Down Expand Up @@ -184,8 +183,8 @@ port_declaration:
port_polarity {
$$ = $1;
}
| ANNOTATION port_polarity {
$$ = TODO;
| '@' IDENTIFIER port_polarity {
$$ = $3;
}
;
port_polarity:
Expand Down Expand Up @@ -314,12 +313,12 @@ switch_block:
;
labeled_statement_list:
{ $$ = ε; }
| KEYWORD_CASE number ':' statement_list labeled_statement_list {
| KEYWORD_CASE expression ':' statement_list labeled_statement_list {
cst; stream << $2 << ": " << std::endl << $4 << std::endl << $5;
$$ = dup;
}
| KEYWORD_DEFAULT ':' statement_list {
cst; stream << "default: " << std::endl << $3;
| KEYWORD_DEFAULT expression ':' statement_list {
cst; stream << "default: " << std::endl << $4;
$$ = dup;
}
;
Expand Down
3 changes: 2 additions & 1 deletion Headers/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ namespace Phi {
public:
std::vector<Error> errorList;
std::vector<std::string> files;
std::vector<std::string> currentFileLines;
char* top = NULL;
bool _HACK_FOR_ALWAYS = false;

void setFile(std::string currentFile) { files.push_back(currentFile); }
std::string setFile(std::string currentFile);
bool error();
void printErrors();
};
Expand Down
3 changes: 1 addition & 2 deletions Headers/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

namespace Phi {
namespace Utils {
std::vector<std::string> split(const std::string* string, std::string delimiter);
std::vector<std::string> split(const std::string* string, char delimiter);
}
}

#endif
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ CPP_LY_SOURCES = $(YACC_OUT) $(LEX_OUT)
CPP_SOURCES = $(shell find Sources -mindepth 1 -maxdepth 1)
CPP_HEADERS = $(shell find Headers -mindepth 1 -maxdepth 1) $(BUILD_DIR)/git_version.h $(LEX_HEADER)

LIBRARY_HEADER_PATHS = $(addprefix -I, $(shell find Submodules -mindepth 1 -maxdepth 1))
LIBRARY_HEADER_PATHS = $(addprefix -I, $(shell find Submodules -mindepth 1 -maxdepth 1)) $(addprefix -I, $(shell find Submodules -name include))
LIBRARY_SOURCES =
CPP_LIBRARY_SOURCES =

Expand Down
38 changes: 36 additions & 2 deletions Sources/Context.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
// Project Headers
#include "Context.h"
#include "Utils.h"

// Flex/Bison
#include <phi.yy.hh>

// Libraries
#include <termcolor/termcolor.hpp>

// CPP STL
#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>

void Phi::Parser::error(Location const& loc, const std::string& string) {
context->errorList.push_back({loc, string});
}
Expand All @@ -15,14 +25,38 @@ void Phi::Context::printErrors() {
auto& error = errorList[i];
auto& loc = error.loc;
auto& message = error.message;
auto& content = "IMPLEMENT_TOKEN";

std::cerr << *loc.begin.filename << ":" << loc.begin.line << ":" << loc.begin.column << ": " << message << ": unexpected " << content<< std::endl;
std::cerr << termcolor::bold << *loc.begin.filename << ":" << loc.begin.line << ":" << loc.begin.column << ": " << termcolor::red << message << termcolor::reset << std::endl;
std::cerr << currentFileLines[loc.begin.line - 1] << std::endl;
std::cerr << termcolor::bold << termcolor::green <<
std::setw(loc.begin.column + 1) << "^" <<
std::string(loc.end.column - loc.begin.column - 2, '~') <<
termcolor::reset << std::endl;
}
std::cerr << errorCount << ((errorCount == 1) ? " error" : " errors") << " generated." << std::endl;
}
}

std::string Phi::Context::setFile(std::string currentFile) {
files.push_back(currentFile);

auto file = std::ifstream(currentFile);
if (file.fail()) {
std::cerr << "Could not open file '" << currentFile << "'." << std::endl;
throw "couldNotOpen";
}

std::stringstream stringstream;
stringstream << file.rdbuf();
file.close();

auto dump = stringstream.str();
currentFileLines = Phi::Utils::split(&dump, '\n');

return dump;

}

bool Phi::Context::error() {
return errorList.size() > 0;
}
3 changes: 1 addition & 2 deletions Sources/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@

// Number
Phi::Node::Number::Number(std::string interpretable) {
auto regex = std::regex("([0-9]+)([bodx])([0-9zx]+)");
auto regex = std::regex("([0-9]+)([bodx])([A-F0-9zx]+)");
auto match = std::smatch();
std::regex_match(interpretable, match, regex); // If it doesn't match, the regex here and in the .l file are mismatched.

auto prospectiveWidth = std::stoi(match[1]);
if (prospectiveWidth < 0 || prospectiveWidth > maxWidth) {
throw "Width out of range.";
Expand Down
4 changes: 2 additions & 2 deletions Sources/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
#include <iostream>
#include <sstream>

std::vector<std::string> Phi::Utils::split(const std::string* string, std::string delimiter) {
std::vector<std::string> Phi::Utils::split(const std::string* string, char delimiter) {
std::vector<std::string> returnValue;
std::stringstream ss;
ss.str(*string);

std::string item;
while (std::getline(ss, item, delimiter[0])) {
while (std::getline(ss, item, delimiter)) {
returnValue.push_back(item);
}

Expand Down
14 changes: 2 additions & 12 deletions Sources/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,13 @@ int main(int argc, char* argv[]) {
auto outputFilename = filename;
outputFilename.replace(extensionPosition, 4, ".v");

std::stringstream stringstream;
auto file = std::ifstream(filename);
if (file.fail()) {
std::cerr << "Could not open file '" << filename << "'." << std::endl;
return EX_NOINPUT;
}
stringstream << file.rdbuf();
file.close();
auto context = Phi::Context();
auto input = context.setFile(argv[1]);

auto input = stringstream.str();
yy_scan_string(input.c_str());

// Parse
auto context = Phi::Context();
auto parser = Phi::Parser(&context);

context.setFile(argv[1]);
auto parsingResult = parser.parse();

if (context.error()) {
Expand Down
1 change: 1 addition & 0 deletions Submodules/termcolor
Submodule termcolor added at 91c29f

0 comments on commit 9099129

Please sign in to comment.