SupaLidlGame/Debug/Transpiler/LiteralExpression.cs

43 lines
967 B
C#
Raw Permalink Normal View History

using System.Text.RegularExpressions;
namespace SupaLidlGame.Debug.Transpiler;
public class LiteralExpression : Expression
{
public Token Literal { get; set; }
public LiteralExpression(Token literal, int line, int col)
: base(line, col)
{
Literal = literal;
}
2023-09-29 11:32:15 -07:00
public string EscapedLiteral()
{
return Literal.Value.Replace("\"", "\\\"")
.Replace("'", "\\'")
.Replace("\n", "\\n")
.Replace("\t", "\\t");
}
public override string Transpile()
{
2023-09-29 11:32:15 -07:00
var val = EscapedLiteral();
if (Literal.Type == TokenType.NodePath)
{
2023-09-26 10:25:55 -07:00
return $"from.call(\"{val}\")";
}
else if (Literal.Type == TokenType.String)
{
2023-09-29 11:32:15 -07:00
return $"\"{val}\"";
}
return Literal.Value;
}
public string TranspileNodePath()
{
2023-09-29 11:32:15 -07:00
var val = EscapedLiteral();
return $"to_node_path.call(\"{val}\")";
}
}