Skip to content

Commit

Permalink
Parsing for interface literals.
Browse files Browse the repository at this point in the history
  • Loading branch information
asoffer committed Dec 29, 2023
1 parent ab4dead commit 61173dd
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 0 deletions.
8 changes: 8 additions & 0 deletions ir/emit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,14 @@ void HandleParseTreeNodeEnumLiteral(ParseNodeIndex, EmitContext&) {
NTH_UNIMPLEMENTED();
}

void HandleParseTreeNodeInterfaceLiteralStart(ParseNodeIndex, EmitContext&) {
NTH_UNIMPLEMENTED();
}

void HandleParseTreeNodeInterfaceLiteral(ParseNodeIndex, EmitContext&) {
NTH_UNIMPLEMENTED();
}

void HandleParseTreeNodeWhileLoopStart(ParseNodeIndex index,
EmitContext& context) {
context.queue.front().branches.push_back(
Expand Down
10 changes: 10 additions & 0 deletions ir/ir.cc
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,16 @@ void HandleParseTreeNodeEnumLiteral(ParseNodeIndex, IrContext&,
NTH_UNIMPLEMENTED();
}

void HandleParseTreeNodeInterfaceLiteralStart(ParseNodeIndex, IrContext&,
diag::DiagnosticConsumer&) {
NTH_UNIMPLEMENTED();
}

void HandleParseTreeNodeInterfaceLiteral(ParseNodeIndex, IrContext&,
diag::DiagnosticConsumer&) {
NTH_UNIMPLEMENTED();
}

void HandleParseTreeNodeWhileLoopStart(ParseNodeIndex index, IrContext& context,
diag::DiagnosticConsumer& diag) {}

Expand Down
1 change: 1 addition & 0 deletions lexer/token_kind.xmacro.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ IC_XMACRO_TOKEN_KIND_KEYWORD(While, "while")
IC_XMACRO_TOKEN_KIND_KEYWORD(Scope, "scope")
IC_XMACRO_TOKEN_KIND_KEYWORD(Return, "return")
IC_XMACRO_TOKEN_KIND_KEYWORD(Enum, "enum")
IC_XMACRO_TOKEN_KIND_KEYWORD(Interface, "interface")

IC_XMACRO_TOKEN_KIND_TERMINAL_EXPRESSION(True, "true")
IC_XMACRO_TOKEN_KIND_TERMINAL_EXPRESSION(False, "false")
Expand Down
2 changes: 2 additions & 0 deletions parse/node.xmacro.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,15 @@ IC_XMACRO_PARSE_NODE(IndexArgumentStart)
IC_XMACRO_PARSE_NODE(FunctionTypeParameters)
IC_XMACRO_PARSE_NODE(FunctionLiteralSignature)
IC_XMACRO_PARSE_NODE(FunctionLiteralStart)
IC_XMACRO_PARSE_NODE(InterfaceLiteralStart)
IC_XMACRO_PARSE_NODE(NoReturns)
IC_XMACRO_PARSE_NODE(Return)
IC_XMACRO_PARSE_NODE_DECLARATION(Declaration)
IC_XMACRO_PARSE_NODE_STATEMENT(Statement)
IC_XMACRO_PARSE_NODE_STATEMENT(IfStatement)
IC_XMACRO_PARSE_NODE_STATEMENT(WhileLoop)
IC_XMACRO_PARSE_NODE_EXPRESSION(ScopeLiteral)
IC_XMACRO_PARSE_NODE_EXPRESSION(InterfaceLiteral)
IC_XMACRO_PARSE_NODE_EXPRESSION(Identifier)
IC_XMACRO_PARSE_NODE_EXPRESSION(ExpressionPrecedenceGroup)
IC_XMACRO_PARSE_NODE_EXPRESSION(MemberExpression)
Expand Down
30 changes: 30 additions & 0 deletions parse/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,13 @@ void Parser::HandleResolveEnumLiteral(ParseTree& tree) {
tree.append(ParseNode::Kind::EnumLiteral, state.token, state.subtree_start);
}

void Parser::HandleResolveInterfaceLiteral(ParseTree& tree) {
PopScope();
auto state = pop_state();
tree.append(ParseNode::Kind::InterfaceLiteral, state.token,
state.subtree_start);
}

void Parser::HandleWhileLoopBody(ParseTree& tree) {
tree.append_leaf(ParseNode::Kind::WhileLoopBodyStart, *iterator_);
tree.back().scope_index = PushScope();
Expand Down Expand Up @@ -789,6 +796,29 @@ void Parser::HandleAtom(ParseTree& tree) {
});
return;
} break;
case Token::Kind::Interface: {
tree.append_leaf(ParseNode::Kind::InterfaceLiteralStart, *iterator_++);
tree.back().scope_index = PushScope();
if (iterator_->kind() != Token::Kind::LeftBracket) { NTH_UNIMPLEMENTED(); }
++iterator_;
if (iterator_->kind() != Token::Kind::Identifier) { NTH_UNIMPLEMENTED(); }
++iterator_;
if (iterator_->kind() != Token::Kind::RightBracket) { NTH_UNIMPLEMENTED(); }
++iterator_;
if (iterator_->kind() != Token::Kind::LeftBrace) { NTH_UNIMPLEMENTED(); }
ExpandState(
State{
.kind = State::Kind::BracedStatementSequence,
.ambient_precedence = Precedence::Loosest(),
.subtree_start = tree.size(),
},
State{
.kind = State::Kind::ResolveInterfaceLiteral,
.ambient_precedence = Precedence::Loosest(),
.subtree_start = tree.size() - 1,
});
return;
} break;
case Token::Kind::Scope: {
tree.append_leaf(ParseNode::Kind::ScopeLiteralStart, *iterator_++);
tree.back().scope_index = PushScope();
Expand Down
64 changes: 64 additions & 0 deletions parse/parser_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1213,4 +1213,68 @@ NTH_TEST("parser/enum/multiple") {
DeclaredIdentifier())))));
}

NTH_TEST("parser/interface/empty") {
diag::NullConsumer d;
TokenBuffer buffer = lex::Lex(R"(interface [T] {})", d);
auto tree = Parse(buffer, d).parse_tree;
NTH_EXPECT(
FromRoot(tree) >>= Module(
ModuleStart(),
StatementSequence(
ScopeStart(),
Statement(StatementStart(),
InterfaceLiteral(InterfaceLiteralStart(),
StatementSequence(ScopeStart()))))));
}

NTH_TEST("parser/interface/declaration") {
diag::NullConsumer d;
TokenBuffer buffer = lex::Lex(R"(interface [T] {
let x ::= T
})", d);
auto tree = Parse(buffer, d).parse_tree;
NTH_EXPECT(
FromRoot(tree) >>=
Module(ModuleStart(),
StatementSequence(
ScopeStart(),
Statement(StatementStart(),
InterfaceLiteral(
InterfaceLiteralStart(),
StatementSequence(
ScopeStart(),
Statement(StatementStart(),
Declaration(DeclarationStart(),
DeclaredIdentifier(),
Identifier()))))))));
}

NTH_TEST("parser/interface/multiple-declaration") {
diag::NullConsumer d;
TokenBuffer buffer = lex::Lex(R"(interface [T] {
let x ::= T
let y ::= T
})",
d);
auto tree = Parse(buffer, d).parse_tree;
NTH_EXPECT(
FromRoot(tree) >>=
Module(ModuleStart(),
StatementSequence(
ScopeStart(),
Statement(StatementStart(),
InterfaceLiteral(
InterfaceLiteralStart(),
StatementSequence(
ScopeStart(),
Statement(StatementStart(),
Declaration(DeclarationStart(),
DeclaredIdentifier(),
Identifier())),
Statement(StatementStart(),
Declaration(DeclarationStart(),
DeclaredIdentifier(),
Identifier()))))))));
}

} // namespace ic
2 changes: 2 additions & 0 deletions parse/state.xmacro.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ IC_XMACRO_PARSER_STATE(ResolveWhileLoop)

IC_XMACRO_PARSER_STATE(ResolveEnumLiteral)

IC_XMACRO_PARSER_STATE(ResolveInterfaceLiteral)

IC_XMACRO_PARSER_STATE(ResolveReturn)

IC_XMACRO_PARSER_STATE(Atom)
Expand Down

0 comments on commit 61173dd

Please sign in to comment.