2024-04-21 22:24:35 +08:00
|
|
|
|
using Canon.Core.CodeGenerators;
|
|
|
|
|
using Canon.Core.Enums;
|
|
|
|
|
using Canon.Core.LexicalParser;
|
2024-04-07 16:47:28 +08:00
|
|
|
|
|
|
|
|
|
namespace Canon.Core.SyntaxNodes;
|
|
|
|
|
|
|
|
|
|
public class Factor : NonTerminatedSyntaxNode
|
|
|
|
|
{
|
|
|
|
|
public override NonTerminatorType Type => NonTerminatorType.Factor;
|
|
|
|
|
|
|
|
|
|
public static Factor Create(List<SyntaxNodeBase> children)
|
|
|
|
|
{
|
|
|
|
|
return new Factor { Children = children };
|
|
|
|
|
}
|
2024-04-21 22:24:35 +08:00
|
|
|
|
|
|
|
|
|
public override void GenerateCCode(CCodeBuilder builder)
|
|
|
|
|
{
|
|
|
|
|
if (Children.Count == 1)
|
|
|
|
|
{
|
|
|
|
|
//factor -> num
|
|
|
|
|
if (Children[0].IsTerminated)
|
|
|
|
|
{
|
|
|
|
|
var token = Children[0].Convert<TerminatedSyntaxNode>().Token;
|
|
|
|
|
if (token.TokenType == SemanticTokenType.Number)
|
|
|
|
|
{
|
|
|
|
|
builder.AddString(" " + token.LiteralValue);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// factor -> variable
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Children[0].GenerateCCode(builder);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//factor -> ( expression )
|
|
|
|
|
else if (Children.Count == 3)
|
|
|
|
|
{
|
|
|
|
|
builder.AddString(" (");
|
|
|
|
|
Children[1].GenerateCCode(builder);
|
|
|
|
|
builder.AddString(")");
|
|
|
|
|
}
|
|
|
|
|
//factor -> id ( expression )
|
|
|
|
|
else if (Children.Count == 4)
|
|
|
|
|
{
|
|
|
|
|
builder.AddString(" " + Children[0].Convert<TerminatedSyntaxNode>().Token.
|
|
|
|
|
Convert<IdentifierSemanticToken>().IdentifierName);
|
|
|
|
|
builder.AddString("(");
|
|
|
|
|
Children[2].GenerateCCode(builder);
|
|
|
|
|
builder.AddString(")");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{ //factor -> not factor
|
|
|
|
|
builder.AddString(" (");
|
|
|
|
|
if (Children[0].Convert<TerminatedSyntaxNode>().Token.TokenType == SemanticTokenType.Keyword)
|
|
|
|
|
{
|
|
|
|
|
builder.AddString("!");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
builder.AddString("-");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Children[1].GenerateCCode(builder);
|
|
|
|
|
builder.AddString(")");
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-07 16:47:28 +08:00
|
|
|
|
}
|