Skip to content

Commit b8e4d6b

Browse files
author
Минин Степан Александрович
committed
refactor(TopDownParser): LexicalDeclaration
simplify declaration parsing logic Replaced `AddToDeclaration` with `DeclarationAssignmentExpression` Prohibited undefined vars like `let x` grammar-wise
1 parent 2dfb4c7 commit b8e4d6b

2 files changed

Lines changed: 17 additions & 35 deletions

File tree

src/Application/HydraScript.Application.StaticAnalysis/Visitors/DeclarationVisitor.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ public VisitUnit Visit(LexicalDeclaration visitable)
5656
if (_symbolTables[visitable.Scope].ContainsSymbol(new VariableSymbolId(assignment.Destination.Id)))
5757
throw new DeclarationAlreadyExists(assignment.Destination.Id);
5858

59-
var destinationType = assignment.DestinationType?.Accept(
60-
_typeBuilder) ?? _typesService.Undefined;
59+
var destinationType = assignment.DestinationType?.Accept(_typeBuilder) ?? _typesService.Undefined;
6160

6261
if (destinationType == _typesService.Undefined &&
6362
assignment.Source is ImplicitLiteral or ArrayLiteral { Expressions.Count: 0 })

src/Domain/HydraScript.Domain.FrontEnd/Parser/Impl/TopDownParser.Declaration.cs

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,8 @@ private FunctionDeclaration FunctionDeclaration()
6868
Expect("Comma");
6969
}
7070

71-
var rp = Expect("RightParen");
72-
73-
TypeValue returnType = new TypeIdentValue(
74-
TypeId: new IdentifierReference(name: "undefined")
75-
{ Segment = rp.Segment });
71+
Expect("RightParen");
72+
TypeValue returnType = TypeIdentValue.Undefined;
7673

7774
if (CurrentIs("Colon"))
7875
{
@@ -94,12 +91,12 @@ private LexicalDeclaration LexicalDeclaration()
9491
Expect("Keyword", readOnly ? "const" : "let");
9592
var declaration = new LexicalDeclaration(readOnly);
9693

97-
AddToDeclaration(declaration);
94+
declaration.AddAssignment(DeclarationAssignmentExpression());
9895

9996
while (CurrentIs("Comma"))
10097
{
10198
Expect("Comma");
102-
AddToDeclaration(declaration);
99+
declaration.AddAssignment(DeclarationAssignmentExpression());
103100
}
104101

105102
return declaration;
@@ -110,45 +107,31 @@ private LexicalDeclaration LexicalDeclaration()
110107
/// Typed -> Type Initializer?
111108
/// Initializer -> '=' Expression
112109
/// </summary>
113-
private void AddToDeclaration(LexicalDeclaration declaration)
110+
private AssignmentExpression DeclarationAssignmentExpression()
114111
{
115112
var ident = Expect("Ident");
116113
var identRef = new IdentifierReference(ident.Value) { Segment = ident.Segment };
117-
var assignment = new AssignmentExpression(
118-
new MemberExpression(identRef),
119-
new ImplicitLiteral(TypeIdentValue.Undefined))
120-
{ Segment = ident.Segment };
121114

122115
if (CurrentIs("Assign"))
123116
{
124117
var assignSegment = Expect("Assign").Segment;
125-
var expression = Expression();
126-
assignment = new AssignmentExpression(
127-
new MemberExpression(identRef), expression) { Segment = assignSegment };
118+
return new AssignmentExpression(
119+
new MemberExpression(identRef), Expression())
120+
{ Segment = assignSegment };
128121
}
129-
else if (CurrentIs("Colon"))
122+
123+
if (CurrentIs("Colon"))
130124
{
131125
Expect("Colon");
132126
var type = TypeValue();
133-
if (CurrentIs("Assign"))
134-
{
135-
var assignSegment = Expect("Assign").Segment;
136-
var expression = Expression();
137-
assignment = new AssignmentExpression(
138-
new MemberExpression(identRef),
139-
expression, type) { Segment = assignSegment };
140-
}
141-
else
142-
{
143-
var expression = new ImplicitLiteral(type);
144-
assignment = new AssignmentExpression(
145-
lhs: new MemberExpression(identRef),
146-
expression,
147-
type);
148-
}
127+
var assignSegment = CurrentIs("Assign") ? Expect("Assign").Segment : string.Empty;
128+
var expression = assignSegment is not "" ? Expression() : new ImplicitLiteral(type);
129+
return new AssignmentExpression(
130+
new MemberExpression(identRef), expression, type)
131+
{ Segment = assignSegment };
149132
}
150133

151-
declaration.AddAssignment(assignment);
134+
throw new ParserException($"Expected ':' or '=' after var name <{ident}>");
152135
}
153136

154137
/// <summary>

0 commit comments

Comments
 (0)