Skip to content
This repository was archived by the owner on Dec 31, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/me/topchetoeu/jscript/compilation/CompileTarget.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package me.topchetoeu.jscript.compilation;

import java.util.Map;
import java.util.Vector;

public class CompileTarget {
public final Vector<Instruction> target = new Vector<>();
public final Map<Long, Instruction[]> functions;

public Instruction add(Instruction instr) {
target.add(instr);
return instr;
}
public Instruction set(int i, Instruction instr) {
return target.set(i, instr);
}
public Instruction get(int i) {
return target.get(i);
}
public int size() { return target.size(); }

public Instruction[] array() { return target.toArray(Instruction[]::new); }

public CompileTarget(Map<Long, Instruction[]> functions) {
this.functions = functions;
}
}
7 changes: 3 additions & 4 deletions src/me/topchetoeu/jscript/compilation/CompoundStatement.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package me.topchetoeu.jscript.compilation;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import me.topchetoeu.jscript.Location;
import me.topchetoeu.jscript.compilation.control.ContinueStatement;
Expand All @@ -21,7 +20,7 @@ public void declare(ScopeRecord varsScope) {
}

@Override
public void compile(List<Instruction> target, ScopeRecord scope, boolean pollute) {
public void compile(CompileTarget target, ScopeRecord scope, boolean pollute) {
for (var stm : statements) {
if (stm instanceof FunctionStatement) {
int start = target.size();
Expand All @@ -42,7 +41,7 @@ public void compile(List<Instruction> target, ScopeRecord scope, boolean pollute

@Override
public Statement optimize() {
var res = new ArrayList<Statement>();
var res = new Vector<Statement>(statements.length);

for (var i = 0; i < statements.length; i++) {
var stm = statements[i].optimize();
Expand Down
4 changes: 1 addition & 3 deletions src/me/topchetoeu/jscript/compilation/DiscardStatement.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package me.topchetoeu.jscript.compilation;

import java.util.List;

import me.topchetoeu.jscript.Location;
import me.topchetoeu.jscript.compilation.values.ConstantStatement;
import me.topchetoeu.jscript.engine.scope.ScopeRecord;
Expand All @@ -10,7 +8,7 @@ public class DiscardStatement extends Statement {
public final Statement value;

@Override
public void compile(List<Instruction> target, ScopeRecord scope, boolean pollute) {
public void compile(CompileTarget target, ScopeRecord scope, boolean pollute) {
value.compile(target, scope, false);

}
Expand Down
4 changes: 2 additions & 2 deletions src/me/topchetoeu/jscript/compilation/Instruction.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,9 @@ public static Instruction loadMember(Object key) {
public static Instruction loadRegex(String pattern, String flags) {
return new Instruction(null, Type.LOAD_REGEX, pattern, flags);
}
public static Instruction loadFunc(int instrN, int varN, int len, int[] captures) {
public static Instruction loadFunc(long id, int varN, int len, int[] captures) {
var args = new Object[3 + captures.length];
args[0] = instrN;
args[0] = id;
args[1] = varN;
args[2] = len;
for (var i = 0; i < captures.length; i++) args[i + 3] = captures[i];
Expand Down
6 changes: 2 additions & 4 deletions src/me/topchetoeu/jscript/compilation/Statement.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package me.topchetoeu.jscript.compilation;

import java.util.List;

import me.topchetoeu.jscript.Location;
import me.topchetoeu.jscript.engine.scope.ScopeRecord;

public abstract class Statement {
private Location _loc;

public boolean pure() { return false; }
public abstract void compile(List<Instruction> target, ScopeRecord scope, boolean pollute);
public abstract void compile(CompileTarget target, ScopeRecord scope, boolean pollute);
public void declare(ScopeRecord varsScope) { }
public Statement optimize() { return this; }

public void compileWithDebug(List<Instruction> target, ScopeRecord scope, boolean pollute) {
public void compileWithDebug(CompileTarget target, ScopeRecord scope, boolean pollute) {
int start = target.size();
compile(target, scope, pollute);
if (target.size() != start) target.get(start).setDebug(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void declare(ScopeRecord varsScope) {
}
}
@Override
public void compile(List<Instruction> target, ScopeRecord scope, boolean pollute) {
public void compile(CompileTarget target, ScopeRecord scope, boolean pollute) {
for (var entry : values) {
if (entry.name == null) continue;
var key = scope.getKey(entry.name);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package me.topchetoeu.jscript.compilation.control;

import java.util.List;

import me.topchetoeu.jscript.Location;
import me.topchetoeu.jscript.compilation.CompileTarget;
import me.topchetoeu.jscript.compilation.Instruction;
import me.topchetoeu.jscript.compilation.Statement;
import me.topchetoeu.jscript.engine.scope.ScopeRecord;
Expand All @@ -14,7 +13,7 @@ public class ArrayStatement extends Statement {
public boolean pure() { return true; }

@Override
public void compile(List<Instruction> target, ScopeRecord scope, boolean pollute) {
public void compile(CompileTarget target, ScopeRecord scope, boolean pollute) {
target.add(Instruction.loadArr(statements.length).locate(loc()));
var i = 0;
for (var el : statements) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package me.topchetoeu.jscript.compilation.control;

import java.util.List;

import me.topchetoeu.jscript.Location;
import me.topchetoeu.jscript.compilation.CompileTarget;
import me.topchetoeu.jscript.compilation.Instruction;
import me.topchetoeu.jscript.compilation.Statement;
import me.topchetoeu.jscript.engine.scope.ScopeRecord;
Expand All @@ -11,7 +10,7 @@ public class BreakStatement extends Statement {
public final String label;

@Override
public void compile(List<Instruction> target, ScopeRecord scope, boolean pollute) {
public void compile(CompileTarget target, ScopeRecord scope, boolean pollute) {
target.add(Instruction.nop("break", label).locate(loc()));
if (pollute) target.add(Instruction.loadValue(null).locate(loc()));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package me.topchetoeu.jscript.compilation.control;

import java.util.List;

import me.topchetoeu.jscript.Location;
import me.topchetoeu.jscript.compilation.CompileTarget;
import me.topchetoeu.jscript.compilation.Instruction;
import me.topchetoeu.jscript.compilation.Statement;
import me.topchetoeu.jscript.engine.scope.ScopeRecord;
Expand All @@ -11,7 +10,7 @@ public class ContinueStatement extends Statement {
public final String label;

@Override
public void compile(List<Instruction> target, ScopeRecord scope, boolean pollute) {
public void compile(CompileTarget target, ScopeRecord scope, boolean pollute) {
target.add(Instruction.nop("cont", label).locate(loc()));
if (pollute) target.add(Instruction.loadValue(null).locate(loc()));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package me.topchetoeu.jscript.compilation.control;

import java.util.List;

import me.topchetoeu.jscript.Location;
import me.topchetoeu.jscript.compilation.CompileTarget;
import me.topchetoeu.jscript.compilation.Instruction;
import me.topchetoeu.jscript.compilation.Statement;
import me.topchetoeu.jscript.engine.scope.ScopeRecord;

public class DebugStatement extends Statement {
@Override
public void compile(List<Instruction> target, ScopeRecord scope, boolean pollute) {
public void compile(CompileTarget target, ScopeRecord scope, boolean pollute) {
target.add(Instruction.debug().locate(loc()));
if (pollute) target.add(Instruction.loadValue(null).locate(loc()));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package me.topchetoeu.jscript.compilation.control;

import java.util.List;

import me.topchetoeu.jscript.Location;
import me.topchetoeu.jscript.compilation.CompileTarget;
import me.topchetoeu.jscript.compilation.Instruction;
import me.topchetoeu.jscript.compilation.Statement;
import me.topchetoeu.jscript.engine.scope.ScopeRecord;
Expand All @@ -12,7 +11,7 @@ public class DeleteStatement extends Statement {
public final Statement value;

@Override
public void compile(List<Instruction> target, ScopeRecord scope, boolean pollute) {
public void compile(CompileTarget target, ScopeRecord scope, boolean pollute) {
value.compile(target, scope, true);
key.compile(target, scope, true);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package me.topchetoeu.jscript.compilation.control;

import java.util.List;

import me.topchetoeu.jscript.Location;
import me.topchetoeu.jscript.compilation.CompileTarget;
import me.topchetoeu.jscript.compilation.CompoundStatement;
import me.topchetoeu.jscript.compilation.Instruction;
import me.topchetoeu.jscript.compilation.Statement;
Expand All @@ -20,7 +19,7 @@ public void declare(ScopeRecord globScope) {
}

@Override
public void compile(List<Instruction> target, ScopeRecord scope, boolean pollute) {
public void compile(CompileTarget target, ScopeRecord scope, boolean pollute) {
if (condition instanceof ConstantStatement) {
int start = target.size();
body.compile(target, scope, false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package me.topchetoeu.jscript.compilation.control;

import java.util.List;

import me.topchetoeu.jscript.Location;
import me.topchetoeu.jscript.compilation.CompileTarget;
import me.topchetoeu.jscript.compilation.Instruction;
import me.topchetoeu.jscript.compilation.Statement;
import me.topchetoeu.jscript.engine.Operation;
Expand All @@ -21,7 +20,7 @@ public void declare(ScopeRecord globScope) {
}

@Override
public void compile(List<Instruction> target, ScopeRecord scope, boolean pollute) {
public void compile(CompileTarget target, ScopeRecord scope, boolean pollute) {
var key = scope.getKey(varName);
if (key instanceof String) target.add(Instruction.makeVar((String)key));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package me.topchetoeu.jscript.compilation.control;

import java.util.List;

import me.topchetoeu.jscript.Location;
import me.topchetoeu.jscript.compilation.Statement;
import me.topchetoeu.jscript.compilation.CompileTarget;
import me.topchetoeu.jscript.compilation.CompoundStatement;
import me.topchetoeu.jscript.compilation.Instruction;
import me.topchetoeu.jscript.compilation.values.ConstantStatement;
Expand All @@ -20,7 +19,7 @@ public void declare(ScopeRecord globScope) {
body.declare(globScope);
}
@Override
public void compile(List<Instruction> target, ScopeRecord scope, boolean pollute) {
public void compile(CompileTarget target, ScopeRecord scope, boolean pollute) {
declaration.compile(target, scope, false);

if (condition instanceof ConstantStatement) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package me.topchetoeu.jscript.compilation.control;

import java.util.List;

import me.topchetoeu.jscript.Location;
import me.topchetoeu.jscript.compilation.CompileTarget;
import me.topchetoeu.jscript.compilation.CompoundStatement;
import me.topchetoeu.jscript.compilation.DiscardStatement;
import me.topchetoeu.jscript.compilation.Instruction;
Expand All @@ -21,7 +20,7 @@ public void declare(ScopeRecord globScope) {
}

@Override
public void compile(List<Instruction> target, ScopeRecord scope, boolean pollute) {
public void compile(CompileTarget target, ScopeRecord scope, boolean pollute) {
if (condition instanceof ConstantStatement) {
if (Values.not(((ConstantStatement)condition).value)) {
if (elseBody != null) elseBody.compileWithDebug(target, scope, pollute);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package me.topchetoeu.jscript.compilation.control;

import java.util.List;

import me.topchetoeu.jscript.Location;
import me.topchetoeu.jscript.compilation.CompileTarget;
import me.topchetoeu.jscript.compilation.Instruction;
import me.topchetoeu.jscript.compilation.Statement;
import me.topchetoeu.jscript.engine.scope.ScopeRecord;
Expand All @@ -11,7 +10,7 @@ public class ReturnStatement extends Statement {
public final Statement value;

@Override
public void compile(List<Instruction> target, ScopeRecord scope, boolean pollute) {
public void compile(CompileTarget target, ScopeRecord scope, boolean pollute) {
if (value == null) target.add(Instruction.loadValue(null).locate(loc()));
else value.compile(target, scope, true);
target.add(Instruction.ret().locate(loc()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package me.topchetoeu.jscript.compilation.control;

import java.util.HashMap;
import java.util.List;

import me.topchetoeu.jscript.Location;
import me.topchetoeu.jscript.compilation.CompileTarget;
import me.topchetoeu.jscript.compilation.Instruction;
import me.topchetoeu.jscript.compilation.Statement;
import me.topchetoeu.jscript.compilation.Instruction.Type;
Expand Down Expand Up @@ -32,7 +32,7 @@ public void declare(ScopeRecord varsScope) {
}

@Override
public void compile(List<Instruction> target, ScopeRecord scope, boolean pollute) {
public void compile(CompileTarget target, ScopeRecord scope, boolean pollute) {
var caseMap = new HashMap<Integer, Integer>();
var stmIndexMap = new HashMap<Integer, Integer>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package me.topchetoeu.jscript.compilation.control;

import java.util.List;

import me.topchetoeu.jscript.Location;
import me.topchetoeu.jscript.compilation.CompileTarget;
import me.topchetoeu.jscript.compilation.Instruction;
import me.topchetoeu.jscript.compilation.Statement;
import me.topchetoeu.jscript.engine.scope.ScopeRecord;
Expand All @@ -11,7 +10,7 @@ public class ThrowStatement extends Statement {
public final Statement value;

@Override
public void compile(List<Instruction> target, ScopeRecord scope, boolean pollute) {
public void compile(CompileTarget target, ScopeRecord scope, boolean pollute) {
value.compile(target, scope, true);
target.add(Instruction.throwInstr().locate(loc()));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package me.topchetoeu.jscript.compilation.control;

import java.util.List;

import me.topchetoeu.jscript.Location;
import me.topchetoeu.jscript.compilation.CompileTarget;
import me.topchetoeu.jscript.compilation.Instruction;
import me.topchetoeu.jscript.compilation.Statement;
import me.topchetoeu.jscript.engine.scope.GlobalScope;
Expand All @@ -23,7 +22,7 @@ public void declare(ScopeRecord globScope) {
}

@Override
public void compile(List<Instruction> target, ScopeRecord scope, boolean pollute) {
public void compile(CompileTarget target, ScopeRecord scope, boolean pollute) {
target.add(Instruction.nop());

int start = target.size(), tryN, catchN = -1, finN = -1;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package me.topchetoeu.jscript.compilation.control;

import java.util.List;

import me.topchetoeu.jscript.Location;
import me.topchetoeu.jscript.compilation.Statement;
import me.topchetoeu.jscript.compilation.CompileTarget;
import me.topchetoeu.jscript.compilation.CompoundStatement;
import me.topchetoeu.jscript.compilation.DiscardStatement;
import me.topchetoeu.jscript.compilation.Instruction;
Expand All @@ -21,7 +20,7 @@ public void declare(ScopeRecord globScope) {
body.declare(globScope);
}
@Override
public void compile(List<Instruction> target, ScopeRecord scope, boolean pollute) {
public void compile(CompileTarget target, ScopeRecord scope, boolean pollute) {
if (condition instanceof ConstantStatement) {
if (Values.toBoolean(((ConstantStatement)condition).value)) {
int start = target.size();
Expand Down Expand Up @@ -68,7 +67,7 @@ public WhileStatement(Location loc, String label, Statement condition, Statement
this.body = body;
}

public static void replaceBreaks(List<Instruction> target, String label, int start, int end, int continuePoint, int breakPoint) {
public static void replaceBreaks(CompileTarget target, String label, int start, int end, int continuePoint, int breakPoint) {
for (int i = start; i < end; i++) {
var instr = target.get(i);
if (instr.type == Type.NOP && instr.is(0, "cont") && (instr.get(1) == null || instr.is(1, label))) {
Expand Down
Loading