-
Notifications
You must be signed in to change notification settings - Fork 51
Add elixir #117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add elixir #117
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
23d5018
Create files
409f95f
First pass
b8e0d09
Use newer Elixir source
3bb8773
Debug
616c8c9
Revert IDE autoformat
d5039ef
Workaround to always generated in assert
d497608
Pregen newline and spaces before answer
76e9d0c
Update prompts
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| """ | ||
| This script translates problems from the OpenAI HumanEval dataset into Elixir. | ||
| """ | ||
|
|
||
| import re | ||
| import ast | ||
| from typing import List, TypeVar | ||
| from base_language_translator import LanguageTranslator | ||
|
|
||
| # We turn multi-line docstrings into single-line comments. This captures the | ||
| # start of the line. | ||
| DOCSTRING_LINESTART_RE = re.compile("""\n(\\s*)""") | ||
|
|
||
| TargetExp = str | ||
|
|
||
|
|
||
| class Translator(LanguageTranslator[TargetExp]): | ||
| USub = "-" | ||
|
|
||
| def file_ext(self) -> str: | ||
| return "elixir" | ||
|
|
||
| def stop(self) -> List[str]: | ||
| return ["\ndefmodule", "\ndefp", "\ndef ", "\n#", "\n\n"] | ||
|
|
||
| def translate_prompt( | ||
| self, name: str, args: List[ast.arg], _returns: ast.expr, description: str | ||
| ) -> str: | ||
| """ """ | ||
| elixir_description = ( | ||
| "# " + re.sub(DOCSTRING_LINESTART_RE, "\n# ", description.strip()) + "\n" | ||
| ) | ||
| arg_names = [arg.arg for arg in args] | ||
| arg_list = ", ".join(arg_names) | ||
| result_list = [ | ||
| elixir_description, | ||
| "defmodule HumanEval do", | ||
| f" def candidate({arg_list}), do: {name}({arg_list})", | ||
| f" def {name}({arg_list}) do", | ||
| f" ", | ||
| ] | ||
| return "\n".join(result_list) | ||
|
|
||
| def test_suite_prefix_lines(self, entry_point: str) -> List[str]: | ||
| """ | ||
| This code goes at the start of the test suite. | ||
| """ | ||
| return [ | ||
| "ExUnit.start()", | ||
| "defmodule HumanEvalTest do", | ||
| " use ExUnit.Case, async: true", | ||
| f" test '{entry_point}' do", | ||
| ] | ||
|
|
||
| def test_suite_suffix_lines(self) -> List[str]: | ||
| return [" end", "end", ""] | ||
|
|
||
| def deep_equality(self, left: TargetExp, right: TargetExp) -> str: | ||
| """ | ||
| All tests are assertions that compare deep equality between left and right. | ||
|
|
||
| Make sure you use the right equality operator for your language. For example, | ||
| == is the wrong operator for Java and OCaml. | ||
| """ | ||
| return " assert {} == {}".format(right, left) | ||
|
|
||
| def gen_literal(self, c: bool | str | int | float | None) -> TargetExp: | ||
| """Translate a literal expression | ||
| c: is the literal value | ||
| """ | ||
| # TODO: Make sure no string weirdness | ||
| if type(c) == bool: | ||
| return str(c).lower() | ||
| elif type(c) == str: | ||
| return f'"{c}"' | ||
| elif c is None: | ||
| return "nil" | ||
| return repr(c) | ||
|
|
||
| def gen_var(self, v: str) -> TargetExp: | ||
| """Translate a variable with name v.""" | ||
| return v | ||
|
|
||
| def gen_list(self, l: List[TargetExp]) -> TargetExp: | ||
| """Translate a list with elements l | ||
| A list [ x, y, z] translates to [ x, y, z ] (an Elixir list) | ||
| """ | ||
| return "[" + ", ".join(l) + "]" | ||
|
|
||
| def gen_tuple(self, t: List[TargetExp]) -> TargetExp: | ||
| """Translate a tuple with elements t | ||
| A tuple (x, y, z) translates to {x, y, z} | ||
| """ | ||
| return "{" + ", ".join(t) + "}" | ||
|
|
||
| def gen_dict(self, keys: List[TargetExp], values: List[TargetExp]) -> TargetExp: | ||
| """Translate a dictionary with keys and values | ||
| A dictionary { "key1": val1, "key2": val2 } translates to %{"key1" => val1, "key2" => val2} | ||
| """ | ||
| return "%{" + ", ".join(f"{k} => {v}" for k, v in zip(keys, values)) + "}" | ||
|
|
||
| def gen_call(self, func: TargetExp, args: List[TargetExp]) -> str: | ||
| """Translate a function call `func(args)` | ||
| A function call f(x, y, z) translates to f(x, y, z) | ||
| """ | ||
| return f"HumanEval.{func}({', '.join(args)})" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import argparse | ||
| from sys import exit | ||
| import subprocess | ||
| from pathlib import Path | ||
| from generic_eval import main as gmain | ||
|
|
||
|
|
||
| def eval_script(path: Path): | ||
| try: | ||
| # Assumes exit-code 0 is all okay | ||
| output = subprocess.run(["elixir", str(path)], capture_output=True, timeout=5) | ||
|
|
||
| if output.returncode == 0: | ||
| status = "OK" | ||
| else: | ||
| outmessage = str(output) | ||
| if "Assertion with == failed" in outmessage: | ||
| status = "AssertionError" | ||
| elif "SyntaxError" in outmessage: | ||
| status = "SyntaxError" | ||
| else: | ||
| status = "Exception" | ||
| returncode = output.returncode | ||
| except subprocess.TimeoutExpired as exc: | ||
| status = "Timeout" | ||
| output = exc | ||
| returncode = -1 | ||
| return { | ||
| "status": status, | ||
| "exit_code": returncode, | ||
| "stdout": "" if output.stdout is None else output.stdout.decode("utf-8"), | ||
| "stderr": "" if output.stderr is None else output.stderr.decode("utf-8"), | ||
| } | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| gmain(eval_script, "Elixir", ".exs") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe use ex?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense