Improve code examples

This commit is contained in:
Voloshin Bogdan 2018-05-17 03:00:10 +03:00
parent 29761537c7
commit a9b9b2545a
3 changed files with 87 additions and 11 deletions

View file

@ -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<T>(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.

77
CodeExamples/t1.pas Normal file
View file

@ -0,0 +1,77 @@
type
Ordering = (_EQ, _LT, _GT);
IEq<T> = interface
function eequal(x, y: T): boolean;
function notEqual(x, y: T): boolean;
end;
Eq<T> = abstract class(IEq<T>)
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<T> = interface(IEq<T>)
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<T1, EqT> = abstract class
where EqT: IEq<T1>, constructor;
public
function eequal(x, y: T1): boolean; virtual;
begin
var eqInst := __ConceptSingleton&<EqT>.&Instance;
Result := eqInst.eequal(x, y);
end;
function notEqual(x, y: T1): boolean; virtual;
begin
var eqInst := __ConceptSingleton&<EqT>.&Instance;
Result := eqInst.notEqual(x, y);
end;
end;
function ArrayEq<T, EqT>(l1, l2: array of T): boolean; where EqT: IEq<T>, constructor;
begin
var eqtinstance := __ConceptSingleton&<EqT>.&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<T, OrdT>(v1, v2, v3: T): T;
where OrdT: IOrd<T>, constructor;
begin
var ordInst := __ConceptSingleton&<OrdT>.&Instance;
Result := ordInst.mmax(v1, ordInst.mmax(v2, v3));
end;
begin
// writeln(Max3&<integer, Ord_integer<Eq_integer>>(1, 2, 3))
end.

View file

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