Skip to content

Commit

Permalink
Updated statement/expression case and added programmingLanguageMap fo…
Browse files Browse the repository at this point in the history
…r consistency
  • Loading branch information
BrentBlanckaert committed Dec 28, 2024
1 parent 10b0eb3 commit 3539227
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
12 changes: 12 additions & 0 deletions tested/dsl/translate_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ class ReturnOracle(dict):
class NaturalLanguageMap(dict):
pass

class ProgrammingLanguageMap(dict):
pass


OptionDict = dict[str, int | bool]
YamlObject = (
Expand All @@ -104,6 +107,7 @@ class NaturalLanguageMap(dict):
| ExpressionString
| ReturnOracle
| NaturalLanguageMap
| ProgrammingLanguageMap
)


Expand Down Expand Up @@ -158,6 +162,13 @@ def _natural_language_map(loader: yaml.Loader, node: yaml.Node) -> NaturalLangua
), f"A natural language map must be an object, got {result} which is a {type(result)}."
return NaturalLanguageMap(result)

def _programming_language_map(loader: yaml.Loader, node: yaml.Node) -> ProgrammingLanguageMap:
result = _parse_yaml_value(loader, node)
assert isinstance(
result, dict
), f"A programming language map must be an object, got {result} which is a {type(result)}."
return ProgrammingLanguageMap(result)


def _parse_yaml(yaml_stream: str) -> YamlObject:
"""
Expand All @@ -170,6 +181,7 @@ def _parse_yaml(yaml_stream: str) -> YamlObject:
yaml.add_constructor("!expression", _expression_string, loader)
yaml.add_constructor("!oracle", _return_oracle, loader)
yaml.add_constructor("!natural_language", _natural_language_map, loader)
yaml.add_constructor("!programming_language", _programming_language_map, loader)

try:
return yaml.load(yaml_stream, loader)
Expand Down
19 changes: 9 additions & 10 deletions tested/nat_translation.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import sys
from typing import cast

import yaml

from tested.dsl.translate_parser import (
ExpressionString,
NaturalLanguageMap,
ProgrammingLanguageMap,
ReturnOracle,
YamlDict,
YamlObject,
Expand Down Expand Up @@ -96,17 +96,16 @@ def translate_testcase(

key_to_set = "statement" if "statement" in testcase else "expression"
if (expr_stmt := testcase.get(key_to_set)) is not None:
# Translate NaturalLanguageMap
expr_stmt = natural_langauge_map_translation(expr_stmt, language)

# Perform translation based of translation stack.
if isinstance(expr_stmt, dict):
testcase[key_to_set] = {
k: format_string(cast(str, v), flat_stack) for k, v in expr_stmt.items()
# Program language translation found
if isinstance(expr_stmt, ProgrammingLanguageMap):
expr_stmt = {
k: natural_langauge_map_translation(v, language) for k, v in expr_stmt.items()
}
elif isinstance(expr_stmt, str):
testcase[key_to_set] = format_string(expr_stmt, flat_stack)
elif isinstance(expr_stmt, NaturalLanguageMap): # Natural language translation found
assert language in expr_stmt
expr_stmt = expr_stmt[language]

testcase[key_to_set] = parse_value(expr_stmt, flat_stack)
else:
if (stdin_stmt := testcase.get("stdin")) is not None:
# Translate NaturalLanguageMap
Expand Down

0 comments on commit 3539227

Please sign in to comment.