ES6 Support Groundwork + Fixes (OLD ONE DON'T LOOK AT ME!!!) #22

Merged
TopchetoEU merged 49 commits from ES6 into master 2024-09-05 14:17:52 +00:00
18 changed files with 1 additions and 142 deletions
Showing only changes of commit 55caf1e206 - Show all commits

View File

@ -123,18 +123,6 @@ public class Environment {
public Environment addAll(Map<Key<?>, ?> map) { public Environment addAll(Map<Key<?>, ?> map) {
return addAll(map, true); return addAll(map, true);
} }
// public Environment addAll(Environment env) {
// this.map.putAll(env.map);
// this.hidden.removeAll(env.map.keySet());
// for (var el : env.multi.entrySet()) {
// for (var val : el.getValue()) {
// add(el.getKey(), val);
// }
// }
// return this;
// }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public Environment remove(Key<?> key) { public Environment remove(Key<?> key) {

View File

@ -59,7 +59,6 @@ public class ParseRes<T> {
@SafeVarargs @SafeVarargs
@SuppressWarnings("all") @SuppressWarnings("all")
// to hell with all of java's bullshit generics that do jack shit nothing
public static <T> ParseRes<T> first(Source src, int i, Parser ...parsers) { public static <T> ParseRes<T> first(Source src, int i, Parser ...parsers) {
int n = Parsing.skipEmpty(src, i); int n = Parsing.skipEmpty(src, i);
ParseRes<T> error = ParseRes.failed(); ParseRes<T> error = ParseRes.failed();

View File

@ -27,7 +27,6 @@ public final class CompileResult {
} }
public final List<IntFunction<Instruction>> instructions; public final List<IntFunction<Instruction>> instructions;
// public final List<Supplier<CompileResult>> childrenTasks;
public final List<CompileResult> children; public final List<CompileResult> children;
public final FunctionMapBuilder map; public final FunctionMapBuilder map;
public final Environment env; public final Environment env;
@ -58,9 +57,6 @@ public final class CompileResult {
instructions.set(i, instr); instructions.set(i, instr);
return this; return this;
} }
// public Instruction get(int i) {
// return instructions.get(i);
// }
public int size() { return instructions.size(); } public int size() { return instructions.size(); }
public void setDebug(Location loc, BreakpointType type) { public void setDebug(Location loc, BreakpointType type) {
@ -87,11 +83,6 @@ public final class CompileResult {
return this.children.size() - 1; return this.children.size() - 1;
} }
// public int addChild(Supplier<CompileResult> supplier) {
// this.childrenTasks.add(() -> supplier.get());
// return childrenTasks.size() - 1;
// }
public Instruction[] instructions() { public Instruction[] instructions() {
var res = new Instruction[instructions.size()]; var res = new Instruction[instructions.size()];
var i = 0; var i = 0;

View File

@ -19,27 +19,6 @@ public abstract class FunctionNode extends Node {
public abstract String name(); public abstract String name();
// @Override public void declare(CompileResult target) {
// if (varName != null && statement) target.scope.define(varName);
// }
// public static void checkBreakAndCont(CompileResult target, int start) {
// for (int i = start; i < target.size(); i++) {
// if (target.get(i).type == Type.NOP) {
// if (target.get(i).is(0, "break") ) {
// throw new SyntaxException(target.map.toLocation(i), "Break was placed outside a loop.");
// }
// if (target.get(i).is(0, "cont")) {
// throw new SyntaxException(target.map.toLocation(i), "Continue was placed outside a loop.");
// }
// }
// }
// }
// protected void compileLoadFunc(CompileResult target, int id, int[] captures, String name) {
// target.add(Instruction.loadFunc(id, true, true, false, name, captures));
// }
protected final int[] captures(int id, CompileResult target) { protected final int[] captures(int id, CompileResult target) {
return ((FunctionScope)target.children.get(id).scope).getCaptureIndices(); return ((FunctionScope)target.children.get(id).scope).getCaptureIndices();
} }
@ -112,9 +91,6 @@ public abstract class FunctionNode extends Node {
scope.finish(); scope.finish();
}); });
// if (pollute) compileLoadFunc(target, target.children.size(), subscope.getCaptureIndices(), name);
// return target.addChild(subtarget);
} }
public final CompileResult compileBody(CompileResult parent, boolean hasArgs, String name, String selfName) { public final CompileResult compileBody(CompileResult parent, boolean hasArgs, String name, String selfName) {
return compileBody(parent.env, new FunctionScope(parent.scope), false, hasArgs, name, selfName); return compileBody(parent.env, new FunctionScope(parent.scope), false, hasArgs, name, selfName);

View File

@ -9,10 +9,6 @@ public class FunctionValueNode extends FunctionNode {
@Override public String name() { return name; } @Override public String name() { return name; }
// @Override public void compile(CompileResult target, boolean pollute, String name, BreakpointType bp) {
// compileBody(target, pollute, true, name, null, bp);
// }
@Override public void compile(CompileResult target, boolean pollute, String name, BreakpointType bp) { @Override public void compile(CompileResult target, boolean pollute, String name, BreakpointType bp) {
var id = target.addChild(compileBody(target, false, name, null)); var id = target.addChild(compileBody(target, false, name, null));
target.add(_i -> Instruction.loadFunc(id, true, true, false, name, captures(id, target))); target.add(_i -> Instruction.loadFunc(id, true, true, false, name, captures(id, target)));

View File

@ -113,11 +113,9 @@ public final class JavaScript {
if (id.result.equals("true")) return ParseRes.res(new BoolNode(loc, true), n); if (id.result.equals("true")) return ParseRes.res(new BoolNode(loc, true), n);
if (id.result.equals("false")) return ParseRes.res(new BoolNode(loc, false), n); if (id.result.equals("false")) return ParseRes.res(new BoolNode(loc, false), n);
// if (id.result.equals("undefined")) return ParseRes.res(new DiscardNode(loc, null), n);
if (id.result.equals("null")) return ParseRes.res(new NullNode(loc), n); if (id.result.equals("null")) return ParseRes.res(new NullNode(loc), n);
if (id.result.equals("this")) return ParseRes.res(new ThisNode(loc), n); if (id.result.equals("this")) return ParseRes.res(new ThisNode(loc), n);
// if (id.result.equals("arguments")) return ParseRes.res(new ArgumentsNode(loc), n); // if (id.result.equals("arguments")) return ParseRes.res(new ArgumentsNode(loc), n);
// if (id.result.equals("globalThis")) return ParseRes.res(new GlobalThisNode(loc), n);
return ParseRes.failed(); return ParseRes.failed();
} }
@ -328,28 +326,6 @@ public final class JavaScript {
var res = func.compileBody(env, new FunctionScope(true), true, true, null, null); var res = func.compileBody(env, new FunctionScope(true), true, true, null, null);
res.buildTask.run(); res.buildTask.run();
return res; return res;
// var target = new CompileResult(env, new FunctionScope(true));
// var stm = ;
// var argsI = target.scope.defineStrict(new Variable("arguments", true), null);
// target.add(Instruction.loadArgs());
// target.add(_i -> Instruction.storeVar(argsI.index()));
// // try {
// stm.resolve(target);
// stm.compile(target, true, false, BreakpointType.NONE);
// // FunctionNode.checkBreakAndCont(target, 0);
// // }
// // catch (SyntaxException e) {
// // target = new CompileResult(env, new LocalScope(new GlobalScope()));
// // target.add(Instruction.throwSyntax(e)).setLocation(stm.loc());
// // }
// target.scope.end();
// target.scope.finish();
// return target;
} }
public static CompileResult compile(Environment env, Filename filename, String raw) { public static CompileResult compile(Environment env, Filename filename, String raw) {

View File

@ -36,7 +36,6 @@ public class VariableDeclareNode extends Node {
} }
} }
} }
// let a = 10, b = "test"; var c = () => a + b
@Override public void compile(CompileResult target, boolean pollute) { @Override public void compile(CompileResult target, boolean pollute) {
for (var entry : values) { for (var entry : values) {
if (entry.name == null) continue; if (entry.name == null) continue;

View File

@ -22,7 +22,6 @@ public class BreakNode extends Node {
} }
target.add(res); target.add(res);
// target.add(Instruction.nop("break", label));
if (pollute) target.add(Instruction.pushUndefined()); if (pollute) target.add(Instruction.pushUndefined());
} }

View File

@ -22,7 +22,6 @@ public class ContinueNode extends Node {
} }
target.add(res); target.add(res);
// () -> Instruction.nop("cont", label));
if (pollute) target.add(Instruction.pushUndefined()); if (pollute) target.add(Instruction.pushUndefined());
} }

View File

@ -34,7 +34,6 @@ public class DoWhileNode extends Node {
int endI = target.size(); int endI = target.size();
end.set(endI + 1); end.set(endI + 1);
// WhileNode.replaceBreaks(target, label, start, mid - 1, mid, end + 1);
target.add(Instruction.jmpIf(start - endI)); target.add(Instruction.jmpIf(start - endI));
} }

View File

@ -75,12 +75,6 @@ public class SwitchNode extends Node {
if (defaultI < 0 || defaultI >= body.length) subtarget.set(start, Instruction.jmp(endI - start)); if (defaultI < 0 || defaultI >= body.length) subtarget.set(start, Instruction.jmp(endI - start));
else subtarget.set(start, Instruction.jmp(statementToIndex.get(defaultI) - start)); else subtarget.set(start, Instruction.jmp(statementToIndex.get(defaultI) - start));
// for (int i = start; i < end; i++) {
// var instr = target.get(i);
// if (instr.type == Type.NOP && instr.is(0, "break") && instr.get(1) == null) {
// target.set(i, Instruction.jmp(end - i));
// }
// }
for (var el : caseToStatement.entrySet()) { for (var el : caseToStatement.entrySet()) {
var i = statementToIndex.get(el.getValue()); var i = statementToIndex.get(el.getValue());
if (i == null) i = endI; if (i == null) i = endI;

View File

@ -34,8 +34,6 @@ public class WhileNode extends Node {
var endI = target.size(); var endI = target.size();
end.set(endI + 1); end.set(endI + 1);
// replaceBreaks(target, label, mid + 1, end, start, end + 1);
target.add(Instruction.jmp(start - end.getAsInt())); target.add(Instruction.jmp(start - end.getAsInt()));
target.set(mid, Instruction.jmpIfNot(end.getAsInt() - mid + 1)); target.set(mid, Instruction.jmpIfNot(end.getAsInt() - mid + 1));
if (pollute) target.add(Instruction.pushUndefined()); if (pollute) target.add(Instruction.pushUndefined());
@ -48,18 +46,6 @@ public class WhileNode extends Node {
this.body = body; this.body = body;
} }
// public static void replaceBreaks(CompileResult 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))) {
// target.set(i, Instruction.jmp(continuePoint - i));
// }
// if (instr.type == Type.NOP && instr.is(0, "break") && (instr.get(1) == null || instr.is(1, label))) {
// target.set(i, Instruction.jmp(breakPoint - i));
// }
// }
// }
public static ParseRes<WhileNode> parse(Source src, int i) { public static ParseRes<WhileNode> parse(Source src, int i) {
var n = Parsing.skipEmpty(src, i); var n = Parsing.skipEmpty(src, i);
var loc = src.loc(i + n); var loc = src.loc(i + n);

View File

@ -28,8 +28,6 @@ public class FunctionScope extends Scope {
@Override public Variable define(Variable var, Location loc) { @Override public Variable define(Variable var, Location loc) {
checkNotEnded(); checkNotEnded();
if (locals.has(var.name)) throw alreadyDefinedErr(loc, var.name); if (locals.has(var.name)) throw alreadyDefinedErr(loc, var.name);
// if (specials.has(var.name)) throw alreadyDefinedErr(loc, var.name);
// if (blacklistNames.contains(var.name)) throw alreadyDefinedErr(loc, var.name);
if (passtrough) { if (passtrough) {
blacklistNames.add(var.name); blacklistNames.add(var.name);

View File

@ -19,13 +19,6 @@ import me.topchetoeu.jscript.runtime.exceptions.SyntaxException;
public class VariableNode extends Node implements AssignableNode { public class VariableNode extends Node implements AssignableNode {
public final String name; public final String name;
// @Override public EvalResult evaluate(CompileResult target) {
// var i = target.scope.getKey(name);
// if (i instanceof String) return EvalResult.NONE;
// else return EvalResult.UNKNOWN;
// }
@Override public Node toAssign(Node val, Operation operation) { @Override public Node toAssign(Node val, Operation operation) {
return new VariableAssignNode(loc(), name, val, operation); return new VariableAssignNode(loc(), name, val, operation);
} }
@ -57,7 +50,7 @@ public class VariableNode extends Node implements AssignableNode {
} }
public static IntFunction<Instruction> toGet(CompileResult target, Location loc, String name) { public static IntFunction<Instruction> toGet(CompileResult target, Location loc, String name) {
return toGet(target, loc, name, () -> Instruction.globGet(name)); return toGet(target, loc, name, () -> Instruction.globGet(name));
} }
public static IntFunction<Instruction> toSet(CompileResult target, Location loc, String name, boolean keep, boolean define) { public static IntFunction<Instruction> toSet(CompileResult target, Location loc, String name, boolean keep, boolean define) {

View File

@ -13,15 +13,6 @@ import me.topchetoeu.jscript.compilation.Node;
public class DiscardNode extends Node { public class DiscardNode extends Node {
public final Node value; public final Node value;
// @Override public EvalResult evaluate(CompileResult target) {
// if (value == null) return EvalResult.FALSY;
// var res = value.evaluate(target);
// if (res.isPure) return EvalResult.FALSY;
// else if (res.never) return EvalResult.NEVER;
// else return EvalResult.FALSY_IMPURE;
// }
@Override public void compile(CompileResult target, boolean pollute) { @Override public void compile(CompileResult target, boolean pollute) {
if (value != null) value.compile(target, false); if (value != null) value.compile(target, false);
if (pollute) target.add(Instruction.pushUndefined()); if (pollute) target.add(Instruction.pushUndefined());

View File

@ -12,14 +12,6 @@ import me.topchetoeu.jscript.compilation.Node;
public class LazyAndNode extends Node { public class LazyAndNode extends Node {
public final Node first, second; public final Node first, second;
// @Override public EvalResult evaluate(CompileResult target) {
// var firstRes = first.evaluate(target);
// if (firstRes.falsy) return firstRes;
// if (!firstRes.isPure) return firstRes;
// return second.evaluate(target);
// }
@Override public void compile(CompileResult target, boolean pollute) { @Override public void compile(CompileResult target, boolean pollute) {
first.compile(target, true); first.compile(target, true);
if (pollute) target.add(Instruction.dup()); if (pollute) target.add(Instruction.dup());

View File

@ -13,14 +13,6 @@ import me.topchetoeu.jscript.compilation.Node;
public class LazyOrNode extends Node { public class LazyOrNode extends Node {
public final Node first, second; public final Node first, second;
// @Override public EvalResult evaluate(CompileResult target) {
// var firstRes = first.evaluate(target);
// if (firstRes.truthy) return firstRes;
// if (!firstRes.isPure) return firstRes;
// return second.evaluate(target);
// }
@Override public void compile(CompileResult target, boolean pollute) { @Override public void compile(CompileResult target, boolean pollute) {
first.compile(target, true); first.compile(target, true);
if (pollute) target.add(Instruction.dup()); if (pollute) target.add(Instruction.dup());

View File

@ -14,15 +14,6 @@ import me.topchetoeu.jscript.compilation.values.VariableNode;
public class TypeofNode extends Node { public class TypeofNode extends Node {
public final Node value; public final Node value;
// @Override public EvalResult evaluate(CompileResult target) {
// if (value instanceof VariableNode) {
// var i = target.scope.getKey(((VariableNode)value).name);
// if (i instanceof String) return EvalResult.NONE;
// }
// return EvalResult.UNKNOWN;
// }
@Override public void compile(CompileResult target, boolean pollute) { @Override public void compile(CompileResult target, boolean pollute) {
if (value instanceof VariableNode varNode) { if (value instanceof VariableNode varNode) {
target.add(VariableNode.toGet(target, varNode.loc(), varNode.name, () -> Instruction.typeof(varNode.name))); target.add(VariableNode.toGet(target, varNode.loc(), varNode.name, () -> Instruction.typeof(varNode.name)));