]> khamidou — GitHub mirrors - mojo.git/commitdiff
added a string type
authorkarim hamidou <khamidou@gmail.com>
Fri, 26 Apr 2013 09:02:07 +0000 (11:02 +0200)
committerkarim hamidou <khamidou@gmail.com>
Fri, 26 Apr 2013 09:02:07 +0000 (11:02 +0200)
Makefile
mstrings.c
mstrings.h
object.c
object.h

index 74cce364da48d69d0e9ff367321065c9f03f6c5f..9cb32f92c561694ea4e23e431d29b7812039361d 100644 (file)
--- 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)
index a1bdeea213317eae3284f710f82dc2a6329a8f80..bc673493b81ba14245ea8a8881de222a7b48d648 100644 (file)
@@ -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 = "+";
index a9caf485e9a563157fc2fe685264db4554f364bc..a24a9e563974d45a0d64d0a45469b49abd56b9e7 100644 (file)
@@ -2,6 +2,10 @@
 #ifndef __MOJO_STRINGS__
 #define __MOJO_STRINGS__
 
+struct mojo_string {
+    int length;
+    char *contents;
+};
 
 
 #endif
index 24416a6d2f276eee2bb05192d7708760d11f2602..d98a0d7a488a217519c75675f821196a2ae2b941 100644 (file)
--- 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);
 }
 
index eef7eb71ffed1eb9a5f33ff29d85e847ebabed4f..7d897f2d323e0e56d44a3746371c508d82f9d320 100644 (file)
--- 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 */