Add typeclass declaration translation

This commit is contained in:
Bogdan Voloshin 2018-04-13 18:25:29 +03:00
parent cd9912deb7
commit 9e9890b4ab
3 changed files with 36 additions and 8 deletions

View file

@ -6,13 +6,16 @@
ExplicitModelAttribute = class(System.Attribute)
end;
(*
[Concept] SumTC<T> = 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<T> = class where T: constructor;
class _instance: T;
class inited: boolean := false;
@ -42,11 +45,11 @@
Result := _instance;
end;
end;
end;*)
function Sum3<T, SumT>(v1, v2, v3: T): T; where SumT: SumTC<T>, constructor;
begin
var s := ConceptSingleton&<SumT>.&Instance;
var s := __ConceptSingleton&<SumT>.&Instance;
Result := s.sum(v1, s.sum(v2, v3));
end;

View file

@ -24,6 +24,8 @@ namespace PascalABCCompiler.SyntaxTreeConverters
//--- Обработка синтаксически сахарных узлов
SyntaxVisitors.TypeclassVisitors.ReplaceTypeclassVisitor.New.ProcessNode(root);
// loop
LoopDesugarVisitor.New.ProcessNode(root);

View file

@ -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))
{