コード例 #1
0
ファイル: Parser.cpp プロジェクト: Newky/3rdYear
void Parser::ProcDecl() {
    wchar_t* name;
    Obj *obj;
    int adr;
    Expect(9);
    Ident(name);
    obj = tab->NewObj(name, proc, undef);
    obj->adr = gen->pc;
    if (coco_string_equal(name, L"Main")) gen->progStart = gen->pc;
    tab->OpenScope();
    Expect(10);
    Expect(11);
    Expect(12);
    gen->Emit(ENTER, 0);
    adr = gen->pc - 2;
    while (StartOf(1)) {
        if (la->kind == 28 || la->kind == 29) {
            VarDecl();
        } else {
            Stat();
        }
    }
    Expect(13);
    gen->Emit(LEAVE);
    gen->Emit(RET);
    gen->Patch(adr, tab->topScope->nextAdr);
    tab->CloseScope();
}
コード例 #2
0
ファイル: Parser.cpp プロジェクト: feimengspirit/compiler
void Parser::VarDecls()
{
	if(look.kind == PUBLIC || look.kind == LBRACE
		|| look.kind == IF || look.kind == WHILE
		|| look.kind == SYSTEM_OUT_PRINTLN 
		|| look.kind == ID)
		return;
	VarDecl();
	VarDecls();
}
コード例 #3
0
statementBlock* sintactico::Block()
{
    if(CurrentToken->tipo == LLAVE_I)
    {
        list<var_decl*> *var_declarations = new list<var_decl*>();
        list<statement*> *statements = new list<statement*>();
        CurrentToken = Lexer->NexToken();
        while(CurrentToken->tipo == KWBOOL || CurrentToken->tipo == KWINT)
        {
            statementDeclaration::Type type;

            if(CurrentToken->tipo == KWBOOL){
                type = statementDeclaration::BOOL;
            }else{
                type = statementDeclaration::INT;
            }
            var_decl* varDecl = VarDecl();
            varDecl->setType(type);

            var_declarations->push_back(varDecl);
            if(Fallo)
                return NULL;

        }
        while(CurrentToken->tipo == IDENTIFICADOR || CurrentToken->tipo == KWIF || CurrentToken->tipo == KWWHILE
              ||CurrentToken->tipo == KWFOR || CurrentToken->tipo == KWRETURN || CurrentToken->tipo == KWBREAK
              ||CurrentToken->tipo == KWBREAK || CurrentToken->tipo == KWCONTINUE || CurrentToken->tipo == KWPRINT
              ||CurrentToken->tipo == KWREAD || CurrentToken->tipo == LLAVE_I)
        {
            statements->push_back(Statement());//aqui hay problemas
            if(Fallo)
                return NULL;
        }

        if(CurrentToken->tipo != LLAVE_D)
        {
            Fallo = true;
            MensajeError("Se esperaba '}'",Lexer->linea);
            return NULL;
        }
        CurrentToken = Lexer->NexToken();
        return new statementBlock(var_declarations, statements,Lexer->linea);
    }
    else
    {
        Fallo = true;
        MensajeError("Se esperaba '}'",Lexer->linea);
        return NULL;
    }

}
コード例 #4
0
ファイル: Parser.cpp プロジェクト: Newky/3rdYear
void Parser::Taste() {
    wchar_t* name;
    InitDeclarations();
    Expect(27);
    Ident(name);
    tab->OpenScope();
    Expect(12);
    while (la->kind == 28 || la->kind == 29) {
        VarDecl();
    }
    while (la->kind == 9) {
        ProcDecl();
    }
    Expect(13);
    tab->CloseScope();
}
コード例 #5
0
ファイル: Parser.cpp プロジェクト: Newky/3rdYear
void Parser::Stat() {
    int type;
    wchar_t* name;
    Obj *obj;
    int adr, adr2, loopstart;
    switch (la->kind) {
    case 1: {
        Ident(name);
        obj = tab->Find(name);
        if (la->kind == 17) {
            Get();
            if (obj->kind != var) Err(L"cannot assign to procedure");
            Expr(type);
            Expect(18);
            if (type != obj->type) Err(L"incompatible types");
            if (obj->level == 0) gen->Emit(STOG, obj->adr);
            else gen->Emit(STO, obj->adr);
        } else if (la->kind == 10) {
            Get();
            Expect(11);
            Expect(18);
            if (obj->kind != proc) Err(L"object is not a procedure");
            gen->Emit(CALL, obj->adr);
        } else SynErr(36);
        break;
    }
    case 1: {
        Ident(name);
        obj = tab->Find(name);
        Expect(17);
        Expr(type);
        Expect(19);
        Expr(type2);
        Expect(20);
        Expr(type3);
        Expect(18);
        break;
    }
    case 21: {
        Get();
        Expect(10);
        Expr(type);
        Expect(11);
        if (type != boolean) Err(L"boolean type expected");
        gen->Emit(FJMP, 0);
        adr = gen->pc - 2;
        Stat();
        if (la->kind == 22) {
            Get();
            gen->Emit(JMP, 0);
            adr2 = gen->pc - 2;
            gen->Patch(adr, gen->pc);
            adr = adr2;
            Stat();
        }
        gen->Patch(adr, gen->pc);
        break;
    }
    case 23: {
        Get();
        loopstart = gen->pc;
        Expect(10);
        Expr(type);
        Expect(11);
        if (type != boolean) Err(L"boolean type expected");
        gen->Emit(FJMP, 0);
        adr = gen->pc - 2;
        Stat();
        gen->Emit(JMP, loopstart);
        gen->Patch(adr, gen->pc);
        break;
    }
    case 24: {
        Get();
        loopstart = gen->pc;
        Expect(10);
        Expr(ty);
        Expect(18);
        Expr(type);
        Expect(18);
        Expr(ty);
        Expect(11);
        if (type != boolean) Err(L"boolean type expected");
        gen->Emit(FJMP, 0);
        adr = gen->pc - 2;
        Stat();
        break;
    }
    case 25: {
        Get();
        Ident(name);
        Expect(18);
        obj = tab->Find(name);
        if (obj->type != integer) Err(L"integer type expected");
        gen->Emit(READ);
        if (obj->level == 0) gen->Emit(STOG, obj->adr);
        else gen->Emit(STO, obj->adr);
        break;
    }
    case 26: {
        Get();
        Expr(type);
        Expect(18);
        if (type != integer) Err(L"integer type expected");
        gen->Emit(WRITE);
        break;
    }
    case 12: {
        Get();
        while (StartOf(1)) {
            if (StartOf(2)) {
                Stat();
            } else {
                VarDecl();
            }
        }
        Expect(13);
        break;
    }
    default:
        SynErr(37);
        break;
    }
}
コード例 #6
0
ファイル: parser.c プロジェクト: emarteca/Compiler
void DeclSeq()
{
  /*
    DeclSeq -> { CONST { ConstDecl ; }
                 | TYPE { TypeDecl ; } 
                 | VAR { VarDecl ; } }
                 { ProcDecl ; | ForwardDecl ; }
  */

  if ( debugMode) printf( "In DeclSeq\n");

  while ( sym == CONST_SYM || sym == TYPE_SYM || sym == VAR_SYM)
  {
    writesym();
    switch( sym)
    {
      case CONST_SYM:
        nextsym();
        // start of ConstDecl is an ident
        while ( sym == ident)
        {
          ConstDecl();
          accept( semic, 151);
        }
        break;
      case TYPE_SYM:
        nextsym();
        while ( sym == ident)
        {
          TypeDecl();
          accept( semic, 151);
        }
        break;
      case VAR_SYM:
        nextsym();
        while ( sym == ident)
        {
          VarDecl();
          accept( semic, 151);
        }
    }
  }  

  while ( sym == PROCEDURE_SYM)
  {
    writesym();
    nextsym();
    if ( sym == carat) 
    {
      // then it's ForwardDecl
      writesym();
      nextsym();
      if ( sym != ident)
      {
        Receiver();
      }
      identdef();
      if ( sym == lparen)
      {
        FormParams();
      }
    }
    else
    {
      // it's ProcDecl
      if ( sym != ident)
      {
        Receiver();
      }
      identdef();
      if ( sym == lparen)
      {
        FormParams();
      }
      accept( semic, 151);
      ProcBody();
      accept( ident, 124);
    }
    accept( semic, 151);
  }

  if ( debugMode) printf( "Out DeclSeq\n");
}
コード例 #7
0
			)
			EmptyParaList()
		)
		TypeList(
			Int
			EmptyTypeList()
		)
		CompStmt(
			StmtList(
				DeclarationStmt(
					VarDecl(
						result
						TypeList(
							Int
							EmptyTypeList()
						)
						VarInitializer(
							IntLiteral(
								0
							)
						)
					)
					EmptyDeclarationList()
				)
				StmtList(
					IfThenElseStmt(
						BinExpr(
							VarExpr(
								n
							)
							>
							IntLiteral(