Some FillParentsInDirectChilds() in TreeHelper.cs

This commit is contained in:
miks1965 2018-05-28 00:35:33 +03:00
parent abddd5e1d6
commit fda348a317
3 changed files with 33 additions and 20 deletions

View file

@ -43,7 +43,9 @@ function AddC(ex1,ex2: Expr) := new Add(ex1,ex2);
function MultC(ex1,ex2: Expr) := new Mult(ex1,ex2);
function VC(name: string) := new V(name);
function Simplify(e: Expr): Expr;
function Simplify(e: Expr): Expr; forward;
function Simplify1(e: Expr): Expr;
begin
match e with
Mult(Cons(c),Cons(c1)): Result := ConsC(c*c1);
@ -52,36 +54,43 @@ begin
Add(Cons(c),ex) when c=0: Result := Simplify(ex);
Add(ex,Cons(c)) when c=0: Result := Simplify(ex);
Add(Cons(c),Cons(c1)): Result := ConsC(c+c1);
Add(Cons(c),ex): Result := AddC(ex,ConsC(c)); // константы - в хвосте!
Mult(Cons(c),ex): Result := MultC(ex,ConsC(c));
Add(Add(ex,Cons(c)),Cons(c1)): Result := AddC(ex,ConsC(c+c1)); // ассоциативность
Mult(Mult(ex,Cons(c)),Cons(c1)): Result := MultC(ex,ConsC(c*c1)); // ассоциативность
Neg(Cons(c)): Result := ConsC(-c);
Mult(e1,e2): Result := MultC(Simplify(e1),Simplify(e2));
Add(e1,e2): Result := AddC(Simplify(e1),Simplify(e2));
Neg(e1): Result := NegC(Simplify(e1));
else Result := e;
end;
end;
procedure Print(e: Expr);
function Simplify(e: Expr): Expr;
begin
match e with
Mult(l,r): begin Print(l); Print('*'); Print(r); end;
Add(l,r): begin Print(l); Print('+'); Print(r); end;
Neg(n): begin Print('-'); Print(n); end;
Cons(c): Print(c);
V(x): Print(x);
Mult(e1,e2): Result := Simplify1(MultC(Simplify(e1),Simplify(e2)));
Add(e1,e2): Result := Simplify1(AddC(Simplify(e1),Simplify(e2)));
Neg(e1): Result := Simplify1(NegC(Simplify(e1)));
else Result := e;
end;
end;
function Str(e: Expr): string;
begin
match e with
Mult(l,r): Result := Str(l) + ' * ' + Str(r);
Add(l,r): Result := Str(l) + ' + ' + Str(r);
Neg(n): Result := '-' + Str(n);
Cons(c): Result := c.ToString;
V(x): Result := x;
end;
end;
begin
// 0 + 1*x + 0 + 0*(2 + a)
var r: Expr := AddC(ConsC(0),AddC(MultC(ConsC(1),VC('x')),ConsC(0)));
// 0 + 1*x + 1 + 0*(2 + a) + 2
var r: Expr := AddC(ConsC(1),AddC(MultC(ConsC(1),VC('x')),ConsC(1)));
r := AddC(r,MultC(ConsC(0),AddC(ConsC(2),VC('a'))));
Print(r);
Println;
loop 2 do
begin
r := Simplify(r);
Print(r);
Println;
end;
r := AddC(r,ConsC(2));
Str(r).Println;
r := Simplify(r);
Str(r).Println;
end.

View file

@ -798,6 +798,7 @@ namespace PascalABCCompiler.SyntaxTree
this.proc_body = proc_body;
source_context = sc;
is_short_definition = false;
FillParentsInDirectChilds();
}
public procedure_definition(procedure_header proc_header, proc_block proc_body)
{
@ -805,6 +806,7 @@ namespace PascalABCCompiler.SyntaxTree
this.proc_body = proc_body;
source_context = null;
is_short_definition = false;
FillParentsInDirectChilds();
}
public static procedure_definition EmptyDefaultConstructor

View file

@ -15644,7 +15644,9 @@ namespace PascalABCCompiler.TreeConverter
{
//self variable
if ((lv.function as common_method_node).self_variable == lv)
{
AddError(lloc, "VARIABLE_{0}_READONLY", lv.name);
}
}
return create_variable_reference(dn, lloc);
}