Write some tests #33
@ -45,8 +45,8 @@ env.add(Compiler.KEY, (_env, filename, raw, mapper) -> {
|
|||||||
|
|
||||||
// We'll register the source and function source mappings for debugging
|
// We'll register the source and function source mappings for debugging
|
||||||
DebugContext.get(env).onSource(filename, raw);
|
DebugContext.get(env).onSource(filename, raw);
|
||||||
for (var el : res.bodies()) {
|
for (var el : res.all()) {
|
||||||
DebugContext.get(env).onFunctionLoad(el, res.map(mapper));
|
DebugContext.get(env).onFunctionLoad(el.body(), el.map(mapper));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Finally, we will construct the function
|
// Finally, we will construct the function
|
||||||
|
@ -25,6 +25,7 @@ import java.util.LinkedList;
|
|||||||
public final class CompileResult {
|
public final class CompileResult {
|
||||||
public static final Key<Void> DEBUG_LOG = new Key<>();
|
public static final Key<Void> DEBUG_LOG = new Key<>();
|
||||||
|
|
||||||
|
private FunctionBody body;
|
||||||
public final List<Instruction> instructions;
|
public final List<Instruction> instructions;
|
||||||
public final List<CompileResult> children;
|
public final List<CompileResult> children;
|
||||||
public final Map<FunctionNode, CompileResult> childrenMap = new HashMap<>();
|
public final Map<FunctionNode, CompileResult> childrenMap = new HashMap<>();
|
||||||
@ -71,17 +72,17 @@ public final class CompileResult {
|
|||||||
setLocationAndDebug(instructions.size() - 1, loc, type);
|
setLocationAndDebug(instructions.size() - 1, loc, type);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Iterable<FunctionBody> bodies() {
|
public Iterable<CompileResult> all() {
|
||||||
var stack = new Stack<FunctionBody>();
|
var stack = new Stack<CompileResult>();
|
||||||
stack.push(body());
|
stack.push(this);
|
||||||
|
|
||||||
return () -> new Iterator<FunctionBody>() {
|
return () -> new Iterator<CompileResult>() {
|
||||||
@Override public FunctionBody next() {
|
@Override public CompileResult next() {
|
||||||
if (stack.empty()) return null;
|
if (stack.empty()) return null;
|
||||||
else {
|
else {
|
||||||
var res = stack.pop();
|
var res = stack.pop();
|
||||||
for (var el : res.children) {
|
for (var child : res.children) {
|
||||||
stack.push(el);
|
stack.push(child);
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@ -110,6 +111,8 @@ public final class CompileResult {
|
|||||||
return map.build(scope.localNames(), scope.capturableNames(), scope.captureNames());
|
return map.build(scope.localNames(), scope.capturableNames(), scope.captureNames());
|
||||||
}
|
}
|
||||||
public FunctionBody body() {
|
public FunctionBody body() {
|
||||||
|
if (body != null) return body;
|
||||||
|
|
||||||
var builtChildren = new FunctionBody[children.size()];
|
var builtChildren = new FunctionBody[children.size()];
|
||||||
for (var i = 0; i < children.size(); i++) builtChildren[i] = children.get(i).body();
|
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);
|
for (var instr : instrRes) System.out.println(instr);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new FunctionBody(
|
return body = new FunctionBody(
|
||||||
scope.localsCount(), scope.capturablesCount(), scope.capturesCount(),
|
scope.localsCount(), scope.capturablesCount(), scope.capturesCount(),
|
||||||
length, instrRes, builtChildren
|
length, instrRes, builtChildren
|
||||||
);
|
);
|
||||||
|
@ -62,8 +62,8 @@ public class SimpleRepl {
|
|||||||
var body = res.body();
|
var body = res.body();
|
||||||
|
|
||||||
DebugContext.get(env).onSource(filename, raw);
|
DebugContext.get(env).onSource(filename, raw);
|
||||||
for (var el : res.bodies()) {
|
for (var el : res.all()) {
|
||||||
DebugContext.get(env).onFunctionLoad(el, res.map(mapper));
|
DebugContext.get(env).onFunctionLoad(el.body(), el.map(mapper));
|
||||||
}
|
}
|
||||||
|
|
||||||
return new CodeFunction(env, filename.toString(), body, new Value[0][]);
|
return new CodeFunction(env, filename.toString(), body, new Value[0][]);
|
||||||
@ -708,7 +708,7 @@ public class SimpleRepl {
|
|||||||
res.defineOwnField(env, "construct", new NativeFunction(args -> {
|
res.defineOwnField(env, "construct", new NativeFunction(args -> {
|
||||||
var func = (FunctionValue)args.get(0);
|
var func = (FunctionValue)args.get(0);
|
||||||
var target = args.get(1);
|
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());
|
if (target == Value.UNDEFINED) return func.constructNoSelf(env, funcArgs.toArray());
|
||||||
else return func.construct(env, target, funcArgs.toArray());
|
else return func.construct(env, target, funcArgs.toArray());
|
||||||
|
Loading…
Reference in New Issue
Block a user