Skip to content

Commit 2cd745c

Browse files
committed
oslc didn't understand hex integer constants (like 0x01fc). #653
1 parent ae33065 commit 2cd745c

File tree

7 files changed

+42
-3
lines changed

7 files changed

+42
-3
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Language, standard libary, and compiler changes (for shader writers):
1313
nested `{ {...}, {...} }`. #640 (1.8.0)
1414
* oslc (and liboslcomp) now supports UTF-8 filenames, including on
1515
Windows. #643 (1.8.0)
16+
* osl now accepts hexidecimal integer constants (such as `0x01ff`).
17+
#653 (1.8.1)
1618
* Standard library additions/changes:
1719

1820
API changes, new options, new ShadingSystem features (for renderer writers):

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,8 @@ TESTSUITE ( and-or-not-synonyms aastep arithmetic array array-derivs array-range
538538
function-earlyreturn function-simple function-outputelem
539539
geomath getattribute-camera getsymbol-nonheap gettextureinfo
540540
group-outputs groupstring
541-
hyperb ieee_fp if incdec initops intbits isconnected isconstant
541+
hex hyperb
542+
ieee_fp if incdec initops intbits isconnected isconstant
542543
layers layers-Ciassign layers-entry layers-lazy
543544
layers-nonlazycopy layers-repeatedoutputs
544545
linearstep

src/doc/languagespec.tex

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
6363
}
6464
\date{{\large Date: 14 Jan, 2016 \\
65-
(with corrections, 14 Jun 2016)
65+
(with corrections, 5 Jul 2016)
6666
}
6767
\bigskip
6868
\bigskip
@@ -1464,7 +1464,8 @@ \section{{\cf int}}
14641464
least 32 bits.
14651465

14661466
Integer constants are constructed the same way as in C. The following
1467-
are examples of {\cf int} constants: {\cf 1}, {\cf -32}, etc.
1467+
are examples of {\cf int} constants: {\cf 1}, {\cf -32}, etc. Integer
1468+
constants may be specified as hexidecimal, for example: {\cf 0x01cf}.
14681469

14691470
Unlike C, no unsigned, bool, char, short, or long types are supplied.
14701471
This is to simplify the process of writing shaders (as well as
@@ -5089,7 +5090,12 @@ \section*{Lexical elements}
50895090

50905091
<digit-sequence> ::= <digit> \{ <digit> \}
50915092

5093+
<hexdigit> ::= <digit> | "a" | "A" | "b" | "B" | "c" | "C" | "d" | "D" | "e" | "E" | "f" | "F"
5094+
5095+
<hexdigit-sequence> ::= <hexdigit> \{ <hexdigit> \}
5096+
50925097
<integer> ::= <sign> <digit-sequence>
5098+
\alt <sign> "0x" <hexdigit-sequence>
50935099

50945100
<floating-point> ::= <digit-sequence> <decimal-part-opt> <exponent-opt>
50955101
\alt <decimal-part> <exponent-opt>

src/liboslcomp/osllex.l

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ ALPHA [A-Za-z]
6464
DIGIT [0-9]
6565
/* Integer literal */
6666
INTEGER {DIGIT}+
67+
HEXINTEGER 0[xX][0-9a-fA-F]+
6768
/* floating point literal (E, FLT1, FLT2, FLT3 are just helpers)
6869
* NB: we don't allow leading +/- due to ambiguity between
6970
* whether "a-0.5" is really "a -0.5" or "a - 0.5". Resolve this
@@ -204,6 +205,22 @@ void preprocess (const char *yytext);
204205
SETLINE;
205206
return INT_LITERAL;
206207
}
208+
{HEXINTEGER} {
209+
long long llval = strtoll (yytext, (char**)NULL, 16);
210+
// we do not detect overflow when the value is INT_MAX+1,
211+
// because negation happens later and -(INT_MAX+1) == INT_MIN
212+
if (llval > ((long long)UINT_MAX)+1) {
213+
oslcompiler->error (oslcompiler->filename(),
214+
oslcompiler->lineno(),
215+
"integer overflow, value must be between %d and %d.",
216+
INT_MIN, INT_MAX);
217+
}
218+
yylval.i = (int)llval;
219+
SETLINE;
220+
return INT_LITERAL;
221+
}
222+
223+
207224

208225
{FLT} {
209226
yylval.f = atof (yytext);

testsuite/hex/ref/out.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Compiled test.osl -> test.oso
2+
a = 26 (hex 1a), b = -1 (hex ffffffff)
3+

testsuite/hex/run.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env python
2+
3+
command = testshade("test")

testsuite/hex/test.osl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
shader
2+
test ()
3+
{
4+
int a = 0x001a;
5+
int b = 0xffffffff;
6+
printf ("a = %d (hex %x), b = %d (hex %x)\n", a, a, b, b);
7+
}

0 commit comments

Comments
 (0)