LinuxQuestions.org
Review your favorite Linux distribution.
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-18-2011, 02:05 PM   #61
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335

One way to fix your code is to define function_t as such:
Code:
typedef struct function_t { function_TEMPLATE } function_t;
But I must ask... why in hell are you intentionally obfuscating the readability of the code with macro definitions? From a professional standpoint, it looks like crap. Don't get offended! It is merely my opinion, for which I am entitled to possess.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 02-18-2011, 02:11 PM   #62
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 dwhitney67 View Post
One way to fix your code is to define function_t as such:
Code:
typedef struct function_t { function_TEMPLATE } function_t;
Why is function_t mentioned twice? I don't want to make an instance of function_t, if that's what it does.

Quote:
Originally Posted by dwhitney67 View Post
But I must ask... why in hell are you intentionally obfuscating the readability of the code with macro definitions? From a professional standpoint, it looks like crap. Don't get offended! It is merely my opinion, for which I am entitled to possess.
That's the best way I know of to achieve polymorphism in C. The idea is that all the contents of object_t are at the beginning of function_t, that way you can have an object_t pointer and treat it like an object_t, even if it's actually pointing to a function_t.

Last edited by MTK358; 02-18-2011 at 02:13 PM.
 
Old 02-19-2011, 08:41 AM   #63
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 changed it like this:

Code:
#define function_TEMPLATE \
    object_TEMPLATE \
    array_t *param_names; \
    env_t *env; \
    object_t* (*call)(function_t*, array_t*);
struct function { function_TEMPLATE };
typedef struct function function_t;
It still doesn't work.

EDIT:

This is what it looks like after being run through the preprocessor:

Code:
struct function { object_type_t objtype; array_t *param_names; env_t *env; object_t* (*call)(function_t*, array_t*); };
typedef struct function function_t;
I see nothing wrong with it.

Last edited by MTK358; 02-19-2011 at 10:12 AM.
 
Old 02-19-2011, 02:02 PM   #64
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,786

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
You need to put the typedef for function_t before it's first use:
Code:
typedef struct function function_t; /* this goes first! */
#define function_TEMPLATE \
    object_TEMPLATE \
    array_t *param_names; \
    env_t *env; \
    object_t* (*call)(function_t*, array_t*);
struct function { function_TEMPLATE };

Last edited by ntubski; 02-19-2011 at 02:03 PM. Reason: grammar
 
Old 02-19-2011, 03:07 PM   #65
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
That fixed that issue. I didn't notice that the definition for function_t included function_t.

But now there's another one:

Code:
node_function.h:11:5: error: expected specifier-qualifier-list before ‘node_t’
node_function.h:14:70: error: expected declaration specifiers or ‘...’ before ‘node_t’
Code:
#ifndef __NODE_FUNCTION_H_INCLUDE_GUARD
#define __NODE_FUNCTION_H_INCLUDE_GUARD

#include "function.h"
#include "object.h"
#include "ast.h"

typedef struct node_function node_function_t;
struct node_function {
    function_TEMPLATE
    node_t *node;
};

node_function_t* node_function_new(array_t *param_names, env_t *env, node_t *node);

#endif
This can't have te same problem because the definition does not include node_function_t. And what's a "specifier-qualifier-list"?
 
Old 02-19-2011, 05:23 PM   #66
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,786

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Where is the declaration for node_t?
 
Old 02-19-2011, 05:38 PM   #67
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
In ast.h.
 
Old 02-19-2011, 06:22 PM   #68
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,786

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
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.
 
Old 02-19-2011, 07:01 PM   #69
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
ast.h:

Code:
#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>

typedef enum {
    NODETYPE_NUMBER,   
    //NODETYPE_FLOAT,     
    NODETYPE_STRING,    // create String
    NODETYPE_IF,        // evaluate only if condition true/false
    NODETYPE_WHILE,     // loop while condition true
    NODETYPE_DOWHILE,   // loop while condition true, eval body at least once
    NODETYPE_CALL,      // call function
    NODETYPE_MEMBER, // get member from env ('.' operator)
    NODETYPE_ASSIGN,    // assign value to lvalue ('=' operator)
    NODETYPE_FUNC,      // create anonymous function
    NODETYPE_THIS,
    NODETYPE_THROW,
    NODETYPE_TRY,
	NODETYPE_INSIDE,
	NODETYPE_BOOLEAN,
	NODETYPE_BLOCK,
	NODETYPE_SCOPE,
} nodetype_t;

typedef struct node node_t;
struct node {
    nodetype_t type;
    int child_count;
    node_t **children;
    void *payload;
};

extern jmpbuf_t exception_jmpbuf;
extern object_t *exception_object;

node_t* node_new(nodetype_t type, char *payload, int child_count, ...);

object_t *create_exception_env(object_t *name, object_t *message);

void node_eval_throw_exception(object_t *e);

object_t* node_eval(node_t *node, env_t *env);

#endif
 
Old 02-19-2011, 07:33 PM   #70
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,786

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
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.
 
Old 02-20-2011, 09:41 AM   #71
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Code:
ast.c:143:30: error: incompatible types when assigning to type ‘jmp_buf’ from type ‘struct __jmp_buf_tag *’
ast.c:145:30: error: incompatible types when assigning to type ‘jmp_buf’ from type ‘struct __jmp_buf_tag *’
What does this mean? I read about it and heard that jmp_buf is actually an array, but why doesn't this work?
 
Old 02-20-2011, 02:22 PM   #72
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
Code:
ast.c:143:30: error: incompatible types when assigning to type ‘jmp_buf’ from type ‘struct __jmp_buf_tag *’
ast.c:145:30: error: incompatible types when assigning to type ‘jmp_buf’ from type ‘struct __jmp_buf_tag *’
What does this mean? I read about it and heard that jmp_buf is actually an array, but why doesn't this work?
Copy-paste your code, including declaration of 'jmp_buf' and of '__jmp_buf_tag'.

What are you trying to do ? To assign to the whole array and not to array element ?
 
Old 02-20-2011, 02:35 PM   #73
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 never mentioned "__jmp_buf_tag" anywhere in my code. All I'm doing is assigning a variable to another, both of type "jmp_buf".

Code:
extern jmp_buf exception_jmpbuf;                                      ast.h, line 44
extern object_t *exception_object;

-----

#include "ast.h"                                                      ast.c, line 1

jmp_buf exception_jmpbuf;
object_t *exception_object;

<snip>

    else if (type == NODETYPE_THROW)                                  line 133
    {
        node_eval_throw_exception(node_eval(node->children[0], env));
		
    }
    else if (type == NODETYPE_TRY)
    {
        jmp_buf prev_jmpbuf = exception_jmpbuf;
        if (setjmp(exception_jmpbuf) == 0) {
            node_eval(node->children[0], env);
            exception_jmpbuf = prev_jmpbuf;                           line 143
        } else {
            exception_jmpbuf = prev_jmpbuf;                           line 145
            env_set(env, "exception", exception_object);
            node_eval(node->children[1], env);
            env_del(env, "exception");
            exception_object = NULL;
        }
        return NULL;
    }

Last edited by MTK358; 02-20-2011 at 02:39 PM.
 
Old 02-20-2011, 03:43 PM   #74
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
I never mentioned "__jmp_buf_tag" anywhere in my code. ...
Then probably it's a result of some macro substitution. You need to look at preprocessed code (gcc -E ....).
 
Old 02-20-2011, 04:14 PM   #75
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
it still says "jmp_buf" everywhere.

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


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 12:38 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