Skip to content
Snippets Groups Projects
Verified Commit 8a0cfdcf authored by James Ellison's avatar James Ellison
Browse files

challenge 2: amazing 'hand written' interpreter

parent a71dfe16
No related branches found
No related tags found
No related merge requests found
.direnv/ .direnv/
.env
...@@ -10,7 +10,7 @@ if target.endswith("@soton.ac.uk"): ...@@ -10,7 +10,7 @@ if target.endswith("@soton.ac.uk"):
with requests.get(f"https://www.ecs.soton.ac.uk/people/{target}") as request: with requests.get(f"https://www.ecs.soton.ac.uk/people/{target}") as request:
if request.status_code != 200: if request.status_code != 200:
print("Invalid email ID.") print("Invalid email ID.")
exit() exit(1)
name = REGEX.search(request.text) name = REGEX.search(request.text)
if name: if name:
......
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)
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
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
with pkgs; { with pkgs; {
devShells.default = mkShellNoCC { devShells.default = mkShellNoCC {
buildInputs = [ buildInputs = [
(python3.withPackages (pyPkgs: [ pyPkgs.requests ])) (python3.withPackages (pyPkgs: [ pyPkgs.requests pyPkgs.pydantic pyPkgs.openai ]))
]; ];
}; };
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment