Skip to content

Commit

Permalink
Allow character to integer casts
Browse files Browse the repository at this point in the history
  • Loading branch information
ngallagher committed Mar 24, 2019
1 parent dd1a8ec commit 891bfc3
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 142 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Expand Up @@ -2,3 +2,13 @@
.idea
.project
*.iml

tern-common/\.classpath

tern-compile/\.classpath

tern-core/\.classpath

tern-parse/\.classpath

tern-tree/\.classpath
26 changes: 0 additions & 26 deletions tern-common/.classpath

This file was deleted.

2 changes: 1 addition & 1 deletion tern-common/pom.xml
Expand Up @@ -13,7 +13,7 @@
<scope>test</scope>
</dependency>
</dependencies>
<build>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
32 changes: 0 additions & 32 deletions tern-compile/.classpath

This file was deleted.

22 changes: 22 additions & 0 deletions tern-compile/src/test/java/org/ternlang/compile/CastTest.java
Expand Up @@ -48,6 +48,14 @@ public class CastTest extends TestCase {
"assert iterator.next() == `4`;\n"+
"assert iterator.next() == 5;\n";

private static final String SOURCE_5 =
"let x = 'a'.charAt(0) as Integer;\n"+
"assert x == 97;\n";

private static final String SOURCE_6 =
"let x = 'a' as Character;\n"+
"assert x == 'a';\n";

public void testCharacterCast() throws Exception {;
Compiler compiler = ClassPathCompilerBuilder.createCompiler();
Executable executable = compiler.compile(SOURCE_1);
Expand All @@ -72,4 +80,18 @@ public void testYieldCast() throws Exception {
Executable executable = compiler.compile(SOURCE_4);
executable.execute();
}

public void testCharacterToInteger() throws Exception {
Compiler compiler = ClassPathCompilerBuilder.createCompiler();
System.err.println(SOURCE_5);
Executable executable = compiler.compile(SOURCE_5);
executable.execute();
}

public void testStringToCharacter() throws Exception {
Compiler compiler = ClassPathCompilerBuilder.createCompiler();
System.err.println(SOURCE_6);
Executable executable = compiler.compile(SOURCE_6);
executable.execute();
}
}
27 changes: 0 additions & 27 deletions tern-core/.classpath

This file was deleted.

Expand Up @@ -44,4 +44,19 @@ public class IntegerConverter extends NumberConverter {
public IntegerConverter(Type type) {
super(type, INTEGER_TYPES, INTEGER_SCORES);
}

@Override
public Object convert(Object value) throws Exception {
if(value != null) {
Class actual = value.getClass();

if(actual == Character.class) {
Character code = (Character)value;
int number = code.charValue();

return number;
}
}
return super.convert(value);
}
}
28 changes: 0 additions & 28 deletions tern-parse/.classpath

This file was deleted.

28 changes: 0 additions & 28 deletions tern-tree/.classpath

This file was deleted.

0 comments on commit 891bfc3

Please sign in to comment.