-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add Constant Conversion Helpers
- Loading branch information
Showing
2 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System; | ||
using DistIL.IR; | ||
using Silverfly.Nodes; | ||
|
||
namespace Silverfly.Backend; | ||
|
||
public static class AstNodeExtensions | ||
{ | ||
public static Value? ToConstant(this AstNode astNode) | ||
{ | ||
if (astNode is LiteralNode literalNode) | ||
{ | ||
return literalNode.Value switch | ||
{ | ||
bool b => ConstInt.CreateI(b ? 1 : 0), | ||
char ch => ConstInt.CreateI(ch), | ||
byte b => ConstInt.CreateI(b), | ||
short s => ConstInt.CreateI(s), | ||
int i => ConstInt.CreateI(i), | ||
long l => ConstInt.CreateL(l), | ||
|
||
sbyte sb => ConstInt.CreateI(sb), | ||
ushort us => ConstInt.CreateI(us), | ||
|
||
float f => ConstFloat.CreateS(f), | ||
double d => ConstFloat.CreateD(d), | ||
|
||
string s => ConstString.Create(s), | ||
|
||
null => ConstNull.Create(), | ||
_ => throw new ArgumentOutOfRangeException() | ||
}; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
|
||
public static BinaryOp MapBinOperator(string op) => | ||
op switch | ||
{ | ||
"+" => BinaryOp.Add, | ||
"-" => BinaryOp.Sub, | ||
"*" => BinaryOp.Mul, | ||
"/" => BinaryOp.FDiv, | ||
_ => throw new ArgumentOutOfRangeException(nameof(op), op, null) | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters