def declarations(allocVar): while SC.sym == CONST: getSym() if SC.sym == IDENT: ident = SC.val; getSym() else: mark("constant name expected") if SC.sym == EQ: getSym() else: mark("= expected") x = expression() if type(x) == Const: newDecl(ident, x) else: mark('expression not constant') while SC.sym == TYPE: getSym() if SC.sym == IDENT: ident = SC.val; getSym() else: mark("type name expected") if SC.sym == EQ: getSym() else: mark("= expected") x = typ(); newDecl(ident, x) # x is of type ST.Type start = len(topScope()) while SC.sym == VAR: getSym(); typedIds() var = allocVar(topScope(), start) while SC.sym == PROCEDURE: getSym() if SC.sym == LPAREN: getSym() if SC.sym == IDENT: r = SC.val; getSym() else: mark("identifier expected") if SC.sym == COLON: getSym() else: mark("':' expected") tp = typ().val if SC.sym == RPAREN: getSym() else: mark(") expected") else: r = None if SC.sym == IDENT: ident = SC.val; getSym() else: mark("procedure name expected") newDecl(ident, Proc([], [])) # entered without parameters sc = topScope(); openScope() # new scope for parameters and body if r: newDecl(r, Var(tp)) if SC.sym == LPAREN: getSym() else: mark("( expected") if SC.sym == IDENT: typedIds() fp = topScope() if SC.sym == RPAREN: getSym() else: mark(") expected") d = len(fp) if SC.sym == RARROW: getSym() if SC.sym == LPAREN: getSym() else: mark('( expected') typedIds() if SC.sym == RPAREN: getSym() else: mark(') expected') sc[-1].par, sc[-1].res = fp[:d], fp[d:] # procedure parameters updated para = CG.genProcStart(ident, fp[:d], fp[d:]) body(ident, para); closeScope() # scope for parameters and body closed return var
def typedIds(kind): if SC.sym == IDENT: tid = [SC.val] getSym() else: mark("identifier expected") tid = [] while SC.sym == COMMA: getSym() if SC.sym == IDENT: tid.append(SC.val) getSym() else: mark('identifier expected') if SC.sym == COLON: getSym() tp = typ().val if tp != None: for i in tid: newDecl(i, kind(tp)) else: mark("':' expected")
def typedIds(): if SC.sym == IDENT: tid = [SC.val]; getSym() else: mark("identifier expected") while SC.sym == COMMA: getSym() if SC.sym == IDENT: tid.append(SC.val); getSym() else: mark('identifier expected') if SC.sym == COLON: getSym() else: mark("':' expected") tp = typ().val for i in tid: newDecl(i, Var(tp)) while SC.sym == COMMA: getSym() if SC.sym == IDENT: tid = [SC.val]; getSym() else: mark("identifier expected") while SC.sym == COMMA: getSym() if SC.sym == IDENT: tid.append(SC.val); getSym() else: mark('identifier expected') if SC.sym == COLON: getSym() else: mark("':' expected") tp = typ().val for i in tid: newDecl(i, Var(tp))
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 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 declarations(allocVar): if SC.sym not in FIRSTDECL | FOLLOWDECL: getSym() mark("'begin' or declaration expected") while SC.sym not in FIRSTDECL | STRONGSYMS | FOLLOWDECL: getSym() while SC.sym == CONST: getSym() if SC.sym == IDENT: ident = SC.val getSym() if SC.sym == EQ: getSym() else: mark("= expected") x = expression() if type(x) == Const: newDecl(ident, x) else: mark('expression not constant') else: mark("constant name expected") if SC.sym == SEMICOLON: getSym() else: mark("; expected") while SC.sym == TYPE: getSym() if SC.sym == IDENT: ident = SC.val getSym() if SC.sym == EQ: getSym() else: mark("= expected") x = typ() newDecl(ident, x) # x is of type ST.Type if SC.sym == SEMICOLON: getSym() else: mark("; expected") else: mark("type name expected") start = len(topScope()) while SC.sym == VAR: getSym() typedIds(Var) if SC.sym == SEMICOLON: getSym() else: mark("; expected") varsize = allocVar(topScope(), start) while SC.sym == PROCEDURE: getSym() if SC.sym == IDENT: getSym() else: mark("procedure name expected") ident = SC.val newDecl(ident, Proc([])) # entered without parameters sc = topScope() CG.genProcStart() openScope() # new scope for parameters and body if SC.sym == LPAREN: getSym() if SC.sym in {VAR, IDENT}: if SC.sym == VAR: getSym() typedIds(Ref) else: typedIds(Var) while SC.sym == SEMICOLON: getSym() if SC.sym == VAR: getSym() typedIds(Ref) else: typedIds(Var) else: mark("formal parameters expected") fp = topScope() sc[-1].par = fp[:] # procedure parameters updated if SC.sym == RPAREN: getSym() else: mark(") expected") else: fp = [] parsize = CG.genFormalParams(fp) if SC.sym == SEMICOLON: getSym() else: mark("; expected") localsize = declarations(CG.genLocalVars) CG.genProcEntry(ident, parsize, localsize) x = compoundStatement() CG.genProcExit(x, parsize, localsize) closeScope() # scope for parameters and body closed if SC.sym == SEMICOLON: getSym() else: mark("; expected") return varsize