Add first_assignment_defines_type field to Tree.cs (#3370)

This commit is contained in:
Александр Земляк 2026-01-04 14:05:57 +02:00 committed by GitHub
parent 43f02326f2
commit 72e40bf4e9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 32 additions and 14 deletions

View file

@ -53,16 +53,6 @@ namespace Languages.SPython.Frontend.Converters
private void BuildMainFunction(statement_list _statement_list)
{
statement_list mainBody = _statement_list.TypedClone();
// Нужно поправить это клонирование
for (var i = 0; i < _statement_list.Count; i++)
{
if (_statement_list[i] is assign ass && ass.first_assignment_defines_type)
{
((assign)mainBody[i]).first_assignment_defines_type = true;
}
}
mainBody.source_context = mainBody.Parent.source_context; // source context для MAIN охватывает весь файл
procedure_header _procedure_header = new procedure_header(mainFunctionName);
block _block = new block(null, mainBody, mainBody.source_context);

View file

@ -684,6 +684,7 @@ namespace PascalABCCompiler.SyntaxTree
_assign.to = _read_node() as addressed_value;
_assign.from = _read_node() as expression;
_assign.operator_type = (Operators)br.ReadByte();
_assign.first_assignment_defines_type = br.ReadBoolean();
}

View file

@ -180,6 +180,7 @@ namespace PascalABCCompiler.SyntaxTree
_assign.from.visit(this);
}
bw.Write((byte)_assign.operator_type);
bw.Write(_assign.first_assignment_defines_type);
}

View file

@ -910,28 +910,31 @@ namespace PascalABCCompiler.SyntaxTree
///<summary>
///Конструктор с параметрами.
///</summary>
public assign(addressed_value _to,expression _from,Operators _operator_type)
public assign(addressed_value _to,expression _from,Operators _operator_type,bool _first_assignment_defines_type)
{
this._to=_to;
this._from=_from;
this._operator_type=_operator_type;
this._first_assignment_defines_type=_first_assignment_defines_type;
FillParentsInDirectChilds();
}
///<summary>
///Конструктор с параметрами.
///</summary>
public assign(addressed_value _to,expression _from,Operators _operator_type,SourceContext sc)
public assign(addressed_value _to,expression _from,Operators _operator_type,bool _first_assignment_defines_type,SourceContext sc)
{
this._to=_to;
this._from=_from;
this._operator_type=_operator_type;
this._first_assignment_defines_type=_first_assignment_defines_type;
source_context = sc;
FillParentsInDirectChilds();
}
protected addressed_value _to;
protected expression _from;
protected Operators _operator_type;
protected bool _first_assignment_defines_type;
///<summary>
///Левый операнд оператора присваивания (чему присваивать).
@ -982,6 +985,21 @@ namespace PascalABCCompiler.SyntaxTree
}
}
///<summary>
///Необходимо ли задать тип переменной равным типу правой части
///</summary>
public bool first_assignment_defines_type
{
get
{
return _first_assignment_defines_type;
}
set
{
_first_assignment_defines_type=value;
}
}
/// <summary> Создает копию узла </summary>
public override syntax_tree_node Clone()
@ -1006,6 +1024,7 @@ namespace PascalABCCompiler.SyntaxTree
copy.from.Parent = copy;
}
copy.operator_type = operator_type;
copy.first_assignment_defines_type = first_assignment_defines_type;
return copy;
}

View file

@ -381,11 +381,17 @@ namespace PascalABCCompiler.SyntaxTree
{ }
public assign(string name, bool value) : this(new ident(name), new bool_const(value))
{ }
public assign(addressed_value _to, expression _from, Operators _operator_type) : this(_to, _from, _operator_type, false)
{ }
public assign(addressed_value _to, expression _from, Operators _operator_type, SourceContext sc) : this(_to, _from, _operator_type, false, sc)
{ }
public override string ToString()
{
return string.Format("{0} {1} {2}", to, OperatorServices.ToString(operator_type, StringConstants.pascalLanguageName), from);
}
public bool first_assignment_defines_type = false;
}
public partial class bin_expr

View file

@ -71,6 +71,7 @@
<SyntaxField Name="to" SyntaxType="addressed_value" />
<SyntaxField Name="from" SyntaxType="expression" />
<ExtendedField Name="operator_type" Type="Operators" CreateVariable="false" DeleteVariable="false" />
<ExtendedField Name="first_assignment_defines_type" Type="bool" CreateVariable="false" DeleteVariable="false" />
</Fields>
<Methods />
<Tags>
@ -3654,7 +3655,7 @@
<HelpData Key="assign.assign(ident left,expression ex): this(new ident_list(left),ex,Operators.Assignment)" Value="" />
<HelpData Key="assign.assign(string left,expression ex): this(new ident(left),ex)" Value="" />
<HelpData Key="assign.assign(string left,expression ex, SourceContext sc): this(new ident(left),ex,sc)" Value="" />
<HelpData Key="assign.first_assignment_defines_type" Value="Для SPython" />
<HelpData Key="assign.first_assignment_defines_type" Value="Необходимо ли задать тип переменной равным типу правой части" />
<HelpData Key="assign.from" Value="Выражение в правой части" />
<HelpData Key="assign.operator_type" Value="Тип оператора присваивания" />
<HelpData Key="assign.override string ToString()" Value="" />