Skip to content

Commit

Permalink
feat(LibCore): add raw_input function and update entry point
Browse files Browse the repository at this point in the history
  • Loading branch information
HsiangNianian committed Nov 2, 2024
1 parent b659a0e commit 7c6d26c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
50 changes: 50 additions & 0 deletions luah/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import argparse
import sys
from .LibCore import raw_input

class Cli:
def __init__(self):
self.parser = argparse.ArgumentParser(description="luah")
self._setup_arguments()
self.args = self.parser.parse_args()

if self.args.file:
self.process_file(self.args.file)
elif len(sys.argv) > 1 and sys.argv[1].endswith(".lua"):
self.process_file(sys.argv[1])

def _setup_arguments(self):
self.parser.add_argument("--version", action="version", version="luah 0.1.0")
self.parser.add_argument("--verbose", action="store_true", help="verbose mode")
self.parser.add_argument("--debug", action="store_true", help="debug mode")
self.parser.add_argument("--quiet", action="store_true", help="quiet mode")
self.parser.add_argument("--config", help="config file")
self.parser.add_argument("--file", help="file to process")
self.parser.add_argument("--log", help="log file")
self.parser.add_argument("--log-level", help="log level")
self.parser.add_argument("--log-format", help="log format")
self.parser.add_argument("--log-date-format", help="log date format")
self.parser.add_argument("--log-file-max-size", help="log file max size")
self.parser.add_argument("--log-file-backup-count", help="log file backup count")
self.parser.add_argument("--log-file-rotation-count", help="log file rotation count")
self.parser.add_argument("--log-file-rotation-interval", help="log file rotation interval")
self.parser.add_argument("--log-file-rotation-backup-count", help="log file rotation backup count")
self.parser.add_argument("--log-file-rotation-backup-interval", help="log file rotation backup interval")

def process_file(self, file_name):
try:
raw_input(file_name)
except Exception as e:
print(f"Error processing file {file_name}: {e}")

def get_args(self):
return self.args

def get_help(self):
return self.parser.format_help()

if __name__ == "__main__":
cli = Cli()
args = cli.get_args()
if args.verbose:
print("Verbose mode is enabled.")
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ repository = "https://github.com/HydroRoll-Team/luah"
documentation = "https://luah.hydroroll.team/"

[project.scripts]
luah = "luah.cli:main"
luah = "luah.__main__:Cli"

[tool.maturin]
features = ["pyo3/extension-module"]
Expand Down
14 changes: 14 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use std::env;
use std::fs::File;
use std::io::BufReader;

use pyo3::prelude::*;

pub mod lex;
Expand All @@ -12,9 +16,19 @@ fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
Ok((a + b).to_string())
}

#[pyfunction]
fn raw_input(prompt: String) -> PyResult<()> {
let file = File::open(&prompt).unwrap();

let proto = parse::load(BufReader::new(file));
vm::ExeState::new().execute(&proto, &Vec::new());
Ok(())
}

#[pymodule]
#[pyo3(name = "LibCore")]
fn libcore(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
m.add_function(wrap_pyfunction!(raw_input, m)?)?;
Ok(())
}

0 comments on commit 7c6d26c

Please sign in to comment.