refactor: rename compileWithDebug to compile

This commit is contained in:
TopchetoEU 2023-12-14 21:07:16 +02:00
parent 0b5178e9fd
commit 773bc72f3e
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4
12 changed files with 41 additions and 36 deletions

View File

@ -32,10 +32,17 @@ public class CompileTarget {
public void setDebug(int i, BreakpointType type) { public void setDebug(int i, BreakpointType type) {
var instr = target.get(i); var instr = target.get(i);
instr.breakpoint = type; instr.breakpoint = type;
breakpoints.add(target.get(i).location);
var old = bpToInstr.put(instr.location, instr); if (type == BreakpointType.NONE) {
if (old != null) old.breakpoint = BreakpointType.NONE; breakpoints.remove(target.get(i).location);
bpToInstr.remove(instr.location, instr);
}
else {
breakpoints.add(target.get(i).location);
var old = bpToInstr.put(instr.location, instr);
if (old != null) old.breakpoint = BreakpointType.NONE;
}
} }
public void setDebug(BreakpointType type) { public void setDebug(BreakpointType type) {
setDebug(target.size() - 1, type); setDebug(target.size() - 1, type);

View File

@ -24,7 +24,7 @@ public class CompoundStatement extends Statement {
} }
@Override @Override
public void compileWithDebug(CompileTarget target, ScopeRecord scope, boolean pollute, BreakpointType type) { public void compile(CompileTarget target, ScopeRecord scope, boolean pollute, BreakpointType type) {
if (separateFuncs) for (var stm : statements) { if (separateFuncs) for (var stm : statements) {
if (stm instanceof FunctionStatement && ((FunctionStatement)stm).statement) { if (stm instanceof FunctionStatement && ((FunctionStatement)stm).statement) {
stm.compile(target, scope, false); stm.compile(target, scope, false);
@ -37,18 +37,14 @@ public class CompoundStatement extends Statement {
var stm = statements[i]; var stm = statements[i];
if (separateFuncs && stm instanceof FunctionStatement) continue; if (separateFuncs && stm instanceof FunctionStatement) continue;
if (i != statements.length - 1) stm.compileWithDebug(target, scope, false, BreakpointType.STEP_OVER); if (i != statements.length - 1) stm.compile(target, scope, false, BreakpointType.STEP_OVER);
else stm.compileWithDebug(target, scope, polluted = pollute, BreakpointType.STEP_OVER); else stm.compile(target, scope, polluted = pollute, BreakpointType.STEP_OVER);
} }
if (!polluted && pollute) { if (!polluted && pollute) {
target.add(Instruction.loadValue(loc(), null)); target.add(Instruction.loadValue(loc(), null));
} }
} }
@Override
public void compile(CompileTarget target, ScopeRecord scope, boolean pollute) {
compileWithDebug(target, scope, pollute, BreakpointType.STEP_IN);
}
public CompoundStatement setEnd(Location loc) { public CompoundStatement setEnd(Location loc) {
this.end = loc; this.end = loc;

View File

@ -8,10 +8,9 @@ public abstract class Statement {
private Location _loc; private Location _loc;
public boolean pure() { return false; } public boolean pure() { return false; }
public abstract void compile(CompileTarget target, ScopeRecord scope, boolean pollute);
public void declare(ScopeRecord varsScope) { } public void declare(ScopeRecord varsScope) { }
public void compileWithDebug(CompileTarget target, ScopeRecord scope, boolean pollute, BreakpointType type) { public void compile(CompileTarget target, ScopeRecord scope, boolean pollute, BreakpointType type) {
int start = target.size(); int start = target.size();
compile(target, scope, pollute); compile(target, scope, pollute);
@ -20,6 +19,9 @@ public abstract class Statement {
target.setDebug(start, type); target.setDebug(start, type);
} }
} }
public void compile(CompileTarget target, ScopeRecord scope, boolean pollute) {
compile(target, scope, pollute, BreakpointType.NONE);
}
public Location loc() { return _loc; } public Location loc() { return _loc; }
public void setLoc(Location loc) { _loc = loc; } public void setLoc(Location loc) { _loc = loc; }

View File

@ -19,9 +19,9 @@ public class DoWhileStatement extends Statement {
@Override @Override
public void compile(CompileTarget target, ScopeRecord scope, boolean pollute) { public void compile(CompileTarget target, ScopeRecord scope, boolean pollute) {
int start = target.size(); int start = target.size();
body.compileWithDebug(target, scope, false, BreakpointType.STEP_OVER); body.compile(target, scope, false, BreakpointType.STEP_OVER);
int mid = target.size(); int mid = target.size();
condition.compileWithDebug(target, scope, true, BreakpointType.STEP_OVER); condition.compile(target, scope, true, BreakpointType.STEP_OVER);
int end = target.size(); int end = target.size();
WhileStatement.replaceBreaks(target, label, start, mid - 1, mid, end + 1); WhileStatement.replaceBreaks(target, label, start, mid - 1, mid, end + 1);

View File

@ -33,7 +33,7 @@ public class ForInStatement extends Statement {
target.add(Instruction.storeVar(loc(), scope.getKey(varName))); target.add(Instruction.storeVar(loc(), scope.getKey(varName)));
} }
object.compileWithDebug(target, scope, true, BreakpointType.STEP_OVER); object.compile(target, scope, true, BreakpointType.STEP_OVER);
target.add(Instruction.keys(loc(), true)); target.add(Instruction.keys(loc(), true));
int start = target.size(); int start = target.size();

View File

@ -18,15 +18,15 @@ public class ForStatement extends Statement {
} }
@Override @Override
public void compile(CompileTarget target, ScopeRecord scope, boolean pollute) { public void compile(CompileTarget target, ScopeRecord scope, boolean pollute) {
declaration.compileWithDebug(target, scope, false, BreakpointType.STEP_OVER); declaration.compile(target, scope, false, BreakpointType.STEP_OVER);
int start = target.size(); int start = target.size();
condition.compileWithDebug(target, scope, true, BreakpointType.STEP_OVER); condition.compile(target, scope, true, BreakpointType.STEP_OVER);
int mid = target.size(); int mid = target.size();
target.add(Instruction.nop(null)); target.add(Instruction.nop(null));
body.compileWithDebug(target, scope, false, BreakpointType.STEP_OVER); body.compile(target, scope, false, BreakpointType.STEP_OVER);
int beforeAssign = target.size(); int beforeAssign = target.size();
assignment.compileWithDebug(target, scope, false, BreakpointType.STEP_OVER); assignment.compile(target, scope, false, BreakpointType.STEP_OVER);
int end = target.size(); int end = target.size();
WhileStatement.replaceBreaks(target, label, mid + 1, end, beforeAssign, end + 1); WhileStatement.replaceBreaks(target, label, mid + 1, end, beforeAssign, end + 1);

View File

@ -16,33 +16,31 @@ public class IfStatement extends Statement {
if (elseBody != null) elseBody.declare(globScope); if (elseBody != null) elseBody.declare(globScope);
} }
@Override @Override public void compile(CompileTarget target, ScopeRecord scope, boolean pollute, BreakpointType breakpoint) {
public void compileWithDebug(CompileTarget target, ScopeRecord scope, boolean pollute, BreakpointType breakpoint) { condition.compile(target, scope, true, breakpoint);
condition.compileWithDebug(target, scope, true, breakpoint);
if (elseBody == null) { if (elseBody == null) {
int i = target.size(); int i = target.size();
target.add(Instruction.nop(null)); target.add(Instruction.nop(null));
body.compileWithDebug(target, scope, pollute, breakpoint); body.compile(target, scope, pollute, breakpoint);
int endI = target.size(); int endI = target.size();
target.set(i, Instruction.jmpIfNot(loc(), endI - i)); target.set(i, Instruction.jmpIfNot(loc(), endI - i));
} }
else { else {
int start = target.size(); int start = target.size();
target.add(Instruction.nop(null)); target.add(Instruction.nop(null));
body.compileWithDebug(target, scope, pollute, breakpoint); body.compile(target, scope, pollute, breakpoint);
target.add(Instruction.nop(null)); target.add(Instruction.nop(null));
int mid = target.size(); int mid = target.size();
elseBody.compileWithDebug(target, scope, pollute, breakpoint); elseBody.compile(target, scope, pollute, breakpoint);
int end = target.size(); int end = target.size();
target.set(start, Instruction.jmpIfNot(loc(), mid - start)); target.set(start, Instruction.jmpIfNot(loc(), mid - start));
target.set(mid - 1, Instruction.jmp(loc(), end - mid + 1)); target.set(mid - 1, Instruction.jmp(loc(), end - mid + 1));
} }
} }
@Override @Override public void compile(CompileTarget target, ScopeRecord scope, boolean pollute) {
public void compile(CompileTarget target, ScopeRecord scope, boolean pollute) { compile(target, scope, pollute, BreakpointType.STEP_IN);
compileWithDebug(target, scope, pollute, BreakpointType.STEP_IN);
} }
public IfStatement(Location loc, Statement condition, Statement body, Statement elseBody) { public IfStatement(Location loc, Statement condition, Statement body, Statement elseBody) {

View File

@ -37,7 +37,7 @@ public class SwitchStatement extends Statement {
var caseToStatement = new HashMap<Integer, Integer>(); var caseToStatement = new HashMap<Integer, Integer>();
var statementToIndex = new HashMap<Integer, Integer>(); var statementToIndex = new HashMap<Integer, Integer>();
value.compileWithDebug(target, scope, true, BreakpointType.STEP_OVER); value.compile(target, scope, true, BreakpointType.STEP_OVER);
for (var ccase : cases) { for (var ccase : cases) {
target.add(Instruction.dup(loc())); target.add(Instruction.dup(loc()));
@ -53,7 +53,7 @@ public class SwitchStatement extends Statement {
for (var stm : body) { for (var stm : body) {
statementToIndex.put(statementToIndex.size(), target.size()); statementToIndex.put(statementToIndex.size(), target.size());
stm.compileWithDebug(target, scope, false, BreakpointType.STEP_OVER); stm.compile(target, scope, false, BreakpointType.STEP_OVER);
} }
int end = target.size(); int end = target.size();

View File

@ -4,6 +4,7 @@ import me.topchetoeu.jscript.Location;
import me.topchetoeu.jscript.compilation.CompileTarget; import me.topchetoeu.jscript.compilation.CompileTarget;
import me.topchetoeu.jscript.compilation.Instruction; import me.topchetoeu.jscript.compilation.Instruction;
import me.topchetoeu.jscript.compilation.Statement; import me.topchetoeu.jscript.compilation.Statement;
import me.topchetoeu.jscript.compilation.Instruction.BreakpointType;
import me.topchetoeu.jscript.engine.scope.GlobalScope; import me.topchetoeu.jscript.engine.scope.GlobalScope;
import me.topchetoeu.jscript.engine.scope.LocalScopeRecord; import me.topchetoeu.jscript.engine.scope.LocalScopeRecord;
import me.topchetoeu.jscript.engine.scope.ScopeRecord; import me.topchetoeu.jscript.engine.scope.ScopeRecord;
@ -22,7 +23,7 @@ public class TryStatement extends Statement {
} }
@Override @Override
public void compile(CompileTarget target, ScopeRecord scope, boolean pollute) { public void compile(CompileTarget target, ScopeRecord scope, boolean pollute, BreakpointType bpt) {
target.add(Instruction.nop(null)); target.add(Instruction.nop(null));
int start = target.size(), catchStart = -1, finallyStart = -1; int start = target.size(), catchStart = -1, finallyStart = -1;
@ -45,7 +46,7 @@ public class TryStatement extends Statement {
target.add(Instruction.tryEnd(loc())); target.add(Instruction.tryEnd(loc()));
} }
target.queueDebug(BreakpointType.STEP_OVER);
target.set(start - 1, Instruction.tryStart(loc(), catchStart, finallyStart, target.size() - start)); target.set(start - 1, Instruction.tryStart(loc(), catchStart, finallyStart, target.size() - start));
if (pollute) target.add(Instruction.loadValue(loc(), null)); if (pollute) target.add(Instruction.loadValue(loc(), null));
} }

View File

@ -22,7 +22,7 @@ public class WhileStatement extends Statement {
condition.compile(target, scope, true); condition.compile(target, scope, true);
int mid = target.size(); int mid = target.size();
target.add(Instruction.nop(null)); target.add(Instruction.nop(null));
body.compileWithDebug(target, scope, false, BreakpointType.STEP_OVER); body.compile(target, scope, false, BreakpointType.STEP_OVER);
int end = target.size(); int end = target.size();

View File

@ -13,7 +13,7 @@ public class CallStatement extends Statement {
public final boolean isNew; public final boolean isNew;
@Override @Override
public void compileWithDebug(CompileTarget target, ScopeRecord scope, boolean pollute, BreakpointType type) { public void compile(CompileTarget target, ScopeRecord scope, boolean pollute, BreakpointType type) {
if (isNew) func.compile(target, scope, true); if (isNew) func.compile(target, scope, true);
else if (func instanceof IndexStatement) { else if (func instanceof IndexStatement) {
((IndexStatement)func).compile(target, scope, true, true); ((IndexStatement)func).compile(target, scope, true, true);
@ -33,7 +33,7 @@ public class CallStatement extends Statement {
} }
@Override @Override
public void compile(CompileTarget target, ScopeRecord scope, boolean pollute) { public void compile(CompileTarget target, ScopeRecord scope, boolean pollute) {
compileWithDebug(target, scope, pollute, BreakpointType.STEP_IN); compile(target, scope, pollute, BreakpointType.STEP_IN);
} }
public CallStatement(Location loc, boolean isNew, Statement func, Statement ...args) { public CallStatement(Location loc, boolean isNew, Statement func, Statement ...args) {

View File

@ -8,6 +8,7 @@ import me.topchetoeu.jscript.compilation.CompoundStatement;
import me.topchetoeu.jscript.compilation.FunctionBody; import me.topchetoeu.jscript.compilation.FunctionBody;
import me.topchetoeu.jscript.compilation.Instruction; import me.topchetoeu.jscript.compilation.Instruction;
import me.topchetoeu.jscript.compilation.Statement; import me.topchetoeu.jscript.compilation.Statement;
import me.topchetoeu.jscript.compilation.Instruction.BreakpointType;
import me.topchetoeu.jscript.compilation.Instruction.Type; import me.topchetoeu.jscript.compilation.Instruction.Type;
import me.topchetoeu.jscript.engine.scope.ScopeRecord; import me.topchetoeu.jscript.engine.scope.ScopeRecord;
import me.topchetoeu.jscript.exceptions.SyntaxException; import me.topchetoeu.jscript.exceptions.SyntaxException;
@ -106,7 +107,7 @@ public class FunctionStatement extends Statement {
} }
} }
@Override public void compile(CompileTarget target, ScopeRecord scope, boolean pollute) { @Override public void compile(CompileTarget target, ScopeRecord scope, boolean pollute) {
compile(target, scope, pollute, null); compile(target, scope, pollute, BreakpointType.NONE);
} }
public FunctionStatement(Location loc, Location end, String varName, String[] args, boolean statement, CompoundStatement body) { public FunctionStatement(Location loc, Location end, String varName, String[] args, boolean statement, CompoundStatement body) {