// Return a sequence of types for the sequence of terms. Type_seq* get_type(Term_seq* e) { Type_seq* types = new Type_seq(); for (Term* t : *e) types->push_back(get_type(t)); return types; }
// Elaborate each type in the function type. Type const* Elaborator::elaborate(Function_type const* t) { Type_seq ts; ts.reserve(t->parameter_types().size()); for (Type const* t1 : t->parameter_types()) ts.push_back(elaborate(t1)); Type const* r = elaborate(t->return_type()); return get_function_type(ts, r); }
// 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"); }
// 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"); }