Skip to content
al2698 edited this page Jun 14, 2022 · 1 revision

enum

enum { // token : 0-127 直接用該字母表達, 128 以後用代號。
  Num = 128, Fun, Sys, Glo, Loc, Id,
  Char, Else, Enum, If, Int, Return, Sizeof, While, Do,
  Assign, Cond, Lor, Lan, Or, Xor, And, Eq, Ne, Lt, Gt, Le, Ge, Shl, Shr, Add, Sub, Mul, Div, Mod, Inc, Dec, Brak
};

compile

int compile(int fd) {
  int i, *t;
  // 編譯器
  p = "char else enum if int return sizeof while do"
      "open read write close printf malloc free memset memcmp exit void main";
  //i = Char; while (i <= While) { next(); id[Tk] = i++; } // add keywords to symbol table
  i = Char; while(i <= Do){next(); id[Tk] = i++;}
  i = OPEN; while (i <= EXIT) { next(); id[Class] = Sys; id[Type] = INT; id[Val] = i++; } // add library to symbol table
  next(); id[Tk] = Char; // handle void type
  next(); idmain = id; // keep track of main

  if (!(lp = p = malloc(poolsz))) { printf("could not malloc(%d) source area\n", poolsz); return -1; }
  if ((i = read(fd, p, poolsz-1)) <= 0) { printf("read() returned %d\n", i); return -1; }
  p[i] = 0; // 設定程式 p 字串結束符號 \0

  return prog();
}
Clone this wiki locally