/*
 * Class:     ai_madara_knowledge_Variables
 * Method:    jni_get
 * Signature: (JLjava/lang/String;)J
 */
jlong JNICALL Java_ai_madara_knowledge_Variables_jni_1get(
    JNIEnv* env, jobject, jlong cptr, jstring var)
{
  const char* nativeVar = env->GetStringUTFChars(var, 0);

  Variables* vars = (Variables*)cptr;
  KnowledgeRecord* result(0);

  if (vars)
  {
    result = new KnowledgeRecord(vars->get(nativeVar));
  }
  else
  {
    // user has tried to use a deleted object. Clean up and throw

    madara::utility::java::throw_dead_obj_exception(env,
        "Variables::get: "
        "Variables objects are released already");
  }

  env->ReleaseStringUTFChars(var, nativeVar);
  return (jlong)result;
}
示例#2
0
char* FastASCII::readField(const char* field,
                           const char* line,
                           unsigned int index,
                           void* data)
{
    unsigned int i;
    ScreenManager *S = ScreenManager::singleton();
    CalcServer::CalcServer *C = CalcServer::CalcServer::singleton();
    Variables* vars = C->variables();
    ArrayVariable *var = (ArrayVariable*)vars->get(field);

    // Extract the variable type data
    unsigned int n = vars->typeToN(var->type());
    size_t type_size = vars->typeToBytes(var->type());
    char *type = new char[strlen(var->type()) + 1];
    strcpy(type, var->type());
    if(strchr(type, '*'))
        strcpy(strchr(type, '*'), "");

    // Point to the chunk of data to become read
    void* ptr = (void*)((char*)data + type_size * index);

    // Start reading the sub-fields
    char* pos = (char*)line;
    for(i = 0; i < n; i++){
        char* end_pos = NULL;
        // Let's use different tools depending on the type to become read
        if(!strcmp(type, "unsigned int") ||
           strstr(type, "uivec")){
            unsigned int val = (unsigned int)strtol(pos, &end_pos, 10);
            if(pos == end_pos){
                char *msg = new char[strlen(var->type()) + 64];
                S->addMessageF(3, "Failure reading a field value\n");
                sprintf(msg, "\tWhile extracting it from \"%s\"\n", pos);
                S->addMessage(0, msg);
            }
            memcpy(ptr, &val, sizeof(unsigned int));
        }
        else if(!strcmp(type, "int") ||
           strstr(type, "ivec")){
            int val = (int)strtol(pos, &end_pos, 10);
            if(pos == end_pos){
                char *msg = new char[strlen(var->type()) + 64];
                S->addMessageF(3, "Failure reading a field value\n");
                sprintf(msg, "\tWhile extracting it from \"%s\"\n", pos);
                S->addMessage(0, msg);
            }
            memcpy(ptr, &val, sizeof(int));
        }
        else{
            float val = (float)strtof(pos, &end_pos);
            if(pos == end_pos){
                char *msg = new char[strlen(var->type()) + 64];
                S->addMessageF(3, "Failure reading a field value\n");
                sprintf(msg, "\tWhile extracting it from \"%s\"\n", pos);
                S->addMessage(0, msg);
            }
            memcpy(ptr, &val, sizeof(float));
        }

        // Go to the next field, we already asserted that there are fields
        // enough, so we don't need to care about that
        ptr += type_size / n;
        pos = strchr(end_pos, ',');
        if(pos)
            pos++;
    }

    delete[] type;
    return pos;
}
示例#3
0
int VirtualMachine::runFile(ByteCodeFileReader& reader){
	int header = reader.readHeader();

	if(header != ('E' + 'D' + 'D' + 'I')){
		cout << "Not an EDDI compiled file" << endl;
		return 1;
	}

	StringPool pool;

	int strings = reader.readInt();

	cout << "String pool size = " << strings << endl;

	for(int i = 0; i < strings; i++){
		int index = reader.readInt();
		string value = reader.readLitteral();

		pool.add(index, value);
	}

	vector<Instruction> instructions;
	int current = 0;

	map<int, int> branches;

	while(reader.hasMore()){
		ByteCode bytecode = reader.readByteCode();

		if(bytecode == LABEL){
			int branche = reader.readInt();

			branches[branche] = current;
		} else {
			Instruction instruction;
			instruction.bytecode = bytecode;
	
			if(instruction.bytecode > LABEL && instruction.bytecode <= JUMP_IF_NOT){
				instruction.operand = reader.readInt();	
			}

			instructions.push_back(instruction);
			++current;
		}	
	}

	Stack stack;
	Variables variables;

	int programCounter = 0;

	while(true){
		Instruction instruction = instructions[programCounter];
		ByteCode bytecode = instruction.bytecode;
		
		programCounter++;

		switch(bytecode){
			case LDCS:
			case LDCI:
				stack.push(instruction.operand);

				break;
			case PRINTI:
				cout << stack.pop() << endl;

				break;
			case PRINTS:
				cout << pool.get(stack.pop()) << endl;

				break;
			case SSTORE:
			case ISTORE:{
				unsigned int variable = (unsigned int) instruction.operand;
				
				variables.assign(variable, stack.pop());
				
				break;
			}
			case SLOAD:
			case ILOAD:{
				unsigned int variable = (unsigned int) instruction.operand;

				stack.push(variables.get(variable));	
				
				break;
			}
			case IADD:{
				int rhs = stack.pop();
				int lhs = stack.pop();
				
				stack.push(lhs + rhs);
				
				break;
			}
			case ISUB:{
				int rhs = stack.pop();
				int lhs = stack.pop();
				
				stack.push(lhs - rhs);
				
				break;
			}
			case IMUL:{
				int rhs = stack.pop();
				int lhs = stack.pop();
				
				stack.push(lhs * rhs);
				
				break;
			}
			case IDIV:{
				int rhs = stack.pop();
				int lhs = stack.pop();
				
				stack.push(lhs / rhs);
				
				break;
			}
			case IMOD:{
				int rhs = stack.pop();
				int lhs = stack.pop();
				
				stack.push(lhs % rhs);
				
				break;
			}
			case SADD:{
				string rhs = pool.get(stack.pop());
				string lhs = pool.get(stack.pop());

				int index = pool.addNew(lhs + rhs);	

				stack.push(index);

				break;
			}
			case JUMP: {
				programCounter = branches[instruction.operand];
		
				break;
			} 
			case JUMP_IF: {
				int result = stack.pop();

				if(result == 1){
					programCounter = branches[instruction.operand];
				}
		
				break;
			} 
		 
			case JUMP_IF_NOT: {
				int result = stack.pop();

				if(result == 0){
					programCounter = branches[instruction.operand];
				}
		
				break;
			} 

			case EQUALS: {
				int rhs = stack.pop();
				int lhs = stack.pop();

				if(lhs == rhs){
					stack.push(1);
				} else {
					stack.push(0);
				}
		
				break;
			} 
			case NOT_EQUALS: {
				int rhs = stack.pop();
				int lhs = stack.pop();

				if(lhs != rhs){
					stack.push(1);
				} else {
					stack.push(0);
				}
		
				break;
			} 
			case GREATER_THAN: {
				int rhs = stack.pop();
				int lhs = stack.pop();

				if(lhs > rhs){
					stack.push(1);
				} else {
					stack.push(0);
				}
		
				break;
			} 
			case LESS_THAN: {
				int rhs = stack.pop();
				int lhs = stack.pop();

				if(lhs < rhs){
					stack.push(1);
				} else {
					stack.push(0);
				}
		
				break;
			} 
			case GREATER_THAN_EQUALS: {
				int rhs = stack.pop();
				int lhs = stack.pop();

				if(lhs >= rhs){
					stack.push(1);
				} else {
					stack.push(0);
				}
		
				break;
			} 
			case LESS_THAN_EQUALS: {
				int rhs = stack.pop();
				int lhs = stack.pop();

				if(lhs <= rhs){
					stack.push(1);
				} else {
					stack.push(0);
				}
		
				break;
			} 
			case END:
				return 0;
		}
	}

	return 1;
}