HuoLanguage / huo

interpreted language written in C
MIT License
213 stars 21 forks source link

Program size limited to 256 characters? #9

Closed tiras-j closed 8 years ago

tiras-j commented 8 years ago

I might be losing my mind but isn't your struct String defined to allocate 256 characters of space as per:

struct String {
    char body[256];
    int length;
};

However when you read the target file into a stack allocated String struct in huo.c there is no check against the number of characters read:

char c;
while ((c = fgetc(fp)) != EOF){
    file.body[file.length] = c;
    file.length++;
}

Out of curiosity, why not just mmap the file and then you can index into it? Anyways I may be missing something completely obvious here, it's a bit late.

Also this is awesome, I've been toying with the idea of making a small compiled language to play around with LLVM but maybe an interpreter would be more fun to play around with :)

incrediblesound commented 8 years ago

Haha yes that is a really silly issue, I increased all the struct array sizes recently but I must have missed the string. I also have not yet implemented the finer error checking, I was too busy obsessing over posix threads and fun stuff like that :-)

Thanks for bringing up mmap, I had actually never heard of it! I'll see if that would work for me. And I definitely recommend writing your own interpreter, I've been having a lot of fun with this one!

incrediblesound commented 8 years ago

I upped the string size but I found that if I set it too high it caused segfault which is a little strange. I think the better solution anyway is to some other strategy for opening files like mmap.

tiras-j commented 8 years ago

Hmm... It's interesting that a stack allocated array would cause a segfault like that, unless perhaps your overflowing the default stack size.

But definitely either malloc'ing some space and copying the file in (as you're currently doing, just with heap allocated memory) or mmap'ing would give you the same indexable behavior that I'm guessing the lexer expects. mmap is more efficient but might be less portable if either of those are concerns.

Anyways good luck! You've inspired me to build an interpreter to play around with. Maybe I'll finally make a Show HN post some day haha.

incrediblesound commented 8 years ago

Fixed by @TheLoneWolfling