Skip to content
This repository has been archived by the owner on Jul 13, 2019. It is now read-only.

Commit

Permalink
Implement strings
Browse files Browse the repository at this point in the history
Implement code generation for string types. Fix record type
offsets. Fix string sizes.

Fix #2
Fix #7
  • Loading branch information
io7m committed Nov 21, 2015
1 parent 1e0a392 commit bd41e72
Show file tree
Hide file tree
Showing 15 changed files with 847 additions and 38 deletions.
Expand Up @@ -227,7 +227,12 @@ protected abstract SExpressionType newStringSExpr(
final TypeExprString<IdentifierType, TType> e =
TypeExprString.class.cast(ch.checkTypeExpression(te));
final TString ti = TString.class.cast(e.getType());
Assert.assertEquals(BigInteger.valueOf(256L), ti.getSizeInBits().getValue());
Assert.assertEquals(
BigInteger.valueOf(288L),
ti.getSizeInBits().getValue());
Assert.assertEquals(
BigInteger.valueOf(32L),
ti.getMaximumStringLength().getValue());
Assert.assertEquals("UTF-8", ti.getEncoding());
}

Expand Down Expand Up @@ -268,7 +273,9 @@ protected abstract SExpressionType newStringSExpr(
final TypeExprArray<IdentifierType, TType> e =
TypeExprArray.class.cast(ch.checkTypeExpression(te));
final TArray ta = TArray.class.cast(e.getType());
Assert.assertEquals(BigInteger.valueOf(32L * 32L), ta.getSizeInBits().getValue());
Assert.assertEquals(
BigInteger.valueOf(32L * 32L),
ta.getSizeInBits().getValue());
Assert.assertEquals(
BigInteger.valueOf(32L), ta.getElementCount().getValue());
Assert.assertEquals(TIntegerSigned.class, ta.getElementType().getClass());
Expand Down Expand Up @@ -636,7 +643,9 @@ c, new CapsSupportingIntegerMatrices(

final TRecord.FieldPaddingOctets f = TRecord.FieldPaddingOctets.class.cast(
tt.getFieldsInDeclarationOrder().get(0));
Assert.assertEquals(BigInteger.valueOf(8L * 8L), f.getSizeInBits().getValue());
Assert.assertEquals(
BigInteger.valueOf(8L * 8L),
f.getSizeInBits().getValue());
}

@Test public final void testTypeDeclRecordPaddingOctets_Error0()
Expand Down
Expand Up @@ -78,6 +78,28 @@
(field m4d [matrix [float 64] 4 4])
])

(record Strings [
(field s [string 64 "UTF-8"])
(field x [integer signed 32])
])

(record NestedString2 [
(padding-octets 4)
(field s [string 4 "UTF-8"])
])

(record NestedString1 [
(padding-octets 4)
(field s [string 4 "UTF-8"])
(field n NestedString2)
])

(record NestedString0 [
(padding-octets 4)
(field s [string 4 "UTF-8"])
(field n NestedString1)
])

(packed OpenGL565 [
(field r [integer unsigned-normalized 5])
(field g [integer unsigned-normalized 6])
Expand Down
Expand Up @@ -218,7 +218,7 @@ private static void check4x4FZero(final MatrixReadable4x4FType m)
MatricesTest.check3x3DZero(v.getM3dReadable());
MatricesTest.check3x3FZero(v.getM3fReadable());
// MatricesTest.check4x4FZero(v.getM4fReadable());
check4x4DZero(v.getM4dReadable());
MatricesTest.check4x4DZero(v.getM4dReadable());

final MatrixReadable4x4FType k = v.getM4fReadable();

Expand Down Expand Up @@ -279,7 +279,7 @@ private static void check4x4FZero(final MatrixReadable4x4FType m)
// MatricesTest.check3x3DZero(v.getM3dReadable());
MatricesTest.check3x3FZero(v.getM3fReadable());
MatricesTest.check4x4FZero(v.getM4fReadable());
check4x4DZero(v.getM4dReadable());
MatricesTest.check4x4DZero(v.getM4dReadable());

final MatrixReadable3x3DType k = v.getM3dReadable();

Expand Down Expand Up @@ -332,7 +332,7 @@ private static void check4x4FZero(final MatrixReadable4x4FType m)
MatricesTest.check3x3DZero(v.getM3dReadable());
// MatricesTest.check3x3FZero(v.getM3fReadable());
MatricesTest.check4x4FZero(v.getM4fReadable());
check4x4DZero(v.getM4dReadable());
MatricesTest.check4x4DZero(v.getM4dReadable());

final MatrixReadable3x3FType k = v.getM3fReadable();

Expand Down Expand Up @@ -379,7 +379,7 @@ private static void check4x4FZero(final MatrixReadable4x4FType m)
MatricesTest.check3x3DZero(v.getM3dReadable());
MatricesTest.check2x2FZero(v.getM3fReadable());
MatricesTest.check4x4FZero(v.getM4fReadable());
check4x4DZero(v.getM4dReadable());
MatricesTest.check4x4DZero(v.getM4dReadable());

final MatrixReadable2x2DType k = v.getM2dReadable();

Expand Down Expand Up @@ -420,7 +420,7 @@ private static void check4x4FZero(final MatrixReadable4x4FType m)
MatricesTest.check3x3DZero(v.getM3dReadable());
MatricesTest.check3x3FZero(v.getM3fReadable());
MatricesTest.check4x4FZero(v.getM4fReadable());
check4x4DZero(v.getM4dReadable());
MatricesTest.check4x4DZero(v.getM4dReadable());

final MatrixReadable2x2FType k = v.getM2fReadable();

Expand Down
@@ -0,0 +1,73 @@
/*
* Copyright © 2015 <code@io7m.com> http://io7m.com
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

package com.io7m.jpra.compiler.tests.java.output;

import com.io7m.jpra.compiler.tests.java.generation.code
.NestedString0ByteBuffered;
import com.io7m.jpra.compiler.tests.java.generation.code.NestedString0Type;
import com.io7m.jpra.compiler.tests.java.generation.code
.NestedString1WritableType;
import com.io7m.jpra.compiler.tests.java.generation.code
.NestedString2WritableType;
import com.io7m.jpra.runtime.java.JPRACursor1DByteBufferedChecked;
import com.io7m.jpra.runtime.java.JPRACursor1DType;
import com.io7m.jpra.runtime.java.JPRAStringTruncation;
import com.io7m.jpra.runtime.java.JPRAStringType;
import org.junit.Assert;
import org.junit.Test;

import java.nio.ByteBuffer;

public final class NestedString0Test
{
@Test public void testNesting()
{
final ByteBuffer buf = ByteBuffer.allocate(2 * 36);
final JPRACursor1DType<NestedString0Type> c =
JPRACursor1DByteBufferedChecked.newCursor(
buf,
NestedString0ByteBuffered::newValueWithOffset);

for (int index = 0; index < 2; ++index) {
c.setElementIndex(index);

final NestedString0Type n0 = c.getElementView();
final JPRAStringType n0s = n0.getSWritable();
final NestedString1WritableType n1w = n0.getNWritable();
final JPRAStringType n1s = n1w.getSWritable();
final NestedString2WritableType n2w = n1w.getNWritable();
final JPRAStringType n2s = n2w.getSWritable();

Assert.assertEquals("", n0s.getNewValue());
Assert.assertEquals("", n1s.getNewValue());
Assert.assertEquals("", n2s.getNewValue());

n0s.setValue("ABCD", JPRAStringTruncation.TRUNCATE);
n1s.setValue("EFGH", JPRAStringTruncation.TRUNCATE);
n2s.setValue("IJKL", JPRAStringTruncation.TRUNCATE);

final int offset = index * 36;
BufferChecks.checkRangeInclusiveIsZero(buf, offset + 0, offset + 3);
BufferChecks.checkRangeInclusiveIsZero(buf, offset + 12, offset + 15);
BufferChecks.checkRangeInclusiveIsZero(buf, offset + 24, offset + 27);

Assert.assertEquals("ABCD", n0s.getNewValue());
Assert.assertEquals("EFGH", n1s.getNewValue());
Assert.assertEquals("IJKL", n2s.getNewValue());
}
}
}
Expand Up @@ -115,22 +115,12 @@ static String getSetterBooleanSetName(

static String getGetterRecordReadableName(final FieldName name)
{
final String text = JPRAGeneratedNames.getRecased(name.toString());
final StringBuilder sb = new StringBuilder(128);
sb.append("get");
sb.append(text);
sb.append("Readable");
return sb.toString();
return JPRAGeneratedNames.getObjectReadableName(name);
}

static String getGetterRecordWritableName(final FieldName name)
{
final String text = JPRAGeneratedNames.getRecased(name.toString());
final StringBuilder sb = new StringBuilder(128);
sb.append("get");
sb.append(text);
sb.append("Writable");
return sb.toString();
return JPRAGeneratedNames.getObjectWritableName(name);
}

static String getRecordImplementationByteBufferedName(
Expand Down Expand Up @@ -200,6 +190,11 @@ static String getNormalizedRawSetterName(final FieldName name)
}

public static String getGetterVectorReadableName(final FieldName name)
{
return JPRAGeneratedNames.getObjectReadableName(name);
}

private static String getObjectReadableName(final FieldName name)
{
final String text = JPRAGeneratedNames.getRecased(name.toString());
final StringBuilder sb = new StringBuilder(128);
Expand All @@ -211,25 +206,20 @@ public static String getGetterVectorReadableName(final FieldName name)

public static String getGetterVectorWritableName(final FieldName name)
{
final String text = JPRAGeneratedNames.getRecased(name.toString());
final StringBuilder sb = new StringBuilder(128);
sb.append("get");
sb.append(text);
sb.append("Writable");
return sb.toString();
return JPRAGeneratedNames.getObjectWritableName(name);
}

public static String getGetterMatrixReadableName(final FieldName name)
{
final String text = JPRAGeneratedNames.getRecased(name.toString());
final StringBuilder sb = new StringBuilder(128);
sb.append("get");
sb.append(text);
sb.append("Readable");
return sb.toString();
return JPRAGeneratedNames.getObjectReadableName(name);
}

public static String getGetterMatrixWritableName(final FieldName name)
{
return JPRAGeneratedNames.getObjectWritableName(name);
}

private static String getObjectWritableName(final FieldName name)
{
final String text = JPRAGeneratedNames.getRecased(name.toString());
final StringBuilder sb = new StringBuilder(128);
Expand All @@ -238,4 +228,14 @@ public static String getGetterMatrixWritableName(final FieldName name)
sb.append("Writable");
return sb.toString();
}

public static String getGetterStringReadableName(final FieldName name)
{
return JPRAGeneratedNames.getObjectReadableName(name);
}

public static String getGetterStringWritableName(final FieldName name)
{
return JPRAGeneratedNames.getObjectWritableName(name);
}
}
Expand Up @@ -32,12 +32,15 @@
import com.io7m.jpra.model.types.TString;
import com.io7m.jpra.model.types.TVector;
import com.io7m.jpra.model.types.TypeMatcherType;
import com.io7m.jpra.runtime.java.JPRAStringByteBuffered;
import com.io7m.jpra.runtime.java.JPRAStringType;
import com.io7m.junreachable.UnreachableCodeException;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.TypeSpec;

import javax.lang.model.element.Modifier;
import java.nio.charset.Charset;

/**
* A type matcher that produces constructor field assignment statements for a
Expand Down Expand Up @@ -68,6 +71,25 @@ final class RecordFieldImplementationConstructorProcessor

@Override public Unit matchString(final TString t)
{
final FieldName f_name = this.field.getName();
final String field_name = JPRAGeneratedNames.getFieldName(f_name);
final String offset_name = JPRAGeneratedNames.getOffsetConstantName(f_name);

this.constructor_builder.addStatement(
"this.$N = $T.newString($N, $N + $N, $N, $T.forName($S), $L)",
field_name,
JPRAStringByteBuffered.class,
"in_buffer",
offset_name,
"in_base_offset",
"in_pointer",
Charset.class,
t.getEncoding(),
t.getMaximumStringLength().getValue().intValue());

this.class_builder.addField(
JPRAStringType.class, field_name, Modifier.FINAL, Modifier.PRIVATE);

return Unit.unit();
}

Expand Down Expand Up @@ -148,11 +170,12 @@ private void recordOrPackedField(
final String field_name = JPRAGeneratedNames.getFieldName(f_name);
final String offset_name = JPRAGeneratedNames.getOffsetConstantName(f_name);
this.constructor_builder.addStatement(
"this.$N = $N.newValueWithOffset($N, $N, $N)",
"this.$N = $N.newValueWithOffset($N, $N, $N + $N)",
field_name,
t_imp_name,
"in_buffer",
"in_pointer",
"in_base_offset",
offset_name);

final PackageNameQualified p = pkg_ctxt.getName();
Expand Down
Expand Up @@ -33,6 +33,8 @@
import com.io7m.jpra.model.types.TString;
import com.io7m.jpra.model.types.TVector;
import com.io7m.jpra.model.types.TypeMatcherType;
import com.io7m.jpra.runtime.java.JPRAStringReadableType;
import com.io7m.jpra.runtime.java.JPRAStringType;
import com.io7m.junreachable.UnimplementedCodeException;
import com.io7m.junreachable.UnreachableCodeException;
import com.squareup.javapoet.ClassName;
Expand Down Expand Up @@ -78,8 +80,28 @@ final class RecordFieldImplementationProcessor
{
this.generateFieldOffsetConstant();

// TODO: Generated method stub!
throw new UnimplementedCodeException();
final String reader_name =
JPRAGeneratedNames.getGetterStringReadableName(this.field.getName());
final String writer_name =
JPRAGeneratedNames.getGetterStringWritableName(this.field.getName());

final String f_name = JPRAGeneratedNames.getFieldName(this.field.getName());

final MethodSpec.Builder read_b = MethodSpec.methodBuilder(reader_name);
read_b.addModifiers(Modifier.PUBLIC);
read_b.addAnnotation(Override.class);
read_b.returns(JPRAStringReadableType.class);
read_b.addStatement("return this.$N", f_name);
this.class_builder.addMethod(read_b.build());

final MethodSpec.Builder write_b = MethodSpec.methodBuilder(writer_name);
write_b.addModifiers(Modifier.PUBLIC);
write_b.addAnnotation(Override.class);
write_b.returns(JPRAStringType.class);
write_b.addStatement("return this.$N", f_name);
this.class_builder.addMethod(write_b.build());

return Unit.unit();
}

@Override public Unit matchBooleanSet(
Expand Down

0 comments on commit bd41e72

Please sign in to comment.