/** * Parses the "new" keyword */ int CodeParser::parseKeyword_New( Scope* scope, TokenStack* stack ) { int errorCode = 0; // parses the object definition: new string("hello"); // get the object type TypeReference* type = parseType( scope, stack ); // parse the method parameters list<Parameter*> params; if( ( errorCode = parseMethodParameters( scope, stack, params ) ) ) { return errorCode; } // setup code to create the object on the stack // PARAMS params // NEWINST type CodeBuffer* cb = scope->getCodeBuffer(); OFFSET_T offset1 = cb->putInstruction( PARAMS << A_OP ); OFFSET_T offset2 = cb->putInstruction( NEWINST << A_OP ); // add the parameter and reference data scope->addParameterData( new ParameterData( state->nextParamId(), params, offset1 ) ); scope->addReferenceData( new ReferenceData( state->nextDataRefId(), type->getName(), offset2 ) ); return errorCode; }
/** * Sets up code to copy the address of the given data into * the given field at runtime */ void CodeParser::setupAssignment( Scope* scope, Field* field, const char* value ) { // get scope's code buffer CodeBuffer* cb = scope->getCodeBuffer(); // setup the assignment: field = data (e.g. name = "results") OFFSET_T offset1 = cb->putInstruction( STAGE << A_OP | ARG_DIRECT << A_ARG ); // STAGE data OFFSET_T offset2 = cb->putInstruction( SETPTR << A_OP | ARG_PTR << A_ARG ); // SETPTR field // setup the reference data scope->addReferenceData( new ReferenceData( state->nextDataRefId(), value, offset1 ) ); scope->addReferenceData( new ReferenceData( state->nextDataRefId(), field->getName(), offset2 ) ); }
/** * Parses the method call within an expression */ int CodeParser::parseExpression_MethodCall( const char* methodName, Scope* scope, TokenStack* stack ) { int errorCode = 0; list<Parameter*> params; // parse the method parameters if( ( errorCode = parseMethodParameters( scope, stack, params ) ) ) { return errorCode; } // generate the method signature const char* signature = generateSignature( methodName, params ); // lookup the method Method* method = lookupMethod( scope, signature ); if( method == NULL ) { // generate the error message char* errorMessage = new char[256]; sprintf( errorMessage, "Method '%s' could not be found", signature ); // display the error message SYNTAX_ERROR( errorMessage, stack->last() ); delete errorMessage; return -1; } else { printf( "CodeParser::parseExpression: Method call - [%s]\n", signature ); // get the code buffer CodeBuffer* cb = scope->getCodeBuffer(); // parse the method parameter // setup the method call // instruction: PARAMS methodParams // instruction: INVOKE methodSig OFFSET_T offset1 = cb->putInstruction( PARAMS << A_OP ); OFFSET_T offset2 = cb->putInstruction( INVOKE << A_OP ); // add the parameter and reference data scope->addParameterData( new ParameterData( state->nextParamId(), params, offset1 ) ); scope->addReferenceData( new ReferenceData( state->nextDataRefId(), signature, offset2 ) ); return 0; } }