From 59533ff973d9b84d7136a527722ec1eeebebaae1 Mon Sep 17 00:00:00 2001 From: Isak Simonsson Date: Sat, 2 Aug 2025 16:27:48 +0200 Subject: [PATCH 1/3] Refactor: Eliminate repeated strcmp calls in builtin_var - Seperate handling of 'def' and '=' into distinct blocks. - Changes applied consistently across all C source files containing builtin_var. --- src/conditionals.c | 11 ++++++++--- src/functions.c | 18 +++++++++--------- src/hand_rolled_parser.c | 11 ++++++++--- src/strings.c | 11 ++++++++--- 4 files changed, 33 insertions(+), 18 deletions(-) diff --git a/src/conditionals.c b/src/conditionals.c index 698f642..640c1e1 100644 --- a/src/conditionals.c +++ b/src/conditionals.c @@ -513,9 +513,14 @@ lval* builtin_var(lenv* e, lval* a, char* func) { "Got %i, Expected %i.", func, syms->count, a->count-1); - for (int i = 0; i < syms->count; i++) { - if (strcmp(func, "def") == 0) { lenv_def(e, syms->cell[i], a->cell[i+1]); } - if (strcmp(func, "=") == 0) { lenv_put(e, syms->cell[i], a->cell[i+1]); } + if (strcmp(func, "def") == 0) { + for (int i = 0; i < syms->count; i++) { + lenv_def(e, syms->cell[i], a->cell[i + 1]); + } + } else if (strcmp(func, "=") == 0) { + for (int i = 0; i < syms->count; i++) { + lenv_put(e, syms->cell[i], a->cell[i + 1]); + } } lval_del(a); diff --git a/src/functions.c b/src/functions.c index b2dffcf..cc6ba55 100644 --- a/src/functions.c +++ b/src/functions.c @@ -489,15 +489,15 @@ lval* builtin_var(lenv* e, lval* a, char* func) { "Function '%s' passed too many arguments for symbols. " "Got %i, Expected %i.", func, syms->count, a->count-1); - for (int i = 0; i < syms->count; i++) { - /* If 'def' define in globally. If 'put' define in locally */ - if (strcmp(func, "def") == 0) { - lenv_def(e, syms->cell[i], a->cell[i+1]); - } - - if (strcmp(func, "=") == 0) { - lenv_put(e, syms->cell[i], a->cell[i+1]); - } + /* If 'def' define in globally. If 'put' define in locally */ + if (strcmp(func, "def") == 0) { + for (int i = 0; i < syms->count; i++) { + lenv_def(e, syms->cell[i], a->cell[i + 1]); + } + } else if (strcmp(func, "=") == 0) { + for (int i = 0; i < syms->count; i++) { + lenv_put(e, syms->cell[i], a->cell[i + 1]); + } } lval_del(a); diff --git a/src/hand_rolled_parser.c b/src/hand_rolled_parser.c index c923fd2..806951f 100644 --- a/src/hand_rolled_parser.c +++ b/src/hand_rolled_parser.c @@ -578,9 +578,14 @@ lval* builtin_var(lenv* e, lval* a, char* func) { "Got %i, Expected %i.", func, syms->count, a->count-1); - for (int i = 0; i < syms->count; i++) { - if (strcmp(func, "def") == 0) { lenv_def(e, syms->cell[i], a->cell[i+1]); } - if (strcmp(func, "=") == 0) { lenv_put(e, syms->cell[i], a->cell[i+1]); } + if (strcmp(func, "def") == 0) { + for (int i = 0; i < syms->count; i++) { + lenv_def(e, syms->cell[i], a->cell[i + 1]); + } + } else if (strcmp(func, "=") == 0) { + for (int i = 0; i < syms->count; i++) { + lenv_put(e, syms->cell[i], a->cell[i + 1]); + } } lval_del(a); diff --git a/src/strings.c b/src/strings.c index 85945c7..82391d7 100644 --- a/src/strings.c +++ b/src/strings.c @@ -542,9 +542,14 @@ lval* builtin_var(lenv* e, lval* a, char* func) { "Got %i, Expected %i.", func, syms->count, a->count-1); - for (int i = 0; i < syms->count; i++) { - if (strcmp(func, "def") == 0) { lenv_def(e, syms->cell[i], a->cell[i+1]); } - if (strcmp(func, "=") == 0) { lenv_put(e, syms->cell[i], a->cell[i+1]); } + if (strcmp(func, "def") == 0) { + for (int i = 0; i < syms->count; i++) { + lenv_def(e, syms->cell[i], a->cell[i + 1]); + } + } else if (strcmp(func, "=") == 0) { + for (int i = 0; i < syms->count; i++) { + lenv_put(e, syms->cell[i], a->cell[i + 1]); + } } lval_del(a); From 5584a9a8c55134117bfe67c2576ebe4fed8f37c2 Mon Sep 17 00:00:00 2001 From: Isak Simonsson Date: Sat, 2 Aug 2025 16:33:26 +0200 Subject: [PATCH 2/3] docs: Update builtin_var in HTML to match refactored logic --- chapter12_functions.html | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/chapter12_functions.html b/chapter12_functions.html index 57be30f..321a855 100644 --- a/chapter12_functions.html +++ b/chapter12_functions.html @@ -286,15 +286,15 @@

Parent Environment


"Function '%s' passed too many arguments for symbols. " "Got %i, Expected %i.", func, syms->count, a->count-1); - for (int i = 0; i < syms->count; i++) { - /* If 'def' define in globally. If 'put' define in locally */ - if (strcmp(func, "def") == 0) { - lenv_def(e, syms->cell[i], a->cell[i+1]); - } - - if (strcmp(func, "=") == 0) { - lenv_put(e, syms->cell[i], a->cell[i+1]); - } + /* If 'def' define in globally. If 'put' define in locally */ + if (strcmp(func, "def") == 0) { + for (int i = 0; i < syms->count; i++) { + lenv_def(e, syms->cell[i], a->cell[i + 1]); + } + } else if (strcmp(func, "=") == 0) { + for (int i = 0; i < syms->count; i++) { + lenv_put(e, syms->cell[i], a->cell[i + 1]); + } } lval_del(a); From 8b744cb7dd6b888935d158613b5b985258671796 Mon Sep 17 00:00:00 2001 From: Isak Simonsson Date: Sat, 2 Aug 2025 16:41:57 +0200 Subject: [PATCH 3/3] docs: clarify comment explaining 'def' and '=' in builtin_var --- chapter12_functions.html | 2 +- src/functions.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/chapter12_functions.html b/chapter12_functions.html index 321a855..254a8f0 100644 --- a/chapter12_functions.html +++ b/chapter12_functions.html @@ -286,7 +286,7 @@

Parent Environment


"Function '%s' passed too many arguments for symbols. " "Got %i, Expected %i.", func, syms->count, a->count-1); - /* If 'def' define in globally. If 'put' define in locally */ + /* If 'def' define symbol globally. If 'put' define symbol locally */ if (strcmp(func, "def") == 0) { for (int i = 0; i < syms->count; i++) { lenv_def(e, syms->cell[i], a->cell[i + 1]); diff --git a/src/functions.c b/src/functions.c index cc6ba55..f1aecb2 100644 --- a/src/functions.c +++ b/src/functions.c @@ -489,7 +489,7 @@ lval* builtin_var(lenv* e, lval* a, char* func) { "Function '%s' passed too many arguments for symbols. " "Got %i, Expected %i.", func, syms->count, a->count-1); - /* If 'def' define in globally. If 'put' define in locally */ + /* If 'def' define symbol globally. If 'put' define symbol locally */ if (strcmp(func, "def") == 0) { for (int i = 0; i < syms->count; i++) { lenv_def(e, syms->cell[i], a->cell[i + 1]);