Beispiel #1
0
const CType* CAstStringConstant::GetType(void) const
{
  CTypeManager *tm = CTypeManager::Get();

  // string is array of character.
  return tm->GetArray(CToken::unescape(GetValueStr()).length()+1, tm->GetChar());
}
Beispiel #2
0
void CParser::InitSymbolTable(CSymtab *s)
{
  CTypeManager *tm = CTypeManager::Get();
  
  // predefined functions:
  //  - DIM(a: ptr to array; dim: integer): integer
  //  - DOFS(a: ptr to array): integer
  //  - ReadInt(): integer
  //  - WriteInt(i:integer): void
  //  - WriteChar(c:char): void
  //  - WriteStr(str: char[]): void
  //  - WriteLn():void

  //  - DIM(a: ptr to array; dim: integer): integer
  CSymProc *DIM = new CSymProc("DIM", tm->GetInt());
  CSymParam *DIM_a = new CSymParam(0, "a", tm->GetPointer(tm->GetNull()));
  CSymParam *DIM_dim = new CSymParam(1, "dim", tm->GetInt());
  DIM->AddParam(DIM_a);
  DIM->AddParam(DIM_dim);
  s->AddSymbol(DIM);

  //  - DOFS(a: ptr to array): integer
  CSymProc *DOFS = new CSymProc("DOFS", tm->GetInt());
  CSymParam *DOFS_a = new CSymParam(0, "a", tm->GetPointer(tm->GetNull()));
  DOFS->AddParam(DOFS_a);
  s->AddSymbol(DOFS);

  //  - ReadInt(): integer
  CSymProc *ReadInt = new CSymProc("ReadInt", tm->GetInt());
  s->AddSymbol(ReadInt);
  
  //  - WriteInt(i:integer): void
  CSymProc *WriteInt = new CSymProc("WriteInt", tm->GetNull());
  CSymParam *WriteInt_i = new CSymParam(0, "i", tm->GetInt());
  WriteInt->AddParam(WriteInt_i);
  s->AddSymbol(WriteInt);

  //  - WriteChar(c:char): void
  CSymProc *WriteChar = new CSymProc("WriteChar", tm->GetNull());
  CSymParam *WriteChar_c = new CSymParam(0, "c", tm->GetChar());
  WriteChar->AddParam(WriteChar_c);
  s->AddSymbol(WriteChar);

  //  - WriteStr(str: char[]): void
  CSymProc *WriteStr = new CSymProc("WriteStr", tm->GetNull());
  CSymParam *WriteStr_str = new CSymParam(0, "str", tm->GetPointer(tm->GetArray(CArrayType::OPEN, tm->GetChar())));
  WriteStr->AddParam(WriteStr_str);
  s->AddSymbol(WriteStr);

  //  - WriteLn():void
  CSymProc *WriteLn = new CSymProc("WriteLn", tm->GetNull());
  s->AddSymbol(WriteLn);
}
Beispiel #3
0
CAstStringConstant::CAstStringConstant(CToken t, const string value,
                                       CAstScope *s)
  : CAstOperand(t)
{
  CTypeManager *tm = CTypeManager::Get();

  _type = tm->GetArray(strlen(CScanner::unescape(value).c_str())+1,
                       tm->GetChar());
  _value = new CDataInitString(value);

  ostringstream o;
  o << "_str_" << ++_idx;

  _sym = new CSymGlobal(o.str(), _type);
  _sym->SetData(_value);
  s->GetSymbolTable()->AddSymbol(_sym);
}
Beispiel #4
0
void CParser::InitSymbolTable(CSymtab *s)
{
  CTypeManager *tm = CTypeManager::Get();
  CSymProc *fun;

  // function DIM(array: pointer to array; dim: integer): integer
  fun = new CSymProc("DIM", tm->GetInt());
  fun->AddParam(new CSymParam(0, "arr", tm->GetPointer(tm->GetNull())));
  fun->AddParam(new CSymParam(1, "dim", tm->GetInt()));
  s->AddSymbol(fun);

  // function DOFS(array: pointer to array): integer;
  fun = new CSymProc("DOFS", tm->GetInt());
  fun->AddParam(new CSymParam(0, "arr", tm->GetPointer(tm->GetNull())));
  s->AddSymbol(fun);

  // function ReadInt() : integer;
  fun = new CSymProc("ReadInt", tm->GetInt());
  s->AddSymbol(fun);

  // procedure WriteInt(i: integer);
  fun = new CSymProc("WriteInt", tm->GetNull());
  fun->AddParam(new CSymParam(0, "i", tm->GetInt()));
  s->AddSymbol(fun);

  // procedure WriteChar(c: char);
  fun = new CSymProc("WriteChar", tm->GetNull());
  fun->AddParam(new CSymParam(0, "c", tm->GetChar()));
  s->AddSymbol(fun);

  // procedure WriteStr(string: char[]);
  fun = new CSymProc("WriteStr", tm->GetNull());
  fun->AddParam(new CSymParam(0, "str", tm->GetPointer(tm->GetArray(CArrayType::OPEN, tm->GetChar()))));
  s->AddSymbol(fun);

  // procedure WriteLn();
  fun = new CSymProc("WriteLn", tm->GetNull());
  s->AddSymbol(fun);
}
Beispiel #5
0
const CType* CParser::type(bool isParam, bool isFunction) {
  //
  // type ::= basetype | type "[" [ number ] "]".
  // basetype ::= "boolean" | "char" | "integer".
  //
  
  CToken t;
  CToken tk;
  EToken tt = _scanner->Peek().GetType();
  CTypeManager *tm = CTypeManager::Get();
  const CType *type;
  vector<int> nelems;

  // basetype cases.
  switch(tt) {
    case tBoolean:
      Consume(tBoolean, &t);
      tk = t;
      type = dynamic_cast<const CType *>(tm->GetBool());
      break;
    case tChar:
      Consume(tChar, &t);
      tk = t;
      type = dynamic_cast<const CType *>(tm->GetChar());
      break;
    case tInteger:
      Consume(tInteger, &t);
      tk = t;
      type = dynamic_cast<const CType *>(tm->GetInt());
      break;

    default:
      SetError(_scanner->Peek(), "basetype expected.");
      break;
  }

  assert(type != NULL);

  // array case.
  while(_scanner->Peek().GetType() == tLSBrak) {
    Consume(tLSBrak);
    CToken nelem;

    if(_scanner->Peek().GetType() == tNumber) {
      Consume(tNumber, &nelem);
      nelems.push_back(atoi(nelem.GetValue().c_str()));
    }
    else {
      // open arrays are allowed only in parameter.
      if(isParam)
        nelems.push_back(-1);
      else
        SetError(_scanner->Peek(), "expected 'tNumber', got '" + _scanner->Peek().GetName() + "'");
    }
    Consume(tRSBrak);
  }

  for(int i = nelems.size()-1; i >= 0; i--) {
    type = dynamic_cast<const CType *>(tm->GetArray(nelems[i], type));
    assert(type != NULL);
    nelems.pop_back();

    // Arrays are addressed in parameter.
    if(i == 0 && isParam)
      type = dynamic_cast<const CType *>(tm->GetPointer(type));
  }

  if(isFunction && !type->IsScalar()) {
    SetError(tk, "invalid composite type for function.");
  }

  return type;
}