Skip to content

Commit

Permalink
guard against no initializer
Browse files Browse the repository at this point in the history
  • Loading branch information
Stepami committed Oct 16, 2023
1 parent a8ed4d0 commit 2cc2a53
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 18 deletions.
8 changes: 7 additions & 1 deletion Interpreter.Lib/FrontEnd/TopDownParse/Impl/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,13 @@ private void AddToDeclaration(LexicalDeclaration declaration)
{
var ident = Expect("Ident");
var identRef = new IdentifierReference(ident.Value) { Segment = ident.Segment };
AssignmentExpression assignment = null;
var assignment = new AssignmentExpression(
new MemberExpression(identRef),
new ImplicitLiteral(
new TypeIdentValue(
new IdentifierReference("undefined"))))
{ Segment = ident.Segment };

if (CurrentIs("Assign"))
{
var assignSegment = Expect("Assign").Segment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ namespace Interpreter.Lib.IR.Ast.Impl.Nodes.Declarations.AfterTypesAreLoaded;

public class LexicalDeclaration : AfterTypesAreLoadedDeclaration
{
public bool Readonly { get; }
public bool ReadOnly { get; }
public List<AssignmentExpression> Assignments { get; }

public LexicalDeclaration(bool @readonly)
public LexicalDeclaration(bool readOnly)
{
Readonly = @readonly;
ReadOnly = readOnly;
Assignments = new();
}

Expand All @@ -27,7 +27,7 @@ public override IEnumerator<AbstractSyntaxTreeNode> GetEnumerator() =>
Assignments.GetEnumerator();

protected override string NodeRepresentation() =>
Readonly ? "const" : "let";
ReadOnly ? "const" : "let";

public override AddressedInstructions Accept(InstructionProvider visitor) =>
visitor.Visit(this);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Diagnostics.CodeAnalysis;
using Interpreter.Lib.IR.Ast.Impl.Nodes.Expressions.PrimaryExpressions;

namespace Interpreter.Lib.IR.CheckSemantics.Exceptions;

[ExcludeFromCodeCoverage]
public class DeclarationWithoutInitializer : SemanticException
{
public DeclarationWithoutInitializer(IdentifierReference ident, bool readOnly) :
base(ident.Segment, $"'{(readOnly ? "const" : "let")}' without initializer: {ident.Name}") { }
}
11 changes: 9 additions & 2 deletions Interpreter.Lib/IR/CheckSemantics/Visitors/DeclarationVisitor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Interpreter.Lib.IR.Ast;
using Interpreter.Lib.IR.Ast.Impl.Nodes.Declarations.AfterTypesAreLoaded;
using Interpreter.Lib.IR.Ast.Impl.Nodes.Expressions.ComplexLiterals;
using Interpreter.Lib.IR.Ast.Impl.Nodes.Expressions.PrimaryExpressions;
using Interpreter.Lib.IR.CheckSemantics.Exceptions;
using Interpreter.Lib.IR.CheckSemantics.Types;
using Interpreter.Lib.IR.CheckSemantics.Variables.Symbols;
Expand All @@ -26,14 +28,19 @@ public Unit Visit(LexicalDeclaration visitable)
{
if (visitable.SymbolTable.ContainsSymbol(assignment.Destination.Id))
throw new DeclarationAlreadyExists(assignment.Destination.Id);

var destinationType = assignment.DestinationType?.BuildType(
assignment.SymbolTable) ?? "undefined";

if (destinationType == "undefined" &&
assignment.Source is ImplicitLiteral or ArrayLiteral { Expressions.Count: 0 })
throw new DeclarationWithoutInitializer(assignment.Destination.Id, visitable.ReadOnly);

visitable.SymbolTable.AddSymbol(
new VariableSymbol(
assignment.Destination.Id,
destinationType,
visitable.Readonly));
visitable.ReadOnly));
}

return default;
Expand Down

0 comments on commit 2cc2a53

Please sign in to comment.