Hacker News new | past | comments | ask | show | jobs | submit login

In the process of debugging it I noticed that you're compiling but not linking in c99 mode. When I fix that the problem seems to go away:

  $ git diff
  diff --git a/Makefile b/Makefile
  index 1263dd8..6592c59 100644
  --- a/Makefile
  +++ b/Makefile
  @@ -1,16 +1,16 @@
   CC = gcc #/usr/local/bin/gcc-4.2
   PREFIX = /usr/local
   NDEBUG ?= 
  -CFLAGS = -c -std=c99 -Wall -Werror -Wno-unused-variable -Wno-format-security -O3 -static $(NDEBUG) 
  +CFLAGS = -std=c99 -Wall -Werror -Wno-unused-variable -Wno-format-security -O3 -static $(NDEBUG) 
   SRCS = src/carp_instructions.c src/carp_lexer.c src/carp_machine.c src/carp_tokenizer.c src/lib/carp_stack.c src/lib/carp_ht.c
   #$(wildcard src/*.c src/lib/*.c)
   OBJS = *.o
   PROG = carp.out
   
   all:
  -       $(CC) $(CFLAGS) $(SRCS)
  +       $(CC) -c $(CFLAGS) $(SRCS)
	  ar cr libcarp.a $(OBJS)
  -       $(CC) src/carp.c libcarp.a -o $(PROG)
  +       $(CC) $(CFLAGS) src/carp.c libcarp.a -o $(PROG)
   
   #.PHONY: tests
Still, I'm concerned that you might be triggering some sort of undefined behavior, which means it might work for you but not on a slightly different machine or compiler version. I tried printing out token lexemes in carp_run_program, right after the call to tokenize(), and some of the tokens printed binary garbage, suggesting that they might be missing a terminating null, or worse. Does this look right?

  $ git diff
  ...
  diff --git a/src/carp.c b/src/carp.c
  index 9268d98..19e16d9 100644
  --- a/src/carp.c
  +++ b/src/carp.c
  @@ -88,6 +88,8 @@ void carp_print_conditions () {
   
   void carp_run_program (char *fn) {
     carp_tok *tokens = carp_lex_tokenize(fn);
  +  for (carp_tok* tt = tokens; tt; tt=tt->next)
  +    printf("%s %d\n", tt->lexeme, tt->type);
     if (tokens == NULL) {
       fprintf(stderr, "Something went wrong with tokenization.\n");
       exit(1);

  $ make && ./carp.out -f ./examples/carp/call.carp
  gcc  -c -std=c99 -Wall -Werror -Wno-unused-variable -Wno-format-security -O3 -static   src/carp_instructions.c src/carp_lexer.c src/carp_machine.c src/carp_tokenizer.c src/lib/carp_stack.c src/lib/carp_ht.c
  ar cr libcarp.a *.o
  gcc  -std=c99 -Wall -Werror -Wno-unused-variable -Wno-format-security -O3 -static   src/carp.c libcarp.a -o carp.out
  add 3
  gload 6
  -5l 1
  gload 6
  -4l 1
  add 6
  ret 6
  main 3
  push 6
  7�l 1
  push 6
  9�l 1
  call 6
  add 4
  2 1
  ptop 6
  halt 6
  0 1
  16
The trouble with C/C++ is that it's so easy to end up with undefined behavior :/



Actually I can't link with the same CFLAGS; I get an error about missing lcrt0.o or something of the sort.

Also, yeah your code looks fine; I added it in and it worked fine. That's unfortunate. I'll debug soon.


What machine/os are you on?

(Perhaps we should take this offline. My email is in my profile.)


Sent an email :)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: