LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-20-2011, 06:58 PM   #76
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454

Quote:
Originally Posted by MTK358 View Post
it still says "jmp_buf" everywhere.

I think I heard that jmp_buf is a typedef to an array or something?

Preprocessed source should show you this.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 02-20-2011, 07:12 PM   #77
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
This is a line output by the preprocessor:

Code:
        jmp_buf prev_jmpbuf = exception_jmpbuf;
 
Old 02-21-2011, 08:22 AM   #78
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
This is a line output by the preprocessor:

Code:
        jmp_buf prev_jmpbuf = exception_jmpbuf;
What - the preprocessor just "invents" jmp_buf entity ?
 
0 members found this post helpful.
Old 02-21-2011, 09:15 AM   #79
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by Sergei Steshenko View Post
What - the preprocessor just "invents" jmp_buf entity ?
Don't ask me.

That's copied and pasted from the output of "cpp ast.c".
 
Old 02-21-2011, 11:42 AM   #80
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Code:
~/tmp$ echo '#include <setjmp.h>' | cpp | grep jmp_buf
typedef long int __jmp_buf[8];
struct __jmp_buf_tag
    __jmp_buf __jmpbuf;
typedef struct __jmp_buf_tag jmp_buf[1];
extern int setjmp (jmp_buf __env) __attribute__ ((__nothrow__));
extern int __sigsetjmp (struct __jmp_buf_tag __env[1], int __savemask) __attribute__ ((__nothrow__));
extern int _setjmp (struct __jmp_buf_tag __env[1]) __attribute__ ((__nothrow__));
extern void longjmp (struct __jmp_buf_tag __env[1], int __val)
extern void _longjmp (struct __jmp_buf_tag __env[1], int __val)
typedef struct __jmp_buf_tag sigjmp_buf[1];
extern void siglongjmp (sigjmp_buf __env, int __val)
 
Old 02-21-2011, 05:31 PM   #81
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
Don't ask me.

That's copied and pasted from the output of "cpp ast.c".
So, what can you say regarding
http://www.linuxquestions.org/questi...ml#post4266075 ?

Do you still think the preprocessor inserts jmp_buf out of nowhere ?
 
0 members found this post helpful.
Old 02-21-2011, 07:10 PM   #82
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
I don't get your question.
 
Old 02-23-2011, 11:23 AM   #83
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Could someone just explain what the problem is to me and show me how to fix it?
 
Old 02-23-2011, 08:16 PM   #84
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
The problem is that you can't copy an array like that:
Code:
#include <string.h>

void func()
{
    typedef int array_type[1];
    array_type a, b;

    memcpy(a, b, sizeof(array_type)); /* okay */
    a = b;                            /* bad */
}
 
1 members found this post helpful.
Old 02-24-2011, 02:58 PM   #85
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
Could someone just explain what the problem is to me and show me how to fix it?
Looks like you don't use 'grep' to check whether something is in a file.

Also, C99/C++ standards have pretty detailed description of what can and can not be done with arrays.
 
0 members found this post helpful.
Old 02-27-2011, 09:25 AM   #86
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by ntubski View Post
The error message suggests that the declaration for node_t is missing, it's impossible to say more without seeing the rest of the code.
Quote:
Originally Posted by ntubski View Post
I can't reproduce the error with the bits you've posted so far. I did notice that you have jmpbuf_t instead of jmp_buf.
That error still isn't solved, we got distracted with the jmp_buf thing (which doesn't seem to cause an error now).
 
Old 02-27-2011, 09:56 AM   #87
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by MTK358 View Post
Quote:
Originally Posted by ntubski
The error message suggests that the declaration for node_t is missing, it's impossible to say more without seeing the rest of the code.
Quote:
Originally Posted by ntubski
I can't reproduce the error with the bits you've posted so far. I did notice that you have jmpbuf_t instead of jmp_buf.
That error still isn't solved, we got distracted with the jmp_buf thing (which doesn't seem to cause an error now).
If you would like help in solving the problem please post a complete program that demonstrates the problem.
 
Old 02-27-2011, 10:03 AM   #88
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by ntubski View Post
If you would like help in solving the problem please post a complete program that demonstrates the problem.
min 3 characters
Attached Files
File Type: txt stuff.tar.bz2.txt (4.3 KB, 14 views)
 
Old 02-27-2011, 01:32 PM   #89
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
The problem is that ast.h includes node_function.h before it defines node_t.
 
1 members found this post helpful.
Old 02-27-2011, 02:26 PM   #90
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
I went thorough the source with warnings applied, and now it seems to compile. The modified sources are attached.
The unified diff to my changes is as follows:
Code:
diff -Naur your/array.h mine/array.h
--- your/array.h	2011-02-19 17:43:45.000000000 +0200
+++ mine/array.h	2011-02-27 22:19:44.069268191 +0200
@@ -5,11 +5,14 @@
 #include <gc.h>
 #include <stdlib.h>
 #include <stdarg.h>
+#include <unistd.h>
+
+typedef int (comparison_func_t)(void *, void *);
 
 typedef struct {
     void **data;
     size_t count;
-    int (*comparison_func)(void*, void*);
+    comparison_func_t *comparison_func;
 } array_t;
 
 array_t* array_new();
diff -Naur your/ast.c mine/ast.c
--- your/ast.c	2011-02-24 23:07:42.000000000 +0200
+++ mine/ast.c	2011-02-27 21:10:00.009753518 +0200
@@ -38,60 +38,57 @@
 
 object_t* node_eval(node_t *node, env_t *env)
 {
-    nodetype_t type = node->type;
+    switch (node->type) {
 	
-	if (type == NODETYPE_NUMBER)
-	{
+	case NODETYPE_NUMBER:
         return (object_t*) create_number_env(atof(node->payload));
-    }
-    else if (type == NODETYPE_BOOLEAN)
-    {
+
+    case NODETYPE_BOOLEAN:
         return (object_t*) create_boolean_env(strcmp(node->payload, "true") == 0);
-    }
-    else if (type == NODETYPE_STRING)
-    {
+
+    case NODETYPE_STRING:
         return (object_t*) create_string_env(node->payload);
-    }
-    else if (type == NODETYPE_IF)
-    {
-        if (convert_object_to_boolean(eval(node->children[0], env)))
+
+    case NODETYPE_IF:
+        if (convert_object_to_boolean(node_eval(node->children[0], env)))
             return node_eval(node->children[1], env);
         if (node->child_count == 3)
             return node_eval(node->children[2], env);
         return NULL;
-    }
-    else if (type == NODETYPE_WHILE)
+
+    case NODETYPE_WHILE:
     {
         object_t *value = NULL;
-        while (convert_object_to_boolean(eval(node->children[0], env)))
+        while (convert_object_to_boolean(node_eval(node->children[0], env)))
             value = node_eval(node->children[1], env);
         return value;
     }
-    else if (type == NODETYPE_DOWHILE)
+
+    case NODETYPE_DOWHILE:
     {
         object_t *value;
         do {
             value = node_eval(node->children[1], env);
-        } while (convert_object_to_boolean(eval(node->children[0], env)));
+        } while (convert_object_to_boolean(node_eval(node->children[0], env)));
         return value;
-	}
-    else if (type == NODETYPE_BLOCK)
-    {
+    }
+
+    case NODETYPE_BLOCK:
 		node_eval(node->children[0], env);
 		return node_eval(node->children[1], env);
-	}
-    else if (type == NODETYPE_SCOPE)
-    {
+
+    case NODETYPE_SCOPE:
 		return node_eval(node->children[0], env_new(env));
-	}
-    else if (type == NODETYPE_INSIDE)
+
+    case NODETYPE_INSIDE:
     {
 		env_t *inside_env = (env_t*) node_eval(node->children[0], env);
 		if (inside_env == NULL || inside_env->objtype != OBJTYPE_ENV)
 			node_eval_throw_exception(create_exception_env((object_t*) create_string_env("TypeError"), (object_t*) create_string_env("\"inside\" only works with envs")));
 		return node_eval(node->children[1], inside_env);
     }
-    else if (type == NODETYPE_MEMBER)
+
+    case NODETYPE_MEMBER:
     {
         env_t *env_with_var = env;
 		if (node->child_count == 1) env_with_var = (env_t*) node_eval(node->children[1], env);
@@ -99,7 +96,8 @@
 			node_eval_throw_exception(create_exception_env((object_t*) create_string_env("TypeError"), (object_t*) create_string_env("You can only get members from envs")));
 		return env_get(env_with_var, node->payload);
     }
-    else if (type == NODETYPE_ASSIGN)
+
+    case NODETYPE_ASSIGN:
     {
         env_t *env_with_var = env;
 		if (node->child_count == 2) env_with_var = (env_t*) node_eval(node->children[1], env);
@@ -107,12 +105,11 @@
 			node_eval_throw_exception(create_exception_env((object_t*) create_string_env("TypeError"), (object_t*) create_string_env("You can only get members from envs")));
 		return env_set(env_with_var, node->payload, node_eval(node->children[0], env));
     }
-    else if (type == NODETYPE_THIS)
-    {
+
+    case NODETYPE_THIS:
         return (object_t*) env;
 		
-    }
-    else if (type == NODETYPE_CALL)
+    case NODETYPE_CALL:
     {
         function_t *func = (function_t*) node_eval(node->children[0], env);
 		if (func == NULL || func->objtype != OBJTYPE_FUNCTION)
@@ -121,21 +118,20 @@
 		for (i=0; i<((array_t*) (node->payload))->count; i++)
 			array_set(node->payload, i, node_eval(array_get(node->payload, i), env));
 	    return func->call(func, node->payload);
-		
-	}
-    else if (type == NODETYPE_FUNC)
-    {
+    }
+
+    case NODETYPE_FUNC:
 	    return (object_t*) node_function_new(node->payload, env, node->children[0]);
         
     /* Exceptions **********************************************************/
     
-    }
-    else if (type == NODETYPE_THROW)
-    {
+    case NODETYPE_THROW:
         node_eval_throw_exception(node_eval(node->children[0], env));
+
+        /* TODO? */
+        return NULL; 
 		
-    }
-    else if (type == NODETYPE_TRY)
+    case NODETYPE_TRY:
     {
         jmp_buf prev_jmpbuf;
         memcpy(prev_jmpbuf, exception_jmpbuf, sizeof(jmp_buf));
@@ -146,10 +142,15 @@
             memcpy(exception_jmpbuf, prev_jmpbuf, sizeof(jmp_buf));
             env_set(env, "exception", exception_object);
             node_eval(node->children[1], env);
-            env_del(env, "exception");
+            env_remove(env, "exception");
             exception_object = NULL;
         }
         return NULL;
     }
+
+    /* TODO: Ignore unknown node types? */
+    default:
+        return NULL;
+    }
 }
 
diff -Naur your/ast.h mine/ast.h
--- your/ast.h	2011-02-20 17:06:25.000000000 +0200
+++ mine/ast.h	2011-02-27 20:57:25.426144415 +0200
@@ -1,15 +1,6 @@
 #ifndef __AST_H_INCLUDE_GUARD__
 #define __AST_H_INCLUDE_GUARD__
 
-#include "object.h"
-#include "env.h"
-
-#include "number_type.h"
-#include "string_type.h"
-#include "boolean_type.h"
-#include "c_function.h"
-#include "node_function.h"
-
 #include <setjmp.h>
 #include <stdarg.h>
 
@@ -34,6 +25,16 @@
 } nodetype_t;
 
 typedef struct node node_t;
+
+#include "object.h"
+#include "env.h"
+
+#include "number_type.h"
+#include "string_type.h"
+#include "boolean_type.h"
+#include "c_function.h"
+#include "node_function.h"
+
 struct node {
     nodetype_t type;
     int child_count;
diff -Naur your/boolean_type.c mine/boolean_type.c
--- your/boolean_type.c	2011-02-19 22:09:07.000000000 +0200
+++ mine/boolean_type.c	2011-02-27 21:30:21.490559505 +0200
@@ -1,6 +1,6 @@
 #include "boolean_type.h"
 
-int convert_object_to_bool(object_t *obj)
+int convert_object_to_boolean(object_t *obj)
 {
     if (obj == NULL)
         return 0;
@@ -10,16 +10,17 @@
 }
 
 #define getval(env) (*((char*) env->parent->internal_data))
-#define getmemberval(env, name) (*((char*) (env_get(env, name)->internal_data)))
-#define ismemberbool(env, name) (((object_t*) env_get(env, name))->envtype == ENVTYPE_BOOLEAN)
+#define getmemberval(env, name) (*((char*) (((env_t *)env_get(env, name))->internal_data)))
+#define ismemberbool(env, name) (((object_t*) env_get(env, name))->objtype == ENVTYPE_BOOLEAN)
+#define ismembernum(env, name) (((object_t*) env_get(env, name))->objtype == ENVTYPE_NUMBER)
 
 object_t* bool_and(env_t *env)
 {
     if (!ismembernum(env, "x"))
         node_eval_throw_exception(
             create_exception_env(
-                create_string_env("TypeError"),
-                create_string_env("")
+                (object_t *)create_string_env("TypeError"),
+                (object_t *)create_string_env("")
             )
         );
     return (object_t*) create_boolean_env(getval(env) && getmemberval(env, "x"));
@@ -30,8 +31,8 @@
     if (!ismembernum(env, "x"))
         node_eval_throw_exception(
             create_exception_env(
-                create_string_env("TypeError"),
-                create_string_env("")
+                (object_t *)create_string_env("TypeError"),
+                (object_t *)create_string_env("")
             )
         );
     return (object_t*) create_boolean_env(getval(env) && getmemberval(env, "x"));
@@ -42,8 +43,8 @@
     if (!ismembernum(env, "x"))
         node_eval_throw_exception(
             create_exception_env(
-                create_string_env("TypeError"),
-                create_string_env("")
+                (object_t *)create_string_env("TypeError"),
+                (object_t *)create_string_env("")
             )
         );
     return (object_t*) create_boolean_env(getval(env) && getmemberval(env, "x"));
@@ -60,7 +61,7 @@
     this->envtype = ENVTYPE_BOOLEAN;
     
     this->internal_data = GC_malloc(sizeof(char));
-    (char) (*(this->internal_data)) = value;
+    *(char *)(this->internal_data) = value;
     
     env_set(this, "__op_and", c_function_new(array_new_prefill(1, "x"), this, &bool_and));
     env_set(this, "__op_or" , c_function_new(array_new_prefill(1, "x"), this, &bool_or ));
diff -Naur your/boolean_type.h mine/boolean_type.h
--- your/boolean_type.h	2011-02-18 19:03:51.000000000 +0200
+++ mine/boolean_type.h	2011-02-27 20:59:12.744121108 +0200
@@ -5,7 +5,7 @@
 #include "ast.h"
 #include "c_function.h"
 
-int convert_object_to_bool(object_t *obj);
+int convert_object_to_boolean(object_t *obj);
 
 env_t* create_boolean_env(char value);
 
diff -Naur your/build.sh mine/build.sh
--- your/build.sh	2011-02-20 17:52:31.000000000 +0200
+++ mine/build.sh	2011-02-27 22:21:26.282308379 +0200
@@ -1,11 +1,11 @@
-#!/bin/sh
+#!/bin/bash
 
-function print_message
+print_message()
 {
     echo -e '\e[1;32m'"$1"'\e[0m'
 }
 
-function print_error
+print_error()
 {
     echo -e '\e[1;31m'"$1"'\e[0m'
 }
@@ -14,7 +14,7 @@
 do
 	# ./format.sh $file
 
-	gcc -c $file
+	gcc -std=c99 -pedantic -Wall -I/usr/include/gc -O0 -c $file
 	if [ $? '!=' 0 ]
 	then
 		print_error "Failed to compile $file"
@@ -23,7 +23,7 @@
 	print_message "Compiled $file"
 done
 
-gcc *.o
+gcc -std=c99 -pedantic -Wall -I/usr/include/gc -lgc -o prog *.o
 if [ $? '!=' 0 ]
 then
 	print_error "Failed to link executable"
diff -Naur your/c_function.c mine/c_function.c
--- your/c_function.c	2011-02-18 18:18:40.000000000 +0200
+++ mine/c_function.c	2011-02-27 21:41:22.255228128 +0200
@@ -5,7 +5,7 @@
 {
     env_t *env = env_new(this->env);
     int i;
-    for (i=0; i<params.count; i++)
+    for (i=0; i < params->count; i++)
         env_set(env, array_get(this->param_names, i), array_get(params, i));
     return this->c_function(env);
 }
@@ -16,8 +16,8 @@
     this->param_names = param_names;
     this->env = env;
     this->c_function = c_function;
-    this->call = &c_function_call;
+    this->call = (function_call_t *)&c_function_call;
     
-    return this;
+    return (function_t *)this;
 }
 
diff -Naur your/env.c mine/env.c
--- your/env.c	2011-02-19 17:43:25.000000000 +0200
+++ mine/env.c	2011-02-27 21:48:59.538406704 +0200
@@ -6,9 +6,9 @@
     
     this->parent = parent;
     this->keys = array_new();
-    array_set_comparison_func(this->keys, &strcmp);
+    array_set_comparison_func(this->keys, (comparison_func_t *)&strcmp);
     this->values = array_new();
-    this->type = ENVTYPE_GENERIC;
+    this->envtype = ENVTYPE_GENERIC;
     
     return this;
 }
@@ -21,7 +21,8 @@
             return env_get(this->parent, key);
         return NULL;
     }
-    return index;
+    /* TODO: Need to return a pointer, not just the index! */
+    return (void *)this->values->data[index];
 }
 
 void* env_set(env_t *this, char *key, void *value)
@@ -32,7 +33,7 @@
         if (array_indexof(i->keys, key) != -1)
             env_with_member = i;
         i = i->parent;
-    } while (i)
+    } while (i);
     
     array_push(env_with_member->keys, key);
     array_push(env_with_member->values, value);
diff -Naur your/function.h mine/function.h
--- your/function.h	2011-02-19 22:08:30.000000000 +0200
+++ mine/function.h	2011-02-27 21:39:28.826383733 +0200
@@ -6,12 +6,14 @@
 #include "env.h"
 
 typedef struct function function_t;
+typedef object_t *(function_call_t)(function_t *, array_t *);
 #define function_TEMPLATE \
     object_TEMPLATE \
     array_t *param_names; \
     env_t *env; \
-    object_t* (*call)(function_t*, array_t*);
+    function_call_t *call;
 struct function { function_TEMPLATE };
 
+
 #endif
 
diff -Naur your/node_function.c mine/node_function.c
--- your/node_function.c	2011-02-18 18:19:48.000000000 +0200
+++ mine/node_function.c	2011-02-27 21:52:59.663522764 +0200
@@ -5,14 +5,14 @@
 {
     env_t *env = env_new(this->env);
     int i;
-    for (i=0; i<params.count; i++)
+    for (i=0; i<params->count; i++)
         env_set(env, array_get(this->param_names, i), array_get(params, i));
-    return node_eval(this->node, env);
+    return node_eval(((node_function_t *)this)->node, env);
 }
 
 node_function_t* node_function_new(array_t *param_names, env_t *env, node_t *node)
 {
-    node_function_t *this = GC_malloc(sizeof(node_fuction_t));
+    node_function_t *this = GC_malloc(sizeof(node_function_t));
     
     this->param_names = param_names;
     this->env = env;
diff -Naur your/node_function.h mine/node_function.h
--- your/node_function.h	2011-02-19 22:10:16.000000000 +0200
+++ mine/node_function.h	2011-02-27 20:56:34.577574555 +0200
@@ -1,9 +1,9 @@
 #ifndef __NODE_FUNCTION_H_INCLUDE_GUARD
 #define __NODE_FUNCTION_H_INCLUDE_GUARD
 
-#include "function.h"
-#include "object.h"
 #include "ast.h"
+#include "object.h"
+#include "function.h"
 
 typedef struct node_function node_function_t;
 struct node_function {
diff -Naur your/number_type.c mine/number_type.c
--- your/number_type.c	2011-02-18 20:36:48.000000000 +0200
+++ mine/number_type.c	2011-02-27 22:01:33.951742089 +0200
@@ -1,12 +1,16 @@
 #include "number_type.h"
 
+#define ismembernum(env, name) (((object_t*) env_get(env, name))->objtype == ENVTYPE_NUMBER)
+#define getval(env) (*(double *)((env_t *)(((env_t *)env)->parent)->internal_data))
+#define getmemberval(env, name) (*((double *)((env_t *)env_get(env, name))->internal_data))
+
 object_t* number_add(env_t *env)
 {
     if (!ismembernum(env, "x"))
         node_eval_throw_exception(
             create_exception_env(
-                create_string_env("TypeError"),
-                create_string_env("Only numbers can be added to numbers")
+                (object_t *)create_string_env("TypeError"),
+                (object_t *)create_string_env("Only numbers can be added to numbers")
             )
         );
     return (object_t*) create_number_env(getval(env) + getmemberval(env, "x"));
@@ -17,8 +21,8 @@
     if (!ismembernum(env, "x"))
         node_eval_throw_exception(
             create_exception_env(
-                create_string_env("TypeError"),
-                create_string_env("Only numbers can be multiplied by numbers")
+                (object_t *)create_string_env("TypeError"),
+                (object_t *)create_string_env("Only numbers can be multiplied by numbers")
             )
         );
     return (object_t*) create_number_env(getval(env) * getmemberval(env, "x"));
@@ -29,8 +33,8 @@
     if (!ismembernum(env, "x"))
         node_eval_throw_exception(
             create_exception_env(
-                create_string_env("TypeError"),
-                create_string_env("Only numbers can be compared to numbers")
+                (object_t *)create_string_env("TypeError"),
+                (object_t *)create_string_env("Only numbers can be compared to numbers")
             )
         );
     return (object_t*) create_boolean_env(getval(env) == getmemberval(env, "x"));
@@ -41,8 +45,8 @@
     if (!ismembernum(env, "x"))
         node_eval_throw_exception(
             create_exception_env(
-                create_string_env("TypeError"),
-                create_string_env("Only numbers can be compared to numbers")
+                (object_t *)create_string_env("TypeError"),
+                (object_t *)create_string_env("Only numbers can be compared to numbers")
             )
         );
     return (object_t*) create_boolean_env(getval(env) > getmemberval(env, "x"));
@@ -53,7 +57,7 @@
     double i;
     for (i=0; i<getval(env); i++) {
         function_t *func = (function_t*) env_get(env, "f");
-        func->call(func, array_new_preload(1, create_number_env(i)));
+        func->call(func, array_new_prefill(1, create_number_env(i)));
     }    
     return NULL;
 }
@@ -64,7 +68,7 @@
     this->envtype = ENVTYPE_NUMBER;
     
     this->internal_data = GC_malloc(sizeof(double));
-    (double) *this->internal_data = value;
+    *((double *)this->internal_data) = value;
     
     env_set(this, "__op_add", c_function_new(array_new_prefill(1, "other"), this, &number_add));
     env_set(this, "__op_mul", c_function_new(array_new_prefill(1, "other"), this, &number_mul));
diff -Naur your/number_type.h mine/number_type.h
--- your/number_type.h	2011-02-18 18:25:26.000000000 +0200
+++ mine/number_type.h	2011-02-27 22:00:58.862752060 +0200
@@ -5,6 +5,7 @@
 #include "boolean_type.h"
 #include "ast.h"
 #include "c_function.h"
+#include "array.h"
 
 #include <stdlib.h>
 
diff -Naur your/string_type.c mine/string_type.c
--- your/string_type.c	2011-02-18 20:37:06.000000000 +0200
+++ mine/string_type.c	2011-02-27 22:18:07.902051677 +0200
@@ -1,4 +1,5 @@
 #include "string_type.h"
+extern char *strdup(const char *);
 
 char* convert_object_to_string(object_t *obj)
 {
@@ -9,34 +10,45 @@
         env_t *env = (env_t*) obj;
         switch (env->envtype) {
           case ENVTYPE_STRING:
-            return strdup(env->internal_data);
+            return strdup((char *)env->internal_data);
           case ENVTYPE_NUMBER:
-            char *str = GC_malloc(sizeof(char) * 8);
-            sprintf(str, 8, "%f", *((double*) env->internal_data));
+          {
+            char *str = GC_malloc(sizeof(char) * 28);
+            snprintf(str, 27, "%.18g", *((double*) env->internal_data));
             return str;
+          }
+          case ENVTYPE_GENERIC:
+          {
+            char *str = GC_malloc(sizeof(char) * 28);
+            snprintf(str, 27, "(Env at %p)", env->internal_data);
+            return str;
+          }
+          case ENVTYPE_BOOLEAN:
+          {
+            if (*(char *)env->internal_data)
+                return strdup("true");
+            else
+                return strdup("false");
+          }
         }
-        
-        char *str = GC_malloc(sizeof(char) * 24);
-        sprintf(str, 24, "(Env at %p)", *((double*) env->internal_data));
-        return str;
     }
     
-    char *str = GC_malloc(sizeof(char) * 24);
-    sprintf(str, 24, "(Function at %p)", *((double*) env->internal_data));
+    char *str = GC_malloc(sizeof(char) * 28);
+    snprintf(str, 27, "(Function at %p)", ((env_t *)obj)->internal_data);
     return str;
 }
 
-#define getval(env) ((char*) env->parent->internal_data)
-#define getmemberval(env, name) ((char*) env_get(env, name)->internal_data)
-#define ismemberstr(env, name) (env_get(env, name)->envtype == ENVTYPE_STRING)
+#define getval(env) (char *)((env_t *)(((env_t *)env)->parent)->internal_data)
+#define getmemberval(env, name) ((char*)((env_t *)env_get(env, name))->internal_data)
+#define ismemberstr(env, name) (((env_t *)env_get(env, name))->envtype == ENVTYPE_STRING)
 
 object_t* str_eq(env_t *env)
 {
     if (!ismemberstr(env, "x"))
         node_eval_throw_exception(
             create_exception_env(
-                create_string_env("TypeError"),
-                create_string_env("")
+                (object_t *)create_string_env("TypeError"),
+                (object_t *)create_string_env("")
             )
         );
     return (object_t*) create_boolean_env(strcmp(getval(env), getmemberval(env, "x")) == 0);
diff -Naur your/string_type.h mine/string_type.h
--- your/string_type.h	2011-02-18 19:05:58.000000000 +0200
+++ mine/string_type.h	2011-02-27 22:03:49.818829430 +0200
@@ -1,15 +1,15 @@
 #ifndef __STRING_TYPE_H_INCLUDE_GUARD__
 #define __STRING_TYPE_H_INCLUDE_GUARD__
 
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+
 #include "env.h"
 #include "boolean_type.h"
 #include "ast.h"
 #include "c_function.h"
 
-#include <stdlib.h>
-#include <string.h>
-#include <stdio.h>
-
 char* convert_object_to_string(object_t *obj);
 
 env_t* create_string_env(char *value);
Attached Files
File Type: txt newstuff.tar.bz2.txt (4.6 KB, 12 views)
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
would and could somebody help me with my homework assignment please... DarDevy Linux - Newbie 3 04-20-2009 02:43 PM
LXer: Java Data Objects and Service Data Objects in SOA LXer Syndicated Linux News 0 01-17-2009 06:10 AM
Objects in C?? kickzha Programming 6 06-17-2006 08:38 PM
IP address assignment n3tw0rk Linux - Networking 1 01-05-2004 12:23 AM
need help with class assignment tenraek Linux - General 4 04-03-2003 12:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration