4 ms·
Seems like [1] could benefit from use of the X macro [2], should make adding new instructions much easier and you can avoid the hassle of having to keep two sep
by axman6 12y ago
Seems like [1] could benefit from use of the X macro [2], should make adding new instructions much easier and you can avoid the hassle of having to keep two separate tables in sync. There's probably quite a few places where the code could be made clearer by using it. Also in the implementation of your functions, there's a hell of a lot of repetition in all the binary operations, another macro which you pass in the operation and the function name would make life earier:
#define binop(NAME, OP) definstr (NAME) { \
long long b, a; \
if (carp_stack_pop(&m->stack, &b) == -1)\
carp_vm_err(m, CARP_STACK_EMPTY);\
if (carp_stack_pop(&m->stack, &a) == -1)\
carp_vm_err(m, CARP_STACK_EMPTY);\
carp_stack_push(&m->stack, a OP b);}
binop(ADD,+)
binop(MUL,*)
...
The repetition of
if (carp_stack_pop(&m->stack, &a) == -1)
carp_vm_err(m, CARP_STACK_EMPTY);
seems like a good place to just use a function to encapsulate all the error checking and handling.
[1] https://github.com/tekknolagi/carp/blob/master/src/carp_instructions.h https://github.com/tekknolagi/carp/blob/master/src/carp_inst...
[2] http://www.embedded.com/design/programming-languages-and-tools/4403953/C-language-coding-errors-with-X-macros-Part-1 http://www.embedded.com/design/programming-languages-and-too... and the following parts
- tekknolagi 12y agoDefinitely interesting — I shall take a look! Feel free to make a pull request if you're game.
- axman6 12y agoIf I had time and a job where I got to code I would, but it'll be a few days before I have time.
- tekknolagi 12y agoAh, I feel you — no problem! I do really love the binop macro.
- tekknolagi 12y agoQuick question. Is that code you've given ok to use in the project? I just realized that I added and pushed without asking. GPL?
- axman6 12y agoThe binop stuff you can certainly use freely, the X macro stuff is quite well known so I guess that it's also fine to use. I certainly won't be claiming any copyright on the code I shared =) My personal preference for licensing is one of the BSD or MIT licenses, I'd rather see things of mine used to makie the world a better place than force people to share what they've done with it.
- tekknolagi 12y agoThanks :) Having some weird pasting issues with a macro for POP expansion. http://i.imgur.com/4HiltgI.png http://i.imgur.com/4HiltgI.png Edit: Just kidding. That was dumb.
- zura 12y agoBtw, I remember there was a law stating that the code less than 10(?) lines is not copyright-able, no?
- voidlogic 12y agoThat would be a a silly rule, for better or worse 10 lines of Haskell might perform the computation of 500 line of Java (even if they took you the same amount of time to write.. heh).
- tekknolagi 12y agoOr, say, Befunge...
- zura 12y agoWell, maybe they made this rule in days when COBOL was in hype? ;)
- spatulon 12y agoThis is a technique I first came across in the code for lcc, as described in David R. Hanson's book "A Retargetable C Compiler: Design and Implementation". https://sites.google.com/site/lccretargetablecompiler/ https://sites.google.com/site/lccretargetablecompiler/ The code in that book feels pretty dated in a lot of places, but this was a neat trick. Every piece of information related to a token is defined in one place: https://github.com/drh/lcc/blob/master/src/token.h https://github.com/drh/lcc/blob/master/src/token.h Then here's how it's used: https://github.com/drh/lcc/blob/master/src/expr.c#L8 https://github.com/drh/lcc/blob/master/src/expr.c#L8 https://github.com/drh/lcc/blob/master/src/output.c#L107 https://github.com/drh/lcc/blob/master/src/output.c#L107