From 6a75832b598d9c96723fa855528c469ce586aa9b Mon Sep 17 00:00:00 2001 From: Nathan Braswell Date: Thu, 1 May 2014 01:18:01 -0400 Subject: [PATCH] Added trivial standard library and search paths. --- CMakeLists.txt | 2 ++ include/Importer.h | 3 ++- main.cpp | 12 ++++++++++-- src/Importer.cpp | 9 +++++++-- stdlib/io.krak | 30 ++++++++++++++++++++++++++++++ stdlib/math.krak | 9 +++++++++ 6 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 stdlib/io.krak create mode 100644 stdlib/math.krak diff --git a/CMakeLists.txt b/CMakeLists.txt index db3eb07..12ef013 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,3 +12,5 @@ include_directories( ${MY_INCLUDES} ) add_executable(kraken ${MY_SOURCES}) +file(COPY stdlib DESTINATION .) + diff --git a/include/Importer.h b/include/Importer.h index c0b4f55..dc79e7a 100644 --- a/include/Importer.h +++ b/include/Importer.h @@ -16,11 +16,12 @@ class Importer { public: - Importer(Parser* parserIn); + Importer(Parser* parserIn, std::vector includePaths); ~Importer(); NodeTree* import(std::string fileName); std::map*> getASTMap(); private: + std::vector includePaths; Parser* parser; std::vector removeSymbols; std::vector collapseSymbols; diff --git a/main.cpp b/main.cpp index 90028d0..06d6e61 100644 --- a/main.cpp +++ b/main.cpp @@ -18,6 +18,9 @@ #include "util.h" int main(int argc, char* argv[]) { + std::vector includePaths; + includePaths.push_back(""); //Local + if (argc == 2 && std::string(argv[1]) == "--test") { StringReader::test(); RegEx::test(); @@ -25,7 +28,9 @@ int main(int argc, char* argv[]) { //std::cout << strSlice("123", 0, -1) << std::endl; return 0; } - + std::string krakenDir = argv[0]; + krakenDir = strSlice(krakenDir, 0, -(std::string("kraken").length()+1)); + includePaths.push_back(krakenDir + "stdlib/"); std::string programName = argv[1]; std::string grammerFileString = argv[2]; std::string outputName = argv[3]; @@ -134,9 +139,12 @@ int main(int argc, char* argv[]) { //outFile << parser.grammerToDOT() << std::endl; std::cout << "\nParsing" << std::endl; - Importer importer(&parser); + Importer importer(&parser, includePaths); /*NodeTree* AST =*/ + for (auto i : includePaths) + std::cout << i << std::endl; + importer.import(programName); std::map*> ASTs = importer.getASTMap(); diff --git a/src/Importer.cpp b/src/Importer.cpp index 88c367f..edfcc47 100644 --- a/src/Importer.cpp +++ b/src/Importer.cpp @@ -1,8 +1,9 @@ #include "Importer.h" -Importer::Importer(Parser* parserIn) { +Importer::Importer(Parser* parserIn, std::vector includePaths) { //constructor parser = parserIn; + this->includePaths = includePaths; removeSymbols.push_back(Symbol("WS", false)); removeSymbols.push_back(Symbol("\\(", true)); @@ -47,7 +48,11 @@ NodeTree* Importer::import(std::string fileName) { std::string outputName = fileName + "out"; - programInFile.open(fileName); + for (auto i : includePaths) { + programInFile.open(i+fileName); + if (programInFile.is_open()) + break; + } if (!programInFile.is_open()) { std::cout << "Problem opening programInFile " << fileName << "\n"; return NULL; diff --git a/stdlib/io.krak b/stdlib/io.krak new file mode 100644 index 0000000..44c72a2 --- /dev/null +++ b/stdlib/io.krak @@ -0,0 +1,30 @@ +__if_comp__ __C__ __simple_passthrough__ """ + #include +""" + +void print(char* toPrint) { + __if_comp__ __C__ { + __simple_passthrough__ """ + printf(toPrint); + """ + } + return; +} + +void print(int toPrint) { + __if_comp__ __C__ { + __simple_passthrough__ """ + printf("%d", toPrint); + """ + } + return; +} + +void print(float toPrint) { + __if_comp__ __C__ { + __simple_passthrough__ """ + printf("%f", toPrint); + """ + } + return; +} \ No newline at end of file diff --git a/stdlib/math.krak b/stdlib/math.krak new file mode 100644 index 0000000..5b245ab --- /dev/null +++ b/stdlib/math.krak @@ -0,0 +1,9 @@ + +int NotPi = 3; +float Pi = 3.141592654; + +int fibanacci(int num) { + if (num < 2) + return 1; + return fibanacci(num-1) + fibanacci(num-2); +} \ No newline at end of file