Skip to content

Commit

Permalink
Add inspect command to cli
Browse files Browse the repository at this point in the history
  • Loading branch information
fergalwalsh committed Apr 25, 2024
1 parent e869f45 commit 240cdcd
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
25 changes: 25 additions & 0 deletions tealish/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from .base import BaseNode
from .nodes import Node, Program
from .utils import TealishMap
from .types import _structs


class TealWriter:
Expand Down Expand Up @@ -154,6 +155,9 @@ def get_map(self) -> TealishMap:
map.errors = dict(self.error_messages)
return map

def get_structs(self):
return dict(_structs)


def compile_program(source: str) -> Tuple[List[str], TealishMap]:
source_lines = source.split("\n")
Expand All @@ -168,3 +172,24 @@ def reformat_program(source: str) -> str:
output = compiler.reformat()
output = output.strip() + "\n"
return output


def inspect_program(source: str):
source_lines = source.split("\n")
compiler = TealishCompiler(source_lines)
compiler.compile()
structs = compiler.get_structs()
structs_output = {}
for s in structs:
fields = [(name, structs[s].fields[name]) for name in structs[s].fields]
structs_output[s] = {
"size": structs[s].size,
"fields": {
name: {"type": str(f.tealish_type), "size": f.size, "offset": f.offset}
for name, f in fields
},
}
output = {
"structs": structs_output,
}
return output
49 changes: 48 additions & 1 deletion tealish/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pathlib
import click
from typing import List, Optional, Tuple, IO
from tealish import compile_program, reformat_program
from tealish import compile_program, inspect_program, reformat_program
from tealish.errors import CompileError, ParseError
from tealish.langspec import (
fetch_langspec,
Expand Down Expand Up @@ -151,6 +151,19 @@ def format(ctx: click.Context, tealish_file: IO) -> None:
tealish_file.truncate()


@click.command()
@click.argument("tealish_file", type=click.File("r"))
@click.pass_context
def inspect(ctx: click.Context, tealish_file: IO) -> None:
"""Inspect a tealish program"""
input = tealish_file.read()
try:
output = inspect_program(input)
except ParseError as e:
raise click.ClickException(str(e))
print(json.dumps(output, indent=2))


@click.group()
def langspec() -> None:
"""Tools to support new Teal versions by updating the langspec file"""
Expand Down Expand Up @@ -214,6 +227,38 @@ def langspec_diff(url: str) -> None:
click.echo(f"{sig}")


@click.command()
@click.argument("path", type=click.Path(exists=True, path_type=pathlib.Path))
@click.pass_context
def stats(ctx: click.Context, path: pathlib.Path) -> None:
"""Stats for a .tl file"""
paths = []
if path.is_dir():
paths = (
list(path.glob("*.tl"))
+ list(path.glob("*/*.tl"))
+ list(path.glob("*/build/*.teal"))
)
else:
paths = [path]
for path in paths:
# click.echo(f"Compiling {path} to {teal_filename}")
tealish = open(path).readlines()
n = 0
for line in tealish:
line = line.strip()
if line.startswith("#"):
continue
if line.startswith("//"):
continue
if not line:
continue
if line == "end":
continue
n += 1
click.echo(f"{path}: {n} lines")


langspec.add_command(langspec_update, "update")
langspec.add_command(langspec_fetch, "fetch")
langspec.add_command(langspec_diff, "diff")
Expand All @@ -222,3 +267,5 @@ def langspec_diff(url: str) -> None:
cli.add_command(build)
cli.add_command(format)
cli.add_command(langspec)
cli.add_command(stats)
cli.add_command(inspect)

0 comments on commit 240cdcd

Please sign in to comment.