orangeduck / BuildYourOwnLisp

Learn C and build your own programming language in under 1000 lines of code!
http://www.buildyourownlisp.com/
Other
2.84k stars 392 forks source link

[chap 9] invalid struct definition using clang #129

Closed crclark96 closed 6 years ago

crclark96 commented 6 years ago

loving the book

when i was reformatting in chapter nine and added the line struct lval** cell to the struct definition, my version of clang [Apple LLVM version 9.1.0 (clang-902.0.39.1)] threw a bunch of warnings and the following error:

lval.c:123:19: error: incomplete definition of type 'struct lval'
    if (v->cell[i]->type == LVAL_ERR) { return lval_take(v, i); }
        ~~~~~~~~~~^

and this note:

./lval.h:14:10: note: forward declaration of 'struct lval'
  struct lval** cell;

which led me to believe that line of the struct definition was being parsed as its own struct definition.

i fixed this by changing the definition and forward declaring the struct like so:

typedef struct lval lval;

struct lval {
  int type;
  long num;
  /* error and symbol types with string data */
  char *err;
  char *sym;
  /* count and pointer to a list of lval* */
  int count;
  lval** cell;
};
orangeduck commented 6 years ago

Hey,

Thanks, glad you are enjoying the book. Did you make the new struct definition exactly as it appears in the book and still get the error?

typedef struct lval {
  int type;
  long num;
  /* Error and Symbol types have some string data */
  char* err;
  char* sym;
  /* Count and Pointer to a list of "lval*" */
  int count;
  struct lval** cell;
} lval;

I'm wondering if this is some new kind of default behavior in clang...

crclark96 commented 6 years ago

I tried to reproduce the error and I think I had neglected to name the struct before naming the type (like so:

typedef struct {
  int type;
  long num;
  /* Error and Symbol types have some string data */
  char* err;
  char* sym;
  /* Count and Pointer to a list of "lval*" */
  int count;
  struct lval** cell;
} lval;

)

my mistake there! thanks for the response though