Skip to content

Commit

Permalink
today()
Browse files Browse the repository at this point in the history
  • Loading branch information
christoph2 committed Aug 30, 2024
1 parent 67d1ffa commit 568aa70
Show file tree
Hide file tree
Showing 11 changed files with 327 additions and 210 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ repos:
entry: ruff
language: system
types: [python]
args: ["check"]
require_serial: true
- id: check-added-large-files
name: Check for added large files
Expand Down
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ cmake_minimum_required(VERSION 3.7...3.29)
project(pya2l_extensions LANGUAGES C CXX)

cmake_policy(SET CMP0135 NEW)
cmake_policy(SET CMP0094 NEW)

find_package(Python COMPONENTS Interpreter Development)
find_package(Python3 COMPONENTS Interpreter Development)
find_package(pybind11 REQUIRED)

set(ANTLR4_TAG 4.13.1)
Expand Down
71 changes: 57 additions & 14 deletions build_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,63 @@
import re
import subprocess # nosec
import sys
from typing import Optional
import sysconfig
from pathlib import Path

# from pprint import pprint
from tempfile import TemporaryDirectory
from typing import Optional


print("Platform", platform.system())

TOP_DIR = Path(__file__).parent

GIT_TAG_RE = re.compile(r"refs/tags/v?(\d\.\d{1,3}.\d{1,3})")

print("Platform", platform.system())
uname = platform.uname()
if uname.system == "Darwin":
os.environ["MACOSX_DEPLOYMENT_TARGET"] = "11.0"

VARS = sysconfig.get_config_vars()


def get_python_base() -> str:
# Applies in this form only to Windows.
if "base" in VARS and VARS["base"]: # noqa: RUF019
return VARS["base"]
if "installed_base" in VARS and VARS["installed_base"]: # noqa: RUF019
return VARS["installed_base"]


def alternate_libdir(pth: str):
base = Path(pth).parent
libdir = Path(base) / "libs"
if libdir.exists():
# available_libs = os.listdir(libdir)
return str(libdir)
else:
return ""


def get_py_config() -> dict:
pynd = VARS["py_version_nodot"] # Should always be present.
include = sysconfig.get_path("include") # Seems to be cross-platform.
library = f"python{pynd}.lib"
if uname.system == "Windows":
base = get_python_base()
libdir = Path(base) / "libs"
if libdir.exists():
available_libs = os.listdir(libdir)
if library in available_libs:
libdir = str(libdir)
else:
libdir = ""
else:
libdir = alternate_libdir(include)
else:
libdir = VARS["LIBDIR"]
library = VARS["LDLIBRARY"]

return dict(exe=sys.executable, include=include, libdir=libdir, library=library)


def sort_by_version(version: str) -> tuple[int]:
h, m, s = version.split(".")
Expand All @@ -37,7 +81,7 @@ def fetch_tags(repo: str) -> list[str]:
return sorted(tag_set, key=sort_by_version)


def most_recent_tag(repo: str) -> Optional[str]:
def most_recent_tag(repo: str) -> Optional[str]: # noqa: UP007
tags = fetch_tags(repo)
return tags[-1] if tags else None

Expand All @@ -57,15 +101,15 @@ def build_extension(debug: bool = False, use_temp_dir=True) -> None:
debug = int(os.environ.get("DEBUG", 0)) or debug
cfg = "Debug" if debug else "Release"

# Set Python_EXECUTABLE instead if you use PYBIND11_FINDPYTHON
# EXAMPLE_VERSION_INFO shows you how to pass a value into the C++ code
# from Python.
py_cfg = get_py_config()

cmake_args = [
# f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={extdir}{os.sep}",
# "-G Ninja",
f"-DPYTHON_EXECUTABLE={sys.executable}",
f"-DPython3_EXECUTABLE={py_cfg['exe']}",
f"-DPython3_INCLUDE_DIR={py_cfg['include']}",
f"-DPython3_LIBRARY={str(Path(py_cfg['libdir']) / Path(py_cfg['library']))}", # noqa: RUF010
f"-DCMAKE_BUILD_TYPE={cfg}", # not used on MSVC, but no harm
]

build_args = ["--config Release", "--verbose"]
# Adding CMake arguments set as environment variable
# (needed e.g. to build for ARM OSx on conda-forge)
Expand All @@ -90,8 +134,7 @@ def build_extension(debug: bool = False, use_temp_dir=True) -> None:

banner("Step #1: Configure")
# cmake_args += ["--debug-output"]
print("aufruf:", ["cmake", str(TOP_DIR), *cmake_args])
subprocess.run(["cmake", str(TOP_DIR), *cmake_args], cwd=build_temp, check=True) # nosec
subprocess.run(["cmake", "-S", str(TOP_DIR), *cmake_args], cwd=build_temp, check=True) # nosec

cmake_args += [f"--parallel {mp.cpu_count()}"]

Expand Down
19 changes: 9 additions & 10 deletions pya2l/aml.g4
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ predefined_type_name:
;

block_definition:
//block_definition | 'block' tag = tagValue tn = type_name
'block' tag = tagValue (blk = block_definition | tn = type_name)
'block' tag = tagValue (/* blk = block_definition | */ tn = type_name)
;

enum_type_name:
Expand All @@ -83,12 +82,12 @@ enumerator:
;

struct_type_name:
'struct' t0 = identifierValue? '{' l += struct_member* '}'
'struct' t0 = identifierValue? '{' l += struct_member* '}' ';'?
| 'struct' t1 = identifierValue
;

struct_member:
m = member';'
m = member ';'?
;

member:
Expand All @@ -101,20 +100,20 @@ array_specifier:
;

taggedstruct_type_name:
'taggedstruct' t0 = identifierValue? '{' (l += taggedstruct_member)* '}'
'taggedstruct' t0 = identifierValue? '{' (l += taggedstruct_member)* '}' ';'?
| 'taggedstruct' t1 = identifierValue
;

taggedstruct_member:
ts1 = taggedstruct_definition ';'
ts1 = taggedstruct_definition ';'?
| '(' ts0 = taggedstruct_definition ';'? ')' '*' ';'
| bl1 = block_definition ';'
| bl1 = block_definition ';'?
| '(' bl0 = block_definition ';'? ')' '*' ';'
;

taggedstruct_definition:
tag = tagValue mem = member?
| tag = tagValue '(' mem = member ';'? ')' '*' // ';'
| tag = tagValue '(' mem = member ';'? ')' '*'
;

taggedunion_type_name:
Expand All @@ -123,8 +122,8 @@ taggedunion_type_name:
;

tagged_union_member:
t = tagValue m = member? ';'
| b = block_definition ';'
t = tagValue m = member? ';'?
| b = block_definition ';'?
;

numericValue:
Expand Down
74 changes: 24 additions & 50 deletions pya2l/aml/aml_visitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,22 +120,12 @@ std::any AmlVisitor::visitType_definition(amlParser::Type_definitionContext *ctx
}

std::any AmlVisitor::visitType_name(amlParser::Type_nameContext *ctx) {
//const auto ctx_t = ctx->t;
const auto ctx_pr = ctx->pr;
const auto ctx_st = ctx->st;
const auto ctx_ts = ctx->ts;
const auto ctx_tu = ctx->tu;
const auto ctx_en = ctx->en;
#if 0
std::string tag_text{};

if (ctx_t) {
const auto tag_opt = std::any_cast<string_opt_t>(visit(ctx_t));
if (tag_opt) {
tag_text = *tag_opt;
}
}
#endif
if (ctx_pr) {
auto pdt = std::any_cast<AMLPredefinedType>(visit(ctx_pr));
return make_type(pdt);
Expand Down Expand Up @@ -169,13 +159,9 @@ std::any AmlVisitor::visitPredefined_type_name(amlParser::Predefined_type_nameCo
std::any AmlVisitor::visitBlock_definition(amlParser::Block_definitionContext *ctx) {
const auto ctx_tag = ctx->tag;
const auto ctx_tn = ctx->tn;
//const auto ctx_mem = ctx->mem;
//const auto ctx_mult = ctx->mult;

std::string tag_text;
//bool multiple{ false };
Type *tn = nullptr;
//Member member;

if (ctx_tag) {
const auto tag_opt = std::any_cast<string_opt_t>(visit(ctx_tag));
Expand All @@ -187,17 +173,7 @@ std::any AmlVisitor::visitBlock_definition(amlParser::Block_definitionContext *c
if (ctx_tn) {
tn = std::any_cast<Type *>(visit(ctx_tn));
}
#if 0
if (ctx_mem) {
member = std::any_cast<Member>(visit(ctx_mem));
}

if (ctx_mult) {
if (ctx_mult->getText() == "*") {
multiple = true;
}
}
#endif
return BlockDefinition(tag_text, tn/*, member, multiple*/);
}

Expand Down Expand Up @@ -307,51 +283,49 @@ std::any AmlVisitor::visitStruct_type_name(amlParser::Struct_type_nameContext *c

std::any AmlVisitor::visitStruct_member(amlParser::Struct_memberContext *ctx) {
const auto ctx_m = ctx->m;
//const auto ctx_mstar = ctx->mstar;
//const auto ctx_m0 = ctx->m0;

if (ctx_m) {
const auto mem = std::any_cast<Member>(visit(ctx_m));
return StructMember(mem);
}
#if 0
if (ctx_m0) {
if (ctx_m0->getText() == "*") {
if (ctx_mstar) {
const auto mem = std::any_cast<Member>(visit(ctx_mstar));
return StructMember(mem, true);
}
}
}
#endif

return {};
}

std::any AmlVisitor::visitMember(amlParser::MemberContext *ctx) {
const auto ctx_t = ctx->t;
const auto ctx_a = ctx->a;
std::vector<uint64_t> arrary_specifier;
const auto ctx_b = ctx->b;
std::vector<uint64_t> arrary_specifier{};
std::int64_t value{ 0 };
Type *tp = nullptr;
std::optional<BlockDefinition> block{std::nullopt};

if (ctx_t) {
const auto type_name = visit(ctx_t);
if (type_name.has_value()) {
tp = std::any_cast<Type *>(type_name);
}
if (ctx_b) {
block = std::any_cast<BlockDefinition>(visit(ctx_b));
}
else {

for (const auto &elem : ctx_a) {
const auto value_cont = std::any_cast<numeric_t>(visit(elem));
if (ctx_t) {
const auto type_name = visit(ctx_t);
if (type_name.has_value()) {
tp = std::any_cast<Type*>(type_name);
}
}

for (const auto& elem : ctx_a) {
const auto value_cont = std::any_cast<numeric_t>(visit(elem));

if (std::holds_alternative<std::int64_t>(value_cont)) {
value = std::get<std::int64_t>(value_cont);
} else if (std::holds_alternative<long double>(value_cont)) {
value = static_cast<std::int64_t>(std::get<long double>(value_cont));
if (std::holds_alternative<std::int64_t>(value_cont)) {
value = std::get<std::int64_t>(value_cont);
}
else if (std::holds_alternative<long double>(value_cont)) {
value = static_cast<std::int64_t>(std::get<long double>(value_cont));
}
arrary_specifier.push_back(value);
}
arrary_specifier.push_back(value);
}
return Member(tp, arrary_specifier);
return Member(block, tp, arrary_specifier);
}

std::any AmlVisitor::visitArray_specifier(amlParser::Array_specifierContext *ctx) {
Expand Down
Loading

0 comments on commit 568aa70

Please sign in to comment.