Write some tests #33

Merged
topchetoeu merged 13 commits from topchetoeu/tests into master 2025-01-15 18:23:25 +00:00
3 changed files with 16 additions and 13 deletions
Showing only changes of commit 29b0c91c5d - Show all commits

View File

@ -45,8 +45,8 @@ env.add(Compiler.KEY, (_env, filename, raw, mapper) -> {
// We'll register the source and function source mappings for debugging
DebugContext.get(env).onSource(filename, raw);
for (var el : res.bodies()) {
DebugContext.get(env).onFunctionLoad(el, res.map(mapper));
for (var el : res.all()) {
DebugContext.get(env).onFunctionLoad(el.body(), el.map(mapper));
}
// Finally, we will construct the function

View File

@ -25,6 +25,7 @@ import java.util.LinkedList;
public final class CompileResult {
public static final Key<Void> DEBUG_LOG = new Key<>();
private FunctionBody body;
public final List<Instruction> instructions;
public final List<CompileResult> children;
public final Map<FunctionNode, CompileResult> childrenMap = new HashMap<>();
@ -71,17 +72,17 @@ public final class CompileResult {
setLocationAndDebug(instructions.size() - 1, loc, type);
}
public Iterable<FunctionBody> bodies() {
var stack = new Stack<FunctionBody>();
stack.push(body());
public Iterable<CompileResult> all() {
var stack = new Stack<CompileResult>();
stack.push(this);
return () -> new Iterator<FunctionBody>() {
@Override public FunctionBody next() {
return () -> new Iterator<CompileResult>() {
@Override public CompileResult next() {
if (stack.empty()) return null;
else {
var res = stack.pop();
for (var el : res.children) {
stack.push(el);
for (var child : res.children) {
stack.push(child);
}
return res;
}
@ -110,6 +111,8 @@ public final class CompileResult {
return map.build(scope.localNames(), scope.capturableNames(), scope.captureNames());
}
public FunctionBody body() {
if (body != null) return body;
var builtChildren = new FunctionBody[children.size()];
for (var i = 0; i < children.size(); i++) builtChildren[i] = children.get(i).body();
@ -124,7 +127,7 @@ public final class CompileResult {
for (var instr : instrRes) System.out.println(instr);
}
return new FunctionBody(
return body = new FunctionBody(
scope.localsCount(), scope.capturablesCount(), scope.capturesCount(),
length, instrRes, builtChildren
);

View File

@ -62,8 +62,8 @@ public class SimpleRepl {
var body = res.body();
DebugContext.get(env).onSource(filename, raw);
for (var el : res.bodies()) {
DebugContext.get(env).onFunctionLoad(el, res.map(mapper));
for (var el : res.all()) {
DebugContext.get(env).onFunctionLoad(el.body(), el.map(mapper));
}
return new CodeFunction(env, filename.toString(), body, new Value[0][]);
@ -708,7 +708,7 @@ public class SimpleRepl {
res.defineOwnField(env, "construct", new NativeFunction(args -> {
var func = (FunctionValue)args.get(0);
var target = args.get(1);
var funcArgs = (ArrayValue)args.get(2);
var funcArgs = (ArrayLikeValue)args.get(2);
if (target == Value.UNDEFINED) return func.constructNoSelf(env, funcArgs.toArray());
else return func.construct(env, target, funcArgs.toArray());