[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

I want to code a basic interpreter / scripting language. Like

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 5
Thread images: 2

File: 1467401810380.jpg (23KB, 410x410px) Image search: [Google]
1467401810380.jpg
23KB, 410x410px
I want to code a basic interpreter / scripting language.

Like basically each line of the script is an opcode on 32 bits and the interpret will read by default one opcode after the other (so one line after the other), except if there's a jump or something like that.
I also want to be able to store values (like int), on any given line, and read it later...

In this possible? isn't there a really simple language that is exactly like that that I could look up? What I'm unsure how to do is how to store commands on 32 bits and values on 32 bits in the same adress space
>>
File: silly seal.jpg (100KB, 1200x1033px) Image search: [Google]
silly seal.jpg
100KB, 1200x1033px
>>
Yes, here is tons of resources about compiler design. SICP includes a whole chapter about it iirc. A basic google search give a tutorial and an online textbook.
>>
OP here

for instance my program input before converted to opcodes would be:

line 1 - STORE 349 (ie store a value here)
line 2 - INCR 1 (increment adress 1 by 1)
line 3 - PRINT 1 (print what is at adress 1)
line 4 - GOTO 2

I don't know if it's possible to make it work if all opcodes should fit on 1 line (ie 32 bits) and each line can also store a value

But I don't want to have variable opcodes and complex stuff, I just want to be able to iterate on memory locations of the same size
>>
Are you retarded? You are basically asking "how do I read data from a file?" Also why would you use a 32 bit opcode? I bet 256 ops aka 8 bits is just fine.

#define OP_HALT 0
#define OP_INC 1
#define OP_OUT 2

#define CHUNK 256

typedef struct {
uint8_t *code;

uint32_t ip;
uint32_t register;
uint8_t halt;
} mach;

int execfile(const char *file, mach *m) {
m->code = malloc(CHUNK);
if (code == NULL) return 1;

int fd = open(file, O_RDONLY);
if (fd == -1) {
free(m->code);
return 1;
}

// TODO roll your own memory management for dynamic allocs
read(fd, m->code, CHUNK);

// Exec code until halt
while (execop(m)) {}

free(m->code);
return 0;
}

int execop(mach *m) {
// Is machine halted?
if (m->halt) return 1;

uint8_t op;
op = m->code[m->ip];
m->ip++;

// One byte ops
switch(op) {
case OP_HLT:
m->halt = 1;
return 0;
case OP_OUT:
printf("Register: %i\n", m->register);
return 0;
}

uint32_t arg;
// Ops with arg
arg = (uint32_t) m->code[m->ip];
m->ip += 4;

switch (op) {
case OP_INC:
mach->register += arg;
return 0;
default:
// Invalid opcode
return 1;
}
}


And no, I didn't test it, I just wrote it in 5 minutes.
Thread posts: 5
Thread images: 2


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

If you need a post removed click on it's [Report] button and follow the instruction.
If you like this website please support us by donating with Bitcoin at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties. Posts and uploaded images are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that website. If you need information about a Poster - contact 4chan. This project is not affiliated in any way with 4chan.