From 10f0e64684039ef0293f8273786c2bb0e5cd23a8 Mon Sep 17 00:00:00 2001 From: karim hamidou Date: Fri, 26 Apr 2013 11:02:07 +0200 Subject: [PATCH] added a string type --- Makefile | 2 +- mstrings.c | 22 +++++++++++++++++++++- mstrings.h | 4 ++++ object.c | 3 +-- object.h | 2 ++ 5 files changed, 29 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 74cce36..9cb32f9 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -CFLAGS=-c -g +CFLAGS=-c -g -Wparentheses OBJS=lex.yy.o object.o list.o error.o number.o main.o interp.o mojo: $(OBJS) diff --git a/mstrings.c b/mstrings.c index a1bdeea..bc67349 100644 --- a/mstrings.c +++ b/mstrings.c @@ -1,12 +1,32 @@ #include "mstrings.h" +struct mojo_string* dup_string(char *s) +{ + + struct mojo_string *str = zalloc(sizeof(struct mojo_list)); + int len = strlen(s); + str->contents = strdup(s); + str->length = len; + + return str; +} + +void free_string(struct mojo_string *s) +{ + if (s == NULL) + return; + + free(s->contents); + free(s); +} + void create_string_object(void) { /* init number object */ number_object = clone_object(base_object); number_object->type = T_STRING; number_object->name = "String"; - number_object->value.i_value = 0; + number_object->value.s_value = dup_string(""); struct Object *plus_met = clone_object(builtin_object); plus_met->name = "+"; diff --git a/mstrings.h b/mstrings.h index a9caf48..a24a9e5 100644 --- a/mstrings.h +++ b/mstrings.h @@ -2,6 +2,10 @@ #ifndef __MOJO_STRINGS__ #define __MOJO_STRINGS__ +struct mojo_string { + int length; + char *contents; +}; #endif diff --git a/object.c b/object.c index 24416a6..d98a0d7 100644 --- a/object.c +++ b/object.c @@ -76,7 +76,7 @@ struct Object* clone_object(struct Object *o) void free_object(struct Object *o) { - if (o == NULL) + if (o == NULL || o == nil_object || o == base_object || o == number_object) return; /* @@ -94,7 +94,6 @@ void free_object(struct Object *o) mojo_list_free(o->value.l_value); } - /* FIXME : free : the methods list, the methods and finally the object */ free(o); } diff --git a/object.h b/object.h index eef7eb7..7d897f2 100644 --- a/object.h +++ b/object.h @@ -39,7 +39,9 @@ struct Object { struct Object * (*c_method)(struct Object *parent, struct Object *arg1, struct Object *arg2, struct Object *arg3, struct Object *arg4); struct mojo_list *l_value; + struct mojo_string *s_value; struct Object *m_block; + struct Object *v_value; /* the value pointed to by a variable */ } value; struct Object *methods; /* A list object */ -- 2.43.0