コード例 #1
0
ファイル: ast.cpp プロジェクト: Hyunjin95/Compilers
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());
}
コード例 #2
0
ファイル: parser.cpp プロジェクト: Hyunjin95/Compilers
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);
}
コード例 #3
0
ファイル: ast.cpp プロジェクト: SungMinCho/snuplc
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);
}
コード例 #4
0
ファイル: parser.cpp プロジェクト: cseteram/Eggompilers
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);
}
コード例 #5
0
ファイル: parser.cpp プロジェクト: Hyunjin95/Compilers
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;
}
コード例 #6
0
ファイル: parser.cpp プロジェクト: Hyunjin95/Compilers
CAstExpression* CParser::factor(CAstScope *s) {
  //
  // factor ::= qualident | number | boolean | char | string | "(" expression ")" | subroutineCall | "!" factor.
  // FIRST(factor) = { tIdent, tNumber, tTrue, tFalse, tCharacter, tString, tLBrak, tEMark }.
  //
  
  CToken t;
  EToken tt = _scanner->Peek().GetType();
  CTypeManager *tm = CTypeManager::Get();
  CAstExpression *n = NULL;
  CSymtab *symtab = s->GetSymbolTable();

  switch(tt) {
    case tTrue:
      Consume(tTrue, &t);
      n = new CAstConstant(t, tm->GetBool(), 1);
      break;

    case tFalse:
      Consume(tFalse, &t);
      n = new CAstConstant(t, tm->GetBool(), 0);
      break;

    case tNumber:
    {
      Consume(tNumber, &t);
      errno = 0;
      long long v = strtoll(t.GetValue().c_str(), NULL, 10);
      if (errno != 0) SetError(t, "invalid number.");

      // integer range validation check.
      if(v > 2147483648)
        SetError(t, "integer constant outside valid range.");

      n = new CAstConstant(t, tm->GetInt(), v);
      break;
    }

    case tIdent:
    {
      Consume(tIdent, &t);

      if(_scanner->Peek().GetType() == tLBrak) { // subroutineCall
        Consume(tLBrak);
        CAstFunctionCall *f;

        // If subroutineCall calls undefined procedure/function, then set error.
        if(symtab->FindSymbol(t.GetValue(),sLocal) == NULL && symtab->FindSymbol(t.GetValue(), sGlobal) != NULL) {
          // Check if symbol is procedure.
          if(dynamic_cast<const CSymProc *>(symtab->FindSymbol(t.GetValue(), sGlobal)) == NULL)
            SetError(t, "invalid procedure/function identifier.");

          f = new CAstFunctionCall(t, dynamic_cast<const CSymProc *>(symtab->FindSymbol(t.GetValue(), sGlobal)));
        }
        else if(symtab->FindSymbol(t.GetValue(), sLocal) != NULL) {
          // Check if symbol is procedure.
          if(dynamic_cast<const CSymProc *>(symtab->FindSymbol(t.GetValue(), sLocal)) == NULL)
            SetError(t, "invalid procedure/function identifier.");

          f = new CAstFunctionCall(t, dynamic_cast<const CSymProc *>(symtab->FindSymbol(t.GetValue(), sLocal)));
        }
        else
          SetError(t, "undefined identifier.");

        assert(f != NULL);

        if(isExpr(_scanner->Peek())) {
          CAstExpression *expr = expression(s);
          assert(expr != NULL);
 
          // If array, then addressed.
          if(expr->GetType()->IsArray())
            expr = new CAstSpecialOp(expr->GetToken(), opAddress, expr, NULL);

          f->AddArg(expr);

          while(_scanner->Peek().GetType() == tComma) {
            Consume(tComma);
            expr = expression(s);
            assert(expr != NULL);
 
            // If array, then addressed.
            if(expr->GetType()->IsArray())
              expr = new CAstSpecialOp(expr->GetToken(), opAddress, expr, NULL);

            f->AddArg(expr);
          }
        }
        Consume(tRBrak);

        n = f;
      }
      else { // qualident
        if(_scanner->Peek().GetType() == tLSBrak) { // It means array
          CAstArrayDesignator *f;

          if(symtab->FindSymbol(t.GetValue(), sLocal) == NULL && symtab->FindSymbol(t.GetValue(), sGlobal) != NULL) {
            // Check if symbol is procedure.
            if(dynamic_cast<const CSymProc *>(symtab->FindSymbol(t.GetValue(), sGlobal)) != NULL)
              SetError(t, "designator expected.");

            f = new CAstArrayDesignator(t, symtab->FindSymbol(t.GetValue(), sGlobal));
          }
          else if(symtab->FindSymbol(t.GetValue(), sLocal) != NULL) {
            // Check if symbol is procedure.
            if(dynamic_cast<const CSymProc *>(symtab->FindSymbol(t.GetValue(), sLocal)) != NULL)
              SetError(t, "designator expected");

            f = new CAstArrayDesignator(t, symtab->FindSymbol(t.GetValue(), sLocal));
          }
          else {
            SetError(t, "undefined identifier.");
          }
          
          while(_scanner->Peek().GetType() == tLSBrak) {
            Consume(tLSBrak);

            CAstExpression *expr = expression(s);
            assert(expr != NULL);
            f->AddIndex(expr);

            Consume(tRSBrak);
          }

          f->IndicesComplete();

          n = f;
        } // non-array case.
        else {
          if(symtab->FindSymbol(t.GetValue(), sLocal) == NULL && symtab->FindSymbol(t.GetValue(), sGlobal) != NULL) {
            // Check if symbol is procedure.
            if(dynamic_cast<const CSymProc *>(symtab->FindSymbol(t.GetValue(), sGlobal)) != NULL) {
              SetError(t, "designator expected.");
            }
            n = new CAstDesignator(t, symtab->FindSymbol(t.GetValue(), sGlobal));
          }
          else if(symtab->FindSymbol(t.GetValue(), sLocal) != NULL) {
            // Check if symbol is procedure.
            if(dynamic_cast<const CSymProc *>(symtab->FindSymbol(t.GetValue(), sLocal)) != NULL) {
              SetError(t, "designator expected.");
            }
            n = new CAstDesignator(t, symtab->FindSymbol(t.GetValue(), sLocal));
          }
          else
            SetError(t, "undefined identifier.");
        }
      }
      break;
    }

    case tString:
      Consume(tString, &t);
      n = new CAstStringConstant(t, t.GetValue(), s);
      break;

    case tCharacter:
    {
      Consume(tCharacter, &t);
      char res;

      // If character length is 0, it means character is '\0' because invalid character cases are handled in scanner.
      if(t.GetValue().length() == 0)
        res = '\0';
      else if(t.GetValue().length() == 1) {
        if(t.GetValue().at(0) != '\\')
          res = t.GetValue().at(0);
        else SetError(t, "wrong character");
      }
      // unescape.
      else if(t.GetValue().length() == 2) {
        if(t.GetValue().at(0) == '\\') {
          if(t.GetValue().at(1) == 'n') {
            res = '\n';
          }
          else if(t.GetValue().at(1) == 't') {
            res = '\t';
          }
          else if(t.GetValue().at(1) == '"') {
            res = '"';
          }
          else if(t.GetValue().at(1) == '\'') {
            res = '\'';
          }
          else if(t.GetValue().at(1) == '\\') {
            res = '\\';
          }
          else if(t.GetValue().at(1) == '0') {
            res = '\0';
          }
          else
            SetError(t, "wrong character");
        }
      }
      else SetError(t, "wrong character");

      n = new CAstConstant(t, tm->GetChar(), (long long)res);
      break;
    }

    case tLBrak:
      Consume(tLBrak);
      n = expression(s);
      Consume(tRBrak);
      break;

    case tEMark:
      Consume(tEMark, &t);
      n = new CAstUnaryOp(t, opNot, factor(s));
      break;

    default:
//      cout << "got " << _scanner->Peek() << endl;
      SetError(_scanner->Peek(), "factor expected.");
      break;
  }

  return n;
}