Skip to content

Commit

Permalink
Implement initial support for 64-bit ints, closes #129
Browse files Browse the repository at this point in the history
Took 57 minutes
  • Loading branch information
Jan Polák committed Apr 5, 2022
1 parent 63e7147 commit 9215f93
Show file tree
Hide file tree
Showing 7 changed files with 1,307 additions and 1,163 deletions.
16 changes: 14 additions & 2 deletions src/main/java/glslplugin/lang/elements/GLSLTokenTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class GLSLTokenTypes {

public static final IElementType INTEGER_CONSTANT = new GLSLElementType("INTEGER_CONSTANT");
public static final IElementType UINT_CONSTANT = new GLSLElementType("UINT_CONSTANT");
public static final IElementType INT64_CONSTANT = new GLSLElementType("INT64_CONSTANT");
public static final IElementType UINT64_CONSTANT = new GLSLElementType("UINT64_CONSTANT");
public static final IElementType FLOAT_CONSTANT = new GLSLElementType("FLOAT_CONSTANT");
public static final IElementType DOUBLE_CONSTANT = new GLSLElementType("DOUBLE_CONSTANT");
public static final IElementType BOOL_CONSTANT = new GLSLElementType("BOOL_CONSTANT");
Expand All @@ -39,6 +41,8 @@ public class GLSLTokenTypes {
public static final IElementType DOUBLE_TYPE = new GLSLElementType("DOUBLE_TYPE");
public static final IElementType INT_TYPE = new GLSLElementType("INT_TYPE");
public static final IElementType UINT_TYPE = new GLSLElementType("UINT_TYPE");
public static final IElementType INT64_TYPE = new GLSLElementType("INT64_TYPE");
public static final IElementType UINT64_TYPE = new GLSLElementType("UINT64_TYPE");
public static final IElementType BOOL_TYPE = new GLSLElementType("BOOL_TYPE");
public static final IElementType VEC2_TYPE = new GLSLElementType("VEC2_TYPE");
public static final IElementType VEC3_TYPE = new GLSLElementType("VEC3_TYPE");
Expand All @@ -52,6 +56,12 @@ public class GLSLTokenTypes {
public static final IElementType UVEC2_TYPE = new GLSLElementType("UVEC2_TYPE");
public static final IElementType UVEC3_TYPE = new GLSLElementType("UVEC3_TYPE");
public static final IElementType UVEC4_TYPE = new GLSLElementType("UVEC4_TYPE");
public static final IElementType I64VEC2_TYPE = new GLSLElementType("I64VEC2_TYPE");
public static final IElementType I64VEC3_TYPE = new GLSLElementType("I64VEC3_TYPE");
public static final IElementType I64VEC4_TYPE = new GLSLElementType("I64VEC4_TYPE");
public static final IElementType U64VEC2_TYPE = new GLSLElementType("U64VEC2_TYPE");
public static final IElementType U64VEC3_TYPE = new GLSLElementType("U64VEC3_TYPE");
public static final IElementType U64VEC4_TYPE = new GLSLElementType("U64VEC4_TYPE");
public static final IElementType BVEC2_TYPE = new GLSLElementType("BVEC2_TYPE");
public static final IElementType BVEC3_TYPE = new GLSLElementType("BVEC3_TYPE");
public static final IElementType BVEC4_TYPE = new GLSLElementType("BVEC4_TYPE");
Expand Down Expand Up @@ -242,7 +252,9 @@ public class GLSLTokenTypes {

public static final TokenSet INTEGER_TYPE_SPECIFIER_NONARRAY = TokenSet.create(
INT_TYPE, IVEC2_TYPE, IVEC3_TYPE, IVEC4_TYPE,
UINT_TYPE, UVEC2_TYPE, UVEC3_TYPE, UVEC4_TYPE);
UINT_TYPE, UVEC2_TYPE, UVEC3_TYPE, UVEC4_TYPE,
INT64_TYPE, I64VEC2_TYPE, I64VEC3_TYPE, I64VEC4_TYPE,
UINT64_TYPE, U64VEC2_TYPE, U64VEC3_TYPE, U64VEC4_TYPE);

public static final TokenSet BOOL_TYPE_SPECIFIER_NONARRAY = TokenSet.create(BOOL_TYPE, BVEC2_TYPE, BVEC3_TYPE, BVEC4_TYPE);

Expand Down Expand Up @@ -341,7 +353,7 @@ public class GLSLTokenTypes {
BIT_WISE_OPERATORS, LOGICAL_OPERATORS, ASSIGNMENT_OPERATORS);

public static final TokenSet CONSTANT_TOKENS = TokenSet.create(
BOOL_CONSTANT, INTEGER_CONSTANT, UINT_CONSTANT, FLOAT_CONSTANT, DOUBLE_CONSTANT, STRING_CONSTANT);
BOOL_CONSTANT, INTEGER_CONSTANT, UINT_CONSTANT, INT64_CONSTANT, UINT64_CONSTANT, FLOAT_CONSTANT, DOUBLE_CONSTANT, STRING_CONSTANT);

public static final TokenSet EXPRESSION_FIRST_SET = merge(TokenSet.create(
INTEGER_CONSTANT, FLOAT_CONSTANT, BOOL_CONSTANT, STRING_CONSTANT, // constants
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ public enum Type {
BOOL("Bool", GLSLTypes.BOOL),
FLOAT("Float", GLSLTypes.FLOAT),
DOUBLE("Double", GLSLTypes.DOUBLE),
INTEGER("Integer", GLSLTypes.INT),
INT("Integer", GLSLTypes.INT),
UINT("Unsigned integer", GLSLTypes.UINT),
INT64("64-bit Integer", GLSLTypes.INT),
UINT64("Unsigned 64-bit integer", GLSLTypes.UINT),
// https://github.com/KhronosGroup/GLSL/blob/master/extensions/ext/GLSL_EXT_debug_printf.txt
// does not define any GLSL type, just the literal.
STRING("String", null),
Expand Down Expand Up @@ -77,8 +79,10 @@ public Type getLiteralType() {
@Nullable
public static Type getLiteralType(IElementType type){
if (type == GLSLTokenTypes.BOOL_CONSTANT) return Type.BOOL;
if (type == GLSLTokenTypes.INTEGER_CONSTANT) return Type.INTEGER;
if (type == GLSLTokenTypes.INTEGER_CONSTANT) return Type.INT;
if (type == GLSLTokenTypes.UINT_CONSTANT) return Type.UINT;
if (type == GLSLTokenTypes.INT64_CONSTANT) return Type.INT64;
if (type == GLSLTokenTypes.UINT64_CONSTANT) return Type.UINT64;
if (type == GLSLTokenTypes.FLOAT_CONSTANT) return Type.FLOAT;
if (type == GLSLTokenTypes.DOUBLE_CONSTANT) return Type.DOUBLE;
if (type == GLSLTokenTypes.STRING_CONSTANT) return Type.STRING;
Expand Down Expand Up @@ -117,8 +121,10 @@ public Object getConstantValue() {
if("true".equals(text))return true;
else if("false".equals(text))return false;
else return null;
case INTEGER:
case INT:
case UINT:
case INT64:
case UINT64:
try{
return Long.parseLong(text);
}catch (NumberFormatException nfe){
Expand Down
23 changes: 17 additions & 6 deletions src/main/java/glslplugin/lang/elements/types/GLSLScalarType.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
* Scalar type is a type that has only magnitude, no members or elements.
Expand All @@ -37,9 +38,20 @@ public class GLSLScalarType extends GLSLType {
//region Static
public static final GLSLScalarType BOOL = new GLSLScalarType("bool", Boolean.class);
public static final GLSLScalarType DOUBLE = new GLSLScalarType("double", Double.class);
public static final GLSLScalarType FLOAT = new GLSLScalarType("float", Double.class, DOUBLE);
public static final GLSLScalarType UINT = new GLSLScalarType("uint", Long.class, FLOAT, DOUBLE);
public static final GLSLScalarType INT = new GLSLScalarType("int", Long.class, UINT, FLOAT, DOUBLE);
public static final GLSLScalarType FLOAT = new GLSLScalarType("float", Double.class);
public static final GLSLScalarType UINT = new GLSLScalarType("uint", Long.class);
public static final GLSLScalarType INT = new GLSLScalarType("int", Long.class);
// https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_gpu_shader_int64.txt
public static final GLSLScalarType UINT64 = new GLSLScalarType("uint64_t", Long.class);
public static final GLSLScalarType INT64 = new GLSLScalarType("int64_t", Long.class);

static {
INT.implicitConversions = Arrays.asList(UINT, INT64, UINT64, FLOAT, DOUBLE);
UINT.implicitConversions = Arrays.asList(UINT64, FLOAT, DOUBLE);
INT64.implicitConversions = Arrays.asList(UINT64, DOUBLE);
UINT64.implicitConversions = List.of(DOUBLE);
FLOAT.implicitConversions = List.of(DOUBLE);
}

private static final GLSLScalarType[] SCALARS = {BOOL, DOUBLE, FLOAT, UINT, INT};

Expand All @@ -49,12 +61,11 @@ public static boolean isIntegerScalar(GLSLType type){
//endregion

private final String typename;
private final Collection<GLSLType> implicitConversions;
private List<GLSLType> implicitConversions;

private GLSLScalarType(String typename, Class<?> javaType, GLSLType... implicitlyConvertibleTo) {
private GLSLScalarType(String typename, Class<?> javaType) {
super(javaType);
this.typename = typename;
this.implicitConversions = Arrays.asList(implicitlyConvertibleTo);
}

@NotNull
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/glslplugin/lang/elements/types/GLSLTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ public class GLSLTypes {
public static final GLSLMatrixType DMAT3 = register("dmat3", DMAT3x3);
public static final GLSLMatrixType DMAT4 = register("dmat4", DMAT4x4);

// https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_gpu_shader_int64.txt
public static final GLSLScalarType INT64 = register(GLSLScalarType.INT64);
public static final GLSLVectorType I64VEC2 = register(GLSLVectorType.getType(INT64, 2));
public static final GLSLVectorType I64VEC3 = register(GLSLVectorType.getType(INT64, 3));
public static final GLSLVectorType I64VEC4 = register(GLSLVectorType.getType(INT64, 4));
public static final GLSLScalarType UINT64 = register(GLSLScalarType.UINT64);
public static final GLSLVectorType U64VEC2 = register(GLSLVectorType.getType(UINT64, 2));
public static final GLSLVectorType U64VEC3 = register(GLSLVectorType.getType(UINT64, 3));
public static final GLSLVectorType U64VEC4 = register(GLSLVectorType.getType(UINT64, 4));


// Specials
public static final GLSLOpaqueType VOID = GLSLOpaqueType.VOID;

Expand Down Expand Up @@ -153,7 +164,7 @@ public static GLSLType unifyTypes(GLSLType t1, GLSLType t2) {
}

public static boolean isScalar(GLSLType type) {
return type == INT || type == FLOAT || type == BOOL || type == DOUBLE || type == UINT;
return type == INT || type == FLOAT || type == BOOL || type == DOUBLE || type == UINT || type == UINT64 || type == INT64;
}

private static Map<String, GLSLType> types;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ private enum BaseType {
UINT(GLSLScalarType.UINT, "uvec"),
BOOL(GLSLScalarType.BOOL, "bvec"),
FLOAT(GLSLScalarType.FLOAT, "vec"),
DOUBLE(GLSLScalarType.DOUBLE, "dvec");
DOUBLE(GLSLScalarType.DOUBLE, "dvec"),
INT64(GLSLScalarType.INT64, "i64vec"),
UINT64(GLSLScalarType.UINT64, "u64vec"),
;

final GLSLType type;
final String name;
Expand Down
17 changes: 14 additions & 3 deletions src/main/java/glslplugin/lang/scanner/GLSL.flex
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ WHITE_SPACE = [ \t\f]

IDENTIFIER = {NON_DIGIT}({NON_DIGIT} | {DIGIT})*

UINT_SUFFIX = [Uu]
UINT_SUFFIX = [Uu]
INT64_SUFFIX = [Ll]
INTEGER_CONSTANT = ({DECIMAL_CONSTANT} | {HEX_CONSTANT} | {OCTAL_CONSTANT})
DECIMAL_CONSTANT = (0|([1-9]({DIGIT})*))
HEX_CONSTANT = 0[Xx]({HEX_DIGIT})*
Expand Down Expand Up @@ -118,6 +119,8 @@ float {return FLOAT_TYPE; }
double {return DOUBLE_TYPE; }
int {return INT_TYPE; }
uint {return UINT_TYPE; }
int64_t {return INT64_TYPE; }
uint64_t {return UINT64_TYPE; }
bool {return BOOL_TYPE; }
vec2 {return VEC2_TYPE; }
vec3 {return VEC3_TYPE; }
Expand All @@ -131,6 +134,12 @@ ivec4 {return IVEC4_TYPE; }
uvec2 {return UVEC2_TYPE; }
uvec3 {return UVEC3_TYPE; }
uvec4 {return UVEC4_TYPE; }
i64vec2 {return I64VEC2_TYPE; }
i64vec3 {return I64VEC3_TYPE; }
i64vec4 {return I64VEC4_TYPE; }
u64vec2 {return U64VEC2_TYPE; }
u64vec3 {return U64VEC3_TYPE; }
u64vec4 {return U64VEC4_TYPE; }
bvec2 {return BVEC2_TYPE; }
bvec3 {return BVEC3_TYPE; }
bvec4 {return BVEC4_TYPE; }
Expand Down Expand Up @@ -318,8 +327,10 @@ using { return RESERVED_KEYWORD; }

{IDENTIFIER} {return IDENTIFIER;}

{INTEGER_CONSTANT}{UINT_SUFFIX} {return UINT_CONSTANT; }
{INTEGER_CONSTANT} {return INTEGER_CONSTANT; }
{INTEGER_CONSTANT}{UINT_SUFFIX}{INT64_SUFFIX} {return UINT64_CONSTANT; }
{INTEGER_CONSTANT}{UINT_SUFFIX} {return UINT_CONSTANT; }
{INTEGER_CONSTANT}{INT64_SUFFIX} {return INT64_CONSTANT; }
{INTEGER_CONSTANT} {return INTEGER_CONSTANT; }
{FLOATING_CONSTANT}{DOUBLE_SUFFIX} {return DOUBLE_CONSTANT; }
{FLOATING_CONSTANT}{FLOAT_SUFFIX}? {return FLOAT_CONSTANT; }
{STRING_CONSTANT} {return STRING_CONSTANT; }
Expand Down
Loading

0 comments on commit 9215f93

Please sign in to comment.