Skip to content

Commit c25301b

Browse files
committed
Wip: add prim procs
1 parent 3021500 commit c25301b

File tree

7 files changed

+75
-6
lines changed

7 files changed

+75
-6
lines changed

src/convert.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,4 @@ obj cnv_bitmap_string(struct outport *op, obj dat)
5959
sprintf(buff, "<bitmap %dx%d>", bmp->width, bmp->height);
6060
out_writes(op, buff);
6161
return void_o;
62-
}
62+
}

src/custom.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,17 @@ static obj add_extras(int ex, obj env)
371371
" (amb-loop))))",
372372
env);
373373
}
374+
if (ex >= 500) {
375+
define_variable(of_identifier("string-append"),
376+
of_function(string_append_p), env);
377+
define_variable(of_identifier("number->string"),
378+
of_function(number_to_string_p), env);
379+
define_variable(of_identifier("string->symbol"),
380+
of_function(string_to_symbol_p), env);
381+
define_variable(of_identifier("symbol->string"),
382+
of_function(symbol_to_string_p), env);
383+
}
384+
374385
return unspecified;
375386
}
376387

src/outport.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ int out_writes(struct outport *out, const char *str)
9898
return rc < 0 ? EOF : 0;
9999
}
100100

101-
char *out_tostring(struct outport *port)
101+
char *out_copystring(struct outport *port)
102102
{
103103
if (port == NULL) {
104104
eprintf(AREA, "tostring received a null outport.");
@@ -108,7 +108,7 @@ char *out_tostring(struct outport *port)
108108
eprintf(AREA, "cannot get string from a non-string outport");
109109
return NULL;
110110
}
111-
return sb_string(port->sb);
111+
return sb_copy(port->sb);
112112
}
113113

114114
struct outport *new_outport(void)

src/outport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ struct outport *openout_string(void);
1919

2020
int out_writec(struct outport *, char c);
2121
int out_writes(struct outport *, const char *s);
22-
char *out_tostring(struct outport *);
22+
char *out_copystring(struct outport *);
2323
int out_close(struct outport *);
2424

2525
#endif

src/primproc.c

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -748,10 +748,62 @@ obj is_symbol_p(obj args)
748748
return is_symbol(car(args)) ? true_o : false_o;
749749
}
750750

751+
#include "convert.h"
752+
#include "strbldr.h"
753+
754+
obj string_append_p(obj args)
755+
{
756+
struct strbldr *sb = new_strbldr();
757+
758+
for (; is_pair(args); args = cdr(args)) {
759+
obj arg = car(args);
760+
if (!is_string(arg))
761+
return error_argument_type(
762+
AREA, "append-string got non string");
763+
sb_adds(sb, to_string(arg));
764+
}
765+
obj str = of_string(sb_copy(sb));
766+
sb_free(&sb);
767+
return str;
768+
}
769+
770+
obj number_to_string_p(obj args)
771+
{
772+
obj chk;
773+
774+
if (is_err(chk = chkarity("number->string", 1, args)))
775+
return chk;
776+
struct outport *tmp = openout_string();
777+
cnv_number_string(tmp, car(args));
778+
obj str = of_string(out_copystring(tmp));
779+
out_close(tmp);
780+
return str;
781+
}
782+
783+
obj string_to_symbol_p(obj args)
784+
{
785+
obj chk;
786+
787+
if (is_err(chk = chkarity("string->symbol", 1, args)))
788+
return chk;
789+
obj r = of_identifier(to_string(car(args)));
790+
return r;
791+
}
792+
793+
obj symbol_to_string_p(obj args)
794+
{
795+
obj chk;
796+
797+
if (is_err(chk = chkarity("symbol->string", 1, args)))
798+
return chk;
799+
obj r = of_string(to_string(car(args)));
800+
return r;
801+
}
802+
751803
obj pcnt_ex(obj args)
752804
{
753805
(void)args;
754806
displaydat(of_string("Ignoring (%ex ...) - not first expresion."));
755807
newline(emptylst);
756808
return void_o;
757-
}
809+
}

src/primproc.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ obj is_number_p(obj);
5757
obj is_string_p(obj);
5858
obj is_symbol_p(obj);
5959

60+
obj string_append_p(obj);
61+
obj number_to_string_p(obj);
62+
obj string_to_symbol_p(obj);
63+
obj symbol_to_string_p(obj);
64+
6065
obj pcnt_ex(obj);
6166

6267
#endif

test/run

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ test_dir=$(dirname "${BASH_SOURCE[0]}")
44
export bin_dir=$test_dir/../bin
55
source_dir=$test_dir/code
66

7-
file=scratch.sicp
7+
# file=scratch.sicp
8+
file=ch5/ex5.50-cbf.sicp
89

910
echo running: $file
1011
$bin_dir/sicp $source_dir/$file

0 commit comments

Comments
 (0)