pascalabcnet/SyntaxVisitors/BaseVisitors/SmallHelperVisitors.cs
Mikhalkovich Stanislav 490ddaa43a var q := (a,b: integer; c: real) -> begin
end;

теперь работает
2020-08-12 15:22:16 +03:00

86 lines
2.7 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Copyright (c) Ivan Bondarev, Stanislav Mikhalkovich (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
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);
}
}
// есть ли в выражении переменная с данным именем (не включая вложенные лямбды)
// (используется для поиска Result)
public class HasNameVisitor : WalkingVisitorNew
{
private string varname;
public ident id = null;
public static ident HasName(syntax_tree_node sn, string varname)
{
var v = new HasNameVisitor(varname);
v.ProcessNode(sn);
return v.id;
}
public HasNameVisitor(string varname)
{
this.varname = varname.ToLower();
}
public override void visit(ident i)
{
if (i.name.ToLower() == varname.ToLower())
id = i;
}
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)
{
}
}
}