/** * Parses the keyword "return" from the given token stack */ int CodeParser::parseKeyword_Return( Scope* scope, TokenStack* stack ) { // get the scope type ScopeType type = scope->getScopeType(); if( type != METHOD_SCOPE ) { SYNTAX_ERROR( "Statement is only permissible within a method block", stack->last() ); return -1; } // get the method instance Method* method = (Method*)scope; // TODO support "return value" syntax // add the instruction INSTRUCTION_T instruction = RET << A_OP; method->getCodeBuffer()->putInstruction( instruction ); return 0; }
/** * Parses a method definition */ int CodeParser::parseMethod( Scope* scope, const char* name, TypeReference* type, TokenStack* stack ) { int errorCode = 0; const char* signature; list<Parameter*> params; Method *method; // step through the tokens ... int step = 0; bool done = false; while( !errorCode && !done && stack->hasNext() ) { switch( ++step ) { // step 1: create the method case 1: // get the method parameters if( !( errorCode = parseMethodParameters( scope, stack, params ) ) ) { // create the method signature signature = generateSignature( name, params ); // create the method printf( "CodeParser::parseMethod - method '%s' signature = '%s' modifiers = %s\n", name, signature, toBinary( this->modifiers ) ); method = new Method( scope, name, signature, type, this->modifiers ); } // reset the modifier for the current scope this->modifiers = 0; break; // step 2: process the method code block case 2: // must be a code block if( stack->peek()->getType() != tok::CODE_BLOCK ) { SYNTAX_ERROR( "symbol '{' expected", stack->next() ); return NULL; } // process the method code block errorCode = parse( method, ((ComplexToken*)stack->next())->getChildren() ); method->getCodeBuffer()->putInstruction( RET << A_OP ); // RET done = true; break; } } // is there already an error code? if( errorCode ) { return errorCode; } // did it complete? if( !done ) { SYNTAX_ERROR( "unexpected end of statement", stack->last() ); return -1; } // add the method to the scope if( scope->getScopeType() != CLASS_SCOPE ) { SYNTAX_ERROR( "method cannot be defined here", stack->last() ); return -1; } // add the method to the scope ScopeContainer* container = (ScopeContainer*)scope; container->addMethod( method ); return 0; }