示例#1
0
static void loadVarImpl(const AstVar* var, uint16_t localId, uint16_t context, Bytecode* bc, AstNode* at) {
  VarType type = var->type();

  if (!isNumeric(type)) {
    throw TranslationException(at, "Wrong var reference type (only numbers are supported)");
  }

  loadVar(type, localId, context, bc);
}
示例#2
0
TNODE *evalNodeValue(TNODE *current)
{
	TNODE *node;
	if (current->ntype == NT_LIST_START && current->left != NULL)
		current = evalNodeValue(current->left);
	if (current->primIdx != -1)
		node = prims[current->primIdx].prim_function(current);
	else if (current->varIdx != -1)
		node = loadVar(current);
	else
		node = current;
	return node;
}
示例#3
0
TNODE *exec(TNODE *current, char fcn)
{
	int retCurrentNode = 1,
		ival = 0, ileft = 0, iright = 0;
	double fval = 0.0,
		fleft = 0.0,
		fright = 0.0;
	DataType retDataType = DT_UNKNOWN;
	
	if (current->left != NULL)
	{
		if (current->left->primIdx != -1)
		{
			prims[current->left->primIdx].prim_function(current->left);
		}
		loadVar(current->left);
		switch (current->left->dtype)
		{
			case DT_INT:
				ileft = current->left->nInteger;
				fleft = (double)ileft;
				break;
			case DT_DBL:
				fleft = current->left->nRational;
				ileft = (int)fleft;
				break;
			default: break;
		}
		if (retDataType != DT_DBL)
			retDataType = current->left->dtype;
	}
	if (current->right != NULL)
	{
		if (current->right->primIdx != -1)
		{
			prims[current->right->primIdx].prim_function(current->right);
		}
		loadVar(current->right);
		switch (current->right->dtype)
		{
			case DT_INT:
				iright = current->right->nInteger;
				fright = (double)iright;
				break;
			case DT_DBL:
				fright = current->right->nRational;
				iright = (int)fright;
				break;
			default: break;
		}
		if (retDataType != DT_DBL)
			retDataType = current->right->dtype;
	}

	switch(fcn) {
		case '-': //subtract
			ival = ileft - iright;
			fval = fleft - fright;
			if (DEBUG)
				switch (retDataType)
				{
					case DT_INT: printf("\n%d%c%d=%d", ileft, fcn, iright, ival); break;
					case DT_DBL: printf("\n%lf%c%lf=%lf", fleft, fcn, fright, fval); break;
					default: break;
				}
			break;
		case '+': //add
			ival = ileft + iright;
			fval = fleft + fright;
			if (DEBUG)
				switch (retDataType)
				{
					case DT_INT: printf("\n%d%c%d=%d", ileft, fcn, iright, ival); break;
					case DT_DBL: printf("\n%lf%c%lf=%lf", fleft, fcn, fright, fval); break;
					default: break;
				}
			break;
		case '*': //multiply
			ival = ileft * iright;
			fval = fleft * fright;
			if (DEBUG)
				switch (retDataType)
				{
					case DT_INT: printf("\n%d%c%d=%d", ileft, fcn, iright, ival); break;
					case DT_DBL: printf("\n%lf%c%lf=%lf", fleft, fcn, fright, fval); break;
					default: break;
				}
			break;
		case '/': //divide
			if (iright != 0)
				ival = ileft / iright;
			if (fright != 0.0)
				fval = fleft / fright;
			if (DEBUG)
				switch (retDataType)
				{
					case DT_INT: printf("\n%d%c%d=%d", ileft, fcn, iright, ival); break;
					case DT_DBL: printf("\n%lf%c%lf=%lf", fleft, fcn, fright, fval); break;
					default: break;
				}
			break;
		case '>': //greater
			ival = (ileft > iright);
			if (DEBUG)
				switch (retDataType)
				{
					case DT_INT: printf("\n%d%c%d=%d", ileft, fcn, iright, ival); break;
					case DT_DBL: printf("\n%lf%c%lf=%lf", fleft, fcn, fright, fval); break;
					default: break;
				}
			break; 
		case '<': //less
			ival = (ileft < iright);
			if (DEBUG)
				switch (retDataType)
				{
					case DT_INT: printf("\n%d%c%d=%d", ileft, fcn, iright, ival); break;
					case DT_DBL: printf("\n%lf%c%lf=%lf", fleft, fcn, fright, fval); break;
					default: break;
				}
			break; 
		case ':': //assign
			if (DEBUG)
				switch (retDataType)
				{
					case DT_INT: printf("\nv[%d]:=%d", current->left->varIdx, iright); break;
					case DT_DBL: printf("\nv[%d]:=%lf", current->left->varIdx, fright); break;
					default: break;
				}
			fval = fright;
			if (current->left->varIdx != -1 && current->left->varIdx <= NUM_ROGOVARS)
				rogo_vars[current->left->varIdx] = fval;
			break;
		case '=': //eq
			ival = (ileft == iright);
			if (DEBUG)
				switch (retDataType)
				{
					case DT_INT: printf("\n%d%c%d=%d", ileft, fcn, iright, ival); break;
					case DT_DBL: printf("\n%lf%c%lf=%lf", fleft, fcn, fright, fval); break;
					default: break;
				}
			break;
		case '#': //neq
			ival = (ileft != iright);
			if (DEBUG)
				switch (retDataType)
				{
					case DT_INT: printf("\n%d%c%d=%d", ileft, fcn, iright, ival); break;
					case DT_DBL: printf("\n%lf%c%lf=%lf", fleft, fcn, fright, fval); break;
					default: break;
				}
			break;
		case 'L': //lte
			ival = (ileft <= iright);
			if (DEBUG)
				switch (retDataType)
				{
					case DT_INT: printf("\n%d%c%d=%d", ileft, fcn, iright, ival); break;
					case DT_DBL: printf("\n%lf%c%lf=%lf", fleft, fcn, fright, fval); break;
					default: break;
				}
			break;
		case 'G': //gte
			ival = (ileft >= iright);
			if (DEBUG)
				switch (retDataType)
				{
					case DT_INT: printf("\n%d%c%d=%d", ileft, fcn, iright, ival); break;
					case DT_DBL: printf("\n%lf%c%lf=%lf", fleft, fcn, fright, fval); break;
					default: break;
				}
			break;
		case '&': //and
			//printf("\n%d%c%d", ileft, fcn, iright);
			ival = (ileft && iright);
			break;
		case '|': //or
			//printf("\n%d%c%d", ileft, fcn, iright);
			ival = (ileft || iright);
			break;
		case '!': //not
			//printf("\n%c%d", fcn, ileft);
			ival = !ileft;
			break;
		case '?': //if
			
			break;
		case '^': //ifelse
			
			break;
		case 'R': //repeat
			
			break;
		case 'P': //pause
			
			break;
		default:
			break;
	}
	if (retDataType == DT_DBL)
		ival = (int)fval;
	else
		fval = (double)ival;
	
	if (retCurrentNode)
	{
		current->dtype = retDataType;
		current->nInteger = ival;
		current->nRational = fval;
		return current;
	}
	else
	{
		TNODE *newNode = (TNODE *)malloc(sizeof(TNODE));
		newNode->ntype = NT_NUM;
		newNode->dtype = retDataType;
		newNode->varIdx = -1;
		newNode->nInteger = ival;
		newNode->nRational = fval;
		newNode->left = NULL;
		newNode->right = NULL;
		newNode->prev = NULL;
		newNode->next = NULL;
		newNode->primIdx = -1;
		/* "numargs" and "priority" now in primitive array */
		//newNode->numargs = 0;
		//newNode->priority = 0;
		return newNode;
	}
}