public SymbolTable(Parser parser) { this.parser = parser; topScope = null; curLevel = -1; undefObj = new Obj(); undefObj.name = "undef"; undefObj.type = undef; undefObj.kind = var; undefObj.sort = scalar; undefObj.constval = "0"; undefObj.length = 1; undefObj.adr = 0; undefObj.level = 0; undefObj.next = null; }
using System;
// create a new object node in the current scope public Obj NewObj(string name, int kind, int type, int sort, string constval, int length) { Obj p, last, obj = new Obj(); obj.name = name; obj.kind = kind; obj.type = type; obj.sort = sort; obj.constval = constval; obj.length = length; obj.level = curLevel; p = topScope.locals; last = null; while (p != null) { if (p.name == name) parser.SemErr("name declared twice"); last = p; p = p.next; } if (last == null) topScope.locals = obj; else last.next = obj; if (kind == var && sort == scalar) obj.adr = topScope.nextAdr++; if (kind == var && sort == array) {obj.adr = topScope.nextAdr++; topScope.nextAdr = topScope.nextAdr + length; } return obj; }
// close the current scope public void CloseScope() { topScope = topScope.next; curLevel--; }
// open a new scope and make it the current scope (topScope) public void OpenScope() { Obj scop = new Obj(); scop.name = ""; scop.kind = scope; scop.constval = ""; scop.locals = null; scop.nextAdr = 0; scop.next = topScope; topScope = scop; curLevel++; }