Esempio n. 1
0
int findDef(char * pname, char * rules)
{
	char pn[MLEN];
	int lpn = strlen(pname);
	memset(pn, 0 , MLEN);
	strcpy (pn, pname);
	pn[lpn] = ' ';
	pn[lpn+1] = '=';
	pn[lpn+2] = '=';

	return checkExpr(pn, rules);
}
Esempio n. 2
0
void checkStmt(Stmt* stmt)
{
    if(!stmt)
        return;

    Type t1, t2, t3;

    if(stmt->type == CSTAT)
        checkStmtList(stmt->stmtList);
    else if(stmt->type == IFELSE)
    {
        t1 = checkExpr(stmt->expr1);
        if(t1 != BOOL_T) //ERROR
            errorIncompatibleTypeInStatement("if", t1, BOOL_T);

        checkStmt(stmt->stmt1);
        checkStmt(stmt->stmt2);
    }
    else if(stmt->type == WHILE_T)
    {
        t1 = checkExpr(stmt->expr1);
        if(t1 != BOOL_T) //ERROR
            errorIncompatibleTypeInStatement("while", t1, BOOL_T);

        checkStmt(stmt->stmt1);
    }
    else if(stmt->type == RETURN_T)
    {
        t1 = getMethodFromGlobal(currentMethod);
        if(stmt->expr1)
            t2 = checkExpr(stmt->expr1);
        else
            t2 = VOID_T;
        if(t1 != t2) //ERROR
            errorIncompatibleTypeInStatement("return", t2, t1);
    }
    else if(stmt->type == PRINT_T)
    {
        t1 = checkExpr(stmt->expr1);
        if(t1 != BOOL_T && t1 != INT_T) //ERROR
            errorIncompatibleTypeInStatement2("System.out.println", t1, BOOL_T, INT_T);
    }
    else if(stmt->type == STORE)
    {
        t1 = getSymbolFromLocalOrGlobal(stmt->id);
        t2 = -1;
        if(t1 == t2)
            errorCannotFindSymbol(stmt->id);

        t2 = checkExpr(stmt->expr1);
        if(t1 != t2)
            errorIncompatibleTypeInAssign(stmt->id, t2, t1);
    }
    else if(stmt->type == STOREARRAY)
    {
        t1 = getSymbolFromLocalOrGlobal(stmt->id);
        t2 = -1;
        if(t1 == t2)
            errorCannotFindSymbol(stmt->id);

        t2 = checkExpr(stmt->expr1);
        if((t1 != INTARRAY && t1 != BOOLARRAY) || t2 != INT_T) //ERROR
            errorOperatorCannotBeApplied2("[", t1, t2);

        if(t1 == INTARRAY)
            t3 = INT_T;
        else
            t3 = BOOL_T;

        t2 = checkExpr(stmt->expr2);
        if(t3 != t2) //ERROR
            errorIncompatibleTypeInAssignArray(stmt->id, t2, t3);
    }
}
Esempio n. 3
0
Type checkExpr(Expr* expr)
{
    Type t1, t2;

    if(expr->type == BINOP)
    {
        switch(expr->op)
        {
        case PLUS:
        case MINUS:
        case MUL:
        case DIV:
        case REM:
            t1 = checkExpr(expr->expr1);
            t2 = checkExpr(expr->expr2);
            if(t1 != INT_T || t2 != INT_T) //ERROR
                errorOperatorCannotBeApplied2(opsOrig[expr->op], t1, t2);
            return INT_T;
        case LESSER:
        case GREATER:
        case LEQ:
        case GEQ:
            t1 = checkExpr(expr->expr1);
            t2 = checkExpr(expr->expr2);
            if(t1 != INT_T || t2 != INT_T) //ERROR
                errorOperatorCannotBeApplied2(opsOrig[expr->op], t1, t2);
            return BOOL_T;
        case DIF:
        case EQ:
            t1 = checkExpr(expr->expr1);
            t2 = checkExpr(expr->expr2);
            if(t1 != t2) //ERROR
                errorOperatorCannotBeApplied2(opsOrig[expr->op], t1, t2);
            return BOOL_T;
        case AND_T:
        case OR_T:
            t1 = checkExpr(expr->expr1);
            t2 = checkExpr(expr->expr2);
            if(t1 != BOOL_T || t2 != BOOL_T) //ERROR
                errorOperatorCannotBeApplied2(opsOrig[expr->op], t1, t2);
            return BOOL_T;
        }
    }
    else if(expr->type == UNOP)
    {
        if(expr->op == PLUS || expr->op == MINUS)
        {
            t1 = checkExpr(expr->expr1);
            if(t1 != INT_T) //ERROR
                errorOperatorCannotBeApplied(opsOrig[expr->op], t1);
            return INT_T;
        }
        else if(expr->op == NOT)
        {
            t1 = checkExpr(expr->expr1);
            if(t1 != BOOL_T) //ERROR
                errorOperatorCannotBeApplied(opsOrig[expr->op], t1);
            return BOOL_T;
        }
        else if(expr->op == DOTLENGTH_T)
        {
            t1 = checkExpr(expr->expr1);
            if(t1 != INTARRAY && t1 != BOOLARRAY && t1 != STRINGARRAY) //ERROR
                errorOperatorCannotBeApplied(opsOrig[expr->op], t1);
            return INT_T;
        }
    }
    else if(expr->type == ID_T)
    {
        Type r, notFound = -1;
        r = getSymbolFromLocalOrGlobal(expr->idOrLit);
        if(r == notFound)
            errorCannotFindSymbol(expr->idOrLit);
        return r;
    }
    else if(expr->type == INTLIT_T)
    {
        if(!isValidIntLit(expr->idOrLit)) //ERROR
            errorInvalidLiteral(expr->idOrLit);

        return INT_T;
    }
    else if(expr->type == BOOLLIT_T)
        return BOOL_T;
    else if(expr->type == CALL)
    {
        MethodTable* t = getLocalTable(expr->idOrLit);
        if(t == NULL)
            errorCannotFindSymbol(expr->idOrLit);

        int argNum = 0;
        ArgsList* auxArgs = expr->argsList;
        MethodTableEntry* auxParams = t->entries;
        while(auxArgs != NULL || auxParams != NULL)
        {
            t1 = VOID_T;
            if(auxArgs != NULL)
            {
                t1 = checkExpr(auxArgs->expr);
                auxArgs = auxArgs->next;
            }

            t2 = VOID_T;
            if(auxParams != NULL)
            {
                if(auxParams->isParam)
                    t2 = auxParams->type;
                auxParams = auxParams->next;
                if(auxParams && !auxParams->isParam)
                    auxParams = NULL;
            }

            if(t1 != t2) //ERROR
                errorIncompatibleArgument(argNum, expr->idOrLit, t1, t2);

            argNum++;
        }

        return getMethodFromGlobal(expr->idOrLit);
    }
    else if(expr->type == PARSEINT_T)
    {
        t1 = getSymbolFromLocalOrGlobal(expr->idOrLit);
        t2 = -1;
        if(t1 == t2)
            errorCannotFindSymbol(expr->idOrLit);

        t2 = checkExpr(expr->expr1);
        if(t1 != STRINGARRAY || t2 != INT_T) //ERROR
            errorOperatorCannotBeApplied2("Integer.parseInt", t1, t2);

        return INT_T;
    }
    else if(expr->type == INDEX)
    {
        t1 = checkExpr(expr->expr1);
        t2 = checkExpr(expr->expr2);
        if((t1 != INTARRAY && t1 != BOOLARRAY) || t2 != INT_T) //ERROR
            errorOperatorCannotBeApplied2("[", t1, t2);

        if(t1 == INTARRAY)
            return INT_T;
        else
            return BOOL_T;
    }
    else if(expr->type == NEWINTARR)
    {
        t1 = checkExpr(expr->expr1);
        if(t1 != INT_T)
            errorOperatorCannotBeApplied("new int", t1);
        return INTARRAY;
    }
    else if(expr->type == NEWBOOLARR)
    {
        t1 = checkExpr(expr->expr1);
        if(t1 != INT_T)
            errorOperatorCannotBeApplied("new boolean", t1);
        return BOOLARRAY;
    }
}
Esempio n. 4
0
int main ()
{
	FILE* fp;
	char il[MLEN];
	char ilc[MLEN];
	char ribbon[MRIBBON]; //the ribbon of calculations.
	int rp = 0; // the ribbon phrase beggining
	int rn = 0; // ribbon new expressions starts here.
	int re = 0; // ribbon end.
	char rules[MRULES]; 	// list of rules applicable to the case.
	char * crule;
	int rsize;	// rule size;
	int c;
	char * pname;
	int soffs; 	// input offset


	printf("Ssum\n");

	fp = fopen ("defs", "r");
	if (fp == NULL)
	{
		perror("Can't open file with definitions.");
		return -1;
	}
	c = 0;
	do 
	{
		rules[c++] = fgetc(fp);
		if (rules[c-1] == '\n' ) rules[c-1] = rdel[0];
		if (rules[c-1] == '\r' ) rules[c-1] = rdel[0];
	}
	while ( (c < MRULES) && (rules[c-1] != EOF) );

	rules[c-1] = 0; // clear EOL symbol

	printf("%d\n", strlen(rules));
	printf("%s\n", rules );	

	memset((void*)il, 0, MLEN);
	//read input line.
	c = 0;
	do 
	{
		il[c++] = getchar();
	}
	while ( (c < MLEN) && (il[c-1] != '\n') );

	il[c-1] = 0;	
	//clear trailing spaces
	c--;
	while (il [c-1] == ' ' && c>0)	il[--c] = 0;

	if (strlen (il) == 0) 
	{
		printf("Empty input, skipping.\n");
		return 0;
	}
	printf("%d\n", strlen(il));
	printf("%s\n", il );

	
	// normalize string and look for pattern in the definitions.

	normalize(il);

	// check if we have definition of the equation.

	soffs = checkExpr(il, rules);
	printf("Offset = %d\n", soffs);

	if ( soffs >=0 )
	{
		rsize = getRule(soffs, rules); // extracting the rule from the list of rules
		crule = strtok( &rules[rsize], rdel );	//extract full rule string

		printf("crule: %s\n", crule);

		if (applyRule(il, crule) >=0 ) // apply rule type of 'X Y ==' or 'Y X =='.
		{
			printf("Answer: %s\n", crule);
			return 0;
		}

	}else
	{
		printf("Does not match any rule. Continue...\n");
	}

	// now the main loop of pattern search and defs application
	memset(ribbon, 0, MRIBBON);

	strcpy (ribbon, il);
	re += strlen(il);
	ribbon[re++] = edel[0];
	ribbon[re++] = '\n';


	printf("%s\n", ribbon);

	strcpy(ilc, il); // copy input line.

	// getting names from rules list.
	pname = strtok (il," ");
	printf("Name: %s\n", pname);

	soffs = findDef(pname, rules); // looking for definition of name in the rules list.
	printf("Offset = %d\n", soffs);

	if ( soffs >=0 )
	{
		rsize = getRule(soffs, rules); // extracting the rule from the list of rules
		crule = strtok( &rules[rsize], rdel );	//extract full rule string

		printf("Definition: %s\n", crule);

		// apply name
		printf("il before: %s\n", ilc);
		if ( applyName(ilc, crule, pname) >=0 )
		{
			printf("Converted string: %s\n", crule);

			//add to ribbon
			addToRibbon(ribbon, crule);
		}

	}else
	{
		printf("Definition of name is not found. Please define the name and retry.\n");
		return 0;
	}

	printf("Ribbon: %s\n", ribbon);

	// find all names in the definition.
	// find all operations in rules.




	close(fp);
	return 0;
}