Canon/Canon.Core/SyntaxNodes/MultiplyOperator.cs
jackfiled 3a584751dc feat: 针对C的代码生成 (#50)
Co-authored-by: Lan_G <2911328695@qq.com>
Reviewed-on: PostGuard/Canon#50
2024-04-21 22:24:35 +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)
{
//实数除法需要将操作数强转为float
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(" /");
}
}
}
}