Exemplo n.º 1
0
Type* compileBasicType(void) {
  Type* type;

  switch (lookAhead->tokenType) {
  case KW_INTEGER: 
    eat(KW_INTEGER); 
    type = makeIntType();
    break;
  case KW_CHAR: 
    eat(KW_CHAR); 
    type = makeCharType();
    break;
  case KW_FLOAT: 
    eat(KW_FLOAT); 
    type = makeFloatType();
    break;
  case KW_STRING: 
    eat(KW_STRING); 
    type = makeStringType();
    break;
  default:
    error(ERR_INVALID_BASICTYPE, lookAhead->lineNo, lookAhead->colNo);
    break;
  }
  return type;
}
Exemplo n.º 2
0
Type* compileType(void) {
  Type* type;
  Type* elementType;
  int arraySize;
  Object* obj;

  switch (lookAhead->tokenType) {
  case KW_INTEGER: 
    eat(KW_INTEGER);
    type =  makeIntType();
    break;
  case KW_CHAR: 
    eat(KW_CHAR); 
    type = makeCharType();
    break;
  case KW_STRING:
    eat(KW_STRING);
    type=makeStringType();
    break;
  case KW_FLOAT:
    eat(KW_FLOAT);
    type=makeFloatType();
    break;
  case KW_ARRAY:
    eat(KW_ARRAY);
    eat(SB_LSEL);
    eat(TK_NUMBER);

    arraySize = currentToken->value;

    eat(SB_RSEL);
    eat(KW_OF);
    elementType = compileType();
    type = makeArrayType(arraySize, elementType);
    break;
  case TK_IDENT:
    eat(TK_IDENT);
    obj = checkDeclaredType(currentToken->string);
    type = duplicateType(obj->typeAttrs->actualType);
    break;
  default:
    error(ERR_INVALID_TYPE, lookAhead->lineNo, lookAhead->colNo);
    break;
  }
  return type;
}
Exemplo n.º 3
0
void initSymTab(void) {
  Object* param;

  symtab = (SymTab*) malloc(sizeof(SymTab));
  symtab->globalObjectList = NULL;
  symtab->program = NULL;
  symtab->currentScope = NULL;
  
  readcFunction = createFunctionObject("READC");
  declareObject(readcFunction);
  readcFunction->funcAttrs->returnType = makeCharType();

  readiFunction = createFunctionObject("READI");
  declareObject(readiFunction);
  readiFunction->funcAttrs->returnType = makeIntType();

  readfFunction = createFunctionObject("READF");
  declareObject(readfFunction);
  readfFunction->funcAttrs->returnType = makeFloatType();

  readsFunction = createFunctionObject("READS");
  declareObject(readsFunction);
  readsFunction->funcAttrs->returnType = makeStringType();

  writeiProcedure = createProcedureObject("WRITEI");
  declareObject(writeiProcedure);
  enterBlock(writeiProcedure->procAttrs->scope);
    param = createParameterObject("i", PARAM_VALUE);
    param->paramAttrs->type = makeIntType();
    declareObject(param);
  exitBlock();

  writecProcedure = createProcedureObject("WRITEC");
  declareObject(writecProcedure);
  enterBlock(writecProcedure->procAttrs->scope);
    param = createParameterObject("ch", PARAM_VALUE);
    param->paramAttrs->type = makeCharType();
    declareObject(param);
  exitBlock();

  writesProcedure = createProcedureObject("WRITES");
  declareObject(writesProcedure);
  enterBlock(writesProcedure->procAttrs->scope);
    param = createParameterObject("str", PARAM_VALUE);
    param->paramAttrs->type = makeStringType();
    declareObject(param);
  exitBlock();

  writefProcedure = createProcedureObject("WRITEF");
  declareObject(writefProcedure);
  enterBlock(writefProcedure->procAttrs->scope);
    param = createParameterObject("f", PARAM_VALUE);
    param->paramAttrs->type = makeFloatType();
    declareObject(param);
  exitBlock();

  writelnProcedure = createProcedureObject("WRITELN");
  declareObject(writelnProcedure);

  intType = makeIntType();
  charType = makeCharType();
  floatType = makeFloatType();
  stringType = makeStringType();
}