|
| 1 | +""" |
| 2 | +This script translates problems from the OpenAI HumanEval dataset into Fennel. |
| 3 | +
|
| 4 | +- Home: https://fennel-lang.org/ |
| 5 | +- Compiler: https://git.sr.ht/~technomancy/fennel |
| 6 | +- Test library: https://git.sr.ht/~technomancy/faith |
| 7 | +""" |
| 8 | + |
| 9 | +import ast |
| 10 | +from typing import List |
| 11 | + |
| 12 | + |
| 13 | +class Translator: |
| 14 | + |
| 15 | + USub = "-" |
| 16 | + |
| 17 | + stop = ["\n(fn", "\n;", "\n("] |
| 18 | + |
| 19 | + def file_ext(self): |
| 20 | + return "fnl" |
| 21 | + |
| 22 | + def translate_prompt( |
| 23 | + self, name: str, args: List[ast.arg], _returns, description: str |
| 24 | + ) -> str: |
| 25 | + fnl_args = " ".join([arg.arg for arg in args]) |
| 26 | + fnl_description = description.replace('"', '\\"') |
| 27 | + self.entry_point = name |
| 28 | + return f'(fn {name} [{fnl_args}]\n"{fnl_description}"\n' |
| 29 | + |
| 30 | + def test_suite_prefix_lines(self, entry_point) -> List[str]: |
| 31 | + return [ |
| 32 | + "(local faith (require :faith))", |
| 33 | + f"(local candidate {entry_point})", |
| 34 | + "(fn test-human-eval []", |
| 35 | + ] |
| 36 | + |
| 37 | + def test_suite_suffix_lines(self) -> List[str]: |
| 38 | + return [")", "{: test-human-eval}"] |
| 39 | + |
| 40 | + def deep_equality(self, left: str, right: str) -> str: |
| 41 | + return f" (faith.= {right} {left})" # Expected on the left. |
| 42 | + |
| 43 | + def gen_literal(self, c: bool | str | int | float): |
| 44 | + if type(c) is bool: |
| 45 | + return "true" if c else "false" |
| 46 | + elif type(c) is str: |
| 47 | + return f'"{c}"' |
| 48 | + elif c is None: |
| 49 | + return "nil" |
| 50 | + return repr(c) |
| 51 | + |
| 52 | + def gen_var(self, variable: str) -> str: |
| 53 | + return variable |
| 54 | + |
| 55 | + def gen_list(self, list: List[str]) -> str: |
| 56 | + return "[" + " ".join(list) + "]" |
| 57 | + |
| 58 | + def gen_tuple(self, tuple: List[str]) -> str: |
| 59 | + return "[" + " ".join(tuple) + "]" |
| 60 | + |
| 61 | + def gen_dict(self, keys: List[str], values: List[str]) -> str: |
| 62 | + pairs = " ".join(f"{k} {v}" for k, v in zip(keys, values)) |
| 63 | + return "{" + pairs + "}" |
| 64 | + |
| 65 | + def gen_call(self, func: str, args: List[str]) -> str: |
| 66 | + return "(" + func + " " + " ".join(args) + ")" |
0 commit comments