Skip to content

Commit

Permalink
Added directory searching.
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipPragerUrbina committed Jun 14, 2022
1 parent 891e9f9 commit 328812e
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class Environment {
}
private:
Environment* m_enclosing = nullptr;
//TODO test performance here

//could be faster?
std::unordered_map<std::string,object> m_values;
std::unordered_map<std::string,bool> m_constants;
Expand Down
28 changes: 27 additions & 1 deletion File.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,29 @@
#include <iostream>
#include <fstream>
#include <vector>
#include <filesystem>
#ifdef _WIN32
#include <windows.h>
#elif
#include <unistd.h>
#endif
//helper function for getting executable directory
std::string getExeDirectory(std::string filename)
{
#ifdef _WIN32
// Windows specific
wchar_t szPath[MAX_PATH];
GetModuleFileNameW( NULL, szPath, MAX_PATH );
#else
// Linux specific
char szPath[PATH_MAX];
ssize_t count = readlink( "/proc/self/exe", szPath, PATH_MAX );
if( count < 0 || count >= PATH_MAX )
return {}; // some error
szPath[count] = '\0';
#endif
return (std::filesystem::path{ szPath }.parent_path() / "").string() + "/" + filename; // to finish the folder path with (back)slash
}

//file class for handling multiple files
class File {
Expand All @@ -35,7 +58,7 @@ class File {
std::cout << "File read successfully" << "\n";

}else{
std::cerr << "Can not open file: " << filename << "\n";
std::cout << "Can not open file: " << filename << "\n";
}
}
//is file read good
Expand All @@ -57,6 +80,9 @@ class File {
const std::string operator[](const int i) {
return m_lines[i];
}
const std::string getName(){
return m_filename;
}

private:
//file data
Expand Down
2 changes: 1 addition & 1 deletion Interpreter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ class Interpreter : public Visitor{
object visitBinaryExpression(Binary* expression){
object right = eval(expression->m_right);
object left = eval(expression->m_left);
//TODO add operator overloading

switch (expression->m_operator_.type) {
case BANG_EQUAL: return !isEqual(left, right);
case EQUAL_EQUAL: return isEqual(left, right);
Expand Down
6 changes: 5 additions & 1 deletion Parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ typedef std::vector<Statement*> statementList;

class Parser {
public:
std::string link_directory = "";
//create parser from tokens
Parser( std::vector<Token> tokens, ErrorHandler* error_handler){
m_error_handler = error_handler;
Expand Down Expand Up @@ -120,6 +121,9 @@ class Parser {
} else if(match({LINK})){
Token directory = consume(STRING, "Expected link directory.");
consume(SEMICOLON, "Expected ; after include.");
if(link_directory != ""){
directory .value = link_directory + "/" + std::get<std::string>(directory.value);
}
return new IncludeStatement(directory, getLine(), true);
}
m_error_handler->error("Expected imports or includes.");
Expand Down Expand Up @@ -231,7 +235,7 @@ class Parser {

return statements;
}
//TODO make proper statements for break and continue.

Statement* loopStatement(){
Token keyword = m_tokens[m_current-1];
consume(SEMICOLON, "Expected ; after expression.");
Expand Down
72 changes: 48 additions & 24 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//define language version
#define DOGE_LANGUAGE_VERSION "v0.93"
#define DOGE_LANGUAGE_VERSION "v0.76"
#include "popl.hpp"
#include <iostream>

#include "File.hpp"

#include "Scanner.hpp"
#include "Parser.hpp"
#include "IRCompiler.hpp"
#include "Analyzer.hpp"

#include "File.hpp"
#include <windows.h>
#include <filesystem>

Expand All @@ -19,8 +19,8 @@ int main(int argc, char **argv) {
popl::OptionParser command_line("Doge compiler");
//set options
auto help_option = command_line.add<popl::Switch>("h", "help", "Print help message");
auto source_filename_option = command_line.add<popl::Value<std::string>>("s", "source", "Set source filename","main.doge");
auto output_filename_option = command_line.add<popl::Value<std::string>>("o", "output", "Set output filename","output.exe");
auto source_filename_option = command_line.add<popl::Value<std::string>>("s", "source", "Set source filename. Can be .doge or .dogel","main.doge");
auto output_filename_option = command_line.add<popl::Value<std::string>>("o", "output", "Set output filename. Can be .o, .exe, .s, or .ll","output.exe");
auto print_llvm_option = command_line.add<popl::Switch>("p", "print", "Print llvm ir");
auto error_option = command_line.add<popl::Switch>("e", "error", "Only error check");
auto optimize_disable_option = command_line.add<popl::Switch>("u", "unoptimized", "Disable optimizations");
Expand Down Expand Up @@ -59,7 +59,6 @@ int main(int argc, char **argv) {
std::cout << "Building " << source_filename << " to " << object_filename << " "<< code_filename<< " " << executable_filename << " \n";
Color::end();

//TODO work with multiple custom source files
//check if source is valid
std::filesystem::path source_path(source_filename);
if (!exists(source_path)) {
Expand All @@ -70,22 +69,7 @@ int main(int argc, char **argv) {
std::cerr << "File is not a .doge or .dogel file: " + source_filename;
return 1;
}
//check if source was maodified
auto source_modify_time = last_write_time(source_path);
if (exists(build_path)) {
auto build_modify_time = last_write_time(build_path);
if (source_modify_time < build_modify_time) {
//source has not changed since build, just run or exit
Color::start(GREEN);
std::cout << "\nNo changes detected.\n";
Color::end();
if(executable_filename != ""){
std::cout << "\nRunning build...\n";
system(executable_filename.c_str());
}
return 0;
}
}

//create error handler
ErrorHandler error_handler;
Expand All @@ -112,6 +96,11 @@ int main(int argc, char **argv) {
std::cout << "\nParsing: " + include +" \n";
File file(include);
if (!file.read()) { return 1; }
//check for changes
auto include_modify_time = last_write_time(std::filesystem::path(include));
if (source_modify_time < include_modify_time) {
source_modify_time = include_modify_time;
}
Scanner include_scanner(file.getData(), &error_handler);
include_scanner.scan();
Parser include_parser(include_scanner.getTokens(), &error_handler);
Expand All @@ -120,15 +109,50 @@ int main(int argc, char **argv) {
}
for (std::string import: parser.m_imports) {
std::cout << "\nParsing: " + import +".dogel \n";
File file(import + ".dogel");
if (!file.read()) { return 1; }
Scanner include_scanner(file.getData(), &error_handler);
File* file = new File(import + ".dogel");
std::string link_directory = "";
if (!file->read()) {
delete file;
//check in exe directory
File* new_file = new File(getExeDirectory(import+".dogel"));
if(!new_file->read()){
return 1;
}
link_directory = getExeDirectory("");
file = new_file;
}
//check for changes
//check for changes
auto include_modify_time = last_write_time(std::filesystem::path( file->getName()));
if (source_modify_time < include_modify_time) {
source_modify_time = include_modify_time;
}
Scanner include_scanner(file->getData(), &error_handler);
include_scanner.scan();
Parser include_parser(include_scanner.getTokens(), &error_handler);
include_parser.link_directory = link_directory;
external_files[import] = include_parser.parse();
if (error_handler.hasErrors()) { return 1; }
delete file;
}


//check if source was modified
if (exists(build_path)) {
auto build_modify_time = last_write_time(build_path);
if (source_modify_time < build_modify_time) {
//source has not changed since build, just run or exit
Color::start(GREEN);
std::cout << "\nNo changes detected.\n";
Color::end();
if(executable_filename != ""){
std::cout << "\nRunning build...\n";
system(executable_filename.c_str());
}
return 0;
}
}

//error check
std::cout << "\nChecking... \n";
Analyzer analyzer;
Expand Down

0 comments on commit 328812e

Please sign in to comment.