summaryrefslogtreecommitdiffhomepage
path: root/src/CompilerKit
diff options
context:
space:
mode:
author😄 Amlal El Mahrouss <amlal@nekernel.org>2026-03-18 08:52:07 +0100
committerGitHub <noreply@github.com>2026-03-18 08:52:07 +0100
commitb8db61d6514afdec98bc8edbc22ceeed189b8309 (patch)
treefa3d378b50667f96c60a3f412547083fbc0786ac /src/CompilerKit
parent6dc565002cbb1c84004d760d5f24e69646082b7a (diff)
parent3925c485946ae8e95fcd94329a23357cac4226b0 (diff)
Merge pull request #76 from ne-foss-org/nectar-frontend-check
[FEAT] Nectar syntax checker module.
Diffstat (limited to 'src/CompilerKit')
-rw-r--r--src/CompilerKit/src/CodeGenerator+AssemblyFactory.cpp13
-rw-r--r--src/CompilerKit/src/Frontends/NectarCompiler+AMD64.cpp12
-rw-r--r--src/CompilerKit/src/Frontends/NectarCompiler+Chk.cpp66
-rw-r--r--src/CompilerKit/src/Frontends/NectarCompiler+PTX.cpp6
4 files changed, 85 insertions, 12 deletions
diff --git a/src/CompilerKit/src/CodeGenerator+AssemblyFactory.cpp b/src/CompilerKit/src/CodeGenerator+AssemblyFactory.cpp
index 1dc24a0..04ed2be 100644
--- a/src/CompilerKit/src/CodeGenerator+AssemblyFactory.cpp
+++ b/src/CompilerKit/src/CodeGenerator+AssemblyFactory.cpp
@@ -28,11 +28,16 @@ Int32 AssemblyFactory::Compile(STLString sourceFile, const Int32& arch) {
auto compiledUnit = sourceFile + ".ignore";
- std::filesystem::copy(sourceFile, compiledUnit);
- auto ret = this->fMounted->CompileToFormat(compiledUnit, arch);
- std::filesystem::remove(compiledUnit);
+ try {
+ std::filesystem::copy(sourceFile, compiledUnit);
+ auto ret = this->fMounted->CompileToFormat(compiledUnit, arch);
+ std::filesystem::remove(compiledUnit);
+ return ret;
+ } catch (...) {
+ std::filesystem::remove(compiledUnit);
+ }
- return ret;
+ return NECTAR_UNIMPLEMENTED;
}
///! @brief mount assembly backend.
diff --git a/src/CompilerKit/src/Frontends/NectarCompiler+AMD64.cpp b/src/CompilerKit/src/Frontends/NectarCompiler+AMD64.cpp
index f5ac27f..85e12ea 100644
--- a/src/CompilerKit/src/Frontends/NectarCompiler+AMD64.cpp
+++ b/src/CompilerKit/src/Frontends/NectarCompiler+AMD64.cpp
@@ -17,7 +17,7 @@
/* (c) Amlal El Mahrouss 2024-2026 */
/// @author Amlal El Mahrouss (amlal@nekernel.org)
-/// @file NectarCompiler+AMD64.cc
+/// @file NectarCompiler+AMD64.cpp
/// @brief NECTAR Compiler Driver.
/////////////////////////////////////
@@ -66,6 +66,8 @@ struct CompilerState final {
static CompilerState kState;
+static bool kFreestandingMode = true;
+
/// \brief Embed Scope of a class.
static Int32 kOnClassScope = 0;
@@ -279,7 +281,7 @@ CompilerKit::SyntaxLeafList::SyntaxLeaf CompilerFrontendNectarAMD64::Compile(
CompilerKit::SyntaxLeafList::SyntaxLeaf syntax_tree;
CompilerKit::STLString syntax_rem_buffer;
- if (text.empty()) return syntax_tree;
+ if (!NectarCheckFrontend(text)) return syntax_tree;
std::size_t index{};
std::vector<std::pair<CompilerKit::SyntaxKeyword, std::size_t>> keywords_list;
@@ -1600,12 +1602,14 @@ class AssemblyNectarInterfaceAMD64 final CK_ASSEMBLY_INTERFACE {
prevRes = res.fUserValue;
}
- // Output header
+ // Output bits and imports.
if (!kNasmOutput)
out_fp << "%bits 64\n";
else {
out_fp << "[bits 64]\n";
- out_fp << "extern __operator_new\nextern __operator_delete\n";
+ if (kFreestandingMode) {
+ out_fp << "extern __operator_new\nextern __operator_delete\n";
+ }
}
// For NASM output: emit extern declarations for undefined symbols
diff --git a/src/CompilerKit/src/Frontends/NectarCompiler+Chk.cpp b/src/CompilerKit/src/Frontends/NectarCompiler+Chk.cpp
index f3f4da6..cc30592 100644
--- a/src/CompilerKit/src/Frontends/NectarCompiler+Chk.cpp
+++ b/src/CompilerKit/src/Frontends/NectarCompiler+Chk.cpp
@@ -14,4 +14,68 @@
/* Nectar Compiler Check Driver. */
/* This is part of the CompilerKit. */
-/* (c) Amlal El Mahrouss 2024-2026 */ \ No newline at end of file
+/* (c) Amlal El Mahrouss 2026 */
+
+using namespace CompilerKit;
+
+static bool kInIfBody = false;
+static bool kInCtxBody = false;
+
+CK_IMPORT_C bool NectarCheckFrontend(CompilerKit::STLString& input) {
+ if (input.empty()) return false;
+
+ if (input.ends_with(":")) {
+ if (!input.ends_with("):")) {
+ Detail::print_error("Invalid keyword 'else if' is not a Nectar keyword!", "check");
+ return false;
+ }
+
+ kInIfBody = true;
+ }
+
+ if (input.find("(") != CompilerKit::STLString::npos) {
+ if (input.find(")") == CompilerKit::STLString::npos) {
+ Detail::print_error("Invalid call to function, Nectar expects the ')' character at the end!",
+ "check");
+ return false;
+ }
+ }
+
+ if (input.find("let ") != CompilerKit::STLString::npos && !input.ends_with(";")) {
+ if (input.find(":=") != CompilerKit::STLString::npos) {
+ Detail::print_error("A declaration must always end with ';'", "check");
+ return false;
+ }
+ }
+
+ if (input.find("const ") != CompilerKit::STLString::npos && !input.ends_with(";")) {
+ if (input.find(":=") != CompilerKit::STLString::npos) {
+ Detail::print_error("A declaration must always end with ';'", "check");
+ return false;
+ }
+ }
+
+ if (input.starts_with("let ") && !input.ends_with(";")) {
+ if (input.find(":=") != CompilerKit::STLString::npos) {
+ Detail::print_error("A declaration must always end with ';'", "check");
+ return false;
+ }
+ }
+
+ if (input.starts_with("const ") && !input.ends_with(";")) {
+ if (input.find(":=") != CompilerKit::STLString::npos) {
+ Detail::print_error("A declaration must always end with ';'", "check");
+ return false;
+ }
+ }
+
+ if (input == "}" || input == "}\n" || input == "}\r\n") {
+ if (kInIfBody) kInIfBody = false;
+ }
+
+ if (input == "}" || input == "}\n" || input == "}\r\n") {
+ if (kInCtxBody) kInCtxBody = false;
+ }
+
+ return true;
+}
diff --git a/src/CompilerKit/src/Frontends/NectarCompiler+PTX.cpp b/src/CompilerKit/src/Frontends/NectarCompiler+PTX.cpp
index a7136b8..5d15a77 100644
--- a/src/CompilerKit/src/Frontends/NectarCompiler+PTX.cpp
+++ b/src/CompilerKit/src/Frontends/NectarCompiler+PTX.cpp
@@ -272,7 +272,7 @@ static std::vector<std::pair<CompilerKit::STLString, std::uintptr_t>> kOriginMap
/////////////////////////////////////////////////////////////////////////////////////////
static auto nectar_get_impl_member(const CompilerKit::STLString& class_name,
- const CompilerKit::STLString& member_name) {
+ const CompilerKit::STLString& member_name) -> CompilerStructMap {
// Find or create struct map entry
for (auto& sm : kContext.fStructMapVector) {
if (sm.fName == class_name) {
@@ -280,7 +280,7 @@ static auto nectar_get_impl_member(const CompilerKit::STLString& class_name,
}
}
- return CompilerStructMap{};
+ return {};
}
CompilerKit::SyntaxLeafList::SyntaxLeaf CompilerFrontendNectarPTX::Compile(
@@ -288,7 +288,7 @@ CompilerKit::SyntaxLeafList::SyntaxLeaf CompilerFrontendNectarPTX::Compile(
CompilerKit::SyntaxLeafList::SyntaxLeaf syntax_tree;
CompilerKit::STLString syntax_rem_buffer;
- if (text.empty()) return syntax_tree;
+ if (!NectarCheckFrontend(text)) return syntax_tree;
std::size_t index{};
std::vector<std::pair<CompilerKit::SyntaxKeyword, std::size_t>> keywords_list;