diff --git a/CodeExamples/TypeclassTranslation.pas b/CodeExamples/TypeclassTranslation.pas index b2df6a5aa..aed28616d 100644 --- a/CodeExamples/TypeclassTranslation.pas +++ b/CodeExamples/TypeclassTranslation.pas @@ -6,13 +6,16 @@ ExplicitModelAttribute = class(System.Attribute) end; - +(* [Concept] SumTC = abstract class public constructor; begin end; function sum(v1, v2: T): T; abstract; + end;*) + SumTC[T] = typeclass + function sum(v1, v2: T): T; end; @@ -27,7 +30,7 @@ Result := v1 + v2; end; end; - +(* ConceptSingleton = class where T: constructor; class _instance: T; class inited: boolean := false; @@ -42,11 +45,11 @@ Result := _instance; end; - end; + end;*) function Sum3(v1, v2, v3: T): T; where SumT: SumTC, constructor; begin - var s := ConceptSingleton&.&Instance; + var s := __ConceptSingleton&.&Instance; Result := s.sum(v1, s.sum(v2, v3)); end; diff --git a/SyntaxTreeConverters/StandardSyntaxConverter.cs b/SyntaxTreeConverters/StandardSyntaxConverter.cs index 732c904af..f046844b8 100644 --- a/SyntaxTreeConverters/StandardSyntaxConverter.cs +++ b/SyntaxTreeConverters/StandardSyntaxConverter.cs @@ -24,6 +24,8 @@ namespace PascalABCCompiler.SyntaxTreeConverters //--- Обработка синтаксически сахарных узлов + SyntaxVisitors.TypeclassVisitors.ReplaceTypeclassVisitor.New.ProcessNode(root); + // loop LoopDesugarVisitor.New.ProcessNode(root); diff --git a/SyntaxVisitors/TypeclassVisitors/ReplaceTypeclassVisitor.cs b/SyntaxVisitors/TypeclassVisitors/ReplaceTypeclassVisitor.cs index 21c247cc9..201ece03a 100644 --- a/SyntaxVisitors/TypeclassVisitors/ReplaceTypeclassVisitor.cs +++ b/SyntaxVisitors/TypeclassVisitors/ReplaceTypeclassVisitor.cs @@ -16,6 +16,14 @@ namespace SyntaxVisitors.TypeclassVisitors } + public static ReplaceTypeclassVisitor New + { + get + { + return new ReplaceTypeclassVisitor(); + } + } + bool VisitInstanceDeclaration(type_declaration instanceDeclaration) { var instanceDefinition = instanceDeclaration.type_def as instance_definition; @@ -61,25 +69,40 @@ namespace SyntaxVisitors.TypeclassVisitors typeclassDefTranslated.attribute = class_attribute.Abstract; for (int i = 0; i < typeclassDefTranslated.body.class_def_blocks.Count; i++) { - var cm = typeclassDefTranslated.body.class_def_blocks[i]; + var cm = typeclassDefTranslated.body.class_def_blocks[i].members; for (int j = 0; j < cm.Count; j++) { - (cm[j] as function_header)?.proc_attributes.Add(new procedure_attribute("abstract", proc_attribute.attr_abstract)); + // TODO: or override if implementation exists (cm[j] as procedure_header)?.proc_attributes.Add(new procedure_attribute("abstract", proc_attribute.attr_abstract)); } } - // TODO: add constructor + + { + // TODO: add constructor + var cm = typeclassDefTranslated.body.class_def_blocks[0]; + var def = new procedure_definition( + new constructor(), + new statement_list(new empty_statement())); + def.proc_body.Parent = def; + def.proc_header.proc_attributes = new procedure_attributes_list(); + + cm.Add(def); + } + + var templates = new ident_list(); templates.source_context = typeclassName.restriction_args.source_context; for (int i = 0; i < typeclassName.restriction_args.Count; i++) { - templates.Add((typeclassName.restriction_args[i] as named_type_reference).names[0]); + templates.Add((typeclassName.restriction_args.params_list[i] as named_type_reference).names[0]); } var typeclassNameTanslated = new template_type_name(typeclassName.name, templates, typeclassName.source_context); + Replace(typeclassDeclaration, new type_declaration(typeclassNameTanslated, typeclassDefTranslated, typeclassDeclaration.source_context)); + /* if (typeclassInstanceDeclarations.ContainsKey(typeclassName.name)) {