Emit integers
This commit is contained in:
parent
3bc8ef0d7d
commit
52198147e2
@ -462,10 +462,18 @@ namespace Parser.Binding
|
|||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
private BoundNumberDoubleLiteralExpression BindNumberLiteralExpression(NumberLiteralExpressionSyntaxNode node)
|
private BoundNumberLiteralExpression BindNumberLiteralExpression(NumberLiteralExpressionSyntaxNode node)
|
||||||
{
|
{
|
||||||
var value = (double)node.Number.Value!;
|
var value = (double)node.Number.Value!;
|
||||||
return NumberDoubleLiteral(node, value);
|
var intValue = (int)Math.Round(value);
|
||||||
|
if (intValue == value)
|
||||||
|
{
|
||||||
|
return NumberIntLiteral(node, intValue);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return NumberDoubleLiteral(node, value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private BoundExpression BindParenthesizedExpression(ParenthesizedExpressionSyntaxNode node)
|
private BoundExpression BindParenthesizedExpression(ParenthesizedExpressionSyntaxNode node)
|
||||||
|
@ -44,7 +44,8 @@
|
|||||||
LambdaExpression,
|
LambdaExpression,
|
||||||
MemberAccessExpression,
|
MemberAccessExpression,
|
||||||
NamedFunctionHandleExpression,
|
NamedFunctionHandleExpression,
|
||||||
NumberLiteralExpression,
|
NumberDoubleLiteralExpression,
|
||||||
|
NumberIntLiteralExpression,
|
||||||
ParenthesizedExpression,
|
ParenthesizedExpression,
|
||||||
StringLiteralExpression,
|
StringLiteralExpression,
|
||||||
TypedFunctionCallExpression,
|
TypedFunctionCallExpression,
|
||||||
|
@ -515,7 +515,14 @@ namespace Parser.Binding
|
|||||||
public override TypeSymbol Type => throw new System.NotImplementedException();
|
public override TypeSymbol Type => throw new System.NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
public class BoundNumberDoubleLiteralExpression : BoundExpression
|
public abstract class BoundNumberLiteralExpression : BoundExpression
|
||||||
|
{
|
||||||
|
protected BoundNumberLiteralExpression(SyntaxNode syntax) : base(syntax)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class BoundNumberDoubleLiteralExpression : BoundNumberLiteralExpression
|
||||||
{
|
{
|
||||||
public BoundNumberDoubleLiteralExpression(SyntaxNode syntax, double value)
|
public BoundNumberDoubleLiteralExpression(SyntaxNode syntax, double value)
|
||||||
: base(syntax)
|
: base(syntax)
|
||||||
@ -524,12 +531,12 @@ namespace Parser.Binding
|
|||||||
}
|
}
|
||||||
|
|
||||||
public double Value { get; }
|
public double Value { get; }
|
||||||
public override BoundNodeKind Kind => BoundNodeKind.NumberLiteralExpression;
|
public override BoundNodeKind Kind => BoundNodeKind.NumberDoubleLiteralExpression;
|
||||||
|
|
||||||
public override TypeSymbol Type => TypeSymbol.Double;
|
public override TypeSymbol Type => TypeSymbol.Double;
|
||||||
}
|
}
|
||||||
|
|
||||||
public class BoundNumberIntLiteralExpression : BoundExpression
|
public class BoundNumberIntLiteralExpression : BoundNumberLiteralExpression
|
||||||
{
|
{
|
||||||
public BoundNumberIntLiteralExpression(SyntaxNode syntax, int value)
|
public BoundNumberIntLiteralExpression(SyntaxNode syntax, int value)
|
||||||
: base(syntax)
|
: base(syntax)
|
||||||
@ -537,8 +544,8 @@ namespace Parser.Binding
|
|||||||
Value = value;
|
Value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double Value { get; }
|
public int Value { get; }
|
||||||
public override BoundNodeKind Kind => BoundNodeKind.NumberLiteralExpression;
|
public override BoundNodeKind Kind => BoundNodeKind.NumberIntLiteralExpression;
|
||||||
|
|
||||||
public override TypeSymbol Type => TypeSymbol.Int;
|
public override TypeSymbol Type => TypeSymbol.Int;
|
||||||
}
|
}
|
||||||
|
@ -245,8 +245,10 @@ namespace Parser.Binding
|
|||||||
RewriteMemberAccessExpression((BoundMemberAccessExpression)node),
|
RewriteMemberAccessExpression((BoundMemberAccessExpression)node),
|
||||||
BoundNodeKind.NamedFunctionHandleExpression =>
|
BoundNodeKind.NamedFunctionHandleExpression =>
|
||||||
RewriteNamedFunctionHandleExpression((BoundNamedFunctionHandleExpression)node),
|
RewriteNamedFunctionHandleExpression((BoundNamedFunctionHandleExpression)node),
|
||||||
BoundNodeKind.NumberLiteralExpression =>
|
BoundNodeKind.NumberDoubleLiteralExpression =>
|
||||||
RewriteNumberLiteralExpression((BoundNumberDoubleLiteralExpression)node),
|
RewriteNumberDoubleLiteralExpression((BoundNumberDoubleLiteralExpression)node),
|
||||||
|
BoundNodeKind.NumberIntLiteralExpression =>
|
||||||
|
RewriteNumberIntLiteralExpression((BoundNumberIntLiteralExpression)node),
|
||||||
BoundNodeKind.StringLiteralExpression =>
|
BoundNodeKind.StringLiteralExpression =>
|
||||||
RewriteStringLiteralExpression((BoundStringLiteralExpression)node),
|
RewriteStringLiteralExpression((BoundStringLiteralExpression)node),
|
||||||
BoundNodeKind.TypedVariableExpression =>
|
BoundNodeKind.TypedVariableExpression =>
|
||||||
@ -287,7 +289,12 @@ namespace Parser.Binding
|
|||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual BoundExpression RewriteNumberLiteralExpression(BoundNumberDoubleLiteralExpression node)
|
public virtual BoundExpression RewriteNumberDoubleLiteralExpression(BoundNumberDoubleLiteralExpression node)
|
||||||
|
{
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual BoundExpression RewriteNumberIntLiteralExpression(BoundNumberIntLiteralExpression node)
|
||||||
{
|
{
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
@ -540,8 +540,11 @@ namespace Parser.Emitting
|
|||||||
case BoundNodeKind.IdentifierNameExpression:
|
case BoundNodeKind.IdentifierNameExpression:
|
||||||
EmitIdentifierNameExpression((BoundIdentifierNameExpression)node, ilProcessor);
|
EmitIdentifierNameExpression((BoundIdentifierNameExpression)node, ilProcessor);
|
||||||
break;
|
break;
|
||||||
case BoundNodeKind.NumberLiteralExpression:
|
case BoundNodeKind.NumberDoubleLiteralExpression:
|
||||||
EmitNumberLiteralExpression((BoundNumberDoubleLiteralExpression)node, ilProcessor);
|
EmitNumberDoubleLiteralExpression((BoundNumberDoubleLiteralExpression)node, ilProcessor);
|
||||||
|
break;
|
||||||
|
case BoundNodeKind.NumberIntLiteralExpression:
|
||||||
|
EmitNumberIntLiteralExpression((BoundNumberIntLiteralExpression)node, ilProcessor);
|
||||||
break;
|
break;
|
||||||
case BoundNodeKind.StringLiteralExpression:
|
case BoundNodeKind.StringLiteralExpression:
|
||||||
EmitStringLiteralExpression((BoundStringLiteralExpression)node, ilProcessor);
|
EmitStringLiteralExpression((BoundStringLiteralExpression)node, ilProcessor);
|
||||||
@ -566,6 +569,10 @@ namespace Parser.Emitting
|
|||||||
{
|
{
|
||||||
ilProcessor.Emit(OpCodes.Call, _doubleToMObject);
|
ilProcessor.Emit(OpCodes.Call, _doubleToMObject);
|
||||||
}
|
}
|
||||||
|
if ((fromType, toType) == (TypeSymbol.Int, TypeSymbol.MObject))
|
||||||
|
{
|
||||||
|
ilProcessor.Emit(OpCodes.Call, _intToMObject);
|
||||||
|
}
|
||||||
else if ((fromType, toType) == (TypeSymbol.String, TypeSymbol.MObject))
|
else if ((fromType, toType) == (TypeSymbol.String, TypeSymbol.MObject))
|
||||||
{
|
{
|
||||||
ilProcessor.Emit(OpCodes.Call, _stringToMObject);
|
ilProcessor.Emit(OpCodes.Call, _stringToMObject);
|
||||||
@ -633,11 +640,16 @@ namespace Parser.Emitting
|
|||||||
ilProcessor.Emit(OpCodes.Callvirt, _getItemFromDictionary);
|
ilProcessor.Emit(OpCodes.Callvirt, _getItemFromDictionary);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void EmitNumberLiteralExpression(BoundNumberDoubleLiteralExpression node, ILProcessor ilProcessor)
|
private void EmitNumberDoubleLiteralExpression(BoundNumberDoubleLiteralExpression node, ILProcessor ilProcessor)
|
||||||
{
|
{
|
||||||
ilProcessor.Emit(OpCodes.Ldc_R8, node.Value);
|
ilProcessor.Emit(OpCodes.Ldc_R8, node.Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void EmitNumberIntLiteralExpression(BoundNumberIntLiteralExpression node, ILProcessor ilProcessor)
|
||||||
|
{
|
||||||
|
ilProcessor.Emit(OpCodes.Ldc_I4, node.Value);
|
||||||
|
}
|
||||||
|
|
||||||
private void EmitStringLiteralExpression(BoundStringLiteralExpression node, ILProcessor ilProcessor)
|
private void EmitStringLiteralExpression(BoundStringLiteralExpression node, ILProcessor ilProcessor)
|
||||||
{
|
{
|
||||||
ilProcessor.Emit(OpCodes.Ldstr, node.Value);
|
ilProcessor.Emit(OpCodes.Ldstr, node.Value);
|
||||||
|
@ -250,8 +250,10 @@ namespace Parser
|
|||||||
EvaluateMemberAccess((BoundMemberAccessExpression)node),
|
EvaluateMemberAccess((BoundMemberAccessExpression)node),
|
||||||
BoundNodeKind.NamedFunctionHandleExpression =>
|
BoundNodeKind.NamedFunctionHandleExpression =>
|
||||||
EvaluateNamedFunctionHandleExpression((BoundNamedFunctionHandleExpression)node),
|
EvaluateNamedFunctionHandleExpression((BoundNamedFunctionHandleExpression)node),
|
||||||
BoundNodeKind.NumberLiteralExpression =>
|
BoundNodeKind.NumberDoubleLiteralExpression =>
|
||||||
EvaluateNumberLiteralExpression((BoundNumberDoubleLiteralExpression)node),
|
EvaluateNumberDoubleLiteralExpression((BoundNumberDoubleLiteralExpression)node),
|
||||||
|
BoundNodeKind.NumberIntLiteralExpression =>
|
||||||
|
EvaluateNumberIntLiteralExpression((BoundNumberIntLiteralExpression)node),
|
||||||
BoundNodeKind.StringLiteralExpression =>
|
BoundNodeKind.StringLiteralExpression =>
|
||||||
EvaluateStringLiteralExpression((BoundStringLiteralExpression)node),
|
EvaluateStringLiteralExpression((BoundStringLiteralExpression)node),
|
||||||
BoundNodeKind.UnaryOperationExpression =>
|
BoundNodeKind.UnaryOperationExpression =>
|
||||||
@ -420,7 +422,12 @@ namespace Parser
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private MObject? EvaluateNumberLiteralExpression(BoundNumberDoubleLiteralExpression node)
|
private MObject? EvaluateNumberDoubleLiteralExpression(BoundNumberDoubleLiteralExpression node)
|
||||||
|
{
|
||||||
|
return MObject.CreateDoubleNumber(node.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
private MObject? EvaluateNumberIntLiteralExpression(BoundNumberIntLiteralExpression node)
|
||||||
{
|
{
|
||||||
return MObject.CreateDoubleNumber(node.Value);
|
return MObject.CreateDoubleNumber(node.Value);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user