preliminary support for constants

This commit is contained in:
David Renshaw 2014-09-02 17:38:06 -04:00
parent 58193832ca
commit 957b513c99
3 changed files with 32 additions and 10 deletions

View file

@ -318,11 +318,11 @@ private:
case schema::Value::INT8: return kj::strTree(value.getInt8()); case schema::Value::INT8: return kj::strTree(value.getInt8());
case schema::Value::INT16: return kj::strTree(value.getInt16()); case schema::Value::INT16: return kj::strTree(value.getInt16());
case schema::Value::INT32: return kj::strTree(value.getInt32()); case schema::Value::INT32: return kj::strTree(value.getInt32());
case schema::Value::INT64: return kj::strTree(value.getInt64(), "ll"); case schema::Value::INT64: return kj::strTree(value.getInt64(), "L");
case schema::Value::UINT8: return kj::strTree(value.getUint8(), "u"); case schema::Value::UINT8: return kj::strTree(value.getUint8());
case schema::Value::UINT16: return kj::strTree(value.getUint16(), "u"); case schema::Value::UINT16: return kj::strTree(value.getUint16());
case schema::Value::UINT32: return kj::strTree(value.getUint32(), "u"); case schema::Value::UINT32: return kj::strTree(value.getUint32());
case schema::Value::UINT64: return kj::strTree(value.getUint64(), "llu"); case schema::Value::UINT64: return kj::strTree(value.getUint64(), "L");
case schema::Value::FLOAT32: return kj::strTree(value.getFloat32(), "f"); case schema::Value::FLOAT32: return kj::strTree(value.getFloat32(), "f");
case schema::Value::FLOAT64: return kj::strTree(value.getFloat64()); case schema::Value::FLOAT64: return kj::strTree(value.getFloat64());
case schema::Value::ENUM: { case schema::Value::ENUM: {
@ -1349,10 +1349,10 @@ private:
case schema::Value::ENUM: case schema::Value::ENUM:
return ConstText { return ConstText {
false, false,
kj::strTree("static constexpr ", typeName_, ' ', upperCase, " = ", kj::strTree("public static final ", typeName_, ' ', upperCase, " = ",
literalValue(constProto.getType(), constProto.getValue()), ";\n"), literalValue(constProto.getType(), constProto.getValue()), ";\n"),
scope.size() == 0 ? kj::strTree() : kj::strTree( scope.size() == 0 ? kj::strTree() : kj::strTree(
"constexpr ", typeName_, ' ', scope, upperCase, ";\n") "final ", typeName_, ' ', scope, upperCase, ";\n")
}; };
case schema::Value::TEXT: { case schema::Value::TEXT: {
@ -1531,6 +1531,8 @@ private:
scope, name, schema, scope, name, schema,
KJ_MAP(n, nestedTexts) { return kj::mv(n.outerTypeDef); }, indent); KJ_MAP(n, nestedTexts) { return kj::mv(n.outerTypeDef); }, indent);
KJ_LOG(ERROR, top.outerTypeDecl);
return NodeText { return NodeText {
kj::mv(top.outerTypeDecl), kj::mv(top.outerTypeDecl),
@ -1631,8 +1633,8 @@ private:
auto constText = makeConstText(scope, name, schema.asConst()); auto constText = makeConstText(scope, name, schema.asConst());
return NodeTextNoSchema { return NodeTextNoSchema {
scope.size() == 0 ? kj::strTree() : kj::strTree(" ", kj::mv(constText.decl)), kj::strTree(),
scope.size() > 0 ? kj::strTree() : kj::mv(constText.decl), kj::mv(constText.decl),
kj::strTree(), kj::strTree(),
kj::strTree(), kj::strTree(),

View file

@ -32,6 +32,10 @@ class EncodingSuite extends FunSuite {
} }
test("Constants") {
assert(true == TestConstants.BOOL_CONST);
}
// to debug, do this: // to debug, do this:
//Serialize.writeMessage((new java.io.FileOutputStream("/Users/dwrensha/Desktop/test.dat")).getChannel(), //Serialize.writeMessage((new java.io.FileOutputStream("/Users/dwrensha/Desktop/test.dat")).getChannel(),

View file

@ -162,3 +162,19 @@ struct TestGroups {
} }
} }
} }
struct TestConstants {
# const voidConst :Void = void;
const boolConst :Bool = true;
const int8Const :Int8 = -123;
const int16Const :Int16 = -12345;
const int32Const :Int32 = -12345678;
const int64Const :Int64 = -123456789012345;
# const uint8Const :UInt8 = 234;
# const uint16Const :UInt16 = 45678;
# const uint32Const :UInt32 = 3456789012;
# const uint64Const :UInt64 = 12345678901234567890;
const float32Const :Float32 = 1234.5;
const float64Const :Float64 = -123e45;
# ...
}