2019-07-28 23:53:15 +03:00
|
|
|
|
// Copyright (c) Ivan Bondarev, Stanislav Mikhalkovich (for details please see \doc\copyright.txt)
|
2017-02-19 16:29:24 +03:00
|
|
|
|
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
|
|
|
|
|
|
using System;
|
2016-07-18 13:07:08 +03:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
|
|
using PascalABCCompiler;
|
|
|
|
|
|
using PascalABCCompiler.SyntaxTree;
|
|
|
|
|
|
|
|
|
|
|
|
namespace SyntaxVisitors
|
|
|
|
|
|
{
|
|
|
|
|
|
public class FindLocalDefsVisitor : WalkingVisitorNew // Запускать только для подпрограмм
|
|
|
|
|
|
{
|
|
|
|
|
|
public ISet<string> ids = new HashSet<string>();
|
|
|
|
|
|
private bool indef = false;
|
|
|
|
|
|
public override void visit(ident id)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (indef)
|
|
|
|
|
|
ids.Add(id.name);
|
|
|
|
|
|
}
|
|
|
|
|
|
public override void visit(var_def_statement defs)
|
|
|
|
|
|
{
|
|
|
|
|
|
indef = true;
|
|
|
|
|
|
ProcessNode(defs.vars); // исключаем типы - просматриваем только имена переменных
|
|
|
|
|
|
indef = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Print()
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var x in ids)
|
|
|
|
|
|
Console.Write(x + ", ");
|
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public class FindMainIdentsVisitor : WalkingVisitorNew // в выражении yield это надо будет.
|
|
|
|
|
|
{
|
|
|
|
|
|
public HashSet<string> vars = new HashSet<string>();
|
|
|
|
|
|
public override void visit(ident id)
|
|
|
|
|
|
{
|
|
|
|
|
|
vars.Add(id.name);
|
|
|
|
|
|
}
|
|
|
|
|
|
public override void visit(dot_node dn)
|
|
|
|
|
|
{
|
|
|
|
|
|
ProcessNode(dn.left);
|
|
|
|
|
|
if (dn.right.GetType() != typeof(ident))
|
|
|
|
|
|
ProcessNode(dn.right);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-10-26 20:12:42 +03:00
|
|
|
|
|
2020-08-12 15:22:16 +03:00
|
|
|
|
// есть ли в выражении переменная с данным именем (не включая вложенные лямбды)
|
|
|
|
|
|
// (используется для поиска Result)
|
|
|
|
|
|
public class HasNameVisitor : WalkingVisitorNew
|
2018-10-26 20:12:42 +03:00
|
|
|
|
{
|
|
|
|
|
|
private string varname;
|
2018-12-01 13:08:46 +03:00
|
|
|
|
public ident id = null;
|
2020-08-12 15:22:16 +03:00
|
|
|
|
public static ident HasName(syntax_tree_node sn, string varname)
|
2018-12-01 13:08:46 +03:00
|
|
|
|
{
|
2020-08-12 15:22:16 +03:00
|
|
|
|
var v = new HasNameVisitor(varname);
|
|
|
|
|
|
v.ProcessNode(sn);
|
2018-12-01 13:08:46 +03:00
|
|
|
|
return v.id;
|
|
|
|
|
|
}
|
2020-08-12 15:22:16 +03:00
|
|
|
|
public HasNameVisitor(string varname)
|
2018-10-26 20:12:42 +03:00
|
|
|
|
{
|
|
|
|
|
|
this.varname = varname.ToLower();
|
|
|
|
|
|
}
|
2018-12-01 13:08:46 +03:00
|
|
|
|
public override void visit(ident i)
|
2018-10-26 20:12:42 +03:00
|
|
|
|
{
|
2019-07-06 01:10:31 +03:00
|
|
|
|
if (i.name.ToLower() == varname.ToLower())
|
2018-12-01 13:08:46 +03:00
|
|
|
|
id = i;
|
2018-10-26 20:12:42 +03:00
|
|
|
|
}
|
|
|
|
|
|
public override void visit(dot_node dn)
|
|
|
|
|
|
{
|
|
|
|
|
|
ProcessNode(dn.left);
|
|
|
|
|
|
if (dn.right.GetType() != typeof(ident))
|
|
|
|
|
|
ProcessNode(dn.right);
|
|
|
|
|
|
}
|
|
|
|
|
|
public override void visit(function_lambda_definition fd)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-07-18 13:07:08 +03:00
|
|
|
|
}
|