summaryrefslogtreecommitdiffstats
path: root/source/slang/emit.cpp
diff options
context:
space:
mode:
authorTim Foley <tfoley@nvidia.com>2017-06-28 10:20:16 -0700
committerTim Foley <tfoley@nvidia.com>2017-06-28 11:08:03 -0700
commitd601921b71ed44835e8d4fa6f13ff7aefcf7649d (patch)
tree79b9227ef038d173d780a440035e616dc31104bb /source/slang/emit.cpp
parent4b3936e2983dcecd36a3437bd6c7eef8d5fbbffa (diff)
Actually respect suffixes on numeric literals.
- Add logic to extract the value and suffix from a numeric literal - This duplicates some of the lexing logic, but this is hard to avoid without redundant runtime work - Note that I'm not using and stdlib string-to-number code. This should be more robust once it is working, but it is obviously error prone in the near term. The main up-sides to this are: - We can handle binary integer literals - We can handle hexadecimal floating-point literals without stdlib support - We can hypothetically support digit separators, if we ever wanted - The parser looks at the suffix characters sliced off by the lexer, and tries to pick a type to use for a literal - It uses `NULL` if there is no suffix, to avoid some nasty order dependencies where the stdlib might need to parse a number before it has seen the definition of `int` - Right now I only handle a few cases, so there may be bugs lurking here - The emit logic needs to handle the fact that a literal node in the AST might have a non-default type attached. - Right now I just quickly check for the most likely types, and emit the literal with a matching suffix. This doesn't seem robust if any source language supports a suffix for a type where a target has no corresponding suffix. In the long term some amount of casting is probably required.
Diffstat (limited to 'source/slang/emit.cpp')
-rw-r--r--source/slang/emit.cpp41
1 files changed, 41 insertions, 0 deletions
diff --git a/source/slang/emit.cpp b/source/slang/emit.cpp
index 5272204f6..9a1d44641 100644
--- a/source/slang/emit.cpp
+++ b/source/slang/emit.cpp
@@ -219,6 +219,8 @@ static void Emit(EmitContext* context, double value)
Emit(context, buffer);
}
+static void emitTokenWithLocation(EmitContext* context, Token const& token);
+
// Expressions
// Determine if an expression should not be emitted when it is the base of
@@ -900,14 +902,53 @@ static void EmitExprWithPrecedence(EmitContext* context, RefPtr<ExpressionSyntax
{
needClose = MaybeEmitParens(context, outerPrec, kPrecedence_Atomic);
+ char const* suffix = "";
+ auto type = litExpr->Type.type;
switch (litExpr->ConstType)
{
case ConstantExpressionSyntaxNode::ConstantType::Int:
+ if(!type)
+ {
+ // Special case for "rewrite" mode
+ emitTokenWithLocation(context, litExpr->token);
+ break;
+ }
+ if(type->Equals(ExpressionType::GetInt()))
+ {}
+ else if(type->Equals(ExpressionType::GetUInt()))
+ {
+ suffix = "u";
+ }
+ else
+ {
+ assert(!"unimplemented");
+ }
Emit(context, litExpr->IntValue);
+ Emit(context, suffix);
break;
+
+
case ConstantExpressionSyntaxNode::ConstantType::Float:
+ if(!type)
+ {
+ // Special case for "rewrite" mode
+ emitTokenWithLocation(context, litExpr->token);
+ break;
+ }
+ if(type->Equals(ExpressionType::GetFloat()))
+ {}
+ else if(type->Equals(ExpressionType::getDoubleType()))
+ {
+ suffix = "l";
+ }
+ else
+ {
+ assert(!"unimplemented");
+ }
Emit(context, litExpr->FloatValue);
+ Emit(context, suffix);
break;
+
case ConstantExpressionSyntaxNode::ConstantType::Bool:
Emit(context, litExpr->IntValue ? "true" : "false");
break;