2013-09-26 15:16:58 -04:00
|
|
|
#include <string>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <fstream>
|
2013-10-02 03:15:20 -04:00
|
|
|
#include <vector>
|
2013-09-26 15:16:58 -04:00
|
|
|
|
2013-12-31 02:53:52 -06:00
|
|
|
#include <cstring>
|
|
|
|
|
|
2013-05-11 16:13:46 -04:00
|
|
|
#include "NodeTree.h"
|
2013-07-28 19:45:08 -04:00
|
|
|
#include "Symbol.h"
|
2013-06-13 14:25:10 -04:00
|
|
|
#include "Lexer.h"
|
2013-07-30 01:42:51 -04:00
|
|
|
#include "LALRParser.h"
|
2013-07-31 23:51:05 -04:00
|
|
|
#include "RNGLRParser.h"
|
2013-09-26 15:16:58 -04:00
|
|
|
|
2013-12-31 23:43:49 -06:00
|
|
|
#include "Importer.h"
|
2013-10-02 03:15:20 -04:00
|
|
|
#include "ASTData.h"
|
2013-11-01 02:52:18 -04:00
|
|
|
#include "CGenerator.h"
|
2014-05-21 12:01:45 -04:00
|
|
|
#include "Poset.h"
|
2013-05-11 16:13:46 -04:00
|
|
|
|
2013-12-19 10:39:36 -06:00
|
|
|
#include "util.h"
|
2014-05-20 22:21:07 -04:00
|
|
|
#include "Tester.h"
|
2013-05-11 16:13:46 -04:00
|
|
|
|
|
|
|
|
int main(int argc, char* argv[]) {
|
2014-05-01 01:18:01 -04:00
|
|
|
std::vector<std::string> includePaths;
|
2014-06-10 00:53:30 -07:00
|
|
|
includePaths.push_back(""); //Local
|
|
|
|
|
|
|
|
|
|
if (argc <= 1) {
|
2015-03-15 18:41:55 -04:00
|
|
|
std::cerr << "Kraken invocation: kraken sourceFile.krak" << std::endl;
|
|
|
|
|
std::cerr << "Kraken invocation: kraken sourceFile.krak outputName" << std::endl;
|
|
|
|
|
std::cerr << "Kraken invocation: kraken grammerFile.kgm sourceFile.krak outputName" << std::endl;
|
2015-03-14 02:42:07 -04:00
|
|
|
std::cerr << "Or for testing do: kraken --test [optional list of names of file (.krak .expected_results) without extentions to run]" << std::endl;
|
2014-06-10 00:53:30 -07:00
|
|
|
return 0;
|
|
|
|
|
}
|
2014-05-01 01:18:01 -04:00
|
|
|
|
2014-05-20 22:21:07 -04:00
|
|
|
if (argc >= 2 && std::string(argv[1]) == "--test") {
|
2013-10-25 02:04:22 -07:00
|
|
|
StringReader::test();
|
2013-10-26 23:29:23 -07:00
|
|
|
RegEx::test();
|
2013-10-26 23:05:25 -07:00
|
|
|
Lexer::test();
|
2013-12-19 10:39:36 -06:00
|
|
|
//std::cout << strSlice("123", 0, -1) << std::endl;
|
2014-05-21 13:14:16 -04:00
|
|
|
Poset<int>::test();
|
2014-05-21 12:01:45 -04:00
|
|
|
|
2014-05-20 22:21:07 -04:00
|
|
|
if (argc >= 3) {
|
|
|
|
|
std::string testResults, line;
|
|
|
|
|
int passed = 0, failed = 0;
|
|
|
|
|
Tester test(argv[0], "../krakenGrammer.kgm");
|
2015-01-09 14:28:07 -05:00
|
|
|
// find the max length so we can pad the string and align the results
|
|
|
|
|
unsigned int maxLineLength = 0;
|
|
|
|
|
for (int i = 2; i < argc; i++) {
|
|
|
|
|
int strLen = std::string(argv[i]).length();
|
|
|
|
|
maxLineLength = maxLineLength < strLen ? strLen : maxLineLength;
|
|
|
|
|
}
|
2014-05-20 22:21:07 -04:00
|
|
|
for (int i = 2; i < argc; i++) {
|
|
|
|
|
bool result = test.run(argv[i]);
|
|
|
|
|
if (result)
|
2015-01-09 14:28:07 -05:00
|
|
|
line = padWithSpaces(std::string(argv[i]), maxLineLength) + "\t\tpassed!\n", passed++;
|
2014-05-20 22:21:07 -04:00
|
|
|
else
|
2015-01-09 14:28:07 -05:00
|
|
|
line = padWithSpaces(std::string(argv[i]), maxLineLength) + "\t\tFAILED!!!!\n", failed++;
|
2014-05-20 22:21:07 -04:00
|
|
|
std::cout << line << std::endl;
|
|
|
|
|
testResults += line;
|
|
|
|
|
}
|
|
|
|
|
std::cout << "===========Done Testing===========" << std::endl;
|
|
|
|
|
std::cout << testResults << std::endl;
|
|
|
|
|
std::cout << "Test results: " << passed << "/" << passed+failed << std::endl;
|
|
|
|
|
}
|
2013-10-25 02:04:22 -07:00
|
|
|
return 0;
|
|
|
|
|
}
|
2014-05-01 01:18:01 -04:00
|
|
|
std::string krakenDir = argv[0];
|
|
|
|
|
krakenDir = strSlice(krakenDir, 0, -(std::string("kraken").length()+1));
|
2014-05-20 23:53:19 -04:00
|
|
|
includePaths.push_back(krakenDir + "stdlib/"); //Add the stdlib directory that exists in the same directory as the kraken executable to the path
|
2015-03-15 18:41:55 -04:00
|
|
|
|
|
|
|
|
std::string grammerFileString = "../krakenGrammer.kgm";
|
|
|
|
|
std::string programName;
|
|
|
|
|
std::string outputName;
|
|
|
|
|
if (argc > 3) {
|
|
|
|
|
grammerFileString = argv[1];
|
|
|
|
|
programName = argv[2];
|
|
|
|
|
outputName = argv[3];
|
|
|
|
|
} else if (argc == 3) {
|
|
|
|
|
programName = argv[1];
|
|
|
|
|
outputName = argv[2];
|
|
|
|
|
} else {
|
|
|
|
|
programName = argv[1];
|
|
|
|
|
outputName = join(slice(split(programName, '.'), 0, -2), "."); // without extension
|
|
|
|
|
}
|
2013-12-31 23:43:49 -06:00
|
|
|
|
|
|
|
|
std::ifstream grammerInFile, compiledGrammerInFile;
|
2014-05-20 23:53:19 -04:00
|
|
|
std::ofstream compiledGrammerOutFile;
|
2013-12-28 21:55:43 -05:00
|
|
|
|
2013-12-28 21:54:22 -05:00
|
|
|
grammerInFile.open(grammerFileString);
|
2013-05-29 20:43:35 -04:00
|
|
|
if (!grammerInFile.is_open()) {
|
2015-03-14 02:42:07 -04:00
|
|
|
std::cerr << "Problem opening grammerInFile " << grammerFileString << "\n";
|
2013-05-29 20:43:35 -04:00
|
|
|
return(1);
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-31 02:53:52 -06:00
|
|
|
compiledGrammerInFile.open(grammerFileString + ".comp", std::ios::binary | std::ios::ate);
|
2014-05-20 23:53:19 -04:00
|
|
|
if (!compiledGrammerInFile.is_open())
|
2015-03-14 02:42:07 -04:00
|
|
|
std::cerr << "Problem opening compiledGrammerInFile " << grammerFileString + ".comp" << "\n";
|
2014-05-20 23:53:19 -04:00
|
|
|
|
2013-05-20 19:34:15 -04:00
|
|
|
//Read the input file into a string
|
2013-12-31 23:43:49 -06:00
|
|
|
std::string grammerInputFileString;
|
2013-05-20 19:34:15 -04:00
|
|
|
std::string line;
|
2013-05-29 20:43:35 -04:00
|
|
|
while(grammerInFile.good()) {
|
|
|
|
|
getline(grammerInFile, line);
|
|
|
|
|
grammerInputFileString.append(line+"\n");
|
|
|
|
|
}
|
2013-11-01 02:52:18 -04:00
|
|
|
grammerInFile.close();
|
2013-05-29 20:43:35 -04:00
|
|
|
|
2013-07-31 23:51:05 -04:00
|
|
|
//LALRParser parser;
|
|
|
|
|
RNGLRParser parser;
|
2013-05-29 20:43:35 -04:00
|
|
|
parser.loadGrammer(grammerInputFileString);
|
2013-12-31 02:53:52 -06:00
|
|
|
|
|
|
|
|
//Start binary stuff
|
|
|
|
|
bool compGramGood = false;
|
|
|
|
|
if (compiledGrammerInFile.is_open()) {
|
|
|
|
|
std::cout << "Compiled grammer file exists, reading it in" << std::endl;
|
|
|
|
|
std::streampos compGramSize = compiledGrammerInFile.tellg();
|
|
|
|
|
char* binaryTablePointer = new char [compGramSize];
|
|
|
|
|
compiledGrammerInFile.seekg(0, std::ios::beg);
|
|
|
|
|
compiledGrammerInFile.read(binaryTablePointer, compGramSize);
|
|
|
|
|
compiledGrammerInFile.close();
|
2013-12-31 23:43:49 -06:00
|
|
|
//Check magic number
|
2013-12-31 02:53:52 -06:00
|
|
|
if (binaryTablePointer[0] == 'K' && binaryTablePointer[1] == 'R' && binaryTablePointer[2] == 'A' && binaryTablePointer[3] == 'K') {
|
|
|
|
|
std::cout << "Valid Kraken Compiled Grammer File" << std::endl;
|
|
|
|
|
int gramStringLength = *((int*)(binaryTablePointer+4));
|
2014-01-01 17:29:19 -06:00
|
|
|
//std::cout << "The grammer string is stored to be " << gramStringLength << " characters long, gramString is "
|
|
|
|
|
//<< grammerInputFileString.length() << " long. Remember 1 extra for null terminator!" << std::endl;
|
2013-12-31 02:53:52 -06:00
|
|
|
if (grammerInputFileString.length() != gramStringLength-1 ||
|
|
|
|
|
(strncmp(grammerInputFileString.c_str(), (binaryTablePointer+4+sizeof(int)), gramStringLength) != 0)) {
|
|
|
|
|
//(one less for null terminator that is stored)
|
|
|
|
|
std::cout << "The Grammer has been changed, will re-create" << std::endl;
|
|
|
|
|
} else {
|
|
|
|
|
compGramGood = true;
|
2014-01-01 17:29:19 -06:00
|
|
|
std::cout << "Grammer file is up to date." << std::endl;
|
2013-12-31 02:53:52 -06:00
|
|
|
parser.importTable(binaryTablePointer + 4 + sizeof(int) + gramStringLength); //Load table starting at the table section
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2015-03-14 02:42:07 -04:00
|
|
|
std::cerr << grammerFileString << ".comp is NOT A Valid Kraken Compiled Grammer File, aborting" << std::endl;
|
2013-12-31 02:53:52 -06:00
|
|
|
return -1;
|
|
|
|
|
}
|
2015-03-23 14:35:28 -04:00
|
|
|
delete [] binaryTablePointer;
|
2013-12-31 02:53:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!compGramGood) {
|
|
|
|
|
//The load failed because either the file does not exist or it is not up-to-date.
|
|
|
|
|
std::cout << "Compiled grammer file does not exist or is not up-to-date, generating table and writing it out" << std::endl;
|
|
|
|
|
compiledGrammerOutFile.open(grammerFileString + ".comp", std::ios::binary);
|
|
|
|
|
if (!compiledGrammerOutFile.is_open())
|
2015-03-14 02:42:07 -04:00
|
|
|
std::cerr << "Could not open compiled file to write either!" << std::endl;
|
2014-01-19 18:20:52 -05:00
|
|
|
compiledGrammerOutFile.write("KRAK", sizeof(char)*4); //Let us know when we load it that this is a kraken grammer file, but don't write out
|
|
|
|
|
compiledGrammerOutFile.flush(); // the grammer txt until we create the set, so that if we fail creating it it won't look valid
|
|
|
|
|
parser.createStateSet();
|
2013-12-31 02:53:52 -06:00
|
|
|
int* intBuffer = new int;
|
|
|
|
|
*intBuffer = grammerInputFileString.length()+1;
|
|
|
|
|
compiledGrammerOutFile.write((char*)intBuffer, sizeof(int));
|
|
|
|
|
delete intBuffer;
|
|
|
|
|
compiledGrammerOutFile.write(grammerInputFileString.c_str(), grammerInputFileString.length()+1); //Don't forget null terminator
|
|
|
|
|
|
|
|
|
|
parser.exportTable(compiledGrammerOutFile);
|
|
|
|
|
compiledGrammerOutFile.close();
|
|
|
|
|
}
|
|
|
|
|
//End binary stuff
|
|
|
|
|
|
2013-08-16 00:03:26 -04:00
|
|
|
std::cout << "\nParsing" << std::endl;
|
2013-05-11 16:13:46 -04:00
|
|
|
|
2015-03-15 18:41:55 -04:00
|
|
|
std::cout << "\n output name: " << outputName << std::endl;
|
|
|
|
|
std::cout << "\n program name: " << programName << std::endl;
|
|
|
|
|
Importer importer(&parser, includePaths, outputName); // Output name for directory to put stuff in
|
2013-05-30 19:49:19 -04:00
|
|
|
|
2014-05-01 01:18:01 -04:00
|
|
|
for (auto i : includePaths)
|
|
|
|
|
std::cout << i << std::endl;
|
|
|
|
|
|
2014-01-01 17:29:19 -06:00
|
|
|
importer.import(programName);
|
2014-02-18 21:55:00 -05:00
|
|
|
std::map<std::string, NodeTree<ASTData>*> ASTs = importer.getASTMap();
|
2013-05-11 16:13:46 -04:00
|
|
|
|
2014-05-20 23:53:19 -04:00
|
|
|
//Do optimization, etc. here.
|
2013-12-22 01:34:59 -06:00
|
|
|
//None at this time, instead going straight to C in this first (more naive) version
|
2013-11-01 02:52:18 -04:00
|
|
|
|
|
|
|
|
//Code generation
|
|
|
|
|
//For right now, just C
|
2014-01-01 17:29:19 -06:00
|
|
|
|
|
|
|
|
CGenerator().generateCompSet(ASTs, outputName);
|
2013-05-11 16:13:46 -04:00
|
|
|
return(0);
|
|
|
|
|
}
|
2014-06-10 00:53:30 -07:00
|
|
|
|