diff --git a/HydraScript.Lib/FrontEnd/TopDownParse/Impl/Parser.cs b/HydraScript.Lib/FrontEnd/TopDownParse/Impl/Parser.cs index e30f5cf7..ffde4232 100644 --- a/HydraScript.Lib/FrontEnd/TopDownParse/Impl/Parser.cs +++ b/HydraScript.Lib/FrontEnd/TopDownParse/Impl/Parser.cs @@ -725,54 +725,20 @@ private ObjectLiteral ObjectLiteral() { Expect("LeftCurl"); var properties = new List(); - var methods = new List(); 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(); - 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() diff --git a/HydraScript.Lib/IR/Ast/Impl/Nodes/Expressions/ComplexLiterals/ObjectLiteral.cs b/HydraScript.Lib/IR/Ast/Impl/Nodes/Expressions/ComplexLiterals/ObjectLiteral.cs index 54ca7ae9..be97943d 100644 --- a/HydraScript.Lib/IR/Ast/Impl/Nodes/Expressions/ComplexLiterals/ObjectLiteral.cs +++ b/HydraScript.Lib/IR/Ast/Impl/Nodes/Expressions/ComplexLiterals/ObjectLiteral.cs @@ -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; @@ -8,31 +7,21 @@ namespace HydraScript.Lib.IR.Ast.Impl.Nodes.Expressions.ComplexLiterals; public class ObjectLiteral : ComplexLiteral { public List Properties { get; } - public List Methods { get; } - public ObjectLiteral(IEnumerable properties, IEnumerable methods) + public ObjectLiteral(IEnumerable properties) { Properties = new List(properties); Properties.ForEach(prop => prop.Parent = this); - - Methods = new List(methods); - Methods.ForEach(m => m.Parent = this); } public override IEnumerator GetEnumerator() => - Properties.Concat(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); } \ No newline at end of file diff --git a/HydraScript.Lib/IR/Ast/Impl/Nodes/Expressions/Expression.cs b/HydraScript.Lib/IR/Ast/Impl/Nodes/Expressions/Expression.cs index c3ec5ada..9da02668 100644 --- a/HydraScript.Lib/IR/Ast/Impl/Nodes/Expressions/Expression.cs +++ b/HydraScript.Lib/IR/Ast/Impl/Nodes/Expressions/Expression.cs @@ -6,12 +6,8 @@ namespace HydraScript.Lib.IR.Ast.Impl.Nodes.Expressions; public abstract class Expression : AbstractSyntaxTreeNode, IVisitable { - protected Expression() - { - } - public abstract AddressedInstructions Accept(ExpressionInstructionProvider visitor); - public override AddressedInstructions Accept(InstructionProvider visitor) => + public sealed override AddressedInstructions Accept(InstructionProvider visitor) => throw new NotSupportedException(); } \ No newline at end of file diff --git a/HydraScript.Lib/IR/Ast/Visitors/ExpressionInstructionProvider.cs b/HydraScript.Lib/IR/Ast/Visitors/ExpressionInstructionProvider.cs index 1dc69694..b7da4851 100644 --- a/HydraScript.Lib/IR/Ast/Visitors/ExpressionInstructionProvider.cs +++ b/HydraScript.Lib/IR/Ast/Visitors/ExpressionInstructionProvider.cs @@ -18,6 +18,7 @@ namespace HydraScript.Lib.IR.Ast.Visitors; public class ExpressionInstructionProvider : IVisitor, IVisitor, + IVisitor, IVisitor, IVisitor, IVisitor, @@ -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; diff --git a/HydraScript.Lib/IR/Ast/Visitors/InstructionProvider.cs b/HydraScript.Lib/IR/Ast/Visitors/InstructionProvider.cs index b04ee727..85d554be 100644 --- a/HydraScript.Lib/IR/Ast/Visitors/InstructionProvider.cs +++ b/HydraScript.Lib/IR/Ast/Visitors/InstructionProvider.cs @@ -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; @@ -20,7 +18,6 @@ public class InstructionProvider : IVisitor, IVisitor, IVisitor, - IVisitor, IVisitor, IVisitor, IVisitor @@ -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()) diff --git a/HydraScript.Lib/IR/CheckSemantics/Visitors/SymbolTableInitializer.cs b/HydraScript.Lib/IR/CheckSemantics/Visitors/SymbolTableInitializer.cs index 06efff67..94bf278a 100644 --- a/HydraScript.Lib/IR/CheckSemantics/Visitors/SymbolTableInitializer.cs +++ b/HydraScript.Lib/IR/CheckSemantics/Visitors/SymbolTableInitializer.cs @@ -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; @@ -11,8 +10,7 @@ public class SymbolTableInitializer : IVisitor, IVisitor, IVisitor, - IVisitor, - IVisitor + IVisitor { private readonly ISymbolTableInitializerService _initializerService; private readonly IStandardLibraryProvider _provider; @@ -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; - } } \ No newline at end of file diff --git a/HydraScript/grammar.txt b/HydraScript/grammar.txt index e03eb1ea..65856ad2 100644 --- a/HydraScript/grammar.txt +++ b/HydraScript/grammar.txt @@ -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 ',')*