def typ(): """ Parses type = ident | "array" "[" expression ".." expression "]" "of" type | "record" typedIds {";" typedIds} "end". Returns a type descriptor """ if SC.sym not in FIRSTTYPE: getSym() mark("type expected", 37) while SC.sym not in FIRSTTYPE | FOLLOWTYPE | STRONGSYMS: getSym() if SC.sym == IDENT: ident = SC.val x = find(ident) getSym() if type(x) == Type: x = Type(x.tp) else: mark('not a type', 38) elif SC.sym == ARRAY: getSym() if SC.sym == LBRAK: getSym() else: mark("'[' expected", 39) x = expression() if type(x) != Const or x.val < 0: mark('bad lower bound', 40) if SC.sym == PERIOD: getSym() else: mark("'.' expected", 41) if SC.sym == PERIOD: getSym() else: mark("'.' expected", 42) y = expression() if type(y) != Const or y.val < x.val: mark('bad upper bound', 43) if SC.sym == RBRAK: getSym() else: mark("']' expected", 44) if SC.sym == OF: getSym() else: mark("'of' expected", 45) z = typ().tp l = y.val - x.val + 1 x = Type(genArray(Array(z, x.val, l))) elif SC.sym == RECORD: getSym() openScope() typedIds(Var) while SC.sym == SEMICOLON: getSym() typedIds(Var) if SC.sym == END: getSym() else: mark("'end' expected", 46) r = topScope() closeScope() x = Type(genRec(Record(r))) else: mark("type expected", 47) x = Type(None) return x
def typ(): if SC.sym not in FIRSTTYPE: getSym() mark("type expected") while SC.sym not in FIRSTTYPE | STRONGSYMS | FOLLOWTYPE: getSym() if SC.sym == IDENT: ident = SC.val x = find(ident) getSym() if type(x) == Type: x = Type(x.val) else: mark('not a type') x = Type(None) elif SC.sym == ARRAY: getSym() if SC.sym == LBRAK: getSym() else: mark("'[' expected") x = expression() if SC.sym == PERIOD: getSym() else: mark("'.' expected") if SC.sym == PERIOD: getSym() else: mark("'.' expected") y = expression() if SC.sym == RBRAK: getSym() else: mark("']' expected") if SC.sym == OF: getSym() else: mark("'of' expected") z = typ().val if type(x) != Const or x.val < 0: mark('bad lower bound') x = Type(None) elif type(y) != Const or y.val < x.val: mark('bad upper bound') x = Type(None) else: x = Type(CG.genArray(Array(z, x.val, y.val - x.val + 1))) elif SC.sym == RECORD: getSym() openScope() typedIds(Var) while SC.sym == SEMICOLON: getSym() typedIds(Var) if SC.sym == END: getSym() else: mark("'end' expected") r = topScope() closeScope() x = Type(CG.genRec(Record(r))) else: x = Type(None) return x
def typ(): if SC.sym == IDENT: ident = SC.val; x = find(ident) if type(x) == Type: x = Type(x.val); getSym() else: mark('type identifier expected') elif SC.sym == LBRAK: getSym(); x = expression() if SC.sym == DOTDOT: getSym() else: mark("'..' expected") y = expression() if SC.sym == RBRAK: getSym() else: mark("']' expected") if SC.sym == RARROW: getSym() else: mark("'→' expected") z = typ().val; if type(x) != Const or x.val < 0: mark('bad lower bound') elif type(y) != Const or y.val < x.val: mark('bad upper bound') else: x = Type(CG.genArray(Array(z, x.val, y.val - x.val + 1))) elif SC.sym == LPAREN: getSym(); openScope(); typedIds() if SC.sym == RPAREN: getSym() else: mark("')' expected") r = topScope(); closeScope() x = Type(CG.genRec(Record(r))) elif SC.sym == FULLPAREN: pass elif SC.sym == SET: getSym(); if SC.sym == LBRAK: getSym() else: mark ("'[' expected") x = expression() if SC.sym == DOTDOT: getSym() else: mark("'..' expected") y = expression() if SC.sym == RBRAK: getSym() else: mark("']' expected") if type(x) != Const: mark('bad lower bound') elif type(y) != Const or y.val < x.val: mark('bad upper bound') else: x = Type(CG.genSet(Set(x.val, y.val - x.val + 1))) else: mark('type expected') return x
def typ(): """ Parses type = ident | "array" "[" expression ".." expression "]" "of" type | "record" typedIds {";" typedIds} "end". Returns a type descriptor """ global depth if SC.sym not in FIRSTTYPE: getSym() mark("type expected") while SC.sym not in FIRSTTYPE | STRONGSYMS | FOLLOWTYPE: getSym() if SC.sym == IDENT: # attribute ident = SC.val write(ident) writeHtml(ident, _class='ident') # x = find(ident) getSym() if type(x) == Type: x = Type(x.tp) else: mark('not a type') x = Type(None) elif SC.sym == ARRAY: depth += 1 # attribute writeln() write(indent * depth) write('array ') writeHtmlLn() writeHtml(htmlIndent * depth) writeHtml('array ') # getSym() if SC.sym == LBRAK: # attribute write('[') writeHtml('[') # getSym() else: mark("'[' expected") x = expression() if SC.sym == PERIOD: # attribute write(' .') writeHtml(' .') # getSym() else: mark("'.' expected") if SC.sym == PERIOD: # attribute write('. ') writeHtml('. ') # getSym() else: mark("'.' expected") y = expression() if SC.sym == RBRAK: # attribute write('] ') writeHtml('] ') # getSym() else: mark("']' expected") if SC.sym == OF: # attribute write('of') writeHtml('of') # getSym() else: mark("'of' expected") z = typ().tp if type(x) != Const or x.val < 0: mark('bad lower bound') x = Type(None) elif type(y) != Const or y.val < x.val: mark('bad upper bound') y = Type(None) else: x = Type(CG.genArray(Array(z, x.val, y.val - x.val + 1))) depth -= 1 elif SC.sym == RECORD: # attributes depth += 1 writeln() write(indent * depth) write('record') writeln() writeHtmlLn() writeHtml(htmlIndent * depth) writeHtml('record') writeHtmlLn() depth += 1 # getSym() # attributes write(indent * depth) write(SC.val) writeHtml(htmlIndent * depth) writeHtml(SC.val) # openScope() typedIds(Var) while SC.sym == SEMICOLON: # attributes write(';') writeln() writeHtml(';') writeHtmlLn() # getSym() # attributes write(indent * depth) write(SC.val) writeHtml(htmlIndent * depth) writeHtml(SC.val) # typedIds(Var) # attributes writeln() writeHtmlLn() depth -= 1 write(indent * depth) writeHtml(htmlIndent * depth) # if SC.sym == END: # attributes write('end') writeHtml('end') # getSym() else: mark("'end' expected") r = topScope() closeScope() x = Type(CG.genRec(Record(r))) depth -= 1 else: x = Type(None) return x
def genArray(a: Array): a.size = a.length * a.base.size return a
def typ(l): """ Parses type(l) = ident «write(ident)» | "array" «writeln; write((l + 1) * indent + 'array ')» "[" «write('[')» expression ".." «write(' .. ')» expression "]" «write(']')» "of" «write(' of ')» type(l)| "record" «writeln; write((l + 1) * indent + 'record')» typedIds(l) {";" «write(';')» typedIds(l)} "end" «writeln; write((l + 1) * indent + 'end')». Returns a type descriptor """ if SC.sym not in FIRSTTYPE: getSym() mark("type expected") while SC.sym not in FIRSTTYPE | STRONGSYMS | FOLLOWTYPE: getSym() if SC.sym == IDENT: write(SC.val) ident = SC.val x = find(ident) getSym() if type(x) == Type: x = Type(x.tp) else: mark('not a type') x = Type(None) elif SC.sym == ARRAY: writeln() write((l + 1) * indent + 'array ') getSym() if SC.sym == LBRAK: write('[') getSym() else: mark("'[' expected") x = expression() if SC.sym == PERIOD: write(' .') getSym() else: mark("'.' expected") if SC.sym == PERIOD: write('. ') getSym() else: mark("'.' expected") y = expression() if SC.sym == RBRAK: write(']') getSym() else: mark("']' expected") if SC.sym == OF: write(' of ') getSym() else: mark("'of' expected") z = typ(l + 1).tp if type(x) != Const or x.val < 0: mark('bad lower bound') x = Type(None) elif type(y) != Const or y.val < x.val: mark('bad upper bound') y = Type(None) else: x = Type(CG.genArray(Array(z, x.val, y.val - x.val + 1))) elif SC.sym == RECORD: writeln() write((l + 1) * indent + 'record ') writeln() write((l + 2) * indent) getSym() openScope() typedIds(Var, l + 2) while SC.sym == SEMICOLON: write(';') writeln() write((l + 2) * indent) getSym() typedIds(Var, l + 2) if SC.sym == END: writeln() write((l + 1) * indent + 'end') getSym() else: mark("'end' expected") r = topScope() closeScope() x = Type(CG.genRec(Record(r))) else: x = Type(None) return x