2024-04-26 10:18:49 +08:00
|
|
|
|
using Canon.Core.Abstractions;
|
|
|
|
|
using Canon.Core.CodeGenerators;
|
2024-04-21 22:24:35 +08:00
|
|
|
|
using Canon.Core.Enums;
|
|
|
|
|
using Canon.Core.LexicalParser;
|
2024-04-07 16:47:28 +08:00
|
|
|
|
|
|
|
|
|
namespace Canon.Core.SyntaxNodes;
|
|
|
|
|
|
|
|
|
|
public class MultiplyOperator : NonTerminatedSyntaxNode
|
|
|
|
|
{
|
|
|
|
|
public override NonTerminatorType Type => NonTerminatorType.MultiplyOperator;
|
|
|
|
|
|
2024-04-26 10:18:49 +08:00
|
|
|
|
public override void PreVisit(SyntaxNodeVisitor visitor)
|
|
|
|
|
{
|
|
|
|
|
visitor.PreVisit(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void PostVisit(SyntaxNodeVisitor visitor)
|
|
|
|
|
{
|
|
|
|
|
visitor.PostVisit(this);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-07 16:47:28 +08:00
|
|
|
|
public static MultiplyOperator Create(List<SyntaxNodeBase> children)
|
|
|
|
|
{
|
|
|
|
|
return new MultiplyOperator { Children = children };
|
|
|
|
|
}
|
2024-04-21 22:24:35 +08:00
|
|
|
|
|
|
|
|
|
public override void GenerateCCode(CCodeBuilder builder)
|
|
|
|
|
{
|
|
|
|
|
var token = Children[0].Convert<TerminatedSyntaxNode>().Token;
|
|
|
|
|
if (token.TokenType == SemanticTokenType.Operator)
|
|
|
|
|
{
|
|
|
|
|
var operatorType = token.Convert<OperatorSemanticToken>().OperatorType;
|
|
|
|
|
if (operatorType == OperatorType.Multiply)
|
|
|
|
|
{
|
|
|
|
|
builder.AddString(" *");
|
|
|
|
|
}
|
|
|
|
|
else if (operatorType == OperatorType.Divide)
|
|
|
|
|
{
|
2024-04-22 21:48:57 +08:00
|
|
|
|
//实数除法,需要将操作数强转为double
|
2024-04-21 22:24:35 +08:00
|
|
|
|
builder.AddString(" /(double)");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var keywordType = token.Convert<KeywordSemanticToken>().KeywordType;
|
|
|
|
|
if (keywordType == KeywordType.And)
|
|
|
|
|
{
|
|
|
|
|
builder.AddString(" &&");
|
|
|
|
|
}
|
|
|
|
|
else if (keywordType == KeywordType.Mod)
|
|
|
|
|
{
|
|
|
|
|
builder.AddString(" %");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
builder.AddString(" /");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-07 16:47:28 +08:00
|
|
|
|
}
|