From 3c5412ca47b5a58270c2ff923179792b42ae64ad Mon Sep 17 00:00:00 2001 From: agent-db0ec53c058f1326 Date: Fri, 17 Apr 2026 01:19:48 +0200 Subject: [PATCH] build(agent): molt-z#db0ec5 iteration --- gridverse/dsl_to_contract.py | 69 +++++++++++++++++++++++++++++++++++ tests/test_dsl_to_contract.py | 17 +++++++++ 2 files changed, 86 insertions(+) create mode 100644 gridverse/dsl_to_contract.py create mode 100644 tests/test_dsl_to_contract.py diff --git a/gridverse/dsl_to_contract.py b/gridverse/dsl_to_contract.py new file mode 100644 index 0000000..a0d5e1c --- /dev/null +++ b/gridverse/dsl_to_contract.py @@ -0,0 +1,69 @@ +""" +Tiny DSL -> contract translator. + +This module provides a minimal, production-friendly, deterministic translator +from a compact DSL representation to a GridVerse LocalProblem payload. +It is intended for the MVP's low-code development flow as a seed for codegen. +""" + +from __future__ import annotations + +from typing import Any, Dict + + +def parse_simple_dsl(text: str) -> Dict[str, Any]: + """ + Parse a very small DSL into a payload dict. + + DSL format (line-based, simple key=value): + site_id=site-01 + description=Example local problem + variables.temperature=22.5 + variables.flow=3.2 + + Nested keys are expressed with dot notation. Arrays are comma-separated + values after a key ending in [] (not currently used, but reserved). + """ + result: Dict[str, Any] = {"site_id": "", "description": "", "variables": {}} + if not text: + return result + for raw in text.strip().splitlines(): + line = raw.strip() + if not line or line.startswith("#"): + continue + if "=" not in line: + continue + key, val = line.split("=", 1) + key = key.strip() + val = val.strip() + if key == "site_id": + result["site_id"] = val + elif key == "description": + result["description"] = val + elif key.startswith("variables."): + var_name = key.split(".", 1)[1] + # naive type inference + parsed = _infer_type(val) + result["variables"][var_name] = parsed + else: + # generic top-level + result[key] = _infer_type(val) + # ensure variables exists + if "variables" not in result: + result["variables"] = {} + return result + + +def _infer_type(value: str) -> Any: + # Try int, float, bool, otherwise string + if value.lower() in {"true", "false"}: + return value.lower() == "true" + try: + return int(value) + except ValueError: + pass + try: + return float(value) + except ValueError: + pass + return value diff --git a/tests/test_dsl_to_contract.py b/tests/test_dsl_to_contract.py new file mode 100644 index 0000000..895d665 --- /dev/null +++ b/tests/test_dsl_to_contract.py @@ -0,0 +1,17 @@ +import json + +from gridverse.dsl_to_contract import parse_simple_dsl + + +def test_parse_simple_dsl_basic(): + dsl = """ + site_id=site-01 + description=Test local problem + variables.temperature=21.5 + variables.flow=3 + """ + payload = parse_simple_dsl(dsl) + assert payload["site_id"] == "site-01" + assert payload["description"] == "Test local problem" + assert payload["variables"]["temperature"] == 21.5 + assert payload["variables"]["flow"] == 3