write-a-C-interpreter icon indicating copy to clipboard operation
write-a-C-interpreter copied to clipboard

Stuck at script parsing

Open jenya7 opened this issue 2 years ago • 2 comments

I test the interpreter

` poolsize = 1 * 1024; line = 1;

// allocate memory for virtual machine
if (!(text = old_text = malloc(poolsize)))
    return -1;

if (!(data = malloc(poolsize))) 
    return -1;

if (!(stack = malloc(poolsize))) 
    return -1;

if (!(symbols = malloc(poolsize)))
    return -1;

memset(text, 0, poolsize);
memset(data, 0, poolsize);
memset(stack, 0, poolsize);
memset(symbols, 0, poolsize);
bp = sp = (int *)((int)stack + poolsize);
ax = 0;

src = "char else enum if int return sizeof while "
      "open read close printf malloc memset memcmp exit void main";

// add keywords to symbol table
i = Char;
while (i <= While) 
{
    next();
    current_id[Token] = i++;
}

// add library to symbol table
i = OPEN;
while (i <= EXIT) 
{
    next();
    current_id[Class] = Sys;
    current_id[Type] = INT;
    current_id[Value] = i++;
}

next(); current_id[Token] = Char;
next(); idmain = current_id; 

src = "int vara; int varb; while (1) { vara=2; varb=3; int varc = vara + varb; if (varc >= 5) return varc; }";

 program();

` It stuck at program() after while at (1). What do I do wrong?

jenya7 avatar Jan 09 '22 11:01 jenya7

@jenya7 All variables should be declared before using. In your code, varc is not declared first. Checkout the example below:

#include <stdio.h>

int main()
{
    int vara;
    int varb;
    int varc;
    while (1)
    {
        vara=2;
        varb=3;
        varc = vara + varb;
        if (varc >= 5)
        return varc;
    };
}

lotabout avatar Jan 09 '22 12:01 lotabout

Still it stops at (1). May be it's mandatory to include all code in void main() { } ?

I see. Thank you, it works.

May I port it to an embedded system (a micro controller) as is?

jenya7 avatar Jan 09 '22 12:01 jenya7