Skip to content

Commit 0ffa4c6

Browse files
committed
Test changes required for stack change
1 parent 4c32b4f commit 0ffa4c6

File tree

4 files changed

+187
-0
lines changed

4 files changed

+187
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package org.snapscript.core.stack;
2+
3+
import java.util.concurrent.atomic.AtomicBoolean;
4+
import java.util.concurrent.atomic.AtomicInteger;
5+
6+
import org.snapscript.core.InternalStateException;
7+
import org.snapscript.core.Result;
8+
import org.snapscript.core.ResultType;
9+
import org.snapscript.core.Scope;
10+
import org.snapscript.core.Statement;
11+
12+
public class CompoundStatement2 {
13+
14+
private final Statement[] statements;
15+
private final AtomicBoolean compile;
16+
private final AtomicInteger depth;
17+
18+
public CompoundStatement2(Statement... statements) {
19+
this.compile = new AtomicBoolean();
20+
this.depth = new AtomicInteger();
21+
this.statements = statements;
22+
}
23+
24+
public Result compile(Scope scope) throws Exception {
25+
Result last = ResultType.getNormal();
26+
27+
if(compile.get()) {
28+
throw new InternalStateException("Statement was already compiled");
29+
}
30+
try {
31+
for(Statement statement : statements) {
32+
Result result = statement.compile(scope);
33+
34+
if(result.isDeclare()){
35+
depth.getAndIncrement();
36+
}
37+
}
38+
} finally {
39+
compile.set(true);
40+
}
41+
return last;
42+
}
43+
44+
public Result execute(Scope2 scope) throws Exception {
45+
Result last = ResultType.getNormal();
46+
State2 state2 = scope.getStack();
47+
State2 next = state2.create(); // allow the stack to grow in a compound statement!!
48+
49+
if(!compile.get()) {
50+
throw new InternalStateException("Statement was not compiled");
51+
}
52+
try {
53+
for(Statement statement : statements) {
54+
Result result = statement.execute(null); // execute(scope); NOT NEED FOR CHANGE
55+
56+
if(!result.isNormal()){
57+
return result;
58+
}
59+
last = result;
60+
}
61+
}finally {
62+
next.clear();
63+
}
64+
return last;
65+
}
66+
67+
68+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.snapscript.core.stack;
2+
3+
import org.snapscript.core.Result;
4+
import org.snapscript.core.Scope;
5+
import org.snapscript.core.error.ThreadStack;
6+
import org.snapscript.core.function.ArgumentConverter;
7+
import org.snapscript.core.function.Function;
8+
import org.snapscript.core.function.Invocation;
9+
import org.snapscript.core.function.Signature;
10+
11+
public class FunctionPointer2 {
12+
13+
private final ThreadStack stack;
14+
private final Function function;
15+
private final Object[] arguments;
16+
17+
public FunctionPointer2(Function function, ThreadStack stack, Object[] arguments) {
18+
this.arguments = arguments;
19+
this.function = function;
20+
this.stack = stack;
21+
}
22+
23+
public Result call(Scope2 scope, Object object) throws Exception{
24+
Signature signature = function.getSignature();
25+
ArgumentConverter converter = signature.getConverter();
26+
Object[] list = converter.convert(arguments);
27+
Invocation invocation = function.getInvocation();
28+
State2 state = scope.getStack();
29+
State2 next = state.create(); /// allow stack to grow after invocation!!
30+
31+
try {
32+
stack.before(function);
33+
//return invocation.invoke(scope, object, list);
34+
return null;
35+
} finally {
36+
stack.after(function);
37+
next.clear(); /// clear the stack that was built up!!!
38+
}
39+
}
40+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package org.snapscript.core.stack;
2+
3+
import java.util.concurrent.atomic.AtomicBoolean;
4+
5+
import org.snapscript.core.Context;
6+
import org.snapscript.core.Module;
7+
import org.snapscript.core.Result;
8+
import org.snapscript.core.ResultType;
9+
import org.snapscript.core.Statement;
10+
import org.snapscript.core.Type;
11+
import org.snapscript.core.convert.ConstraintConverter;
12+
import org.snapscript.core.convert.ConstraintMatcher;
13+
import org.snapscript.core.function.Signature;
14+
import org.snapscript.core.function.SignatureAligner;
15+
16+
public class StatementInvocation2 {
17+
18+
//private final ParameterExtractor extractor;
19+
private final SignatureAligner aligner;
20+
private final AtomicBoolean compile;
21+
private final Statement statement;
22+
private final Type constraint;
23+
24+
public StatementInvocation2(Signature signature, Statement statement, Type constraint) {
25+
//this.extractor = new ParameterExtractor(signature);
26+
this.aligner = new SignatureAligner(signature);
27+
this.compile = new AtomicBoolean();
28+
this.constraint = constraint;
29+
this.statement = statement;
30+
}
31+
32+
public Result invoke(Scope2 scope, Object object, Object... list) throws Exception {
33+
//
34+
// THIS IS THE LIST OF ARGUMENTS... DO NOT NEED TO BE TAKEN FROM
35+
// PREVIIOUS SCOPE!!!!!
36+
//
37+
//
38+
Object[] arguments = aligner.align(list);
39+
Module module = scope.getModule();
40+
Context context = module.getContext();
41+
State2 previous = scope.getStack();
42+
State2 next = previous.create();
43+
44+
try {
45+
if(arguments.length > 0) {
46+
//extractor.extract(previous, next, arguments); // maybe this returns scope.getStack().create()
47+
//
48+
// State2 next = extractor.extract(previous, arguments);
49+
//
50+
51+
}
52+
if(compile.compareAndSet(false, true)) {
53+
// scope = scope.getObject()
54+
//
55+
//statement.compile(scope);
56+
}
57+
ConstraintMatcher matcher = context.getMatcher();
58+
ConstraintConverter converter = matcher.match(constraint);
59+
//
60+
// scope = scope.getObject();
61+
//Result result = statement.execute(scope);
62+
// Object value = result.getValue();
63+
//
64+
// if(value != null) {
65+
// value = converter.convert(value);
66+
// }
67+
// return ResultType.getNormal(value);
68+
return null;
69+
} finally {
70+
next.clear();
71+
}
72+
}
73+
}

snap-core/src/test/java/org/snapscript/core/stack/TraceStatement2.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ public Result compile(Scope2 scope) throws Exception {
2525
}
2626

2727
public Result execute(Scope2 scope) throws Exception {
28+
//
29+
// MAYBE NOT HERE AS THE INVOCATION NEEDS TO GRAB THE PARAMETERS
30+
// FROM THE PREVIOUS STACK/STATE, once you pass the parameters
31+
// from the previous stack on to the new stack then you can
32+
// grow the stack
33+
//
2834
State2 stack = scope.getStack();
2935
State2 outer = stack.create(); // allow the stack to grow!!
3036

0 commit comments

Comments
 (0)