Exemplo n.º 1
0
ConstantValue* compileUnsignedConstant(void) {
  ConstantValue* constValue;
  Object* obj;

  switch (lookAhead->tokenType) {
  case TK_NUMBER:
    eat(TK_NUMBER);
    constValue = makeIntConstant(currentToken->value);
    break;
  case TK_IDENT:
    eat(TK_IDENT);

    obj = checkDeclaredConstant(currentToken->string);
    constValue = duplicateConstantValue(obj->constAttrs->value);

    break;
  case TK_CHAR:
    eat(TK_CHAR);
    constValue = makeCharConstant(currentToken->string[0]);
    break;
  default:
    error(ERR_INVALID_CONSTANT, lookAhead->lineNo, lookAhead->colNo);
    break;
  }
  return constValue;
}
Exemplo n.º 2
0
ConstantValue* compileUnsignedConstant(void) {
    // TODO: create and return an unsigned constant value
    ConstantValue* constValue = NULL;

    switch (lookAhead->tokenType) {
    case TK_NUMBER:
        eat(TK_NUMBER);
        constValue = makeIntConstant(currentToken->value);
        break;
    case TK_IDENT:
        eat(TK_IDENT);
        Object* obj = lookupObject(currentToken->string);

        if (obj == NULL) {
            error(ERR_UNDECLARED_CONSTANT, currentToken->lineNo, currentToken->colNo);
        } else if (obj->kind != OBJ_CONSTANT) {
            error(ERR_INVALID_CONSTANT, currentToken->lineNo, currentToken->colNo);
        }


        constValue = (ConstantValue*) malloc(sizeof(ConstantValue));
        *constValue = *(obj->constAttrs->value);
        break;
    case TK_CHAR:
        eat(TK_CHAR);
        constValue = makeCharConstant(currentToken->string[0]);
        break;
    default:
        error(ERR_INVALID_CONSTANT, lookAhead->lineNo, lookAhead->colNo);
        break;
    }

    return constValue;
}
Exemplo n.º 3
0
ConstantValue* compileUnsignedConstant(void) {
  ConstantValue* constValue;
  Object* obj;

  switch (lookAhead->tokenType) {
  case TK_NUMBER:
    eat(TK_NUMBER);
    constValue = makeIntConstant(currentToken->value);
    break;
  case TK_IDENT:
    eat(TK_IDENT);
    // check if the constant identifier is declared and get its value
    obj = checkDeclaredConstant(currentToken->string);
    if (obj != NULL)
        constValue = duplicateConstantValue(obj->constAttrs->value);
    else
        error(ERR_UNDECLARED_CONSTANT, currentToken->lineNo, currentToken->colNo);

    break;
  case TK_CHAR:
    eat(TK_CHAR);
    constValue = makeCharConstant(currentToken->string[0]);
    break;
  default:
    error(ERR_INVALID_CONSTANT, lookAhead->lineNo, lookAhead->colNo);
    break;
  }
  return constValue;
}
Exemplo n.º 4
0
ConstantValue* compileConstant(void) {
  ConstantValue* constValue;

  switch (lookAhead->tokenType) {
  case SB_PLUS:
    eat(SB_PLUS);
    constValue = compileConstant2();
    break;
  case SB_MINUS:
    eat(SB_MINUS);
    constValue = compileConstant2();
    constValue->intValue = - constValue->intValue;
    break;
  case TK_CHAR:
    eat(TK_CHAR);
    constValue = makeCharConstant(currentToken->string[0]);
    break;
  default:
    constValue = compileConstant2();
    break;
  }
  return constValue;
}
Exemplo n.º 5
0
ConstantValue* compileUnsignedConstant(void) {
  ConstantValue* constValue;
  Object* obj;

  switch (lookAhead->tokenType) {
  case TK_NUMBER:
    eat(TK_NUMBER);
    constValue = makeIntConstant(currentToken->value);
    break;
  case TK_IDENT:
    eat(TK_IDENT);
    // TODO: check if the constant identifier is declared and get its value

    break;
  case TK_CHAR:
    eat(TK_CHAR);
    constValue = makeCharConstant(currentToken->string[0]);
    break;
  default:
    error(ERR_INVALID_CONSTANT, lookAhead->lineNo, lookAhead->colNo);
    break;
  }
  return constValue;
}
Exemplo n.º 6
0
int main() {
	Object* obj;

	initSymTab();

	obj = createProgramObject("PRG");
	enterBlock(obj->progAttrs->scope);

	obj = createConstantObject("c1");
	obj->constAttrs->value = makeIntConstant(10);
	declareObject(obj);

	obj = createConstantObject("c2");
	obj->constAttrs->value = makeCharConstant('a');
	declareObject(obj);

	obj = createTypeObject("t1");
	obj->typeAttrs->actualType = makeArrayType(10, makeIntType());
	declareObject(obj);

	obj = createVariableObject("v1");
	obj->varAttrs->type = makeIntType();
	declareObject(obj);

	obj = createVariableObject("v2");
	obj->varAttrs->type = makeArrayType(10, makeArrayType(10, makeIntType()));
	declareObject(obj);

	obj = createFunctionObject("f");
	obj->funcAttrs->returnType = makeIntType();
	declareObject(obj);

	enterBlock(obj->funcAttrs->scope);

	obj = createParameterObject("p1", PARAM_VALUE, symtab->currentScope->owner);
	obj->paramAttrs->type = makeIntType();
	declareObject(obj);

	obj = createParameterObject("p2", PARAM_REFERENCE, symtab->currentScope->owner);
	obj->paramAttrs->type = makeCharType();
	declareObject(obj);

	exitBlock();

	obj = createProcedureObject("p");
	declareObject(obj);

	enterBlock(obj->procAttrs->scope);

	obj = createParameterObject("v1", PARAM_VALUE, symtab->currentScope->owner);
	obj->paramAttrs->type = makeIntType();
	declareObject(obj);

	obj = createConstantObject("c1");
	obj->constAttrs->value = makeCharConstant('a');
	declareObject(obj);

	obj = createConstantObject("c3");
	obj->constAttrs->value = makeIntConstant(10);
	declareObject(obj);

	obj = createTypeObject("t1");
	obj->typeAttrs->actualType = makeIntType();
	declareObject(obj);

	obj = createTypeObject("t2");
	obj->typeAttrs->actualType = makeArrayType(10, makeIntType());
	declareObject(obj);

	obj = createVariableObject("v2");
	obj->varAttrs->type = makeArrayType(10, makeIntType());
	declareObject(obj);

	obj = createVariableObject("v3");
	obj->varAttrs->type = makeCharType();
	declareObject(obj);

	exitBlock();


	exitBlock();
	printObject(symtab->program, 0);
	cleanSymTab();

	return 0;
}