Canon/Canon.Core/SyntaxNodes/MultiplyOperator.cs
jackfiled d381f56e1d fix: 修复编译中的一些小问题 (#52)
Co-authored-by: Lan_G <2911328695@qq.com>
Reviewed-on: PostGuard/Canon#52
2024-04-22 21:48:57 +08:00

51 lines
1.5 KiB
C#
Raw 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.

using Canon.Core.CodeGenerators;
using Canon.Core.Enums;
using Canon.Core.LexicalParser;
namespace Canon.Core.SyntaxNodes;
public class MultiplyOperator : NonTerminatedSyntaxNode
{
public override NonTerminatorType Type => NonTerminatorType.MultiplyOperator;
public static MultiplyOperator Create(List<SyntaxNodeBase> children)
{
return new MultiplyOperator { Children = children };
}
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)
{
//实数除法需要将操作数强转为double
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(" /");
}
}
}
}