Skip to content

Commit

Permalink
better python api
Browse files Browse the repository at this point in the history
  • Loading branch information
codekansas committed Oct 31, 2024
1 parent a34fa09 commit b5d5c66
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 17 deletions.
23 changes: 23 additions & 0 deletions klang/src/parser/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,22 @@ impl Node {
result.push('\n');
result
}

pub fn to_list(&self) -> Vec<Vec<String>> {
if self.children.is_empty() {
vec![vec![self.text.clone()]]
} else {
self.children
.iter()
.flat_map(|child| {
child.to_list().into_iter().map(|mut line| {
line.push(self.text.clone());
line
})
})
.collect()
}
}
}

pub struct KlangProgram {
Expand Down Expand Up @@ -93,6 +109,13 @@ impl KlangProgram {
.collect::<Vec<String>>()
.join("\n")
}

pub fn to_list(&self) -> Vec<Vec<String>> {
self.program
.iter()
.flat_map(|node| node.to_list())
.collect()
}
}

impl std::fmt::Display for KlangProgram {
Expand Down
4 changes: 2 additions & 2 deletions pyklang/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]

name = "pyklang"
name = "bindings"

version.workspace = true
edition.workspace = true
Expand All @@ -12,7 +12,7 @@ readme.workspace = true

[lib]

name = "pyklang"
name = "bindings"
crate-type = ["cdylib", "rlib"]

[dependencies]
Expand Down
3 changes: 3 additions & 0 deletions pyklang/bindings.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class PyKlangProgram:
def load_binary(path:str) -> PyKlangProgram:
...

def to_list(self) -> list[list[str]]:
...


def get_version() -> str:
...
Expand Down
34 changes: 20 additions & 14 deletions pyklang/kompile.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
"""Defines the PyKlang CLI."""

import argparse
from pathlib import Path

import click

from pyklang.bindings import parse_file


def main() -> None:
parser = argparse.ArgumentParser(description="Kompile a Klang program.")
parser.add_argument("input", help="The input file to compile.")
parser.add_argument("-o", "--output", help="The output file to compile.")
parser.add_argument("-t", "--text", action="store_true", help="Output the text representation of the program.")
args = parser.parse_args()
@click.command()
@click.argument("input_file")
@click.option("-o", "--output", help="The output file to compile.")
@click.option("-i", "--inplace", is_flag=True, help="Overwrite the input file.")
@click.option("-t", "--text", is_flag=True, help="Output the text representation of the program.")
def main(input_file: str, output: str | None, inplace: bool, text: bool) -> None:
"""Kompile a Klang program."""
program = parse_file(input_file)

program = parse_file(args.input)
if inplace:
if output is not None:
raise click.UsageError("Cannot specify both -o and -i")
output = Path(input_file).with_suffix(".ko").as_posix()

if args.output is None:
print(program)
if output is None:
click.echo(program)
else:
Path(args.output).parent.mkdir(parents=True, exist_ok=True)
if args.text:
program.save_text(args.output)
Path(output).parent.mkdir(parents=True, exist_ok=True)
if text:
program.save_text(output)
else:
program.save_binary(args.output)
program.save_binary(output)


if __name__ == "__main__":
Expand Down
3 changes: 3 additions & 0 deletions pyklang/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
# requirements.txt

# CLI
click
2 changes: 1 addition & 1 deletion pyklang/src/bin/stub_gen.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use pyo3_stub_gen::Result;

fn main() -> Result<()> {
let stub = pyklang::stub_info()?;
let stub = bindings::stub_info()?;
stub.generate()?;
Ok(())
}
4 changes: 4 additions & 0 deletions pyklang/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ impl PyKlangProgram {
.map_err(|e| PyValueError::new_err(e.to_string()))?;
Ok(PyKlangProgram { inner: program })
}

fn to_list(&self) -> PyResult<Vec<Vec<String>>> {
Ok(self.inner.to_list().clone())
}
}

#[pyclass]
Expand Down

0 comments on commit b5d5c66

Please sign in to comment.