Skip to content

Commit

Permalink
check object literal without methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Stepami committed Oct 19, 2023
1 parent a5f6657 commit 6bef425
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Interpreter.Lib.BackEnd;
using Interpreter.Lib.IR.Ast.Impl.Nodes.Declarations.AfterTypesAreLoaded;
using Interpreter.Lib.IR.Ast.Visitors;
using Interpreter.Lib.IR.CheckSemantics.Visitors.SemanticChecker;
using Interpreter.Lib.IR.CheckSemantics.Visitors.SymbolTableInitializer;
using Visitor.NET;

Expand All @@ -25,12 +26,15 @@ public override IEnumerator<AbstractSyntaxTreeNode> GetEnumerator() =>

protected override string NodeRepresentation() => "{}";

public override AddressedInstructions Accept(InstructionProvider visitor) =>
public override Unit Accept(SymbolTableInitializer visitor) =>
visitor.Visit(this);

public override Type Accept(SemanticChecker visitor) =>
visitor.Visit(this);

public override AddressedInstructions Accept(ExpressionInstructionProvider visitor) =>
new InstructionProvider().Visit(this);

public override Unit Accept(SymbolTableInitializer visitor) =>
public override AddressedInstructions Accept(InstructionProvider visitor) =>
visitor.Visit(this);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class SemanticChecker :
IVisitor<IdentifierReference, Type>,
IVisitor<ImplicitLiteral, Type>,
IVisitor<ArrayLiteral, Type>,
IVisitor<ObjectLiteral, Type>,
IVisitor<ConditionalExpression, Type>,
IVisitor<BinaryExpression, Type>,
IVisitor<UnaryExpression, Type>,
Expand Down Expand Up @@ -126,6 +127,23 @@ public Type Visit(ArrayLiteral visitable)
throw new WrongArrayLiteralDeclaration(visitable.Segment, type);
}

public Type Visit(ObjectLiteral visitable)
{
var properties = visitable.Properties.Select(prop =>
{
var propType = prop.Expression.Accept(this);
visitable.SymbolTable.AddSymbol(propType switch
{
ObjectType objectType => new ObjectSymbol(prop.Id, objectType),
_ => new VariableSymbol(prop.Id, propType)
});
return new PropertyType(prop.Id, propType);
});
var objectLiteralType = new ObjectType(properties);
visitable.SymbolTable.AddSymbol(new ObjectSymbol(id: "this", objectLiteralType, readOnly: true));
return objectLiteralType;
}

public Type Visit(ConditionalExpression visitable)
{
var tType = visitable.Test.Accept(this);
Expand Down

0 comments on commit 6bef425

Please sign in to comment.