summaryrefslogtreecommitdiffhomepage
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
parent6dc565002cbb1c84004d760d5f24e69646082b7a (diff)
parent3925c485946ae8e95fcd94329a23357cac4226b0 (diff)
Merge pull request #76 from ne-foss-org/nectar-frontend-checkHEADdevelop
[FEAT] Nectar syntax checker module.
-rw-r--r--example/example_02_nectar/example.nc10
-rw-r--r--example/example_05_nectar_gpu/example.nc2
-rw-r--r--include/CompilerKit/CodeGenerator.h3
-rw-r--r--snippets/.gitkeep0
-rw-r--r--snippets/test_snippets/inner.nc6
-rw-r--r--snippets/test_snippets/test_ostream.nc9
-rw-r--r--snippets/test_snippets/test_printf.nc11
-rw-r--r--snippets/test_snippets/test_struct.nc20
-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
-rw-r--r--src/DebuggerKit/src/MachContract.cpp2
13 files changed, 93 insertions, 67 deletions
diff --git a/example/example_02_nectar/example.nc b/example/example_02_nectar/example.nc
index b308bab..5e95c44 100644
--- a/example/example_02_nectar/example.nc
+++ b/example/example_02_nectar/example.nc
@@ -1,13 +1,7 @@
extern exit;
-const terminate()
-{
- let EXIT_TERMINATED = 0x100;
- exit(EXIT_TERMINATED);
-}
-
const main()
{
- terminate();
- return 0;
+ let foo := exit(0);
+ return 0;
}
diff --git a/example/example_05_nectar_gpu/example.nc b/example/example_05_nectar_gpu/example.nc
index f170b31..976b329 100644
--- a/example/example_05_nectar_gpu/example.nc
+++ b/example/example_05_nectar_gpu/example.nc
@@ -1,6 +1,6 @@
extern palloc_bytes;
const main() {
- const dummy := palloc_bytes(0, 1, 0);
+ let dummy := palloc_bytes(0, 1, 0);
return dummy;
}
diff --git a/include/CompilerKit/CodeGenerator.h b/include/CompilerKit/CodeGenerator.h
index ac42879..69af3ba 100644
--- a/include/CompilerKit/CodeGenerator.h
+++ b/include/CompilerKit/CodeGenerator.h
@@ -18,6 +18,9 @@
#define CK_ASSEMBLY_INTERFACE : public ::CompilerKit::IAssembly
#define CK_ENCODER : public ::CompilerKit::IAssemblyEncoder
+/// @brief This function is for internal uses only, do not call it without a wrapper!
+CK_IMPORT_C bool NectarCheckFrontend(CompilerKit::STLString& input);
+
namespace CompilerKit {
class AssemblyFactory;
class IAssembly;
diff --git a/snippets/.gitkeep b/snippets/.gitkeep
deleted file mode 100644
index e69de29..0000000
--- a/snippets/.gitkeep
+++ /dev/null
diff --git a/snippets/test_snippets/inner.nc b/snippets/test_snippets/inner.nc
deleted file mode 100644
index 3004ea7..0000000
--- a/snippets/test_snippets/inner.nc
+++ /dev/null
@@ -1,6 +0,0 @@
-
-let main()
-{
- let foo := 42;
- return foo;
-}
diff --git a/snippets/test_snippets/test_ostream.nc b/snippets/test_snippets/test_ostream.nc
deleted file mode 100644
index 0d0410b..0000000
--- a/snippets/test_snippets/test_ostream.nc
+++ /dev/null
@@ -1,9 +0,0 @@
-#include <GenericsLibrary/ostream.nhh>
-
-let main()
-{
- let io := 0;
- io := ostream{};
- let arr := io.read(0, 0);
- return arr;
-} \ No newline at end of file
diff --git a/snippets/test_snippets/test_printf.nc b/snippets/test_snippets/test_printf.nc
deleted file mode 100644
index c29bb05..0000000
--- a/snippets/test_snippets/test_printf.nc
+++ /dev/null
@@ -1,11 +0,0 @@
-export main;
-
-let main()
-{
- if (0x01 =: 0x01):
- {
- return 0;
- }
-
- return 1;
-} \ No newline at end of file
diff --git a/snippets/test_snippets/test_struct.nc b/snippets/test_snippets/test_struct.nc
deleted file mode 100644
index 71a9492..0000000
--- a/snippets/test_snippets/test_struct.nc
+++ /dev/null
@@ -1,20 +0,0 @@
-extern exit;
-extern malloc;
-
-let construct_foo()
-{
- let io := 0;
- io := malloc(4);
-
- return io;
-}
-
-let main()
-{
- let io := 0x0;
- io := construct_foo();
-
- _ := exit(io);
-
- return first_number;
-} \ No newline at end of file
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;
diff --git a/src/DebuggerKit/src/MachContract.cpp b/src/DebuggerKit/src/MachContract.cpp
index eac0ad8..8a897be 100644
--- a/src/DebuggerKit/src/MachContract.cpp
+++ b/src/DebuggerKit/src/MachContract.cpp
@@ -10,6 +10,8 @@
#include <ThirdParty/Dialogs/Dialogs.h>
#ifdef DK_MACH_DEBUGGER
+
+/// @brief a terrible way to import globals.
#include <DebuggerKit/Common.inl>
/// @internal