def program(): """ Parses program = "program" ident ";" declarations compoundStatement. Generates code if no error is reported """ newObj('boolean', Type(Bool)) Bool.size = 8 # 64 bit sizes newObj('integer', Type(Int)) Int.size = 8 newObj('true', Const(Bool, 1)) newObj('false', Const(Bool, 0)) newObj('read', StdProc([Ref(Int)])) newObj('write', StdProc([Var(Int)])) newObj('writeln', StdProc([])) progStart() if SC.sym == PROGRAM: getSym() else: mark("'program' expected", 64) ident = SC.val if SC.sym == IDENT: getSym() else: mark('program name expected', 65) if SC.sym == SEMICOLON: getSym() else: mark('; expected', 66) declarations(genGlobalVars) progEntry(ident) x = compoundStatement() return progExit(x)
def program(): newDecl('boolean', Type(CG.genBool(Bool))) newDecl('integer', Type(CG.genInt(Int))) newDecl('true', Const(Bool, 1)) newDecl('false', Const(Bool, 0)) newDecl('read', StdProc([Ref(Int)])) newDecl('write', StdProc([Var(Int)])) newDecl('writeln', StdProc([])) CG.genProgStart() if SC.sym == PROGRAM: getSym() else: mark("'program' expected") ident = SC.val if SC.sym == IDENT: getSym() else: mark('program name expected') if SC.sym == SEMICOLON: getSym() else: mark('; expected') declarations(CG.genGlobalVars) CG.genProgEntry(ident) x = compoundStatement() return CG.genProgExit(x)
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 program(): """ Parses program = "program" «write('program ')» ident «write(ident)» ";" «write(';')» declarations compoundStatement(1). Generates code if no error is reported """ newObj('boolean', Type(Bool)) Bool.size = 4 newObj('integer', Type(Int)) Int.size = 4 newObj('true', Const(Bool, 1)) newObj('false', Const(Bool, 0)) newObj('read', StdProc([Ref(Int)])) newObj('write', StdProc([Var(Int)])) newObj('writeln', StdProc([])) CG.progStart() if SC.sym == PROGRAM: # write('program ') writeHtml('program ') # getSym() else: mark("'program' expected") ident = SC.val if SC.sym == IDENT: # write(ident) writeHtml(ident, _class='ident') # getSym() else: mark('program name expected') if SC.sym == SEMICOLON: getSym() # write(';') writeln() writeHtml(';') writeHtmlLn() # else: mark('; expected') declarations(CG.genGlobalVars) CG.progEntry(ident) x = compoundStatement(1) return CG.progExit(x)
def program(): newDecl('boolean', Type(CG.genBool(Bool))) newDecl('integer', Type(CG.genInt(Int))) newDecl('true', Const(Bool, 1)) newDecl('false', Const(Bool, 0)) newDecl('read', StdProc([], [Var(Int)])) newDecl('write', StdProc([Var(Int)], [])) newDecl('writeln', StdProc([], [])) CG.genProgStart() declarations(CG.genGlobalVars) if SC.sym == PROGRAM: getSym() else: mark("'program' expected") ident = SC.val if SC.sym == IDENT: getSym() else: mark('program name expected') openScope(); CG.genProgEntry(ident); x = body(ident, 0) closeScope(); x = CG.genProgExit(x) 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(): 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(): """ 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 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