Made the IncludePreprocessor a bit more robust (added -I option to dasm)

This commit is contained in:
OmniaX-Dev 2026-03-20 01:41:22 +01:00
parent 2486e5e4c0
commit 16b706175d
6 changed files with 120 additions and 83 deletions

View file

@ -3,7 +3,7 @@
.entry _kernel0_main
.header KERNEL0_BOOT
@include <../../sdk/bios_api.dss>
@include <sdk/bios_api.dss>
@include <../drivers/display_driver.dss>
@include <../drivers/keyboard_driver.dss>
@include <memory.dss>

View file

@ -6,19 +6,19 @@ green='\033[0;32m'
clear='\033[0m'
printf "${green}Flashing BIOS...\n${clear}"
./dasm dss/bios/entry.dss -o dragon/bios.bin $1
./dasm dss/bios/entry.dss -o dragon/bios.bin -I dss $1
printf "\n${green}Compiling Drake...\n${clear}"
printf "\n${green}1) loader.dss\n${clear}"
./dasm dss/drake/loader.dss -o dragon/drake_loader.bin $1
./dasm dss/drake/loader.dss -o dragon/drake_loader.bin -I dss $1
./dtools load-binary dragon/disk1.dr dragon/drake_loader.bin 0x00000000
printf "\n${green}2) bootsector.dss\n${clear}"
./dasm dss/drake/bootsector.dss -o dragon/drake_bootsector.bin $1
./dasm dss/drake/bootsector.dss -o dragon/drake_bootsector.bin -I dss $1
./dtools load-binary dragon/disk1.dr dragon/drake_bootsector.bin 0x00000400
printf "\n${green}Compiling DragonOS...\n${clear}"
printf "\n${green}1) kernel0.dss\n${clear}"
./dasm dss/DragonOS/kernel0/kernel0.dss -o dragon/kernel0.bin $1
./dasm dss/DragonOS/kernel0/kernel0.dss -o dragon/kernel0.bin -I dss $1
./dtools load-binary dragon/disk1.dr dragon/kernel0.bin 0x00018000
printf "\n"

View file

@ -3,6 +3,7 @@
#include <ostd/utils/Utils.hpp>
#include <ostd/io/IOHandlers.hpp>
#include <unordered_map>
#include <vector>
namespace dragon
{
@ -96,6 +97,7 @@ namespace dragon
bool save_exports { true };
ostd::String disassembly_file_path { "" };
std::vector<ostd::String> cpu_extensions;
std::vector<ostd::String> include_directories;
};
public:
@ -108,6 +110,7 @@ namespace dragon
inline static const int32_t RETURN_VAL_CLOSE_PROGRAM = 512;
inline static const int32_t RETURN_VAL_TOO_FEW_ARGUMENTS = 1;
inline static const int32_t RETURN_VAL_MISSING_PARAM = 2;
inline static const int32_t RETURN_VAL_INVALID_PARAM = 3;
};
public:

View file

@ -1,5 +1,6 @@
#include "Assembler.hpp"
#include <ostd/math/Random.hpp>
#include <ostd/io/FileSystem.hpp>
namespace dragon
{
@ -35,6 +36,19 @@ namespace dragon
i++;
args.dest_file_path = argv[i];
}
else if (edit == "-I" || edit == "--include-path")
{
if (i == argc - 1)
return RETURN_VAL_MISSING_PARAM;
i++;
ostd::String _include = argv[i];
_include.trim();
if (!_include.endsWith("/"))
_include.add("/");
if (!ostd::FileSystem::directoryExists(_include))
return RETURN_VAL_INVALID_PARAM;
args.include_directories.push_back(_include);
}
else if (edit == "--save-disassembly")
{
if (i == argc - 1)
@ -111,6 +125,9 @@ namespace dragon
tmpCommand = "--debug, -D";
tmpCommand.addRightPadding(commandLength);
out.fg(ostd::ConsoleColors::Blue).p(tmpCommand).fg(ostd::ConsoleColors::Green).p("Used to enable debug mode.").reset().nl();
tmpCommand = "--include-path, -I <include-directory-path>";
tmpCommand.addRightPadding(commandLength);
out.fg(ostd::ConsoleColors::Blue).p(tmpCommand).fg(ostd::ConsoleColors::Green).p("Used specify an include directory path (absolute or relative to the <dasm> executable location).").reset().nl();
tmpCommand = "--help";
tmpCommand.addRightPadding(commandLength);
out.fg(ostd::ConsoleColors::Blue).p(tmpCommand).fg(ostd::ConsoleColors::Green).p("Displays this help message.").reset().nl();

View file

@ -1,24 +1,25 @@
#include "Assembler.hpp"
#include <ostd/io/File.hpp>
#include <ostd/io/FileSystem.hpp>
namespace dragon
{
namespace code
{
std::vector<ostd::String> IncludePreprocessor::loadEntryFile(const ostd::String& filePath)
{
m_guards.clear();
m_lines.clear();
m_directory = "";
m_lines = __load_file(filePath);
{
m_guards.clear();
m_lines.clear();
m_directory = "";
m_lines = __load_file(filePath);
if (m_lines.size() == 0) return { }; //TODO: Error
if (filePath.contains("/"))
m_directory = filePath.new_substr(0, filePath.lastIndexOf("/") + 1);
if (!__can_file_be_included(m_lines))
return { }; //TODO: Error
if (!__include_loop()) return { }; //TODO: Error
if (m_lines.size() == 0) return { }; //TODO: Error
if (filePath.contains("/"))
m_directory = filePath.new_substr(0, filePath.lastIndexOf("/") + 1);
if (!__can_file_be_included(m_lines))
return { }; //TODO: Error
if (!__include_loop()) return { }; //TODO: Error
std::vector<ostd::String> newLines;
for (auto& line : m_lines)
{
@ -28,75 +29,90 @@ namespace dragon
}
m_lines.clear();
m_lines = newLines;
return m_lines;
}
return m_lines;
}
bool IncludePreprocessor::__can_file_be_included(std::vector<ostd::String>& lines)
{
ostd::String guard_name = "";
for (auto& line : lines)
{
ostd::String lineEdit = line.new_trim();
if (lineEdit.new_toLower().startsWith("@guard "))
{
guard_name = lineEdit.new_substr(7).trim();
line = "";
break;
}
}
if (guard_name == "") return true;
for (auto& guard : m_guards)
{
if (guard == guard_name)
return false;
}
m_guards.push_back(guard_name);
return true;
}
bool IncludePreprocessor::__can_file_be_included(std::vector<ostd::String>& lines)
{
ostd::String guard_name = "";
for (auto& line : lines)
{
ostd::String lineEdit = line.new_trim();
if (lineEdit.new_toLower().startsWith("@guard "))
{
guard_name = lineEdit.new_substr(7).trim();
line = "";
break;
}
}
if (guard_name == "") return true;
for (auto& guard : m_guards)
{
if (guard == guard_name)
return false;
}
m_guards.push_back(guard_name);
return true;
}
bool IncludePreprocessor::__include_loop(void)
{
std::vector<ostd::String> lines = m_lines;
bool included = false;
do
{
included = false;
uint32_t i = 0;
for ( ; i < lines.size(); i++)
{
ostd::String line = lines[i];
line.trim();
if (line.new_toLower().startsWith("@include") && line.len() > 8)
{
line.substr(8).trim();
if (!line.startsWith("<") || !line.endsWith(">"))
return false;
line.substr(1, line.len() - 1).trim();
auto file_lines = __load_file(m_directory + line);
if (file_lines.size() == 0)
return false;
lines.erase(lines.begin() + i);
if (__can_file_be_included(file_lines))
{
lines.insert(lines.begin() + i, file_lines.begin(), file_lines.end());
included = true;
}
break;
}
}
} while(included);
m_lines = lines;
return true;
}
bool IncludePreprocessor::__include_loop(void)
{
std::vector<ostd::String> lines = m_lines;
bool included = false;
do
{
included = false;
uint32_t i = 0;
for ( ; i < lines.size(); i++)
{
ostd::String line = lines[i];
line.trim();
if (line.new_toLower().startsWith("@include") && line.len() > 8)
{
line.substr(8).trim();
if (!line.startsWith("<") || !line.endsWith(">"))
return false;
line.substr(1, line.len() - 1).trim();
auto file_lines = __load_file(line);
if (file_lines.size() == 0)
return false;
lines.erase(lines.begin() + i);
if (__can_file_be_included(file_lines))
{
lines.insert(lines.begin() + i, file_lines.begin(), file_lines.end());
included = true;
}
break;
}
}
} while(included);
m_lines = lines;
return true;
}
std::vector<ostd::String> IncludePreprocessor::__load_file(const ostd::String& filePath)
{
ostd::TextFileBuffer file(filePath);
if (!file.exists())
return { }; //TODO: Error
ostd::String source = file.rawContent();
return ostd::String(source.replaceAll("\t", " ")).tokenize("\n", false).getRawData();
}
std::vector<ostd::String> IncludePreprocessor::__load_file(const ostd::String& filePath)
{
const auto& include_dirs = Assembler::Application::args.include_directories;
if (ostd::FileSystem::fileExists(m_directory + filePath))
{
ostd::TextFileBuffer file(m_directory + filePath);
ostd::String source = file.rawContent();
return ostd::String(source.replaceAll("\t", " ")).tokenize("\n", false).getRawData();
}
for (const auto& dir : include_dirs)
{
if (ostd::FileSystem::fileExists(dir + filePath))
{
ostd::TextFileBuffer file(dir + filePath);
ostd::String source = file.rawContent();
return ostd::String(source.replaceAll("\t", " ")).tokenize("\n", false).getRawData();
}
}
return { }; //TODO: Error
}
}
}

View file

@ -1,6 +1,7 @@
#include "DisassemblyLoader.hpp"
#include <ostd/io/File.hpp>
#include <ostd/io/FileSystem.hpp>
#include <ostd/io/Serial.hpp>
#include <algorithm>
@ -92,7 +93,7 @@ namespace dragon
void DisassemblyLoader::loadDirectory(const ostd::String& directoryPath)
{
auto list = ostd::Utils::listFilesInDirectory(directoryPath);
auto list = ostd::FileSystem::listFilesInDirectory(directoryPath);
for (auto& path : list)
loadFile(path.string());
}