From 8a0cfdcf66457b7d7c90e6247c4110bec53fdedb Mon Sep 17 00:00:00 2001 From: joe1g24 <james@jamalam.tech> Date: Fri, 4 Oct 2024 16:23:41 +0100 Subject: [PATCH] challenge 2: amazing 'hand written' interpreter --- .gitignore | 1 + challenge-i/base.py | 2 +- challenge-ii/gpt.py | 49 ++++++++++++++++++++++++++++++++++++++++ challenge-ii/program.txt | 21 +++++++++++++++++ flake.nix | 2 +- 5 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 challenge-ii/gpt.py create mode 100644 challenge-ii/program.txt diff --git a/.gitignore b/.gitignore index 9b42106..cdf0ac6 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .direnv/ +.env diff --git a/challenge-i/base.py b/challenge-i/base.py index 267d228..b180cac 100644 --- a/challenge-i/base.py +++ b/challenge-i/base.py @@ -10,7 +10,7 @@ if target.endswith("@soton.ac.uk"): with requests.get(f"https://www.ecs.soton.ac.uk/people/{target}") as request: if request.status_code != 200: print("Invalid email ID.") - exit() + exit(1) name = REGEX.search(request.text) if name: diff --git a/challenge-ii/gpt.py b/challenge-ii/gpt.py new file mode 100644 index 0000000..a32b193 --- /dev/null +++ b/challenge-ii/gpt.py @@ -0,0 +1,49 @@ +import sys +from pydantic import BaseModel +from openai import OpenAI + +client = OpenAI() + +class Variable(BaseModel): + name: str + value: int + +class ResponseFormat(BaseModel): + variables: list[Variable] + +with open(sys.argv[1], 'r') as file: + code = file.read() + +completion = client.beta.chat.completions.parse( + model="gpt-4o-mini", + messages=[ + { + "role": "system", + "content": '''You are an interpreter for the programming language barebones. Bare Bones is the simple language that Brookshear uses in his book, 'Computer Science: an Overview', to illustrate the power of Turing complete machines and investigate the halting problem. + +Bare Bones has three simple commands for manipulating a variable: + +clear name; +incr name; +decr name; +...which respectively sets variable name to zero, increments it by one and decrements it by one. + +The language also contains one control sequence, a simple loop: + +while name not 0 do; +... +... +end; +... where name is a variable. Note that variables need not be declared before they are used and must be non-negative integers. Statements are delimited by the ; character. + +Note that while loops must be terminated by an end statement, but that they can be nested.''', + }, + { + "role": "user", + "content": code, + }, + ], + response_format=ResponseFormat, +) + +print(completion.choices[0].message.parsed) diff --git a/challenge-ii/program.txt b/challenge-ii/program.txt new file mode 100644 index 0000000..7b542fe --- /dev/null +++ b/challenge-ii/program.txt @@ -0,0 +1,21 @@ +clear X; +incr X; +incr X; +clear Y; +incr Y; +incr Y; +incr Y; +clear Z; +while X not 0 do; + clear W; + while Y not 0 do; + incr Z; + incr W; + decr Y; + end; + while W not 0 do; + incr Y; + decr W; + end; + decr X; +end; \ No newline at end of file diff --git a/flake.nix b/flake.nix index 92c3345..345ef1d 100644 --- a/flake.nix +++ b/flake.nix @@ -19,7 +19,7 @@ with pkgs; { devShells.default = mkShellNoCC { buildInputs = [ - (python3.withPackages (pyPkgs: [ pyPkgs.requests ])) + (python3.withPackages (pyPkgs: [ pyPkgs.requests pyPkgs.pydantic pyPkgs.openai ])) ]; }; } -- GitLab