Feedback for New Interpreter

I don't really see anything that's "interactive", just a means of contracting the source of the interpreted language further.

I don't think I ever understood this comment

// A = [0], B = [1], C = [2], D = [3],

@SemperIdem

The numbers in brackets are the register numbers.

A to D stands for general purpose registers 0 to 3.

? and ?? are the registers all the jmps save for JMP use for comparison, basically you need to use these registers for those jmps to work.

SP is the push/pop depth counter.

LPT stands for loop till, it is used for the loop function.

LPA(1) and LPA(2) are the BEGr or BEGb location.

LPC is the loop counter this is incremented automatically with the LOOP command.

Anymore questions?

Also sorry about the "interactive", I got carried away trying to the interpreter code easier to type in.

I think I created an Interpreter on top. :wink:

No, I got the later ones, it was just the "A,B,C,D" ones that I didn't get, because you don't use them as such, or at least you don't enforce them as such, which also why I commented earlier about bounds-checking the register array.

(It's "any more questions")

@SemperIdem
Here is a better interactive. :slight_smile:

int addr = 0;
String strbuf;

void loop() {
  if (Serial.available()) {
    strbuf = Serial.readString();
    strbuf.toUpperCase();
    Serial.print(strbuf);
    strbuf = strbuf.substring(0, strbuf.length()-1);
  }
  if (strbuf == "LOAD") {}
  else if (strbuf == "NEW") {clr_mem(); Serial.println("Memory clear done.");}
  else if (strbuf == "RUN") {/*BC256(); Serial.println("Program done.");*/}
  else if (strbuf == "SAVE") {}
  else if (strbuf > "") {process_input();}
  strbuf = "";
}

// ==================================================================
// BC256 --Byte Code 256-- INTERACTIVE (1.0) "BETA"
// ==================================================================
const char instr_0[] PROGMEM = "STOP";
const char instr_1[] PROGMEM = "MOVR";
const char instr_2[] PROGMEM = "MOVB";
const char* const instrlist[] PROGMEM = {
  instr_0,instr_1,instr_2,//instr_3,instr_4,instr_5,instr_6,instr_7,instr_8,instr_9,instr_10,instr_11,instr_12,instr_13,instr_14,instr_15,instr_16,instr_17,instr_18,instr_19,instr_20,instr_21,instr_22,instr_23,instr_24,instr_25,instr_26,instr_27,instr_28,instr_29, instr_30,instr_31,instr_32,instr_33,instr_34,instr_35,instr_36,instr_37,instr_38,instr_39,instr_40,instr_41,instr_42,instr_43,instr_44,instr_45,instr_46,instr_47,instr_48,instr_49,instr_50,instr_51,instr_52,instr_53,instr_54,instr_55,instr_56,instr_57,instr_58,instr_59,instr_60,instr_61,instr_62,instr_63,instr_64,instr_65,instr_66,instr_67,instr_68,instr_69,instr_70,instr_71
};
const byte params[] PROGMEM = {
  0, 2, 2,
};

void clr_mem() {
  for (int lp = 0; lp < sizeof(pRAM); lp++) {
    pRAM[lp] = 0;
  }
  addr = 0;
}

String getInstr(byte nb) {
  char cmpstr[7];
  strcpy_P(cmpstr, (char*)pgm_read_word(&(instrlist[nb])));;
  return String(cmpstr);
}

void trimStr(byte trimfrom) {
  strbuf = strbuf.substring(trimfrom, strbuf.length());
}

byte getNum() {
  Serial.println(strbuf);
}

void process_input() {
  byte code = 0, instrargs = 0, bufindex = 0;

  bufindex = strbuf.indexOf(' ');
  if (bufindex == -1) {bufindex = strbuf.length();}
  
  Serial.println(strbuf.substring(0, bufindex));
  
  while (strbuf.substring(0, bufindex) != getInstr(code++));
  code--;
  pRAM[addr++] = code;
  
  instrargs = pgm_read_byte_near(params+code);

  Serial.println(instrargs);
  
  bufindex = strbuf.indexOf(' ');
  
  if (instrargs == 1 and bufindex != -1) {trimStr(bufindex+1); pRAM[addr++] = getNum();}
  else if (instrargs == 2 and bufindex != -1) {
    trimStr(bufindex+1); 
    bufindex = strbuf.indexOf(' ');
    if (bufindex == -1) {Serial.println("Syntax Error!"); addr--; return;}
    pRAM[addr++] = getNum(strbuf.substring(0, bufindex));
    trimStr(bufindex+1);
    pRAM[addr++] = getNum(strbuf);
  }
  return;
}

Thanks, my bad.

Aka "memset"

I'm not a fan of the String class.

What should I use instead of the String class?

Clue:

Sorry, changed the code because I thought you didn't like the String in entirety.
Here is the new code. :wink:

int addr = 0;
byte bufidx = 0;
char buf[16];

void loop() {
  memset(buf, 0, 16);
  if (Serial.available()) {
    bufidx = Serial.readBytes(buf, 16);
  }
  for (byte caps = 0; caps < bufidx; caps++) {
    if (buf[caps] >= 'a' and buf[caps] <= 'z') {buf[caps] -= ' ';}
    if (buf[caps] == 13 or buf[caps] == 10) {buf[caps] = 0;}
  }
  
  if (String(buf) == F("LOAD")) {}
  else if (String(buf) == F("NEW")) {memset(pRAM, 0, sizeof(pRAM)); addr = 0; Serial.println("Memory clear done.");}
  else if (String(buf) == F("RUN")) {BC256(); Serial.println("Program done.");}
  else if (String(buf) == F("SAVE")) {}
  else if (bufidx > 0) {process_input();}
  bufidx = 0;
}

// ==================================================================
// BC256 --Byte Code 256-- INTERACTIVE (1.0) "BETA"
// ==================================================================
const char instr_0[] PROGMEM = "STOP";
const char instr_1[] PROGMEM = "MOVR";
const char instr_2[] PROGMEM = "MOVB";
const char* const instrlist[] PROGMEM = {
  instr_0,instr_1,instr_2,//instr_3,instr_4,instr_5,instr_6,instr_7,instr_8,instr_9,instr_10,instr_11,instr_12,instr_13,instr_14,instr_15,instr_16,instr_17,instr_18,instr_19,instr_20,instr_21,instr_22,instr_23,instr_24,instr_25,instr_26,instr_27,instr_28,instr_29, instr_30,instr_31,instr_32,instr_33,instr_34,instr_35,instr_36,instr_37,instr_38,instr_39,instr_40,instr_41,instr_42,instr_43,instr_44,instr_45,instr_46,instr_47,instr_48,instr_49,instr_50,instr_51,instr_52,instr_53,instr_54,instr_55,instr_56,instr_57,instr_58,instr_59,instr_60,instr_61,instr_62,instr_63,instr_64,instr_65,instr_66,instr_67,instr_68,instr_69,instr_70,instr_71
};
const byte params[] PROGMEM = {
  0, 2, 2,
};

String getInstr(byte nb) {
  char cmpstr[7];
  strcpy_P(cmpstr, (char*)pgm_read_word(&(instrlist[nb])));
  return cmpstr;
}

void process_input() {
  char buf2[7];
  int code = 0;
  byte bufindex = 0, instrargs = 0;
  bool errors = false;

  errors = 0;
  while (buf[bufindex++] != ' ') {if (buf[bufindex-1] == 0) {errors = 1; break;}};

  memset(buf2, 0, 7);
  memcpy(buf2, buf, bufindex-1);

  Serial.println(buf);
  
  while (String(buf2) != getInstr(code++));
  code--;
  pRAM[addr++] = code;
  
  instrargs = pgm_read_byte_near(params+code);

  Serial.println(instrargs);

  errors = 0;
  code = bufindex; bufindex = 0;
  while (buf[code+bufindex] != ' ') {if (buf[code+bufindex] == 0) {errors = 1; break;} bufindex++;};

  if (instrargs == 1 and errors != 1) {}
  else if (instrargs == 2 and errors != 1) {
    memset(buf2, 0, 7);
    memcpy(buf2, buf+code, bufindex);
    Serial.println(buf2[0] - '0');
    pRAM[addr++] = buf2[0] - '0';
  } else if (errors == 1 and instrargs > 0) {Serial.print("Syntax Error With "); Serial.println(buf); addr--;}
  return;
}

Have fun!

That's correct, if I don't have to use it (I'm looking at you ESP32) then I won't.

Also, hint: "topper()"

"toupper()" doesn't work with the char array.

I changed the code 6=in post #29, I'm so sorry.
I'm a bit nuts I must say. :laughing:

Sorry, about the toupper - auto-correct.
Of course it doesn't work on an array, but it does work on the elements.

If you look closely at the String class, you'll see it is largely underpinned by simple C-style string operations, so cut out the bloated middle-man, I say!
I often stop reading when I see the word String in a sketch.

Here is the newly built code, see what you think.
BC256-1_0.ino (20.6 KB)
Thanks for all the tips. :wink:

@anon56112670
The ever so popular blink...

Interactive demo. :smile:

@anon56112670
The ever so popular fade program...

Here is the pretty much finished code.
BC256-1_0.ino (22.6 KB)

Has a bug fix for BEGR, ADDR & ADDB.
Also added CLEAN, cmd.

Any problems you see?

Sorry didn't see that post, will try. :wink:

Here is the pretty much final code:
BC256-1_0.ino (29.4 KB)
With a basic manual:
BC256_basic_manual.pdf (290.2 KB)

A lot of thanks to @anon56112670,
couldn't of done it without you. :laughing:

sp. "couldn't have done"

(I can't look at it right now, I've only got my phone, so I can't see the code)

Just what I was thinking,
only it came out kind of wrong. :smile:
Thanks again.

No rush on reviewing anything.