From a9b9b2545a60e02f5ede973e0d28ca25d0e2131a Mon Sep 17 00:00:00 2001 From: Voloshin Bogdan Date: Thu, 17 May 2018 03:00:10 +0300 Subject: [PATCH] Improve code examples --- .../SimpleTypeclassesImplementation.pas | 19 +++-- CodeExamples/t1.pas | 77 +++++++++++++++++++ .../StandardSyntaxConverter.cs | 2 +- 3 files changed, 87 insertions(+), 11 deletions(-) create mode 100644 CodeExamples/t1.pas diff --git a/CodeExamples/SimpleTypeclassesImplementation.pas b/CodeExamples/SimpleTypeclassesImplementation.pas index 49a40b1e1..99f99fb43 100644 --- a/CodeExamples/SimpleTypeclassesImplementation.pas +++ b/CodeExamples/SimpleTypeclassesImplementation.pas @@ -90,7 +90,7 @@ type function equal(x, y: integer):boolean := x = y; end; -(* + Ord[integer] = instance function compare(x, y: integer): Ordering; begin @@ -101,8 +101,7 @@ type else Result := _GT; end; - end; -*) + end; Show[boolean] = instance @@ -156,7 +155,7 @@ begin end; end; -(* + procedure MySort(var a: array of T); where Ord[T]; begin for var i := 1 to a.Length - 1 do @@ -164,9 +163,9 @@ begin var sorted := true; for var j := 0 to a.Length - 1 - i do begin - if Ord&[T].greater(a[i - 1], a[i]) then + if Ord&[T].greater(a[j], a[j + 1]) then begin - swap(a[i - 1], a[i]); + swap(a[j], a[j + 1]); sorted := false; end; end; @@ -175,7 +174,7 @@ begin break; end; end; -*) + // ---Test Functions--- @@ -227,7 +226,7 @@ begin writeln(isCorrect); end; -(* + procedure TestOrd(); begin var a := Arr(3, 1, 4, 2, 5, 0); @@ -241,12 +240,12 @@ begin writeln(isCorrect); end; -*) + begin TestEq(); TestShow(); TestRead(); - // TestOrd(); + TestOrd(); end. \ No newline at end of file diff --git a/CodeExamples/t1.pas b/CodeExamples/t1.pas new file mode 100644 index 000000000..3035f6c02 --- /dev/null +++ b/CodeExamples/t1.pas @@ -0,0 +1,77 @@ +type + Ordering = (_EQ, _LT, _GT); + + IEq = interface + function eequal(x, y: T): boolean; + function notEqual(x, y: T): boolean; + end; + + + Eq = abstract class(IEq) + public + + function eequal(x, y: T): boolean; virtual; + begin + Result := not notEqual(x, y); + end; + + function notEqual(x, y: T): boolean; virtual; + begin + Result := not eequal(x, y); + end; + end; + + + IOrd = interface(IEq) + function compare(x, y: T): Ordering; + function less(x, y: T): boolean; + function lessEqual(x, y: T): boolean; + function greater(x, y: T): boolean; + function greaterEqual(x, y: T): boolean; + function mmin(x, y: T): T; + function mmax(x, y: T): T; + end; + + + Ord = abstract class + where EqT: IEq, constructor; + public + + function eequal(x, y: T1): boolean; virtual; + begin + var eqInst := __ConceptSingleton&.&Instance; + Result := eqInst.eequal(x, y); + end; + + function notEqual(x, y: T1): boolean; virtual; + begin + var eqInst := __ConceptSingleton&.&Instance; + Result := eqInst.notEqual(x, y); + end; + end; + +function ArrayEq(l1, l2: array of T): boolean; where EqT: IEq, constructor; +begin + var eqtinstance := __ConceptSingleton&.&Instance; + Result := true; + for var i :=0 to l1.Length - 1 do + if eqtinstance.notEqual(l1[i], l2[i]) then + begin + Result := false; + break; + end; +end; + + +function Max3(v1, v2, v3: T): T; + where OrdT: IOrd, constructor; +begin + var ordInst := __ConceptSingleton&.&Instance; + + Result := ordInst.mmax(v1, ordInst.mmax(v2, v3)); +end; + + +begin + // writeln(Max3&>(1, 2, 3)) +end. \ No newline at end of file diff --git a/SyntaxTreeConverters/StandardSyntaxConverter.cs b/SyntaxTreeConverters/StandardSyntaxConverter.cs index fb9464572..ea9d3ddb5 100644 --- a/SyntaxTreeConverters/StandardSyntaxConverter.cs +++ b/SyntaxTreeConverters/StandardSyntaxConverter.cs @@ -32,7 +32,7 @@ namespace PascalABCCompiler.SyntaxTreeConverters SyntaxVisitors.TypeclassVisitors.ReplaceTypeclassVisitor.New(instancesAndRestrictedFunctions).ProcessNode(root); } root.FillParentsInAllChilds(); - new SimplePrettyPrinterVisitor("E:/projs/out.txt").ProcessNode(root); + //new SimplePrettyPrinterVisitor("E:/projs/out.txt").ProcessNode(root); // loop LoopDesugarVisitor.New.ProcessNode(root);