diff --git a/include/bolt/CST.hpp b/include/bolt/CST.hpp index 131e7d9d5..1d435a705 100644 --- a/include/bolt/CST.hpp +++ b/include/bolt/CST.hpp @@ -137,6 +137,8 @@ namespace bolt { CallExpression, InfixExpression, PrefixExpression, + RecordExpressionField, + RecordExpression, ExpressionStatement, ReturnStatement, IfStatement, @@ -146,8 +148,8 @@ namespace bolt { LetBlockBody, LetExprBody, LetDeclaration, - StructDeclarationField, - StructDeclaration, + RecordDeclarationField, + RecordDeclaration, ClassDeclaration, InstanceDeclaration, SourceFile, @@ -1366,6 +1368,49 @@ namespace bolt { }; + class RecordExpressionField : public Node { + public: + + Identifier* Name; + Equals* Equals; + Expression* E; + + inline RecordExpressionField( + Identifier* Name, + class Equals* Equals, + Expression* E + ): Node(NodeKind::RecordExpressionField), + Name(Name), + Equals(Equals), + E(E) {} + + Token* getFirstToken() const override; + Token* getLastToken() const override; + + inline Expression* getExpression() const { + return E; + } + + }; + + class RecordExpression : public Expression { + public: + + LBrace* LBrace; + std::vector> Fields; + RBrace* RBrace; + + inline RecordExpression( + class LBrace* LBrace, + std::vector> Fields, + class RBrace* RBrace + ): Expression(NodeKind::RecordExpression), + LBrace(LBrace), + Fields(Fields), + RBrace(RBrace) {} + + }; + class Statement : public Node { protected: @@ -1640,14 +1685,14 @@ namespace bolt { }; - class StructDeclarationField : public Node { + class RecordDeclarationField : public Node { public: - StructDeclarationField( + RecordDeclarationField( Identifier* Name, class Colon* Colon, class TypeExpression* TypeExpression - ): Node(NodeKind::StructDeclarationField), + ): Node(NodeKind::RecordDeclarationField), Name(Name), Colon(Colon), TypeExpression(TypeExpression) {} @@ -1661,22 +1706,22 @@ namespace bolt { }; - class StructDeclaration : public Node { + class RecordDeclaration : public Node { public: class PubKeyword* PubKeyword; class StructKeyword* StructKeyword; Identifier* Name; class BlockStart* BlockStart; - std::vector Fields; + std::vector Fields; - StructDeclaration( + RecordDeclaration( class PubKeyword* PubKeyword, class StructKeyword* StructKeyword, Identifier* Name, class BlockStart* BlockStart, - std::vector Fields - ): Node(NodeKind::StructDeclaration), + std::vector Fields + ): Node(NodeKind::RecordDeclaration), PubKeyword(PubKeyword), StructKeyword(StructKeyword), Name(Name), @@ -1780,8 +1825,8 @@ namespace bolt { template<> inline NodeKind getNodeType() { return NodeKind::LetBlockBody; } template<> inline NodeKind getNodeType() { return NodeKind::LetExprBody; } template<> inline NodeKind getNodeType() { return NodeKind::LetDeclaration; } - template<> inline NodeKind getNodeType() { return NodeKind::StructDeclarationField; } - template<> inline NodeKind getNodeType() { return NodeKind::StructDeclaration; } + template<> inline NodeKind getNodeType() { return NodeKind::RecordDeclarationField; } + template<> inline NodeKind getNodeType() { return NodeKind::RecordDeclaration; } template<> inline NodeKind getNodeType() { return NodeKind::ClassDeclaration; } template<> inline NodeKind getNodeType() { return NodeKind::InstanceDeclaration; } template<> inline NodeKind getNodeType() { return NodeKind::SourceFile; } diff --git a/include/bolt/CSTVisitor.hpp b/include/bolt/CSTVisitor.hpp index 19b5efc88..953dd4f35 100644 --- a/include/bolt/CSTVisitor.hpp +++ b/include/bolt/CSTVisitor.hpp @@ -127,6 +127,10 @@ namespace bolt { return static_cast(this)->visitInfixExpression(static_cast(N)); case NodeKind::PrefixExpression: return static_cast(this)->visitPrefixExpression(static_cast(N)); + case NodeKind::RecordExpressionField: + return static_cast(this)->visitRecordExpressionField(static_cast(N)); + case NodeKind::RecordExpression: + return static_cast(this)->visitRecordExpression(static_cast(N)); case NodeKind::ExpressionStatement: return static_cast(this)->visitExpressionStatement(static_cast(N)); case NodeKind::ReturnStatement: @@ -145,10 +149,10 @@ namespace bolt { return static_cast(this)->visitLetExprBody(static_cast(N)); case NodeKind::LetDeclaration: return static_cast(this)->visitLetDeclaration(static_cast(N)); - case NodeKind::StructDeclarationField: - return static_cast(this)->visitStructDeclarationField(static_cast(N)); - case NodeKind::StructDeclaration: - return static_cast(this)->visitStructDeclaration(static_cast(N)); + case NodeKind::RecordDeclarationField: + return static_cast(this)->visitStructDeclarationField(static_cast(N)); + case NodeKind::RecordDeclaration: + return static_cast(this)->visitStructDeclaration(static_cast(N)); case NodeKind::ClassDeclaration: return static_cast(this)->visitClassDeclaration(static_cast(N)); case NodeKind::InstanceDeclaration: @@ -416,6 +420,14 @@ namespace bolt { visitExpression(N); } + void visitRecordExpressionField(RecordExpressionField* N) { + visitNode(N); + } + + void visitRecordExpression(RecordExpression* N) { + visitExpression(N); + } + void visitStatement(Statement* N) { visitNode(N); } @@ -460,11 +472,11 @@ namespace bolt { visitNode(N); } - void visitStructDeclarationField(StructDeclarationField* N) { + void visitStructDeclarationField(RecordDeclarationField* N) { visitNode(N); } - void visitStructDeclaration(StructDeclaration* N) { + void visitStructDeclaration(RecordDeclaration* N) { visitNode(N); } @@ -658,6 +670,12 @@ namespace bolt { case NodeKind::PrefixExpression: visitEachChild(static_cast(N)); break; + case NodeKind::RecordExpressionField: + visitEachChild(static_cast(N)); + break; + case NodeKind::RecordExpression: + visitEachChild(static_cast(N)); + break; case NodeKind::ExpressionStatement: visitEachChild(static_cast(N)); break; @@ -685,11 +703,11 @@ namespace bolt { case NodeKind::LetDeclaration: visitEachChild(static_cast(N)); break; - case NodeKind::StructDeclaration: - visitEachChild(static_cast(N)); + case NodeKind::RecordDeclaration: + visitEachChild(static_cast(N)); break; - case NodeKind::StructDeclarationField: - visitEachChild(static_cast(N)); + case NodeKind::RecordDeclarationField: + visitEachChild(static_cast(N)); break; case NodeKind::ClassDeclaration: visitEachChild(static_cast(N)); @@ -959,6 +977,23 @@ namespace bolt { BOLT_VISIT(N->Argument); } + void visitEachChild(RecordExpressionField* N) { + BOLT_VISIT(N->Name); + BOLT_VISIT(N->Equals); + BOLT_VISIT(N->E); + } + + void visitEachChild(RecordExpression* N) { + BOLT_VISIT(N->LBrace); + for (auto [Field, Comma]: N->Fields) { + BOLT_VISIT(Field); + if (Comma) { + BOLT_VISIT(Comma); + } + } + BOLT_VISIT(N->RBrace); + } + void visitEachChild(ExpressionStatement* N) { BOLT_VISIT(N->Expression); } @@ -1029,13 +1064,13 @@ namespace bolt { } } - void visitEachChild(StructDeclarationField* N) { + void visitEachChild(RecordDeclarationField* N) { BOLT_VISIT(N->Name); BOLT_VISIT(N->Colon); BOLT_VISIT(N->TypeExpression); } - void visitEachChild(StructDeclaration* N) { + void visitEachChild(RecordDeclaration* N) { if (N->PubKeyword) { BOLT_VISIT(N->PubKeyword); } diff --git a/src/CST.cc b/src/CST.cc index aa9db755c..1ef91ac64 100644 --- a/src/CST.cc +++ b/src/CST.cc @@ -514,22 +514,22 @@ namespace bolt { return Pattern->getLastToken(); } - Token* StructDeclarationField::getFirstToken() const { + Token* RecordDeclarationField::getFirstToken() const { return Name; } - Token* StructDeclarationField::getLastToken() const { + Token* RecordDeclarationField::getLastToken() const { return TypeExpression->getLastToken(); } - Token* StructDeclaration::getFirstToken() const { + Token* RecordDeclaration::getFirstToken() const { if (PubKeyword) { return PubKeyword; } return StructKeyword; } - Token* StructDeclaration::getLastToken() const { + Token* RecordDeclaration::getLastToken() const { if (Fields.size()) { Fields.back()->getLastToken(); }