summaryrefslogtreecommitdiffhomepage
path: root/src/CompilerKit/src/Frontends/NectarCompiler+Chk.cpp
blob: c3faf49d5c1089e0134c716b44ff5af3d9682c6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026, Amlal El Mahrouss (amlal@nekernel.org)
// Licensed under the Apache License, Version 2.0 (See accompanying
// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0)
// Official repository: https://github.com/ne-foss-org/nectar

/// BUGS: 0

#include <CompilerKit/AST.h>
#include <CompilerKit/Detail/AMD64.h>
#include <CompilerKit/PEF.h>
#include <CompilerKit/UUID.h>
#include <CompilerKit/Utils/Compiler.h>

/* Nectar Compiler Check Driver. */
/* This is part of the CompilerKit. */
/* (c) Amlal El Mahrouss 2026 */

using namespace CompilerKit;

static bool kInIfBody    = false;
static bool kInElseBody  = false;
static bool kInTraitBody = false;

CK_IMPORT_C bool NectarCheckLine(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(":=") != CompilerKit::STLString::npos) {
    if (input.find(";") == CompilerKit::STLString::npos) {
      Detail::print_error("An assignment call must always end with ';'", "check");
      return false;
    }
  }

  if (input.find("(") != CompilerKit::STLString::npos &&
      input.find("const") == CompilerKit::STLString::npos &&
      input.find("let") == CompilerKit::STLString::npos &&
      input.find("if") == CompilerKit::STLString::npos &&
      input.find("else") == CompilerKit::STLString::npos) {
    if (input.find(";") == CompilerKit::STLString::npos) {
      Detail::print_error("A function call 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.find("let ") != CompilerKit::STLString::npos && input.ends_with(";")) {
    if (input.find(":=") == CompilerKit::STLString::npos) {
      Detail::print_error("A declaration must always include 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 == "}" || input == "}\n" || input == "}\r\n") {
    if (kInIfBody) kInIfBody = false;
  }

  if (input == "}" || input == "}\n" || input == "}\r\n") {
    if (kInTraitBody) kInTraitBody = false;
  }

  return true;
}