-
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(compiler): add support for integer expressions
- Added support for parsing and generating integer constants. - At the moment there isn't any error handling, so it will generate invalid expressions (for example an i32 expression being assigned to an i8). - At the moment there's a slight blurring between a _type definition_ vs a _type reference_. In order to fully support constants and checking that the expression is compatible with the type of the constant we'll need to sort that, but for now I've just fudged it enough to work by adding static members for each base type in `BaseType`. - Added some sample Thrift files for constants. Issues: #8
- Loading branch information
1 parent
8dcec32
commit 01479b7
Showing
39 changed files
with
849 additions
and
85 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
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
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,35 @@ | ||
namespace Thrift.Net.Compilation.Binding | ||
{ | ||
using Thrift.Net.Compilation.Symbols; | ||
using Thrift.Net.Compilation.Symbols.Builders; | ||
using static Thrift.Net.Antlr.ThriftParser; | ||
|
||
/// <summary> | ||
/// Binds <see cref="IConstant" /> objects from <see cref="ConstDefinitionContext" /> nodes. | ||
/// </summary> | ||
public class ConstantBinder : Binder<ConstDefinitionContext, IConstant, IDocument> | ||
{ | ||
private readonly IBinderProvider binderProvider; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ConstantBinder" /> class. | ||
/// </summary> | ||
/// <param name="binderProvider">Used to get binders.</param> | ||
public ConstantBinder(IBinderProvider binderProvider) | ||
{ | ||
this.binderProvider = binderProvider; | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected override IConstant Bind(ConstDefinitionContext node, IDocument parent) | ||
{ | ||
var builder = new ConstantBuilder() | ||
.SetBinderProvider(this.binderProvider) | ||
.SetNode(node) | ||
.SetParent(parent) | ||
.SetName(node.name?.Text); | ||
|
||
return builder.Build(); | ||
} | ||
} | ||
} |
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,68 @@ | ||
namespace Thrift.Net.Compilation.Binding | ||
{ | ||
using Thrift.Net.Compilation.Symbols; | ||
using Thrift.Net.Compilation.Symbols.Builders; | ||
using static Thrift.Net.Antlr.ThriftParser; | ||
|
||
/// <summary> | ||
/// Used to bind <see cref="IConstantValue" /> symbols from <see cref="ConstExpressionContext" /> | ||
/// nodes. | ||
/// </summary> | ||
public class ConstantValueBinder : Binder<ConstExpressionContext, IConstantValue, IConstant> | ||
{ | ||
/// <inheritdoc/> | ||
protected override IConstantValue Bind(ConstExpressionContext node, IConstant parent) | ||
{ | ||
var type = GetExpressionType(node); | ||
|
||
var builder = new ConstantValueBuilder() | ||
.SetNode(node) | ||
.SetParent(parent) | ||
.SetRawValue(node.value?.Text) | ||
.SetType(type); | ||
|
||
return builder.Build(); | ||
} | ||
|
||
/// <summary> | ||
/// Gets the type of the constant expression to allow us to check that | ||
/// the expression is assignable to the constant. | ||
/// </summary> | ||
/// <remarks> | ||
/// When calculating the type for numeric types, we attempt to use the | ||
/// smallest possible type that the expression could be represented using. | ||
/// So for integers we'll use the following order: i8, i16, 132, 164. This | ||
/// means that we can automatically figure out whether or not an expression | ||
/// is assignable to the constant type definition using the principle that | ||
/// a smaller type can be assigned to a larger type, but not the other way | ||
/// round. | ||
/// </remarks> | ||
private static BaseType GetExpressionType(ConstExpressionContext node) | ||
{ | ||
if (node.INT_CONSTANT() != null && long.TryParse(node.value?.Text, out var value)) | ||
{ | ||
if (value < int.MinValue || value > int.MaxValue) | ||
{ | ||
return BaseType.I64; | ||
} | ||
else if (value < short.MinValue || value > short.MaxValue) | ||
{ | ||
return BaseType.I32; | ||
} | ||
else if (value < sbyte.MinValue || value > sbyte.MaxValue) | ||
{ | ||
return BaseType.I16; | ||
} | ||
|
||
return BaseType.I8; | ||
} | ||
|
||
if (node.DOUBLE_CONSTANT() != null) | ||
{ | ||
return BaseType.Double; | ||
} | ||
|
||
return BaseType.String; | ||
} | ||
} | ||
} |
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
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
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
Oops, something went wrong.