Ejemplo n.º 1
0
// Parse a function type.
//
//    function-type:
//      '(' [type-list] ')' type
//
// TODO: A function should not be able to return a pack type.
Type&
Parser::function_type()
{
  Type_list types;
  match(lparen_tok);
  if (lookahead() != rparen_tok)
    types = type_list();
  match(rparen_tok);
  match(arrow_tok);
  Type& ret = type();
  return on_function_type(types, ret);
}
Ejemplo n.º 2
0
// Parse a primary type.
//
//    primary-type -> 'bool'
//                  | 'int'
//                  | 'char'
//                  | id-type
//                  | function-type
//
//    function-type -> '(' type-list ')' '->' type
//
//    type-list -> type | type-list ',' type
Type const*
Parser::primary_type()
{
  // id-type
  if (Token tok = match_if(identifier_tok))
    return on_id_type(tok);

  // bool
  if (match_if(bool_kw))
    return get_boolean_type();

  // char
  if (match_if(char_kw))
    return get_character_type();

  // int
  else if (match_if(int_kw))
    return get_integer_type();

  // function-type
  else if (match_if(lparen_tok)) {
    Type_seq ts;
    while (true) {
      ts.push_back(type());
      if (match_if(comma_tok))
        continue;
      else
        break;
    }
    match(rparen_tok);
    match(arrow_tok);
    Type const* t = type();
    return on_function_type(ts, t);
  }

  // error
  //
  // TODO: Make this a little less vague.
  else
    error("invalid type");
}
Ejemplo n.º 3
0
// Parse a primary type.
//
//    primary-type -> 'bool'
//                  | 'int'
//                  | 'char'
//                  | id-type
//                  | function-type
//
//    function-type -> '(' type-list ')' '->' type
//
//    type-list -> type | type-list ',' type
Type const*
Parser::primary_type()
{
  // id-type
  if (Token tok = match_if(identifier_tok))
    return on_id_type(tok);

  // bool
  else if (match_if(bool_kw))
    return get_boolean_type();

  // char
  else if (match_if(char_kw))
    return get_character_type();

  // int
  else if (match_if(int_kw))
    return get_integer_type();

  // uint
  else if (match_if(uint_kw))
    return get_integer_type(false);

  // short
  else if (match_if(short_kw))
    return get_integer_type(16);

  // ushort
  else if (match_if(ushort_kw))
    return get_integer_type(false, 16);

  // long
  else if (match_if(long_kw))
    return get_integer_type(64);

  // ulong
  else if (match_if(ulong_kw))
    return get_integer_type(false, 64);

  // int16
  else if (match_if(int16_kw))
    return get_integer_type(16);

  // uint16
  else if (match_if(uint16_kw))
    return get_integer_type(false, 16);

  // int32
  else if (match_if(int32_kw))
    return get_integer_type();

  // uint32
  else if (match_if(uint32_kw))
    return get_integer_type(false);

  // int64
  else if (match_if(int64_kw))
    return get_integer_type(64);

  // uint64
  else if (match_if(uint64_kw))
    return get_integer_type(false, 64);

  // float
  else if (match_if(float_kw))
      return get_float_type();

  // double
  else if (match_if(double_kw))
      return get_double_type();

  // function-type
  else if (match_if(lparen_tok)) {
    Type_seq ts;
    while (true) {
      ts.push_back(type());
      if (match_if(comma_tok))
        continue;
      else
        break;
    }
    match(rparen_tok);
    match(arrow_tok);
    Type const* t = type();
    return on_function_type(ts, t);
  }

  // error
  //
  // TODO: Make this a little less vague.
  else
    error("invalid type");
}