Skip to content

Commit

Permalink
GH-550 Add crease.panda test
Browse files Browse the repository at this point in the history
  • Loading branch information
dzikoysk committed Jan 23, 2021
1 parent 13c7a34 commit 2e75eb6
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 16 deletions.
72 changes: 72 additions & 0 deletions examples/lang/crease.panda
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
module panda

/*
* Increase and decrease operations on different number types
*/
main {
/* Byte */

mut Byte byte = 1B

log 2 == ++byte
log 2 == byte++
log 2 == --byte
log 2 == byte--

log 1 == byte

/* Short */

mut Short short = 1S

log 2 == ++short
log 2 == short++
log 2 == --short
log 2 == short--

log 1 == short

/* Int */

mut Int int = 1

log 2 == ++int
log 2 == int++
log 2 == --int
log 2 == int--

log 1 == int

/* Long */

mut Long long = 1L

log 2 == ++long
log 2 == long++
log 2 == --long
log 2 == long--

log 1 == long

/* Float */

mut Float float = 1.0F

log 2 == ++float
log 2 == float++
log 2 == --float
log 2 == float--

log 1 == float

/* Double */

mut Double double = 1.0D

log 2 == ++double
log 2 == double++
log 2 == --double
log 2 == double--

log 1 == double
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.panda_lang.language.runtime.MemoryContainer;
import org.panda_lang.language.runtime.ProcessStack;
import org.panda_lang.panda.language.resource.syntax.expressions.subparsers.number.NumberPriorities;
import org.panda_lang.panda.language.resource.syntax.expressions.subparsers.number.NumberUtils;

import java.util.function.Function;

Expand All @@ -33,7 +34,7 @@ final class CreaseExpression extends NumberPriorities implements DynamicExpressi
private final Accessor<?> accessor;
private final boolean grow;
private final boolean post;
private final Function<Object, Object> function;
private final Function<Number, Object> function;

public CreaseExpression(Accessor<?> accessor, boolean grow, boolean post) {
this.accessor = accessor;
Expand All @@ -46,40 +47,42 @@ public CreaseExpression(Accessor<?> accessor, boolean grow, boolean post) {
@SuppressWarnings("unchecked")
public Object evaluate(ProcessStack stack, Object instance) throws Exception {
MemoryContainer memory = accessor.fetchMemoryContainer(stack, instance);
Object before = memory.get(accessor.getMemoryPointer());

Number before = memory.get(accessor.getMemoryPointer());
Object after = function.apply(before);
memory.set(accessor.getMemoryPointer(), after);

return post ? before : after;
return post
? before
: after;
}

private Function<Object, Object> toFunction(int priority) {
private Function<Number, Object> toFunction(int priority) {
switch (priority) {
case INT:
return grow
? value -> (int) value + 1
: value -> (int) value - 1;
? value -> value.intValue() + 1
: value -> value.intValue() - 1;
case LONG:
return grow
? value -> (long) value + 1L
: value -> (long) value - 1L;
? value -> value.longValue() + 1L
: value -> value.longValue() - 1L;
case DOUBLE:
return grow
? value -> (double) value + 1.0D
: value -> (double) value - 1.0D;
? value -> value.doubleValue() + 1.0D
: value -> value.doubleValue() - 1.0D;
case FLOAT:
return grow
? value -> (float) value + 1.0F
: value -> (float) value - 1.0F;
? value -> value.floatValue() + 1.0F
: value -> value.floatValue() - 1.0F;
case BYTE:
return grow
? value -> (byte) value + 1
: value -> (byte) value - 1;
? value -> value.byteValue() + 1
: value -> value.byteValue() - 1;
case SHORT:
return grow
? value -> (short) value + 1
: value -> (short) value - 1;
? value -> value.shortValue() + 1
: value -> value.shortValue() - 1;
default:
throw new PandaParserException("Unknown number type: " + priority);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.panda_lang.panda.examples.lang

import groovy.transform.CompileStatic
import org.junit.jupiter.api.Test

@CompileStatic
class CreaseTest extends LangTestSpecification {

@Test
void 'should compile and execute crease operations' () {
launch 'crease.panda'
}

}

0 comments on commit 2e75eb6

Please sign in to comment.