Johan Rong 3 дней назад
Родитель
Сommit
d3dfad0f4c
5 измененных файлов с 90 добавлено и 19 удалено
  1. 1 1
      .gitignore
  2. 4 2
      flake.nix
  3. 85 6
      src/zelkelbt/__main__.py
  4. 0 5
      src/zelkelbt/config/config.py
  5. 0 5
      src/zelkelc/ast/parser.py

+ 1 - 1
.gitignore

@@ -1,4 +1,4 @@
 .direnv/
 .ruff_cache
 __pycache__/
-build/
+target/

+ 4 - 2
flake.nix

@@ -19,9 +19,11 @@
     in
     {
       devShells.${system}.default = pkgs.mkShell {
-        packages = [
+        packages = with pkgs; [
           pythonEnv
-          pkgs.ruff
+          ruff
+          llvmPackages_latest.llvm
+          clang
         ];
 
         shellHook = ''

+ 85 - 6
src/zelkelbt/__main__.py

@@ -1,19 +1,98 @@
-from src.zelkelbt.config.config import has_config
+import argparse
+import subprocess
+import sys
+
+from src.zelkelbt.build import build_package
+from src.zelkelbt.config import get_config
 
 
 def print_help():
-    print("""
-Zelkel Buildtool
+    print("""Zelkel Build Tools (zk)
 
 Usage: zk [OPTIONS] [COMMAND]
 
 Options:
-    -V, --version
-""")
+    -V, --version           Print version info
+    --help                  Print help
+
+Commands:
+    build, b    Compile the current package
+    clean       Remove the build/ directory
+    run, r      Compile and run the current package""")
 
 
 if __name__ == "__main__":
-    print(has_config())
+    parser = argparse.ArgumentParser(
+        prog="zk",
+        description="Zelkel Build Tools (zk)",
+        formatter_class=argparse.RawTextHelpFormatter,
+        add_help=False,
+    )
+
+    parser.add_argument(
+        "-V",
+        "--version",
+        action="version",
+        version="zk 1.0.0",
+        help="Print version info",
+    )
+    parser.add_argument("--help", action="help", help="Print help")
+
+    subparsers = parser.add_subparsers(dest="command", metavar="[COMMAND]")
+
+    build_parser = subparsers.add_parser(
+        "build", aliases=["b"], help="Compile the current package"
+    )
+    build_parser.add_argument(
+        "--debug-info",
+        action="store_true",
+        help="Enable debug output while building the package",
+    )
+    build_group = build_parser.add_mutually_exclusive_group()
+    build_group.add_argument(
+        "--debug",
+        dest="build_mode",
+        action="store_const",
+        const="debug",
+        default="debug",
+        help="Enable debug build mode",
+    )
+    build_group.add_argument(
+        "--release",
+        dest="build_mode",
+        action="store_const",
+        const="release",
+        help="Enable release build mode",
+    )
+
+    clean_parser = subparsers.add_parser("clean", help="Remove the build/ directory")
+
+    run_parser = subparsers.add_parser(
+        "run", aliases=["r"], help="Compile and run the current package"
+    )
+    run_parser.add_argument(
+        "--debug-info",
+        action="store_true",
+        help="Enable debug output while running the package",
+    )
+
+    parser.usage = "zk [OPTIONS] [COMMAND]"
+
+    args = parser.parse_args()
+
+    if args.command in ["build", "b"]:
+        config = get_config()
+        build_package(args.build_mode, config, args.debug_info)
+    elif args.command == "clean":
+        print("TODO: implement clean command")
+        sys.exit(1)
+    elif args.command in ["run", "r"]:
+        config = get_config()
+        build_package("debug", config, args.debug_info)
+        subprocess.run([f"./target/debug/{config['package']['name']}"], check=True)
+    else:
+        parser.print_help()
+
 
 # tokens = lex(source)
 # print(tokens)

+ 0 - 5
src/zelkelbt/config/config.py

@@ -1,5 +0,0 @@
-from pathlib import Path
-
-
-def has_config():
-    return Path("zelkel.toml").is_file()

+ 0 - 5
src/zelkelc/ast/parser.py

@@ -229,7 +229,6 @@ class Parser:
                             body.append(self.parse_value_declaration(True))
                         case "return":
                             body.append(self.parse_return())
-                            hasReturn = True
                         case _:
                             print(f"Invalid keyword in function body {t.value}")
                             sys.exit(1)
@@ -248,10 +247,6 @@ class Parser:
                     print(f"implement expression statement: {t}")
                     sys.exit(1)
 
-        if hasReturn:
-            print("Function must have return")
-            sys.exit(1)
-
         self.expect(token.RBrace())
 
         irname = f"{path}.f_{name}_{typ}"