Skip to content

Commit

Permalink
#15 - удалил методы из объекта
Browse files Browse the repository at this point in the history
  • Loading branch information
Stepami committed Jul 18, 2024
1 parent 38f0e27 commit 9820043
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 98 deletions.
42 changes: 4 additions & 38 deletions HydraScript.Lib/FrontEnd/TopDownParse/Impl/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -725,54 +725,20 @@ private ObjectLiteral ObjectLiteral()
{
Expect("LeftCurl");
var properties = new List<Property>();
var methods = new List<FunctionDeclaration>();
while (CurrentIs("Ident"))
{
var idToken = Expect("Ident");
var id = new IdentifierReference(idToken.Value)
{ Segment = idToken.Segment };
if (CurrentIs("Colon"))
{
Expect("Colon");
var expr = Expression();
properties.Add(new Property(id, expr));
}
else if (CurrentIs("Arrow"))
{
Expect("Arrow");
Expect("LeftParen");
var args = new List<PropertyTypeValue>();
while (CurrentIs("Ident"))
{
var paramName = Expect("Ident").Value;
Expect("Colon");
var type = TypeValue();
args.Add(new PropertyTypeValue(paramName, type));
if (!CurrentIs("RightParen"))
{
Expect("Comma");
}
}
var rp = Expect("RightParen");
TypeValue returnType = new TypeIdentValue(
TypeId: new IdentifierReference(name: "undefined")
{ Segment = rp.Segment });
if (CurrentIs("Colon"))
{
Expect("Colon");
returnType = TypeValue();
}

var name = new IdentifierReference(idToken.Value) { Segment = idToken.Segment };
methods.Add(new FunctionDeclaration(name, returnType, args, BlockStatement())
{ Segment = idToken.Segment }
);
}
Expect("Colon");
var expr = Expression();
properties.Add(new Property(id, expr));

Expect("SemiColon");
}
Expect("RightCurl");
return new ObjectLiteral(properties, methods);
return new ObjectLiteral(properties);
}

private ArrayLiteral ArrayLiteral()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using HydraScript.Lib.BackEnd;
using HydraScript.Lib.IR.Ast.Impl.Nodes.Declarations.AfterTypesAreLoaded;
using HydraScript.Lib.IR.Ast.Visitors;
using HydraScript.Lib.IR.CheckSemantics.Visitors;

Expand All @@ -8,31 +7,21 @@ namespace HydraScript.Lib.IR.Ast.Impl.Nodes.Expressions.ComplexLiterals;
public class ObjectLiteral : ComplexLiteral
{
public List<Property> Properties { get; }
public List<FunctionDeclaration> Methods { get; }

public ObjectLiteral(IEnumerable<Property> properties, IEnumerable<FunctionDeclaration> methods)
public ObjectLiteral(IEnumerable<Property> properties)
{
Properties = new List<Property>(properties);
Properties.ForEach(prop => prop.Parent = this);

Methods = new List<FunctionDeclaration>(methods);
Methods.ForEach(m => m.Parent = this);
}

public override IEnumerator<AbstractSyntaxTreeNode> GetEnumerator() =>
Properties.Concat<AbstractSyntaxTreeNode>(Methods).GetEnumerator();
Properties.GetEnumerator();

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

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 AddressedInstructions Accept(InstructionProvider visitor) =>
visitor.Visit(this);
}
6 changes: 1 addition & 5 deletions HydraScript.Lib/IR/Ast/Impl/Nodes/Expressions/Expression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@ namespace HydraScript.Lib.IR.Ast.Impl.Nodes.Expressions;
public abstract class Expression : AbstractSyntaxTreeNode,
IVisitable<ExpressionInstructionProvider, AddressedInstructions>
{
protected Expression()
{
}

public abstract AddressedInstructions Accept(ExpressionInstructionProvider visitor);

public override AddressedInstructions Accept(InstructionProvider visitor) =>
public sealed override AddressedInstructions Accept(InstructionProvider visitor) =>
throw new NotSupportedException();
}
17 changes: 16 additions & 1 deletion HydraScript.Lib/IR/Ast/Visitors/ExpressionInstructionProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace HydraScript.Lib.IR.Ast.Visitors;
public class ExpressionInstructionProvider :
IVisitor<PrimaryExpression, AddressedInstructions>,
IVisitor<ArrayLiteral, AddressedInstructions>,
IVisitor<ObjectLiteral, AddressedInstructions>,
IVisitor<Property, AddressedInstructions>,
IVisitor<UnaryExpression, AddressedInstructions>,
IVisitor<BinaryExpression, AddressedInstructions>,
Expand Down Expand Up @@ -58,7 +59,21 @@ public AddressedInstructions Visit(ArrayLiteral visitable)

return result;
}


public AddressedInstructions Visit(ObjectLiteral visitable)
{
var objectId = visitable.Id;
var createObject = new CreateObject(objectId);

var result = new AddressedInstructions { createObject };

result.AddRange(visitable.Properties
.SelectMany(property =>
property.Accept(this)));

return result;
}

public AddressedInstructions Visit(Property visitable)
{
var objectId = visitable.Object.Id;
Expand Down
25 changes: 0 additions & 25 deletions HydraScript.Lib/IR/Ast/Visitors/InstructionProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
using HydraScript.Lib.BackEnd.Addresses;
using HydraScript.Lib.BackEnd.Instructions;
using HydraScript.Lib.BackEnd.Instructions.WithAssignment;
using HydraScript.Lib.BackEnd.Instructions.WithAssignment.ComplexData.Create;
using HydraScript.Lib.BackEnd.Instructions.WithJump;
using HydraScript.Lib.BackEnd.Values;
using HydraScript.Lib.IR.Ast.Impl.Nodes;
using HydraScript.Lib.IR.Ast.Impl.Nodes.Declarations.AfterTypesAreLoaded;
using HydraScript.Lib.IR.Ast.Impl.Nodes.Expressions.ComplexLiterals;
using HydraScript.Lib.IR.Ast.Impl.Nodes.Expressions.PrimaryExpressions;
using HydraScript.Lib.IR.Ast.Impl.Nodes.Statements;

Expand All @@ -20,7 +18,6 @@ public class InstructionProvider :
IVisitor<InsideStatementJump, AddressedInstructions>,
IVisitor<ExpressionStatement, AddressedInstructions>,
IVisitor<ReturnStatement, AddressedInstructions>,
IVisitor<ObjectLiteral, AddressedInstructions>,
IVisitor<FunctionDeclaration, AddressedInstructions>,
IVisitor<WhileStatement, AddressedInstructions>,
IVisitor<IfStatement, AddressedInstructions>
Expand Down Expand Up @@ -97,28 +94,6 @@ public AddressedInstructions Visit(ReturnStatement visitable)
return result;
}

public AddressedInstructions Visit(ObjectLiteral visitable)
{
var objectId = visitable.Id;
var createObject = new CreateObject(objectId);

var result = new AddressedInstructions { createObject };

result.AddRange(visitable.Methods
.SelectMany(method =>
method.Accept(this)
)
);

result.AddRange(visitable.Properties
.SelectMany(property =>
property.Accept(_expressionVisitor)
)
);

return result;
}

public AddressedInstructions Visit(FunctionDeclaration visitable)
{
if (!visitable.Statements.Any())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using HydraScript.Lib.IR.Ast;
using HydraScript.Lib.IR.Ast.Impl.Nodes;
using HydraScript.Lib.IR.Ast.Impl.Nodes.Declarations.AfterTypesAreLoaded;
using HydraScript.Lib.IR.Ast.Impl.Nodes.Expressions.ComplexLiterals;
using HydraScript.Lib.IR.Ast.Impl.Nodes.Statements;
using HydraScript.Lib.IR.CheckSemantics.Visitors.Services;

Expand All @@ -11,8 +10,7 @@ public class SymbolTableInitializer :
IVisitor<AbstractSyntaxTreeNode>,
IVisitor<ScriptBody>,
IVisitor<FunctionDeclaration>,
IVisitor<BlockStatement>,
IVisitor<ObjectLiteral>
IVisitor<BlockStatement>
{
private readonly ISymbolTableInitializerService _initializerService;
private readonly IStandardLibraryProvider _provider;
Expand Down Expand Up @@ -53,12 +51,4 @@ public Unit Visit(BlockStatement visitable)
visitable.StatementList.ForEach(item => item.Accept(this));
return default;
}

public Unit Visit(ObjectLiteral visitable)
{
_initializerService.InitWithNewScope(visitable);
visitable.Properties.ForEach(property => property.Accept(this));
visitable.Methods.ForEach(method => method.Accept(this));
return default;
}
}
7 changes: 2 additions & 5 deletions HydraScript/grammar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,9 @@ Literal -> "NullLiteral"
"StringLiteral"
"BooleanLiteral"
ObjectLiteral -> '{' PropertyDefinitionList '}'
PropertyDefinitionList -> (Property ';')*
Property -> FieldProperty
MethodProperty
PropertyDefinitionList -> (FieldProperty ';')*
FieldProperty -> "Ident" ':' Expression
MethodProperty -> "Ident" '=>' MethodDeclaration
MethodDeclaration -> '(' FunctionParameters? ')' Type? BlockStatement

ArrayLiteral -> '[' ElementList ']'
ElementList -> (Expression ',')*

Expand Down

0 comments on commit 9820043

Please sign in to comment.