From beb3a77161713c960760275d581e5f0a5681cbbd Mon Sep 17 00:00:00 2001 From: PedroJSilva2001 Date: Thu, 30 May 2024 12:30:34 +0100 Subject: [PATCH] Restructure project and add better console logging --- .gitignore | 4 +- .../antlr-generate.sh => antlr-generate.sh | 4 +- .../metadata}/am_map_1.json | 0 .../metadata}/am_map_2.json | 0 .../metadata}/am_map_3.json | 0 .../metadata}/am_map_4.json | 0 .../metadata}/am_map_5.json | 0 .../metadata}/am_map_6.json | 0 .../metadata}/g_game_1.json | 0 .../metadata}/g_game_2.json | 0 .../metadata}/g_game_3.json | 0 .../metadata}/i_video_1.json | 0 .../metadata}/i_video_2.json | 0 .../metadata}/i_video_3.json | 0 .../doom => doom_id-software}/project.json | 3 +- .../doom_id-software/snippets/am_map.c | 95 ++++ .../snippets}/am_map_1.c | 0 .../snippets}/am_map_2.c | 0 .../snippets}/am_map_3.c | 0 .../snippets}/am_map_4.c | 0 .../snippets}/am_map_5.c | 0 .../snippets}/am_map_6.c | 0 .../doom_id-software/snippets/g_game.c | 60 +++ .../snippets}/g_game_1.c | 0 .../snippets}/g_game_2.c | 2 +- .../snippets}/g_game_3.c | 0 .../doom_id-software/snippets/i_video.c | 112 +++++ .../snippets}/i_video_1.c | 0 .../snippets}/i_video_2.c | 0 .../snippets}/i_video_3.c | 0 experiments/linux/project.json | 6 + .../linux => linux/snippets}/cpu_1.c | 0 .../linux => linux/snippets}/cpu_2.c | 0 .../linux => linux/snippets}/cpu_3.c | 0 .../linux => linux/snippets}/cpu_4.c | 0 .../linux => linux/snippets}/cpu_5.c | 0 .../linux => linux/snippets}/groups_1.c | 0 .../linux => linux/snippets}/groups_2.c | 0 .../linux => linux/snippets}/groups_3.c | 0 .../linux => linux/snippets}/groups_4.c | 0 .../linux => linux/snippets}/groups_5.c | 0 .../linux => linux/snippets}/groups_6.c | 0 .../linux => linux/snippets}/pid_1.c | 0 .../linux => linux/snippets}/pid_2.c | 0 .../linux => linux/snippets}/pid_3.c | 0 .../metadata}/ngx_crypt_1.json | 0 .../metadata}/ngx_crypt_2.json | 0 .../metadata}/ngx_http_1.json | 0 .../nginx => nginx_nginx}/project.json | 3 +- experiments/nginx_nginx/snippets/ngx_crypt.c | 69 +++ .../snippets}/ngx_crypt_1.c | 0 .../snippets}/ngx_crypt_2.c | 0 experiments/nginx_nginx/snippets/ngx_http.c | 101 +++++ .../snippets}/ngx_http_1.c | 0 .../snippets}/ngx_http_2.c | 0 experiments/openssl/project.json | 6 + .../openssl => openssl/snippets}/rsa_crpt_1.c | 0 .../openssl => openssl/snippets}/rsa_crpt_2.c | 0 .../openssl => openssl/snippets}/rsa_crpt_3.c | 0 .../openssl => openssl/snippets}/rsa_crpt_4.c | 0 .../openssl => openssl/snippets}/rsa_crpt_5.c | 0 .../openssl => openssl/snippets}/rsa_crpt_6.c | 0 .../openssl => openssl/snippets}/rsa_crpt_7.c | 0 .../openssl => openssl/snippets}/rsa_crpt_8.c | 0 .../openssl => openssl/snippets}/stack_1.c | 0 .../openssl => openssl/snippets}/stack_2.c | 0 .../openssl => openssl/snippets}/stack_3.c | 0 {experiments/prompts => prompts}/prompt_c.md | 0 .../am_map_1_DOOM_id-Software_1710378800.json | 0 .../am_map_2_DOOM_id-Software_1710378809.json | 0 .../am_map_3_DOOM_id-Software_1710378828.json | 0 .../am_map_4_DOOM_id-Software_1710378850.json | 0 .../am_map_5_DOOM_id-Software_1710378865.json | 0 .../am_map_6_DOOM_id-Software_1710378878.json | 0 .../g_game_1_DOOM_id-Software_1710378888.json | 0 .../g_game_2_DOOM_id-Software_1710378896.json | 0 .../g_game_3_DOOM_id-Software_1710378904.json | 0 ...i_video_1_DOOM_id-Software_1710378914.json | 0 ...i_video_2_DOOM_id-Software_1710378934.json | 0 ...i_video_3_DOOM_id-Software_1710378939.json | 0 .../requirements.txt => requirements.txt | 0 {experiments => src}/antlr/Declaration.g4 | 0 {experiments => src}/declaration.py | 0 .../declparser/Declaration.interp | 0 .../declparser/Declaration.tokens | 0 .../declparser/DeclarationLexer.interp | 0 .../declparser/DeclarationLexer.py | 2 +- .../declparser/DeclarationLexer.tokens | 0 .../declparser/DeclarationListener.py | 2 +- .../declparser/DeclarationParser.py | 2 +- src/decls.txt | 50 +++ {experiments => src}/error.py | 0 {experiments => src}/evaluation.py | 0 {experiments => src}/experiments.py | 419 ++++++++---------- src/llm.py | 57 +++ src/logger.py | 22 + {experiments => src}/utils.py | 3 + 97 files changed, 789 insertions(+), 233 deletions(-) rename experiments/antlr-generate.sh => antlr-generate.sh (81%) rename experiments/{metadata/doom => doom_id-software/metadata}/am_map_1.json (100%) rename experiments/{metadata/doom => doom_id-software/metadata}/am_map_2.json (100%) rename experiments/{metadata/doom => doom_id-software/metadata}/am_map_3.json (100%) rename experiments/{metadata/doom => doom_id-software/metadata}/am_map_4.json (100%) rename experiments/{metadata/doom => doom_id-software/metadata}/am_map_5.json (100%) rename experiments/{metadata/doom => doom_id-software/metadata}/am_map_6.json (100%) rename experiments/{metadata/doom => doom_id-software/metadata}/g_game_1.json (100%) rename experiments/{metadata/doom => doom_id-software/metadata}/g_game_2.json (100%) rename experiments/{metadata/doom => doom_id-software/metadata}/g_game_3.json (100%) rename experiments/{metadata/doom => doom_id-software/metadata}/i_video_1.json (100%) rename experiments/{metadata/doom => doom_id-software/metadata}/i_video_2.json (100%) rename experiments/{metadata/doom => doom_id-software/metadata}/i_video_3.json (100%) rename experiments/{metadata/doom => doom_id-software}/project.json (69%) create mode 100644 experiments/doom_id-software/snippets/am_map.c rename experiments/{snippets/doom => doom_id-software/snippets}/am_map_1.c (100%) rename experiments/{snippets/doom => doom_id-software/snippets}/am_map_2.c (100%) rename experiments/{snippets/doom => doom_id-software/snippets}/am_map_3.c (100%) rename experiments/{snippets/doom => doom_id-software/snippets}/am_map_4.c (100%) rename experiments/{snippets/doom => doom_id-software/snippets}/am_map_5.c (100%) rename experiments/{snippets/doom => doom_id-software/snippets}/am_map_6.c (100%) create mode 100644 experiments/doom_id-software/snippets/g_game.c rename experiments/{snippets/doom => doom_id-software/snippets}/g_game_1.c (100%) rename experiments/{snippets/doom => doom_id-software/snippets}/g_game_2.c (99%) rename experiments/{snippets/doom => doom_id-software/snippets}/g_game_3.c (100%) create mode 100644 experiments/doom_id-software/snippets/i_video.c rename experiments/{snippets/doom => doom_id-software/snippets}/i_video_1.c (100%) rename experiments/{snippets/doom => doom_id-software/snippets}/i_video_2.c (100%) rename experiments/{snippets/doom => doom_id-software/snippets}/i_video_3.c (100%) create mode 100644 experiments/linux/project.json rename experiments/{snippets/linux => linux/snippets}/cpu_1.c (100%) rename experiments/{snippets/linux => linux/snippets}/cpu_2.c (100%) rename experiments/{snippets/linux => linux/snippets}/cpu_3.c (100%) rename experiments/{snippets/linux => linux/snippets}/cpu_4.c (100%) rename experiments/{snippets/linux => linux/snippets}/cpu_5.c (100%) rename experiments/{snippets/linux => linux/snippets}/groups_1.c (100%) rename experiments/{snippets/linux => linux/snippets}/groups_2.c (100%) rename experiments/{snippets/linux => linux/snippets}/groups_3.c (100%) rename experiments/{snippets/linux => linux/snippets}/groups_4.c (100%) rename experiments/{snippets/linux => linux/snippets}/groups_5.c (100%) rename experiments/{snippets/linux => linux/snippets}/groups_6.c (100%) rename experiments/{snippets/linux => linux/snippets}/pid_1.c (100%) rename experiments/{snippets/linux => linux/snippets}/pid_2.c (100%) rename experiments/{snippets/linux => linux/snippets}/pid_3.c (100%) rename experiments/{metadata/nginx => nginx_nginx/metadata}/ngx_crypt_1.json (100%) rename experiments/{metadata/nginx => nginx_nginx/metadata}/ngx_crypt_2.json (100%) rename experiments/{metadata/nginx => nginx_nginx/metadata}/ngx_http_1.json (100%) rename experiments/{metadata/nginx => nginx_nginx}/project.json (61%) create mode 100644 experiments/nginx_nginx/snippets/ngx_crypt.c rename experiments/{snippets/nginx => nginx_nginx/snippets}/ngx_crypt_1.c (100%) rename experiments/{snippets/nginx => nginx_nginx/snippets}/ngx_crypt_2.c (100%) create mode 100644 experiments/nginx_nginx/snippets/ngx_http.c rename experiments/{snippets/nginx => nginx_nginx/snippets}/ngx_http_1.c (100%) rename experiments/{snippets/nginx => nginx_nginx/snippets}/ngx_http_2.c (100%) create mode 100644 experiments/openssl/project.json rename experiments/{snippets/openssl => openssl/snippets}/rsa_crpt_1.c (100%) rename experiments/{snippets/openssl => openssl/snippets}/rsa_crpt_2.c (100%) rename experiments/{snippets/openssl => openssl/snippets}/rsa_crpt_3.c (100%) rename experiments/{snippets/openssl => openssl/snippets}/rsa_crpt_4.c (100%) rename experiments/{snippets/openssl => openssl/snippets}/rsa_crpt_5.c (100%) rename experiments/{snippets/openssl => openssl/snippets}/rsa_crpt_6.c (100%) rename experiments/{snippets/openssl => openssl/snippets}/rsa_crpt_7.c (100%) rename experiments/{snippets/openssl => openssl/snippets}/rsa_crpt_8.c (100%) rename experiments/{snippets/openssl => openssl/snippets}/stack_1.c (100%) rename experiments/{snippets/openssl => openssl/snippets}/stack_2.c (100%) rename experiments/{snippets/openssl => openssl/snippets}/stack_3.c (100%) rename {experiments/prompts => prompts}/prompt_c.md (100%) rename {experiments/raw_results => raw_results}/am_map_1_DOOM_id-Software_1710378800.json (100%) rename {experiments/raw_results => raw_results}/am_map_2_DOOM_id-Software_1710378809.json (100%) rename {experiments/raw_results => raw_results}/am_map_3_DOOM_id-Software_1710378828.json (100%) rename {experiments/raw_results => raw_results}/am_map_4_DOOM_id-Software_1710378850.json (100%) rename {experiments/raw_results => raw_results}/am_map_5_DOOM_id-Software_1710378865.json (100%) rename {experiments/raw_results => raw_results}/am_map_6_DOOM_id-Software_1710378878.json (100%) rename {experiments/raw_results => raw_results}/g_game_1_DOOM_id-Software_1710378888.json (100%) rename {experiments/raw_results => raw_results}/g_game_2_DOOM_id-Software_1710378896.json (100%) rename {experiments/raw_results => raw_results}/g_game_3_DOOM_id-Software_1710378904.json (100%) rename {experiments/raw_results => raw_results}/i_video_1_DOOM_id-Software_1710378914.json (100%) rename {experiments/raw_results => raw_results}/i_video_2_DOOM_id-Software_1710378934.json (100%) rename {experiments/raw_results => raw_results}/i_video_3_DOOM_id-Software_1710378939.json (100%) rename experiments/requirements.txt => requirements.txt (100%) rename {experiments => src}/antlr/Declaration.g4 (100%) rename {experiments => src}/declaration.py (100%) rename {experiments => src}/declparser/Declaration.interp (100%) rename {experiments => src}/declparser/Declaration.tokens (100%) rename {experiments => src}/declparser/DeclarationLexer.interp (100%) rename {experiments => src}/declparser/DeclarationLexer.py (98%) rename {experiments => src}/declparser/DeclarationLexer.tokens (100%) rename {experiments => src}/declparser/DeclarationListener.py (96%) rename {experiments => src}/declparser/DeclarationParser.py (99%) create mode 100644 src/decls.txt rename {experiments => src}/error.py (100%) rename {experiments => src}/evaluation.py (100%) rename {experiments => src}/experiments.py (56%) create mode 100644 src/llm.py create mode 100644 src/logger.py rename {experiments => src}/utils.py (90%) diff --git a/.gitignore b/.gitignore index 515f548..f0c776e 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,6 @@ errors.txt declarations.txt .env -build/ \ No newline at end of file +build/ + +*.log \ No newline at end of file diff --git a/experiments/antlr-generate.sh b/antlr-generate.sh similarity index 81% rename from experiments/antlr-generate.sh rename to antlr-generate.sh index e0c0d1c..f77e7db 100755 --- a/experiments/antlr-generate.sh +++ b/antlr-generate.sh @@ -7,10 +7,10 @@ ANTLR_PATH="" # Set the input grammar file -GRAMMAR_FILE="./antlr/Declaration.g4" +GRAMMAR_FILE="./src/antlr/Declaration.g4" # Set the output directory for generated files -OUTPUT_DIR="declparser" +OUTPUT_DIR="./src/declparser" # Generate the ANTLR files java -jar $ANTLR_PATH -o $OUTPUT_DIR -Dlanguage=Python3 -Xexact-output-dir $GRAMMAR_FILE \ No newline at end of file diff --git a/experiments/metadata/doom/am_map_1.json b/experiments/doom_id-software/metadata/am_map_1.json similarity index 100% rename from experiments/metadata/doom/am_map_1.json rename to experiments/doom_id-software/metadata/am_map_1.json diff --git a/experiments/metadata/doom/am_map_2.json b/experiments/doom_id-software/metadata/am_map_2.json similarity index 100% rename from experiments/metadata/doom/am_map_2.json rename to experiments/doom_id-software/metadata/am_map_2.json diff --git a/experiments/metadata/doom/am_map_3.json b/experiments/doom_id-software/metadata/am_map_3.json similarity index 100% rename from experiments/metadata/doom/am_map_3.json rename to experiments/doom_id-software/metadata/am_map_3.json diff --git a/experiments/metadata/doom/am_map_4.json b/experiments/doom_id-software/metadata/am_map_4.json similarity index 100% rename from experiments/metadata/doom/am_map_4.json rename to experiments/doom_id-software/metadata/am_map_4.json diff --git a/experiments/metadata/doom/am_map_5.json b/experiments/doom_id-software/metadata/am_map_5.json similarity index 100% rename from experiments/metadata/doom/am_map_5.json rename to experiments/doom_id-software/metadata/am_map_5.json diff --git a/experiments/metadata/doom/am_map_6.json b/experiments/doom_id-software/metadata/am_map_6.json similarity index 100% rename from experiments/metadata/doom/am_map_6.json rename to experiments/doom_id-software/metadata/am_map_6.json diff --git a/experiments/metadata/doom/g_game_1.json b/experiments/doom_id-software/metadata/g_game_1.json similarity index 100% rename from experiments/metadata/doom/g_game_1.json rename to experiments/doom_id-software/metadata/g_game_1.json diff --git a/experiments/metadata/doom/g_game_2.json b/experiments/doom_id-software/metadata/g_game_2.json similarity index 100% rename from experiments/metadata/doom/g_game_2.json rename to experiments/doom_id-software/metadata/g_game_2.json diff --git a/experiments/metadata/doom/g_game_3.json b/experiments/doom_id-software/metadata/g_game_3.json similarity index 100% rename from experiments/metadata/doom/g_game_3.json rename to experiments/doom_id-software/metadata/g_game_3.json diff --git a/experiments/metadata/doom/i_video_1.json b/experiments/doom_id-software/metadata/i_video_1.json similarity index 100% rename from experiments/metadata/doom/i_video_1.json rename to experiments/doom_id-software/metadata/i_video_1.json diff --git a/experiments/metadata/doom/i_video_2.json b/experiments/doom_id-software/metadata/i_video_2.json similarity index 100% rename from experiments/metadata/doom/i_video_2.json rename to experiments/doom_id-software/metadata/i_video_2.json diff --git a/experiments/metadata/doom/i_video_3.json b/experiments/doom_id-software/metadata/i_video_3.json similarity index 100% rename from experiments/metadata/doom/i_video_3.json rename to experiments/doom_id-software/metadata/i_video_3.json diff --git a/experiments/metadata/doom/project.json b/experiments/doom_id-software/project.json similarity index 69% rename from experiments/metadata/doom/project.json rename to experiments/doom_id-software/project.json index a91f427..9f20975 100644 --- a/experiments/metadata/doom/project.json +++ b/experiments/doom_id-software/project.json @@ -2,6 +2,5 @@ "project": "DOOM", "org": "id-Software", "repository": "https://github.com/id-Software/DOOM", - "license": "GPLv2", - "snippets": "doom" + "license": "GPLv2" } \ No newline at end of file diff --git a/experiments/doom_id-software/snippets/am_map.c b/experiments/doom_id-software/snippets/am_map.c new file mode 100644 index 0000000..8465368 --- /dev/null +++ b/experiments/doom_id-software/snippets/am_map.c @@ -0,0 +1,95 @@ +// Calculates the slope and slope according to the x-axis of a line +// segment in map coordinates (with the upright y-axis n' all) so +// that it can be used with the brain-dead drawing stuff. +void AM_getIslope(mline_t *ml, islope_t *is) +{ + int dx, dy; + + dy = ml->a.y - ml->b.y; + dx = ml->b.x - ml->a.x; + if (!dy) is->islp = (dx<0?-MAXINT:MAXINT); + else is->islp = FixedDiv(dx, dy); + if (!dx) is->slp = (dy<0?-MAXINT:MAXINT); + else is->slp = FixedDiv(dy, dx); +} + +void AM_activateNewScale(void) +{ + m_x += m_w/2; + m_y += m_h/2; + m_w = FTOM(f_w); + m_h = FTOM(f_h); + m_x -= m_w/2; + m_y -= m_h/2; + m_x2 = m_x + m_w; + m_y2 = m_y + m_h; +} + +void AM_saveScaleAndLoc(void) +{ + old_m_x = m_x; + old_m_y = m_y; + old_m_w = m_w; + old_m_h = m_h; +} + +void AM_restoreScaleAndLoc(void) +{ + m_w = old_m_w; + m_h = old_m_h; + if (!followplayer) { + m_x = old_m_x; + m_y = old_m_y; + } else { + m_x = plr->mo->x - m_w/2; + m_y = plr->mo->y - m_h/2; + } + m_x2 = m_x + m_w; + m_y2 = m_y + m_h; + + // Change the scaling multipliers + scale_mtof = FixedDiv(f_w< max_x) + max_x = vertexes[i].x; + + if (vertexes[i].y < min_y) + min_y = vertexes[i].y; + else if (vertexes[i].y > max_y) + max_y = vertexes[i].y; + } + + max_w = max_x - min_x; + max_h = max_y - min_y; + + min_w = 2*PLAYERRADIUS; // const? never changed? + min_h = 2*PLAYERRADIUS; + + a = FixedDiv(f_w<powers, 0, sizeof (p->powers)); + memset (p->cards, 0, sizeof (p->cards)); + p->mo->flags &= ~MF_SHADOW; // cancel invisibility + p->extralight = 0; // cancel gun flashes + p->fixedcolormap = 0; // cancel ir gogles + p->damagecount = 0; // no palette changes + p->bonuscount = 0; +} + +void G_PlayerReborn (int player) +{ + player_t* p; + int i; + int frags[MAXPLAYERS]; + int killcount; + int itemcount; + int secretcount; + + memcpy (frags,players[player].frags,sizeof(frags)); + killcount = players[player].killcount; + itemcount = players[player].itemcount; + secretcount = players[player].secretcount; + + p = &players[player]; + memset (p, 0, sizeof(*p)); + + memcpy (players[player].frags, frags, sizeof(players[player].frags)); + players[player].killcount = killcount; + players[player].itemcount = itemcount; + players[player].secretcount = secretcount; + + p->usedown = p->attackdown = true; // don't do anything immediately + p->playerstate = PST_LIVE; + p->health = MAXHEALTH; + p->readyweapon = p->pendingweapon = wp_pistol; + p->weaponowned[wp_fist] = true; + p->weaponowned[wp_pistol] = true; + p->ammo[am_clip] = 50; + + for (i=0 ; imaxammo[i] = maxammo[i]; + +} \ No newline at end of file diff --git a/experiments/snippets/doom/g_game_1.c b/experiments/doom_id-software/snippets/g_game_1.c similarity index 100% rename from experiments/snippets/doom/g_game_1.c rename to experiments/doom_id-software/snippets/g_game_1.c diff --git a/experiments/snippets/doom/g_game_2.c b/experiments/doom_id-software/snippets/g_game_2.c similarity index 99% rename from experiments/snippets/doom/g_game_2.c rename to experiments/doom_id-software/snippets/g_game_2.c index 7bd354c..e1f3099 100644 --- a/experiments/snippets/doom/g_game_2.c +++ b/experiments/doom_id-software/snippets/g_game_2.c @@ -11,4 +11,4 @@ void G_PlayerFinishLevel(int player) p->fixedcolormap = 0; // cancel ir gogles p->damagecount = 0; // no palette changes p->bonuscount = 0; -} \ No newline at end of file +} \ No newline at end of file diff --git a/experiments/snippets/doom/g_game_3.c b/experiments/doom_id-software/snippets/g_game_3.c similarity index 100% rename from experiments/snippets/doom/g_game_3.c rename to experiments/doom_id-software/snippets/g_game_3.c diff --git a/experiments/doom_id-software/snippets/i_video.c b/experiments/doom_id-software/snippets/i_video.c new file mode 100644 index 0000000..1d46c7f --- /dev/null +++ b/experiments/doom_id-software/snippets/i_video.c @@ -0,0 +1,112 @@ +#define NULL ((void *)0) + +// +// Translates the key currently in X_event +// + +int xlatekey(void) +{ + + int rc; + + switch(rc = XKeycodeToKeysym(X_display, X_event.xkey.keycode, 0)) + { + case XK_Left: rc = KEY_LEFTARROW; break; + case XK_Right: rc = KEY_RIGHTARROW; break; + case XK_Down: rc = KEY_DOWNARROW; break; + case XK_Up: rc = KEY_UPARROW; break; + case XK_Escape: rc = KEY_ESCAPE; break; + case XK_Return: rc = KEY_ENTER; break; + case XK_Tab: rc = KEY_TAB; break; + case XK_F1: rc = KEY_F1; break; + case XK_F2: rc = KEY_F2; break; + case XK_F3: rc = KEY_F3; break; + case XK_F4: rc = KEY_F4; break; + case XK_F5: rc = KEY_F5; break; + case XK_F6: rc = KEY_F6; break; + case XK_F7: rc = KEY_F7; break; + case XK_F8: rc = KEY_F8; break; + case XK_F9: rc = KEY_F9; break; + case XK_F10: rc = KEY_F10; break; + case XK_F11: rc = KEY_F11; break; + case XK_F12: rc = KEY_F12; break; + + case XK_BackSpace: + case XK_Delete: rc = KEY_BACKSPACE; break; + + case XK_Pause: rc = KEY_PAUSE; break; + + case XK_KP_Equal: + case XK_equal: rc = KEY_EQUALS; break; + + case XK_KP_Subtract: + case XK_minus: rc = KEY_MINUS; break; + + case XK_Shift_L: + case XK_Shift_R: + rc = KEY_RSHIFT; + break; + + case XK_Control_L: + case XK_Control_R: + rc = KEY_RCTRL; + break; + + case XK_Alt_L: + case XK_Meta_L: + case XK_Alt_R: + case XK_Meta_R: + rc = KEY_RALT; + break; + + default: + if (rc >= XK_space && rc <= XK_asciitilde) + rc = rc - XK_space + ' '; + if (rc >= 'A' && rc <= 'Z') + rc = rc - 'A' + 'a'; + break; + } + + return rc; +} + +void I_ShutdownGraphics(void) { + // Detach from X server + if (!XShmDetach(X_display, &X_shminfo)) + I_Error("XShmDetach() failed in I_ShutdownGraphics()"); + + // Release shared memory. + shmdt(X_shminfo.shmaddr); + shmctl(X_shminfo.shmid, IPC_RMID, 0); + + // Paranoia. + image->data = NULL; +} + +void I_StartTic(void) { + + if (!X_display) return; + + while (XPending(X_display)) + I_GetEvent(); + + // Warp the pointer back to the middle of the window + // or it will wander off - that is, the game will + // loose input focus within X11. + if (grabMouse) + { + if (!--doPointerWarp) + { + XWarpPointer(X_display, + None, + X_mainWindow, + 0, 0, + 0, 0, + X_width/2, X_height/2); + + doPointerWarp = POINTER_WARP_COUNTDOWN; + } + } + + mousemoved = false; +} \ No newline at end of file diff --git a/experiments/snippets/doom/i_video_1.c b/experiments/doom_id-software/snippets/i_video_1.c similarity index 100% rename from experiments/snippets/doom/i_video_1.c rename to experiments/doom_id-software/snippets/i_video_1.c diff --git a/experiments/snippets/doom/i_video_2.c b/experiments/doom_id-software/snippets/i_video_2.c similarity index 100% rename from experiments/snippets/doom/i_video_2.c rename to experiments/doom_id-software/snippets/i_video_2.c diff --git a/experiments/snippets/doom/i_video_3.c b/experiments/doom_id-software/snippets/i_video_3.c similarity index 100% rename from experiments/snippets/doom/i_video_3.c rename to experiments/doom_id-software/snippets/i_video_3.c diff --git a/experiments/linux/project.json b/experiments/linux/project.json new file mode 100644 index 0000000..af63264 --- /dev/null +++ b/experiments/linux/project.json @@ -0,0 +1,6 @@ +{ + "project": "linux", + "org": "linux", + "repository": "https://github.com/", + "license": "" +} \ No newline at end of file diff --git a/experiments/snippets/linux/cpu_1.c b/experiments/linux/snippets/cpu_1.c similarity index 100% rename from experiments/snippets/linux/cpu_1.c rename to experiments/linux/snippets/cpu_1.c diff --git a/experiments/snippets/linux/cpu_2.c b/experiments/linux/snippets/cpu_2.c similarity index 100% rename from experiments/snippets/linux/cpu_2.c rename to experiments/linux/snippets/cpu_2.c diff --git a/experiments/snippets/linux/cpu_3.c b/experiments/linux/snippets/cpu_3.c similarity index 100% rename from experiments/snippets/linux/cpu_3.c rename to experiments/linux/snippets/cpu_3.c diff --git a/experiments/snippets/linux/cpu_4.c b/experiments/linux/snippets/cpu_4.c similarity index 100% rename from experiments/snippets/linux/cpu_4.c rename to experiments/linux/snippets/cpu_4.c diff --git a/experiments/snippets/linux/cpu_5.c b/experiments/linux/snippets/cpu_5.c similarity index 100% rename from experiments/snippets/linux/cpu_5.c rename to experiments/linux/snippets/cpu_5.c diff --git a/experiments/snippets/linux/groups_1.c b/experiments/linux/snippets/groups_1.c similarity index 100% rename from experiments/snippets/linux/groups_1.c rename to experiments/linux/snippets/groups_1.c diff --git a/experiments/snippets/linux/groups_2.c b/experiments/linux/snippets/groups_2.c similarity index 100% rename from experiments/snippets/linux/groups_2.c rename to experiments/linux/snippets/groups_2.c diff --git a/experiments/snippets/linux/groups_3.c b/experiments/linux/snippets/groups_3.c similarity index 100% rename from experiments/snippets/linux/groups_3.c rename to experiments/linux/snippets/groups_3.c diff --git a/experiments/snippets/linux/groups_4.c b/experiments/linux/snippets/groups_4.c similarity index 100% rename from experiments/snippets/linux/groups_4.c rename to experiments/linux/snippets/groups_4.c diff --git a/experiments/snippets/linux/groups_5.c b/experiments/linux/snippets/groups_5.c similarity index 100% rename from experiments/snippets/linux/groups_5.c rename to experiments/linux/snippets/groups_5.c diff --git a/experiments/snippets/linux/groups_6.c b/experiments/linux/snippets/groups_6.c similarity index 100% rename from experiments/snippets/linux/groups_6.c rename to experiments/linux/snippets/groups_6.c diff --git a/experiments/snippets/linux/pid_1.c b/experiments/linux/snippets/pid_1.c similarity index 100% rename from experiments/snippets/linux/pid_1.c rename to experiments/linux/snippets/pid_1.c diff --git a/experiments/snippets/linux/pid_2.c b/experiments/linux/snippets/pid_2.c similarity index 100% rename from experiments/snippets/linux/pid_2.c rename to experiments/linux/snippets/pid_2.c diff --git a/experiments/snippets/linux/pid_3.c b/experiments/linux/snippets/pid_3.c similarity index 100% rename from experiments/snippets/linux/pid_3.c rename to experiments/linux/snippets/pid_3.c diff --git a/experiments/metadata/nginx/ngx_crypt_1.json b/experiments/nginx_nginx/metadata/ngx_crypt_1.json similarity index 100% rename from experiments/metadata/nginx/ngx_crypt_1.json rename to experiments/nginx_nginx/metadata/ngx_crypt_1.json diff --git a/experiments/metadata/nginx/ngx_crypt_2.json b/experiments/nginx_nginx/metadata/ngx_crypt_2.json similarity index 100% rename from experiments/metadata/nginx/ngx_crypt_2.json rename to experiments/nginx_nginx/metadata/ngx_crypt_2.json diff --git a/experiments/metadata/nginx/ngx_http_1.json b/experiments/nginx_nginx/metadata/ngx_http_1.json similarity index 100% rename from experiments/metadata/nginx/ngx_http_1.json rename to experiments/nginx_nginx/metadata/ngx_http_1.json diff --git a/experiments/metadata/nginx/project.json b/experiments/nginx_nginx/project.json similarity index 61% rename from experiments/metadata/nginx/project.json rename to experiments/nginx_nginx/project.json index b59daab..f08a3d4 100644 --- a/experiments/metadata/nginx/project.json +++ b/experiments/nginx_nginx/project.json @@ -2,6 +2,5 @@ "org": "nginx", "project": "nginx", "repository": "https://github.com/nginx/nginx", - "license": "BSD-2-Clause License", - "snippets": "nginx" + "license": "BSD-2-Clause License" } \ No newline at end of file diff --git a/experiments/nginx_nginx/snippets/ngx_crypt.c b/experiments/nginx_nginx/snippets/ngx_crypt.c new file mode 100644 index 0000000..491eee5 --- /dev/null +++ b/experiments/nginx_nginx/snippets/ngx_crypt.c @@ -0,0 +1,69 @@ +#define NULL ((void *)0) + +static ngx_int_t ngx_crypt_plain(ngx_pool_t *pool, u_char *key, u_char *salt, u_char **encrypted) +{ + size_t len; + u_char *p; + + len = ngx_strlen(key); + + *encrypted = ngx_pnalloc(pool, sizeof("{PLAIN}") - 1 + len + 1); + if (*encrypted == NULL) { + return NGX_ERROR; + } + + p = ngx_cpymem(*encrypted, "{PLAIN}", sizeof("{PLAIN}") - 1); + ngx_memcpy(p, key, len + 1); + + return NGX_OK; +} + +static ngx_int_t ngx_crypt_ssha(ngx_pool_t *pool, u_char *key, u_char *salt, u_char **encrypted) +{ + size_t len; + ngx_int_t rc; + ngx_str_t encoded, decoded; + ngx_sha1_t sha1; + + /* "{SSHA}" base64(SHA1(key salt) salt) */ + + /* decode base64 salt to find out true salt */ + + encoded.data = salt + sizeof("{SSHA}") - 1; + encoded.len = ngx_strlen(encoded.data); + + len = ngx_max(ngx_base64_decoded_length(encoded.len), 20); + + decoded.data = ngx_pnalloc(pool, len); + if (decoded.data == NULL) { + return NGX_ERROR; + } + + rc = ngx_decode_base64(&decoded, &encoded); + + if (rc != NGX_OK || decoded.len < 20) { + decoded.len = 20; + } + + /* update SHA1 from key and salt */ + + ngx_sha1_init(&sha1); + ngx_sha1_update(&sha1, key, ngx_strlen(key)); + ngx_sha1_update(&sha1, decoded.data + 20, decoded.len - 20); + ngx_sha1_final(decoded.data, &sha1); + + /* encode it back to base64 */ + + len = sizeof("{SSHA}") - 1 + ngx_base64_encoded_length(decoded.len) + 1; + + *encrypted = ngx_pnalloc(pool, len); + if (*encrypted == NULL) { + return NGX_ERROR; + } + + encoded.data = ngx_cpymem(*encrypted, "{SSHA}", sizeof("{SSHA}") - 1); + ngx_encode_base64(&encoded, &decoded); + encoded.data[encoded.len] = '\0'; + + return NGX_OK; +} \ No newline at end of file diff --git a/experiments/snippets/nginx/ngx_crypt_1.c b/experiments/nginx_nginx/snippets/ngx_crypt_1.c similarity index 100% rename from experiments/snippets/nginx/ngx_crypt_1.c rename to experiments/nginx_nginx/snippets/ngx_crypt_1.c diff --git a/experiments/snippets/nginx/ngx_crypt_2.c b/experiments/nginx_nginx/snippets/ngx_crypt_2.c similarity index 100% rename from experiments/snippets/nginx/ngx_crypt_2.c rename to experiments/nginx_nginx/snippets/ngx_crypt_2.c diff --git a/experiments/nginx_nginx/snippets/ngx_http.c b/experiments/nginx_nginx/snippets/ngx_http.c new file mode 100644 index 0000000..b4721c5 --- /dev/null +++ b/experiments/nginx_nginx/snippets/ngx_http.c @@ -0,0 +1,101 @@ +#define NULL ((void *)0) + +static ngx_int_t ngx_http_init_phases(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf) +{ + if (ngx_array_init(&cmcf->phases[NGX_HTTP_POST_READ_PHASE].handlers, + cf->pool, 1, sizeof(ngx_http_handler_pt)) + != NGX_OK) + { + return NGX_ERROR; + } + + if (ngx_array_init(&cmcf->phases[NGX_HTTP_SERVER_REWRITE_PHASE].handlers, + cf->pool, 1, sizeof(ngx_http_handler_pt)) + != NGX_OK) + { + return NGX_ERROR; + } + + if (ngx_array_init(&cmcf->phases[NGX_HTTP_REWRITE_PHASE].handlers, + cf->pool, 1, sizeof(ngx_http_handler_pt)) + != NGX_OK) + { + return NGX_ERROR; + } + + if (ngx_array_init(&cmcf->phases[NGX_HTTP_PREACCESS_PHASE].handlers, + cf->pool, 1, sizeof(ngx_http_handler_pt)) + != NGX_OK) + { + return NGX_ERROR; + } + + if (ngx_array_init(&cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers, + cf->pool, 2, sizeof(ngx_http_handler_pt)) + != NGX_OK) + { + return NGX_ERROR; + } + + if (ngx_array_init(&cmcf->phases[NGX_HTTP_PRECONTENT_PHASE].handlers, + cf->pool, 2, sizeof(ngx_http_handler_pt)) + != NGX_OK) + { + return NGX_ERROR; + } + + if (ngx_array_init(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers, + cf->pool, 4, sizeof(ngx_http_handler_pt)) + != NGX_OK) + { + return NGX_ERROR; + } + + if (ngx_array_init(&cmcf->phases[NGX_HTTP_LOG_PHASE].handlers, + cf->pool, 1, sizeof(ngx_http_handler_pt)) + != NGX_OK) + { + return NGX_ERROR; + } + + return NGX_OK; +} + +static ngx_int_t ngx_http_init_headers_in_hash(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf) +{ + ngx_array_t headers_in; + ngx_hash_key_t *hk; + ngx_hash_init_t hash; + ngx_http_header_t *header; + + if (ngx_array_init(&headers_in, cf->temp_pool, 32, sizeof(ngx_hash_key_t)) + != NGX_OK) + { + return NGX_ERROR; + } + + for (header = ngx_http_headers_in; header->name.len; header++) { + hk = ngx_array_push(&headers_in); + if (hk == NULL) { + return NGX_ERROR; + } + + hk->key = header->name; + hk->key_hash = ngx_hash_key_lc(header->name.data, header->name.len); + hk->value = header; + } + + hash.hash = &cmcf->headers_in_hash; + hash.key = ngx_hash_key_lc; + hash.max_size = 512; + hash.bucket_size = ngx_align(64, ngx_cacheline_size); + hash.name = "headers_in_hash"; + hash.pool = cf->pool; + hash.temp_pool = NULL; + + if (ngx_hash_init(&hash, headers_in.elts, headers_in.nelts) != NGX_OK) { + return NGX_ERROR; + } + + return NGX_OK; +} \ No newline at end of file diff --git a/experiments/snippets/nginx/ngx_http_1.c b/experiments/nginx_nginx/snippets/ngx_http_1.c similarity index 100% rename from experiments/snippets/nginx/ngx_http_1.c rename to experiments/nginx_nginx/snippets/ngx_http_1.c diff --git a/experiments/snippets/nginx/ngx_http_2.c b/experiments/nginx_nginx/snippets/ngx_http_2.c similarity index 100% rename from experiments/snippets/nginx/ngx_http_2.c rename to experiments/nginx_nginx/snippets/ngx_http_2.c diff --git a/experiments/openssl/project.json b/experiments/openssl/project.json new file mode 100644 index 0000000..2055a0c --- /dev/null +++ b/experiments/openssl/project.json @@ -0,0 +1,6 @@ +{ + "project": "openssl", + "org": "", + "repository": "https://github.com/", + "license": "" +} \ No newline at end of file diff --git a/experiments/snippets/openssl/rsa_crpt_1.c b/experiments/openssl/snippets/rsa_crpt_1.c similarity index 100% rename from experiments/snippets/openssl/rsa_crpt_1.c rename to experiments/openssl/snippets/rsa_crpt_1.c diff --git a/experiments/snippets/openssl/rsa_crpt_2.c b/experiments/openssl/snippets/rsa_crpt_2.c similarity index 100% rename from experiments/snippets/openssl/rsa_crpt_2.c rename to experiments/openssl/snippets/rsa_crpt_2.c diff --git a/experiments/snippets/openssl/rsa_crpt_3.c b/experiments/openssl/snippets/rsa_crpt_3.c similarity index 100% rename from experiments/snippets/openssl/rsa_crpt_3.c rename to experiments/openssl/snippets/rsa_crpt_3.c diff --git a/experiments/snippets/openssl/rsa_crpt_4.c b/experiments/openssl/snippets/rsa_crpt_4.c similarity index 100% rename from experiments/snippets/openssl/rsa_crpt_4.c rename to experiments/openssl/snippets/rsa_crpt_4.c diff --git a/experiments/snippets/openssl/rsa_crpt_5.c b/experiments/openssl/snippets/rsa_crpt_5.c similarity index 100% rename from experiments/snippets/openssl/rsa_crpt_5.c rename to experiments/openssl/snippets/rsa_crpt_5.c diff --git a/experiments/snippets/openssl/rsa_crpt_6.c b/experiments/openssl/snippets/rsa_crpt_6.c similarity index 100% rename from experiments/snippets/openssl/rsa_crpt_6.c rename to experiments/openssl/snippets/rsa_crpt_6.c diff --git a/experiments/snippets/openssl/rsa_crpt_7.c b/experiments/openssl/snippets/rsa_crpt_7.c similarity index 100% rename from experiments/snippets/openssl/rsa_crpt_7.c rename to experiments/openssl/snippets/rsa_crpt_7.c diff --git a/experiments/snippets/openssl/rsa_crpt_8.c b/experiments/openssl/snippets/rsa_crpt_8.c similarity index 100% rename from experiments/snippets/openssl/rsa_crpt_8.c rename to experiments/openssl/snippets/rsa_crpt_8.c diff --git a/experiments/snippets/openssl/stack_1.c b/experiments/openssl/snippets/stack_1.c similarity index 100% rename from experiments/snippets/openssl/stack_1.c rename to experiments/openssl/snippets/stack_1.c diff --git a/experiments/snippets/openssl/stack_2.c b/experiments/openssl/snippets/stack_2.c similarity index 100% rename from experiments/snippets/openssl/stack_2.c rename to experiments/openssl/snippets/stack_2.c diff --git a/experiments/snippets/openssl/stack_3.c b/experiments/openssl/snippets/stack_3.c similarity index 100% rename from experiments/snippets/openssl/stack_3.c rename to experiments/openssl/snippets/stack_3.c diff --git a/experiments/prompts/prompt_c.md b/prompts/prompt_c.md similarity index 100% rename from experiments/prompts/prompt_c.md rename to prompts/prompt_c.md diff --git a/experiments/raw_results/am_map_1_DOOM_id-Software_1710378800.json b/raw_results/am_map_1_DOOM_id-Software_1710378800.json similarity index 100% rename from experiments/raw_results/am_map_1_DOOM_id-Software_1710378800.json rename to raw_results/am_map_1_DOOM_id-Software_1710378800.json diff --git a/experiments/raw_results/am_map_2_DOOM_id-Software_1710378809.json b/raw_results/am_map_2_DOOM_id-Software_1710378809.json similarity index 100% rename from experiments/raw_results/am_map_2_DOOM_id-Software_1710378809.json rename to raw_results/am_map_2_DOOM_id-Software_1710378809.json diff --git a/experiments/raw_results/am_map_3_DOOM_id-Software_1710378828.json b/raw_results/am_map_3_DOOM_id-Software_1710378828.json similarity index 100% rename from experiments/raw_results/am_map_3_DOOM_id-Software_1710378828.json rename to raw_results/am_map_3_DOOM_id-Software_1710378828.json diff --git a/experiments/raw_results/am_map_4_DOOM_id-Software_1710378850.json b/raw_results/am_map_4_DOOM_id-Software_1710378850.json similarity index 100% rename from experiments/raw_results/am_map_4_DOOM_id-Software_1710378850.json rename to raw_results/am_map_4_DOOM_id-Software_1710378850.json diff --git a/experiments/raw_results/am_map_5_DOOM_id-Software_1710378865.json b/raw_results/am_map_5_DOOM_id-Software_1710378865.json similarity index 100% rename from experiments/raw_results/am_map_5_DOOM_id-Software_1710378865.json rename to raw_results/am_map_5_DOOM_id-Software_1710378865.json diff --git a/experiments/raw_results/am_map_6_DOOM_id-Software_1710378878.json b/raw_results/am_map_6_DOOM_id-Software_1710378878.json similarity index 100% rename from experiments/raw_results/am_map_6_DOOM_id-Software_1710378878.json rename to raw_results/am_map_6_DOOM_id-Software_1710378878.json diff --git a/experiments/raw_results/g_game_1_DOOM_id-Software_1710378888.json b/raw_results/g_game_1_DOOM_id-Software_1710378888.json similarity index 100% rename from experiments/raw_results/g_game_1_DOOM_id-Software_1710378888.json rename to raw_results/g_game_1_DOOM_id-Software_1710378888.json diff --git a/experiments/raw_results/g_game_2_DOOM_id-Software_1710378896.json b/raw_results/g_game_2_DOOM_id-Software_1710378896.json similarity index 100% rename from experiments/raw_results/g_game_2_DOOM_id-Software_1710378896.json rename to raw_results/g_game_2_DOOM_id-Software_1710378896.json diff --git a/experiments/raw_results/g_game_3_DOOM_id-Software_1710378904.json b/raw_results/g_game_3_DOOM_id-Software_1710378904.json similarity index 100% rename from experiments/raw_results/g_game_3_DOOM_id-Software_1710378904.json rename to raw_results/g_game_3_DOOM_id-Software_1710378904.json diff --git a/experiments/raw_results/i_video_1_DOOM_id-Software_1710378914.json b/raw_results/i_video_1_DOOM_id-Software_1710378914.json similarity index 100% rename from experiments/raw_results/i_video_1_DOOM_id-Software_1710378914.json rename to raw_results/i_video_1_DOOM_id-Software_1710378914.json diff --git a/experiments/raw_results/i_video_2_DOOM_id-Software_1710378934.json b/raw_results/i_video_2_DOOM_id-Software_1710378934.json similarity index 100% rename from experiments/raw_results/i_video_2_DOOM_id-Software_1710378934.json rename to raw_results/i_video_2_DOOM_id-Software_1710378934.json diff --git a/experiments/raw_results/i_video_3_DOOM_id-Software_1710378939.json b/raw_results/i_video_3_DOOM_id-Software_1710378939.json similarity index 100% rename from experiments/raw_results/i_video_3_DOOM_id-Software_1710378939.json rename to raw_results/i_video_3_DOOM_id-Software_1710378939.json diff --git a/experiments/requirements.txt b/requirements.txt similarity index 100% rename from experiments/requirements.txt rename to requirements.txt diff --git a/experiments/antlr/Declaration.g4 b/src/antlr/Declaration.g4 similarity index 100% rename from experiments/antlr/Declaration.g4 rename to src/antlr/Declaration.g4 diff --git a/experiments/declaration.py b/src/declaration.py similarity index 100% rename from experiments/declaration.py rename to src/declaration.py diff --git a/experiments/declparser/Declaration.interp b/src/declparser/Declaration.interp similarity index 100% rename from experiments/declparser/Declaration.interp rename to src/declparser/Declaration.interp diff --git a/experiments/declparser/Declaration.tokens b/src/declparser/Declaration.tokens similarity index 100% rename from experiments/declparser/Declaration.tokens rename to src/declparser/Declaration.tokens diff --git a/experiments/declparser/DeclarationLexer.interp b/src/declparser/DeclarationLexer.interp similarity index 100% rename from experiments/declparser/DeclarationLexer.interp rename to src/declparser/DeclarationLexer.interp diff --git a/experiments/declparser/DeclarationLexer.py b/src/declparser/DeclarationLexer.py similarity index 98% rename from experiments/declparser/DeclarationLexer.py rename to src/declparser/DeclarationLexer.py index 53efaa7..43e866d 100644 --- a/experiments/declparser/DeclarationLexer.py +++ b/src/declparser/DeclarationLexer.py @@ -1,4 +1,4 @@ -# Generated from ./antlr/Declaration.g4 by ANTLR 4.13.1 +# Generated from ./src/antlr/Declaration.g4 by ANTLR 4.13.1 from antlr4 import * from io import StringIO import sys diff --git a/experiments/declparser/DeclarationLexer.tokens b/src/declparser/DeclarationLexer.tokens similarity index 100% rename from experiments/declparser/DeclarationLexer.tokens rename to src/declparser/DeclarationLexer.tokens diff --git a/experiments/declparser/DeclarationListener.py b/src/declparser/DeclarationListener.py similarity index 96% rename from experiments/declparser/DeclarationListener.py rename to src/declparser/DeclarationListener.py index cfd30ef..36b642e 100644 --- a/experiments/declparser/DeclarationListener.py +++ b/src/declparser/DeclarationListener.py @@ -1,4 +1,4 @@ -# Generated from ./antlr/Declaration.g4 by ANTLR 4.13.1 +# Generated from ./src/antlr/Declaration.g4 by ANTLR 4.13.1 from antlr4 import * if "." in __name__: from .DeclarationParser import DeclarationParser diff --git a/experiments/declparser/DeclarationParser.py b/src/declparser/DeclarationParser.py similarity index 99% rename from experiments/declparser/DeclarationParser.py rename to src/declparser/DeclarationParser.py index f015eeb..f313b8e 100644 --- a/experiments/declparser/DeclarationParser.py +++ b/src/declparser/DeclarationParser.py @@ -1,4 +1,4 @@ -# Generated from ./antlr/Declaration.g4 by ANTLR 4.13.1 +# Generated from ./src/antlr/Declaration.g4 by ANTLR 4.13.1 # encoding: utf-8 from antlr4 import * from io import StringIO diff --git a/src/decls.txt b/src/decls.txt new file mode 100644 index 0000000..5ea2071 --- /dev/null +++ b/src/decls.txt @@ -0,0 +1,50 @@ +var a : int +var b : int* +var c : int** +var d : int[ ] +var e : int [][] +var f : int* [] +var g : int* * [] +var h : int*[][] +var i : int** [] + +struct A : + +struct B : + memb a : int + memb b : int* + +var j : A + +struct C : + memb a : A + memb b : B + memb c : int + memb d : int * + memb e: int ** + memb f : int [] + memb g : int [][] + memb h : int * [] + memb i : int **[] + memb j : int *[][] + memb k : int **[][] + +alias int_t : int + +alias C_t : C + +alias void_ptr : void* + +alias PtrArr : int **[][] + +struct D : + memb a : C_t + memb b : int_t + +enum E : e1, e2, e3 + +func f1 : void + +func f2 : void * -> UNRESOLVED, int*[], C_t, int_t + +func f3 : UNRESOLVED -> A[], B**, C_t***, int_t \ No newline at end of file diff --git a/experiments/error.py b/src/error.py similarity index 100% rename from experiments/error.py rename to src/error.py diff --git a/experiments/evaluation.py b/src/evaluation.py similarity index 100% rename from experiments/evaluation.py rename to src/evaluation.py diff --git a/experiments/experiments.py b/src/experiments.py similarity index 56% rename from experiments/experiments.py rename to src/experiments.py index 331dd02..22959d2 100644 --- a/experiments/experiments.py +++ b/src/experiments.py @@ -1,61 +1,70 @@ import os import json -import subprocess -import re -import time from datetime import datetime from openai import OpenAI from dotenv import load_dotenv from error import extract_errors, parse_errors, get_errors_as_str -from utils import has_extension, get_last_path_component, get_files_from_path, remove_first_path_component +from utils import has_extension, get_last_path_component, get_files_from_path, get_directories_from_path, remove_first_path_component from declaration import DeclParser from evaluation import evaluate_experiment_result - -CODE_TAG = "" -ERRORS_TAG = "" +from logger import logger +from llm import build_prompt, prompt_llm PROJECT_METADATA = "project.json" JSON_EXTENSION = ".json" -load_dotenv() - -class Paths: - def __init__(self, experiments_path, metadata_path, snippets_path, prompts_path, raw_results_path): +class Workdir: + def __init__(self, workdir, experiments_path, prompts_path, raw_results_path): + self.workdir = workdir self.experiments_path = experiments_path - self.metadata_path = metadata_path - self.snippets_path = snippets_path self.prompts_path = prompts_path self.raw_results_path = raw_results_path -def get_projects(root_path="."): +load_dotenv() + + +def get_projects(experiments_path): projects = {} - for project_path, _, filenames in os.walk(root_path): - if PROJECT_METADATA not in filenames: - continue + dirs = get_directories_from_path(experiments_path) + + for project_dirname in dirs: + project_path = os.path.join(experiments_path, project_dirname) - print(f"Found project in {project_path}") - project_path_id = remove_first_path_component(project_path) - project_metadata_path = os.path.join(project_path, PROJECT_METADATA) + if not os.path.exists(project_metadata_path): + continue + try: with open(project_metadata_path, "r") as project_metadata_file: project_metadata = json.load(project_metadata_file) project_metadata_file.close() except Exception: - print(f"Couldn't open project '{project_path_id}' metadata") + logger.error(f"Couldn't open project '{project_dirname}' metadata") return None + # TODO validate project metadata + project_name = project_metadata["project"] + org_name = project_metadata["org"] + + logger.info(f"Found project {project_name} from organization {org_name}") + experiments = {} - for filename in filenames: + project_experiments_path = os.path.join(project_path, "metadata") + + experiments_filenames = get_files_from_path(project_experiments_path) + + for filename in experiments_filenames: if not has_extension(filename, JSON_EXTENSION) or filename == PROJECT_METADATA: continue + + logger.debug(f"Found experiment '{filename}'") - experiment_metadata_path = os.path.join(project_path, filename) + experiment_metadata_path = os.path.join(project_experiments_path, filename) experiment_name = filename.split(JSON_EXTENSION)[0] @@ -65,76 +74,74 @@ def get_projects(root_path="."): experiments[experiment_name] = experiment_metadata experiment_metadata_file.close() except Exception: - print(f"Couldn't open experiment '{experiment_name}' metadata from project '{project_dirname}'") + logger.error(f"Couldn't open experiment '{experiment_name}' metadata from project '{project_dirname}'") return None - - projects[project_path_id] = { + + projects[project_dirname] = { "metadata": project_metadata, "experiments": experiments } - + return projects -def get_directory_path(experiments_path, dirname): - directory_path = os.path.join(experiments_path, dirname) +def get_directory_path(workdir, dirname): + directory_path = os.path.join(workdir, dirname) if os.path.exists(directory_path): if os.path.isfile(directory_path): - print(f"Found a {dirname} path but it is a file, not a directory") + logger.error(f"Found a '{dirname}' path but it is a file, not a directory. Aborting..") return None - print(f"Found {dirname} directory") + logger.info(f"Found {dirname} directory") else: - print(f"{dirname} directory does not exist. Creating one") + logger.info(f"'{dirname}' directory does not exist. Creating one") os.mkdir(directory_path) return directory_path +def get_project_id(project_name, org_name): + return f"{project_name.lower()}_{org_name.lower()}" + # Selections -def select_experiments_path(): - experiments_path = input("Enter experiments path (press enter for default path \"./\"): ") +def select_experiment_workdir(): + workdir = input("Enter experiment working directory (press enter for default path \"./\"): ") - experiments_path = "./" if experiments_path == "" else experiments_path + workdir = "./" if workdir == "" else workdir - if not os.path.exists(experiments_path): - print("Provided experiments path does not exist") + if not os.path.exists(workdir): + logger.error("Provided experiments path does not exist") return None - return experiments_path + logger.info(f"Selected experiment working directory: {workdir}") -def select_prompt_template(prompts_path): - prompts = get_files_from_path(prompts_path) - prompt_str = ','.join(prompts) + experiments_path = get_directory_path(workdir, "experiments") - while True: - prompt_template_filename = input(f"Select prompt template from the list ({prompt_str}): ") + if experiments_path is None: return None - if not prompt_template_filename in prompts: - print(f"Invalid prompt template '{prompt_template_filename}'. Try again") - else: - break - - prompt_template_path = os.path.join(prompts_path, prompt_template_filename) + prompts_path = get_directory_path(workdir, "prompts") - with open(prompt_template_path, 'r') as file: - prompt_template = file.read() + if prompts_path is None: return None - return prompt_template, prompt_template_filename + raw_results_path = get_directory_path(workdir, "raw_results") + + if raw_results_path is None: return None + + return Workdir(workdir, experiments_path, prompts_path, raw_results_path) def select_project(projects): - project_path_ids = projects.keys() - project_path_ids_str = ', '.join(project_path_ids) + project_ids = projects.keys() + project_ids_str = ', '.join(project_ids) while True: - path_id = input(f"Select project from the list ({project_path_ids_str}): ") + project_id = input(f"Select project from the list ({project_ids_str}): ") - if not path_id in project_path_ids: - print(f"Invalid project '{path_id}'. Try again") + if not project_id in project_ids: + print(f"Invalid project id '{project_id}'. Try again") else: break - return projects[path_id] + return project_id, projects[project_id] def select_experiment(project): experiment_filenames = project["experiments"].keys() @@ -150,22 +157,41 @@ def select_experiment(project): return project["experiments"][filename] -# Handlers -def create_project_metadata(paths: Paths): - project_path = input("Enter experiment project path (relative to metadata path): ") +def select_prompt_template(prompts_path): + prompts = get_files_from_path(prompts_path) - project_path = os.path.join(paths.metadata_path, project_path) + prompt_str = ','.join(prompts) - project_metadata_path = os.path.join(project_path, "project.json") + while True: + prompt_template_filename = input(f"Select prompt template from the list ({prompt_str}): ") + + if not prompt_template_filename in prompts: + print(f"Invalid prompt template '{prompt_template_filename}'. Try again") + else: + break + + prompt_template_path = os.path.join(prompts_path, prompt_template_filename) + with open(prompt_template_path, 'r') as file: + prompt_template = file.read() + + return prompt_template, prompt_template_filename + +# Handlers +def create_project_metadata(workdir: Workdir): project_metadata = { "org": input("Enter project organization: "), "project": input("Enter project name: "), "repository": input("Enter project repository: "), "license": input("Enter project license: "), - "snippets": input("Enter project snippets path (relative to snippets directory): "), } + project_id = get_project_id(project_metadata["project"], project_metadata["org"]) + + project_path = os.path.join(workdir.experiments_path, project_id) + + project_metadata_path = os.path.join(project_path, "project.json") + if not os.path.exists(project_path): os.mkdir(project_path) @@ -180,6 +206,7 @@ def create_project_metadata(paths: Paths): break if option == "n": + logger.info("Aborting creation..") return "" try: @@ -187,22 +214,31 @@ def create_project_metadata(paths: Paths): project_metadata_file.write(json.dumps(project_metadata, indent=4)) project_metadata_file.close() except Exception: - print("Couldn't create project metadata file.") + logger.error("Couldn't create project metadata file.") return None + + logger.info("Project metadata created correctly") + +def create_experiment_metadata(workdir: Workdir): + # TODO select project from list + org = input("Enter project organization: ") + + project = input("Enter project name: ") -def create_experiment_metadata(paths: Paths): - project_path = input("Enter experiment project path (relative to metadata path): ") + project_id = get_project_id(project, org) - project_path = os.path.join(paths.metadata_path, project_path) + project_path = os.path.join(workdir.experiments_path, project_id) project_metadata_path = os.path.join(project_path, "project.json") + logger.debug(project_metadata_path) + if not os.path.exists(project_path): - print("Project path does not exist. Aborting..") + logger.error("Project path does not exist. Aborting..") return None if not (os.path.exists(project_metadata_path) and os.path.isfile(project_metadata_path)): - print("Project metadata does not exist. Aborting..") + logger.error("Project metadata does not exist. Aborting..") return None try: @@ -210,12 +246,10 @@ def create_experiment_metadata(paths: Paths): project_metadata = json.load(metadata_file) metadata_file.close() except Exception: - print("Couldn't open project metadata") + logger.error("Couldn't open project metadata") return None - - project = project_metadata["project"] - org = project_metadata["org"] - print(f"Using project {project} from organization {org}") + + logger.info(f"Using project {project} from organization {org}") experiment = {} @@ -232,59 +266,62 @@ def create_experiment_metadata(paths: Paths): if experiment["language"] in ["c", "cpp"]: break - print("Invalid language. Please enter a valid language.") + logger.error("Invalid language. Please enter a valid language.") snippet_name = experiment["snippet"] - project_snippets_path = project_metadata["snippets"] + project_snippets_path = os.path.join(project_path, "snippets") + #declarations_filepath = input("Enter the path to the LLVM declarations file: ") declarations_filepath = "./declarations.txt" - errors_str = extract_errors(snippet_name, os.path.join(paths.snippets_path, project_snippets_path)) - #print(errors_str) + errors_str = extract_errors(snippet_name, project_snippets_path) + #logger.debug(errors_str) if not errors_str: return experiment["errors"] = parse_errors(errors_str) + ''' try: with open(declarations_filepath, 'r') as file: declarations_file = file.read() - #print(declarations_file) + #logger.debug(declarations_file) tree, _, _ = DeclParser.parse(declarations_file) experiment["declarations"] = DeclParser.get_declarations_as_obj(tree) file.close() except FileNotFoundError: - print("Declarations file does not exist.") + logger.error("Declarations file does not exist.") return + ''' experiment_name = experiment["name"] - experiment_metadata_path = os.path.join(project_path, f"{experiment_name}.json") + experiment_metadata_path = os.path.join(project_path, "metadata", f"{experiment_name}.json") try: with open(experiment_metadata_path, 'w') as experiment_file: experiment_file.write(json.dumps(experiment, indent=4)) experiment_file.close() except Exception: - print("Couldn't create experiment file") + logger.error("Couldn't create experiment file") return - print("Experiment created correctly") - #print(json.dumps(experiment, indent=4)) + logger.info("Experiment created correctly") + #logger.debug(json.dumps(experiment, indent=4)) -def run_experiments(paths: Paths): - projects = get_projects(paths.metadata_path) +def run_experiments(workdir: Workdir): + projects = get_projects(workdir.experiments_path) if projects == None: return while True: - prompt_template, prompt_template_filename = select_prompt_template(paths.prompts_path) + prompt_template, prompt_template_filename = select_prompt_template(workdir.prompts_path) if prompt_template == None: return - print("Select testing mode:\n [0] Specific experiment from a project\n [1] All experiments from a project\n [2] All projects") + print("\nSelect testing mode:\n [0] Specific experiment from a project\n [1] All experiments from a project\n [2] All projects") while True: testing_mode = input(">> ") @@ -294,11 +331,11 @@ def run_experiments(paths: Paths): break if testing_mode == "0": - test_experiment(projects, prompt_template, prompt_template_filename, paths.snippets_path, paths.raw_results_path) + test_experiment(projects, prompt_template, prompt_template_filename, workdir) elif testing_mode == "1": - test_project(projects, prompt_template, prompt_template_filename, paths.snippets_path, paths.raw_results_path) + test_project(projects, prompt_template, prompt_template_filename, workdir) elif testing_mode == "2": - test_all_projects(projects, prompt_template, prompt_template_filename, paths.snippets_path, paths.raw_results_path) + test_all_projects(projects, prompt_template, prompt_template_filename, workdir) while True: continue_exec = input("Continue experiments (y/N): ") @@ -310,101 +347,57 @@ def run_experiments(paths: Paths): if continue_exec.lower() in ["n", "no"]: break -def evaluate_results(project_id, paths: Paths): - print("Evaluating experiment results..") - - raw_results_filenames = get_files_from_path(paths.raw_results_path) - - projects = get_projects(paths.metadata_path) - project = projects[project_id] +def evaluate_results(workdir: Workdir): + logger.info("Evaluating experiment results..") + + raw_results_filenames = get_files_from_path(workdir.raw_results_path) + + projects = get_projects(workdir.experiments_path) for raw_result_filename in raw_results_filenames: - print(raw_result_filename) if not has_extension(raw_result_filename, JSON_EXTENSION): continue - raw_result_path = os.path.join(paths.raw_results_path, raw_result_filename) + logger.info(f"Evaluating result file '{raw_result_filename}'") + + raw_result_path = os.path.join(workdir.raw_results_path, raw_result_filename) try: with open(raw_result_path, 'r') as raw_result_file: raw_result = json.load(raw_result_file) - #print(raw_result) - #if not raw_result: - # print(f"Couldn't load result file '{raw_result_filename}'") + #logger.debug(raw_result) experiment_id = raw_result["experiment"] - experiment_snippet = project["experiments"][experiment_id]["snippet"] + project_name = raw_result["project"] + org_name = raw_result["org"] + + project_id = get_project_id(project_name, org_name) + + experiment_snippet = projects["experiments"][experiment_id]["snippet"] - with open(os.path.join(paths.snippets_path, project["metadata"]["snippets"], experiment_snippet)) as file: - code_snippet = file.read() + snippet_path = os.path.join(workdir.experiments_path, project_id, "snippets", experiment_snippet) + with open(snippet_path) as snippet_file: + code_snippet = snippet_file.read() + logger.debug(code_snippet) + snippet_file.close() llm_answer = raw_result["llm_answer"] - #print(llm_answer) + #logger.debug(llm_answer) evaluate_experiment_result(llm_answer, code_snippet) raw_result_file.close() except Exception as e: - print(f"Couldn't open result file '{raw_result_filename}'") - raise e - -# LLM -def build_prompt(prompt_template, snippet, errors): - code_start_index = prompt_template.find(CODE_TAG) - code_end_index = code_start_index + len(CODE_TAG) - prompt = prompt_template[0:code_start_index] + snippet + prompt_template[code_end_index:] - - errors_start_index = prompt.find(ERRORS_TAG) - prompt = prompt[:errors_start_index] + errors - - return prompt - -def prompt_llm(prompt): - #client = OpenAI() - chat_completion = { - "created": 121214148798, - "model": "3eheheh", - "usage": { - "prompt_tokens": 21, - "completion_tokens": 2, - "total_tokens": 23 - }, - "choices": [ - { - "message": { - "content": "hi" - } - } - ] - } - - #input() - ''' - start_time = time.time() - chat_completion = client.chat.completions.create( - messages=[ - { - "role": "user", - "content": prompt - } - ], - model="gpt-4" - ) - - gen_time = time.time() - start_time - print(f"LLM answered in {gen_time}") - ''' - gen_time = 2 - print(prompt) - print(chat_completion) + #logger.error(f"Couldn't open result file '{raw_result_filename}'") + #raise e + print(e) - return chat_completion, gen_time -def save_experiment_result(chat_completion, gen_time, raw_results_path, experiment_name, +def save_experiment_result(workdir: Workdir, chat_completion, gen_time, experiment_name, project_name, org_name, prompt_name): ''' @@ -435,12 +428,12 @@ def save_experiment_result(chat_completion, gen_time, raw_results_path, experime "prompt_tokens": chat_completion["usage"]["prompt_tokens"], "answer_tokens": chat_completion["usage"]["completion_tokens"], "total_tokens": chat_completion["usage"]["total_tokens"], + "prompt_gen_time": gen_time, "llm_answer": chat_completion["choices"][0]["message"]["content"] } - created_unix = results["created_unix"] - experiment_results_path = os.path.join(raw_results_path, + experiment_results_path = os.path.join(workdir.raw_results_path, f"{experiment_name}_{project_name}_{org_name}_{created_unix}.json") try: @@ -448,18 +441,19 @@ def save_experiment_result(chat_completion, gen_time, raw_results_path, experime results_file.write(json.dumps(results, indent=4)) results_file.close() except Exception: - print("Couldn't create results file") + logger.error("Couldn't create results file") return - print("Experiment results created correctly") + logger.info("Experiment results created correctly") #Tests -def test_experiment(projects, prompt_template, prompt_template_filename, snippets_path, raw_results_path): - project = select_project(projects) +def test_experiment(projects, prompt_template, prompt_template_filename, workdir: Workdir): + project_id, project = select_project(projects) project_name = project["metadata"]["project"] project_org = project["metadata"]["org"] - project_snippets_path = project["metadata"]["snippets"] + + project_snippets_path = os.path.join(workdir.experiments_path, project_id, "snippets") experiment = select_experiment(project) @@ -467,116 +461,97 @@ def test_experiment(projects, prompt_template, prompt_template_filename, snippet experiment_snippet = experiment["snippet"] experiment_function_name = experiment["function_name"] - print(f"Testing experiment '{experiment_name}': snippet '{experiment_snippet}', function name '{experiment_function_name}' from project '{project_name}' and organization '{project_org}'") + logger.info(f"Testing experiment '{experiment_name}': snippet '{experiment_snippet}', function name '{experiment_function_name}' from project '{project_name}' and organization '{project_org}'") - with open(os.path.join(snippets_path, project_snippets_path, experiment_snippet)) as file: + with open(os.path.join(project_snippets_path, experiment_snippet)) as file: snippet = file.read() errors = get_errors_as_str(experiment) prompt = build_prompt(prompt_template, snippet, errors) - chat_completion, gen_time = prompt_llm(prompt) + logger.debug(prompt) - save_experiment_result(chat_completion, gen_time, raw_results_path, experiment_name, project_name, project_org, prompt_template_filename) + chat_completion, gen_time = prompt_llm(prompt) - #print(prompt) + save_experiment_result(workdir, chat_completion, gen_time, experiment_name, project_name, project_org, prompt_template_filename) -def test_project(projects, prompt_template, prompt_template_filename, snippets_path, raw_results_path): - project = select_project(projects) +def test_project(projects, prompt_template, prompt_template_filename, workdir: Workdir): + project_id, project = select_project(projects) project_name = project["metadata"]["project"] project_org = project["metadata"]["org"] - project_snippets_path = project["metadata"]["snippets"] - print(f"Testing project '{project_name}' from organization '{project_org}'") + project_snippets_path = os.path.join(workdir.experiments_path, project_id, "snippets") + + logger.info(f"Testing project '{project_name}' from organization '{project_org}'") for experiment in project["experiments"].values(): experiment_name = experiment["name"] experiment_snippet = experiment["snippet"] experiment_function_name = experiment["function_name"] - print(f"Experiment '{experiment_name}': snippet '{experiment_snippet}', function name '{experiment_function_name}'") + logger.info(f"Experiment '{experiment_name}': snippet '{experiment_snippet}', function name '{experiment_function_name}'") - with open(os.path.join(snippets_path, project_snippets_path, experiment_snippet)) as file: + with open(os.path.join(project_snippets_path, experiment_snippet)) as file: snippet = file.read() errors = get_errors_as_str(experiment) prompt = build_prompt(prompt_template, snippet, errors) - chat_completion, gen_time = prompt_llm(prompt) + logger.debug(prompt) - save_experiment_result(chat_completion, gen_time, raw_results_path, experiment_name, project_name, project_org, prompt_template_filename) + chat_completion, gen_time = prompt_llm(prompt) - #print(prompt) + save_experiment_result(workdir, chat_completion, gen_time, experiment_name, project_name, project_org, prompt_template_filename) -def test_all_projects(projects, prompt_template, prompt_template_filename, snippets_path, raw_results_path): - for dirname, project in projects.items(): - #print(json.dumps(project, indent=2)) +def test_all_projects(projects, prompt_template, prompt_template_filename, workdir: Workdir): + for project_id, project in projects.items(): + #logger.debug(json.dumps(project, indent=2)) project_name = project["metadata"]["project"] project_org = project["metadata"]["org"] - project_snippets_path = project["metadata"]["snippets"] - print(f"Testing project '{project_name}' from organization '{project_org}'") + project_snippets_path = os.path.join(workdir.experiments_path, project_id, "snippets") + + logger.info(f"Testing project '{project_name}' from organization '{project_org}'") for experiment in project["experiments"].values(): experiment_name = experiment["name"] experiment_snippet = experiment["snippet"] experiment_function_name = experiment["function_name"] - print(f"Experiment '{experiment_name}': snippet '{experiment_snippet}', function name '{experiment_function_name}'") + logger.info(f"Experiment '{experiment_name}': snippet '{experiment_snippet}', function name '{experiment_function_name}'") - with open(os.path.join(snippets_path, project_snippets_path, experiment_snippet)) as file: + with open(os.path.join(project_snippets_path, experiment_snippet)) as file: snippet = file.read() errors = get_errors_as_str(experiment) prompt = build_prompt(prompt_template, snippet, errors) + logger.debug(prompt) + chat_completion, gen_time = prompt_llm(prompt) - save_experiment_result(chat_completion, gen_time, raw_results_path, experiment_name, project_name, project_org, prompt_template_filename) - #print(prompt) - + save_experiment_result(workdir, chat_completion, gen_time, experiment_name, project_name, project_org, prompt_template_filename) def main(): - experiments_path = select_experiments_path() + workdir = select_experiment_workdir() - if experiments_path is None: + if workdir is None: return - print(f"Selected experiments path: {experiments_path}") - - metadata_path = get_directory_path(experiments_path, "metadata") - - if metadata_path is None: return - - snippets_path = get_directory_path(experiments_path, "snippets") - - if snippets_path is None: return - - prompts_path = get_directory_path(experiments_path, "prompts") - - if prompts_path is None: return - - raw_results_path = get_directory_path(experiments_path, "raw_results") - - if raw_results_path is None: return - - paths = Paths(experiments_path, metadata_path, snippets_path, prompts_path, raw_results_path) - while True: print( - """Select option: + """\nSelect option: [0] Create new project metadata [1] Create new code snippet metadata [2] Run experiments - [3] Analyse experiment results + [3] Evaluate experiment results [4] Exit """ ) - #print("Select option:\n [1] Create new code snippet metadata\n [2] Run experiments\n [3] Analyse experiments\n [4] Exit\n") while True: option = input(">> ") @@ -586,13 +561,13 @@ def main(): break if option == "0": - create_project_metadata(paths) + create_project_metadata(workdir) elif option == "1": - create_experiment_metadata(paths) + create_experiment_metadata(workdir) elif option == "2": - run_experiments(paths) + run_experiments(workdir) elif option == "3": - evaluate_results("doom", paths) + evaluate_results(workdir) elif option == "4": break diff --git a/src/llm.py b/src/llm.py new file mode 100644 index 0000000..170fcae --- /dev/null +++ b/src/llm.py @@ -0,0 +1,57 @@ +import time +from logger import logger + +from openai import OpenAI + +CODE_FILL_TAG = "" +ERRORS_FILL_TAG = "" + +def build_prompt(prompt_template, snippet, errors): + code_start_index = prompt_template.find(CODE_FILL_TAG) + code_end_index = code_start_index + len(CODE_FILL_TAG) + prompt = prompt_template[0:code_start_index] + snippet + prompt_template[code_end_index:] + + errors_start_index = prompt.find(ERRORS_FILL_TAG) + prompt = prompt[:errors_start_index] + errors + + return prompt + +def prompt_llm(prompt): + #client = OpenAI() + chat_completion = { + "created": 121214148798, + "model": "3eheheh", + "usage": { + "prompt_tokens": 21, + "completion_tokens": 2, + "total_tokens": 23 + }, + "choices": [ + { + "message": { + "content": "hi" + } + } + ] + } + + #input() + ''' + start_time = time.time() + chat_completion = client.chat.completions.create( + messages=[ + { + "role": "user", + "content": prompt + } + ], + model="gpt-4" + ) + + gen_time = time.time() - start_time + logger.info(f"LLM answered in {gen_time}") + ''' + gen_time = 2 + logger.debug(chat_completion) + + return chat_completion, gen_time diff --git a/src/logger.py b/src/logger.py new file mode 100644 index 0000000..2c62781 --- /dev/null +++ b/src/logger.py @@ -0,0 +1,22 @@ +import logging + +def build_logger(): + logger = logging.getLogger("llm_code_patcher") + + console_handler = logging.StreamHandler() + file_handler = logging.FileHandler("llm_code_patcher.log") + + console_formatter = logging.Formatter("[%(levelname)s] %(message)s") + file_formatter = logging.Formatter("%(asctime)s - [%(levelname)s] - %(message)s") + + console_handler.setFormatter(console_formatter) + file_handler.setFormatter(file_formatter) + + logger.addHandler(console_handler) + logger.addHandler(file_handler) + + logger.setLevel(logging.DEBUG) + + return logger + +logger = build_logger() \ No newline at end of file diff --git a/experiments/utils.py b/src/utils.py similarity index 90% rename from experiments/utils.py rename to src/utils.py index e02ffb3..43bbaac 100644 --- a/experiments/utils.py +++ b/src/utils.py @@ -27,6 +27,9 @@ def get_last_path_component(path): def get_files_from_path(directory_path): return [f for f in os.listdir(directory_path) if os.path.isfile(os.path.join(directory_path, f))] +def get_directories_from_path(directory_path): + return [f for f in os.listdir(directory_path) if os.path.isdir(os.path.join(directory_path, f))] + # Used to: # 1. retrieve compiler errors # 2. compile the patched code