build: split up into multiple projects, use kotlin DLS
All checks were successful
tagged-release / Tagged Release (push) Successful in 5m23s

This commit is contained in:
2025-01-10 04:05:17 +02:00
parent 9668bccef1
commit d563fc4919
209 changed files with 459 additions and 266 deletions

View File

@@ -0,0 +1,22 @@
plugins {
id("common-java");
}
description = "A compiler of EcmaScript 5 code to J2S bytecode";
tasks.processResources {
filesMatching("metadata.json", {
expand(
"version" to properties["project_version"],
"name" to properties["project_name"],
);
});
}
tasks.test {
useJUnitPlatform();
}
dependencies {
implementation(project(":common"));
}

View File

@@ -0,0 +1,142 @@
package me.topchetoeu.j2s.compilation;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import me.topchetoeu.j2s.common.FunctionBody;
import me.topchetoeu.j2s.common.Instruction;
import me.topchetoeu.j2s.common.Instruction.BreakpointType;
import me.topchetoeu.j2s.common.environment.Environment;
import me.topchetoeu.j2s.common.environment.Key;
import me.topchetoeu.j2s.common.mapping.FunctionMap;
import me.topchetoeu.j2s.common.mapping.FunctionMap.FunctionMapBuilder;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.compilation.control.TryNode;
import me.topchetoeu.j2s.compilation.scope.FunctionScope;
import me.topchetoeu.j2s.compilation.scope.Variable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
public final class CompileResult {
public static final Key<Void> DEBUG_LOG = new Key<>();
public final List<Instruction> instructions;
public final List<CompileResult> children;
public final Map<FunctionNode, CompileResult> childrenMap = new HashMap<>();
public final Map<FunctionNode, Integer> childrenIndices = new HashMap<>();
public final FunctionMapBuilder map;
public final Environment env;
public int length;
public final FunctionScope scope;
public final Map<TryNode, Variable> catchBindings = new HashMap<>();
public int temp() {
instructions.add(null);
return instructions.size() - 1;
}
public CompileResult add(Instruction instr) {
instructions.add(instr);
return this;
}
public CompileResult set(int i, Instruction instr) {
instructions.set(i, instr);
return this;
}
public int size() { return instructions.size(); }
public void setDebug(Location loc, BreakpointType type) {
map.setDebug(loc, type);
}
public void setLocation(int i, Location loc) {
map.setLocation(i, loc);
}
public void setLocationAndDebug(int i, Location loc, BreakpointType type) {
map.setLocationAndDebug(i, loc, type);
}
public void setDebug(BreakpointType type) {
setDebug(map.last(), type);
}
public void setLocation(Location type) {
setLocation(instructions.size() - 1, type);
}
public void setLocationAndDebug(Location loc, BreakpointType type) {
setLocationAndDebug(instructions.size() - 1, loc, type);
}
public CompileResult addChild(FunctionNode node, CompileResult res) {
this.children.add(res);
this.childrenMap.put(node, res);
this.childrenIndices.put(node, this.children.size() - 1);
return res;
}
public Instruction[] instructions() {
return instructions.toArray(new Instruction[0]);
}
public FunctionMap map(Function<Location, Location> mapper) {
return map.map(mapper).build(scope.localNames(), scope.capturableNames(), scope.captureNames());
}
public FunctionMap map() {
return map.build(scope.localNames(), scope.capturableNames(), scope.captureNames());
}
public FunctionBody body() {
var builtChildren = new FunctionBody[children.size()];
for (var i = 0; i < children.size(); i++) builtChildren[i] = children.get(i).body();
var instrRes = instructions();
if (env.has(DEBUG_LOG)) {
System.out.println("================= BODY =================");
System.out.println("LOCALS: " + scope.localsCount());
System.out.println("CAPTURABLES: " + scope.capturablesCount());
System.out.println("CAPTURES: " + scope.capturesCount());
for (var instr : instrRes) System.out.println(instr);
}
return new FunctionBody(
scope.localsCount(), scope.capturablesCount(), scope.capturesCount(),
length, instrRes, builtChildren
);
}
public CompileResult subtarget() {
return new CompileResult(env, new FunctionScope(scope), this);
}
public CompileResult setEnvironment(Environment env) {
return new CompileResult(env, scope, this);
}
/**
* Returns a compile result with a child of the environment that relates to the given key.
* In essence, this is used to create a compile result which is back at the root environment of the compilation
*/
public CompileResult rootEnvironment(Key<Environment> env) {
return new CompileResult(this.env.get(env).child(), scope, this);
}
public CompileResult subEnvironment() {
return new CompileResult(env.child(), scope, this);
}
public CompileResult(Environment env, FunctionScope scope, int length) {
this.scope = scope;
this.instructions = new ArrayList<>();
this.children = new LinkedList<>();
this.map = FunctionMap.builder();
this.env = env;
this.length = length;
}
private CompileResult(Environment env, FunctionScope scope, CompileResult parent) {
this.scope = scope;
this.instructions = parent.instructions;
this.children = parent.children;
this.map = parent.map;
this.env = env;
}
}

View File

@@ -0,0 +1,112 @@
package me.topchetoeu.j2s.compilation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import me.topchetoeu.j2s.common.Instruction;
import me.topchetoeu.j2s.common.Instruction.BreakpointType;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.common.parsing.ParseRes;
import me.topchetoeu.j2s.common.parsing.Parsing;
import me.topchetoeu.j2s.common.parsing.Source;
public class CompoundNode extends Node {
public final Node[] statements;
public Location end;
@Override public void resolve(CompileResult target) {
for (var stm : statements) stm.resolve(target);
}
@Override public void compileFunctions(CompileResult target) {
for (var stm : statements) stm.compileFunctions(target);
}
public void compile(CompileResult target, boolean pollute, BreakpointType type) {
List<Node> statements = new ArrayList<Node>();
for (var stm : this.statements) {
if (stm instanceof FunctionStatementNode func) {
func.compile(target, false);
}
else statements.add(stm);
}
var polluted = false;
for (var i = 0; i < statements.size(); i++) {
var stm = statements.get(i);
if (i != statements.size() - 1) stm.compile(target, false, BreakpointType.STEP_OVER);
else stm.compile(target, polluted = pollute, BreakpointType.STEP_OVER);
}
if (!polluted && pollute) {
target.add(Instruction.pushUndefined());
}
}
public CompoundNode setEnd(Location loc) {
this.end = loc;
return this;
}
public CompoundNode(Location loc, Node ...statements) {
super(loc);
this.statements = statements;
}
public static ParseRes<CompoundNode> parseComma(Source src, int i, Node prev, int precedence) {
if (precedence > 1) return ParseRes.failed();
var n = Parsing.skipEmpty(src, i);
var loc = src.loc(i + n);
if (!src.is(i + n, ",")) return ParseRes.failed();
n++;
var curr = JavaScript.parseExpression(src, i + n, 2);
if (!curr.isSuccess()) return curr.chainError(src.loc(i + n), "Expected a value after the comma");
n += curr.n;
if (prev instanceof CompoundNode comp) {
var children = new ArrayList<Node>();
children.addAll(Arrays.asList(comp.statements));
children.add(curr.result);
return ParseRes.res(new CompoundNode(loc, children.toArray(new Node[0])), n);
}
else return ParseRes.res(new CompoundNode(loc, prev, curr.result), n);
}
public static ParseRes<CompoundNode> parse(Source src, int i) {
var n = Parsing.skipEmpty(src, i);
var loc = src.loc(i + n);
if (!src.is(i + n, "{")) return ParseRes.failed();
n++;
var statements = new ArrayList<Node>();
while (true) {
n += Parsing.skipEmpty(src, i + n);
if (src.is(i + n, "}")) {
n++;
break;
}
if (src.is(i + n, ";")) {
n++;
continue;
}
var res = JavaScript.parseStatement(src, i + n);
if (!res.isSuccess()) return res.chainError(src.loc(i + n), "Expected a statement");
n += res.n;
statements.add(res.result);
}
return ParseRes.res(new CompoundNode(loc, statements.toArray(new Node[0])).setEnd(src.loc(i + n - 1)), n);
}
}

View File

@@ -0,0 +1,19 @@
package me.topchetoeu.j2s.compilation;
import java.util.function.IntSupplier;
public final class DeferredIntSupplier implements IntSupplier {
private int value;
private boolean set;
public void set(int val) {
if (set) throw new RuntimeException("A deferred int supplier may be set only once");
value = val;
set = true;
}
@Override public int getAsInt() {
if (!set) throw new RuntimeException("Deferred int supplier accessed too early");
return value;
}
}

View File

@@ -0,0 +1,125 @@
package me.topchetoeu.j2s.compilation;
import java.util.List;
import me.topchetoeu.j2s.common.Instruction;
import me.topchetoeu.j2s.common.Instruction.BreakpointType;
import me.topchetoeu.j2s.common.environment.Environment;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.common.parsing.ParseRes;
import me.topchetoeu.j2s.common.parsing.Parsing;
import me.topchetoeu.j2s.common.parsing.Source;
import me.topchetoeu.j2s.compilation.scope.FunctionScope;
import me.topchetoeu.j2s.compilation.values.VariableNode;
public abstract class FunctionNode extends Node {
public final CompoundNode body;
public final List<VariableNode> params;
public final Location end;
public abstract String name();
public final String name(String fallback) {
return this.name() != null ? this.name() : fallback;
}
protected final int[] captures(CompileResult target) {
return target.childrenMap.get(this).scope.getCaptureIndices();
}
protected final Environment rootEnv(Environment env) {
return env.get(JavaScript.COMPILE_ROOT);
}
@Override public void resolve(CompileResult target) { }
public final CompileResult compileBody(Environment env, FunctionScope scope, boolean lastReturn, String selfName) {
var target = new CompileResult(env, scope, params.size());
var i = 0;
body.resolve(target);
for (var param : params) scope.define(param.name);
var hasSelf = false;
if (selfName != null && !scope.has(selfName, false)) {
hasSelf = true;
scope.define(selfName);
}
body.compileFunctions(target);
for (var param : params) {
target.add(Instruction.loadArg(i++)).setLocation(param.loc());
target.add(scope.define(param.name).index().toSet(false)).setLocation(param.loc());
}
if (hasSelf) {
target.add(Instruction.loadCalled());
target.add(scope.define(selfName).index().toSet(false));
}
body.compile(target, lastReturn, BreakpointType.NONE);
return target;
}
public final CompileResult compileBody(CompileResult parent, String selfName) {
return compileBody(rootEnv(parent.env).child(), new FunctionScope(parent.scope), false, selfName);
}
public abstract void compile(CompileResult target, boolean pollute, String name, BreakpointType bp);
public void compile(CompileResult target, boolean pollute, String name) {
compile(target, pollute, name, BreakpointType.NONE);
}
@Override public void compile(CompileResult target, boolean pollute, BreakpointType bp) {
compile(target, pollute, (String)null, bp);
}
@Override public void compile(CompileResult target, boolean pollute) {
compile(target, pollute, (String)null, BreakpointType.NONE);
}
public FunctionNode(Location loc, Location end, List<VariableNode> params, CompoundNode body) {
super(loc);
this.end = end;
this.params = params;
this.body = body;
}
public static void compileWithName(Node stm, CompileResult target, boolean pollute, String name) {
if (stm instanceof FunctionNode) ((FunctionNode)stm).compile(target, pollute, name);
else stm.compile(target, pollute);
}
public static void compileWithName(Node stm, CompileResult target, boolean pollute, String name, BreakpointType bp) {
if (stm instanceof FunctionNode) ((FunctionNode)stm).compile(target, pollute, name, bp);
else stm.compile(target, pollute, bp);
}
public static ParseRes<FunctionNode> parseFunction(Source src, int i, boolean statement) {
var n = Parsing.skipEmpty(src, i);
var loc = src.loc(i + n);
if (!Parsing.isIdentifier(src, i + n, "function")) return ParseRes.failed();
n += 8;
var name = Parsing.parseIdentifier(src, i + n);
if (!name.isSuccess() && statement) return ParseRes.error(src.loc(i + n), "A statement function requires a name");
n += name.n;
n += Parsing.skipEmpty(src, i + n);
var params = JavaScript.parseParameters(src, i + n);
if (!params.isSuccess()) return params.chainError(src.loc(i + n), "Expected a parameter list");
n += params.n;
var body = CompoundNode.parse(src, i + n);
if (!body.isSuccess()) return body.chainError(src.loc(i + n), "Expected a compound statement for function");
n += body.n;
if (statement) return ParseRes.res(new FunctionStatementNode(
loc, src.loc(i + n - 1),
params.result, body.result, name.result
), n);
else return ParseRes.res(new FunctionValueNode(
loc, src.loc(i + n - 1),
params.result, body.result, name.result
), n);
}
}

View File

@@ -0,0 +1,33 @@
package me.topchetoeu.j2s.compilation;
import java.util.List;
import me.topchetoeu.j2s.common.Instruction;
import me.topchetoeu.j2s.common.Instruction.BreakpointType;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.compilation.scope.Variable;
import me.topchetoeu.j2s.compilation.values.VariableNode;
public class FunctionStatementNode extends FunctionNode {
public final String name;
@Override public String name() { return name; }
@Override public void resolve(CompileResult target) {
target.scope.define(new Variable(name, false));
}
@Override public void compileFunctions(CompileResult target) {
target.addChild(this, compileBody(target, name()));
}
@Override public void compile(CompileResult target, boolean pollute, String name, BreakpointType bp) {
target.add(Instruction.loadFunc(target.childrenIndices.get(this), name(name), captures(target))).setLocation(loc());
target.add(VariableNode.toSet(target, end, this.name, false, true)).setLocation(loc());
if (pollute) target.add(Instruction.pushUndefined());
}
public FunctionStatementNode(Location loc, Location end, List<VariableNode> params, CompoundNode body, String name) {
super(loc, end, params, body);
this.name = name;
}
}

View File

@@ -0,0 +1,28 @@
package me.topchetoeu.j2s.compilation;
import java.util.List;
import me.topchetoeu.j2s.common.Instruction;
import me.topchetoeu.j2s.common.Instruction.BreakpointType;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.compilation.values.VariableNode;
public class FunctionValueNode extends FunctionNode {
public final String name;
@Override public String name() { return name; }
@Override public void compileFunctions(CompileResult target) {
target.addChild(this, compileBody(target, name()));
}
@Override public void compile(CompileResult target, boolean pollute, String name, BreakpointType bp) {
target.add(Instruction.loadFunc(target.childrenIndices.get(this), name(name), captures(target))).setLocation(loc());
if (!pollute) target.add(Instruction.discard());
}
public FunctionValueNode(Location loc, Location end, List<VariableNode> params, CompoundNode body, String name) {
super(loc, end, params, body);
this.name = name;
}
}

View File

@@ -0,0 +1,325 @@
package me.topchetoeu.j2s.compilation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import me.topchetoeu.j2s.common.SyntaxException;
import me.topchetoeu.j2s.common.environment.Environment;
import me.topchetoeu.j2s.common.environment.Key;
import me.topchetoeu.j2s.common.parsing.Filename;
import me.topchetoeu.j2s.common.parsing.ParseRes;
import me.topchetoeu.j2s.common.parsing.Parsing;
import me.topchetoeu.j2s.common.parsing.Source;
import me.topchetoeu.j2s.compilation.control.BreakNode;
import me.topchetoeu.j2s.compilation.control.ContinueNode;
import me.topchetoeu.j2s.compilation.control.DebugNode;
import me.topchetoeu.j2s.compilation.control.DeleteNode;
import me.topchetoeu.j2s.compilation.control.DoWhileNode;
import me.topchetoeu.j2s.compilation.control.ForInNode;
import me.topchetoeu.j2s.compilation.control.ForNode;
import me.topchetoeu.j2s.compilation.control.IfNode;
import me.topchetoeu.j2s.compilation.control.ReturnNode;
import me.topchetoeu.j2s.compilation.control.SwitchNode;
import me.topchetoeu.j2s.compilation.control.ThrowNode;
import me.topchetoeu.j2s.compilation.control.TryNode;
import me.topchetoeu.j2s.compilation.control.WhileNode;
import me.topchetoeu.j2s.compilation.scope.FunctionScope;
import me.topchetoeu.j2s.compilation.values.ArgumentsNode;
import me.topchetoeu.j2s.compilation.values.ArrayNode;
import me.topchetoeu.j2s.compilation.values.GlobalThisNode;
import me.topchetoeu.j2s.compilation.values.ObjectNode;
import me.topchetoeu.j2s.compilation.values.RegexNode;
import me.topchetoeu.j2s.compilation.values.ThisNode;
import me.topchetoeu.j2s.compilation.values.VariableNode;
import me.topchetoeu.j2s.compilation.values.constants.BoolNode;
import me.topchetoeu.j2s.compilation.values.constants.NullNode;
import me.topchetoeu.j2s.compilation.values.constants.NumberNode;
import me.topchetoeu.j2s.compilation.values.constants.StringNode;
import me.topchetoeu.j2s.compilation.values.operations.CallNode;
import me.topchetoeu.j2s.compilation.values.operations.ChangeNode;
import me.topchetoeu.j2s.compilation.values.operations.DiscardNode;
import me.topchetoeu.j2s.compilation.values.operations.IndexNode;
import me.topchetoeu.j2s.compilation.values.operations.OperationNode;
import me.topchetoeu.j2s.compilation.values.operations.PostfixNode;
import me.topchetoeu.j2s.compilation.values.operations.TypeofNode;
public final class JavaScript {
public static enum DeclarationType {
@Deprecated
VAR;
}
public static final Key<Environment> COMPILE_ROOT = new Key<>();
static final Set<String> reserved = new HashSet<>(Arrays.asList(
"true", "false", "void", "null", "this", "if", "else", "try", "catch",
"finally", "for", "do", "while", "switch", "case", "default", "new",
"function", "var", "return", "throw", "typeof", "delete", "break",
"continue", "debugger", "implements", "interface", "package", "private",
"protected", "public", "static", "arguments", "class", "extends"
));
public static ParseRes<? extends Node> parseParens(Source src, int i) {
int n = 0;
var openParen = Parsing.parseOperator(src, i + n, "(");
if (!openParen.isSuccess()) return openParen.chainError();
n += openParen.n;
var res = JavaScript.parseExpression(src, i + n, 0);
if (!res.isSuccess()) return res.chainError(src.loc(i + n), "Expected an expression in parens");
n += res.n;
var closeParen = Parsing.parseOperator(src, i + n, ")");
if (!closeParen.isSuccess()) return closeParen.chainError(src.loc(i + n), "Expected a closing paren");
n += closeParen.n;
return ParseRes.res(res.result, n);
}
public static ParseRes<? extends Node> parseSimple(Source src, int i, boolean statement) {
return ParseRes.first(src, i,
(s, j) -> statement ? ParseRes.failed() : ObjectNode.parse(s, j),
(s, j) -> statement ? ParseRes.failed() : FunctionNode.parseFunction(s, j, false),
JavaScript::parseLiteral,
StringNode::parse,
RegexNode::parse,
NumberNode::parse,
ChangeNode::parsePrefixDecrease,
ChangeNode::parsePrefixIncrease,
OperationNode::parsePrefix,
ArrayNode::parse,
JavaScript::parseParens,
CallNode::parseNew,
TypeofNode::parse,
DiscardNode::parse,
DeleteNode::parse,
VariableNode::parse
);
}
public static ParseRes<? extends Node> parseLiteral(Source src, int i) {
var n = Parsing.skipEmpty(src, i);
var loc = src.loc(i + n);
var id = Parsing.parseIdentifier(src, i);
if (!id.isSuccess()) return id.chainError();
n += id.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("null")) return ParseRes.res(new NullNode(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("globalThis")) return ParseRes.res(new GlobalThisNode(loc), n);
return ParseRes.failed();
}
public static ParseRes<Node> parseExpression(Source src, int i, int precedence, boolean statement) {
var n = Parsing.skipEmpty(src, i);
Node prev = null;
while (true) {
if (prev == null) {
var res = parseSimple(src, i + n, statement);
if (res.isSuccess()) {
n += res.n;
prev = res.result;
}
else if (res.isError()) return res.chainError();
else break;
}
else {
var _prev = prev;
ParseRes<Node> res = ParseRes.first(src, i + n,
(s, j) -> OperationNode.parseInstanceof(s, j, _prev, precedence),
(s, j) -> OperationNode.parseIn(s, j, _prev, precedence),
(s, j) -> PostfixNode.parsePostfixIncrease(s, j, _prev, precedence),
(s, j) -> PostfixNode.parsePostfixDecrease(s, j, _prev, precedence),
(s, j) -> OperationNode.parseOperator(s, j, _prev, precedence),
(s, j) -> IfNode.parseTernary(s, j, _prev, precedence),
(s, j) -> IndexNode.parseMember(s, j, _prev, precedence),
(s, j) -> IndexNode.parseIndex(s, j, _prev, precedence),
(s, j) -> CallNode.parseCall(s, j, _prev, precedence),
(s, j) -> CompoundNode.parseComma(s, j, _prev, precedence)
);
if (res.isSuccess()) {
n += res.n;
prev = res.result;
continue;
}
else if (res.isError()) return res.chainError();
break;
}
}
if (prev == null) return ParseRes.failed();
else return ParseRes.res(prev, n);
}
public static ParseRes<Node> parseExpression(Source src, int i, int precedence) {
return parseExpression(src, i, precedence, false);
}
public static ParseRes<Node> parseExpressionStatement(Source src, int i) {
var res = parseExpression(src, i, 0, true);
if (!res.isSuccess()) return res.chainError();
var end = JavaScript.parseStatementEnd(src, i + res.n);
if (!end.isSuccess()) return ParseRes.error(src.loc(i + res.n), "Expected an end of statement");
return res.addN(end.n);
}
public static ParseRes<Node> parseStatement(Source src, int i) {
var n = Parsing.skipEmpty(src, i);
if (src.is(i + n, ";")) return ParseRes.res(new DiscardNode(src.loc(i+ n), null), n + 1);
if (Parsing.isIdentifier(src, i + n, "with")) return ParseRes.error(src.loc(i + n), "'with' statements are not allowed.");
ParseRes<Node> res = ParseRes.first(src, i + n,
VariableDeclareNode::parse,
ReturnNode::parse,
ThrowNode::parse,
ContinueNode::parse,
BreakNode::parse,
DebugNode::parse,
IfNode::parse,
WhileNode::parse,
SwitchNode::parse,
ForNode::parse,
ForInNode::parse,
DoWhileNode::parse,
TryNode::parse,
CompoundNode::parse,
(s, j) -> FunctionNode.parseFunction(s, j, true),
JavaScript::parseExpressionStatement
);
return res.addN(n);
}
public static ParseRes<Boolean> parseStatementEnd(Source src, int i) {
var n = Parsing.skipEmpty(src, i);
if (i + n >= src.size()) return ParseRes.res(true, n);
for (var j = i; j < i + n; j++) {
if (src.is(j, '\n')) return ParseRes.res(true, n);
}
if (src.is(i + n, ';')) return ParseRes.res(true, n + 1);
if (src.is(i + n, '}')) return ParseRes.res(true, n);
return ParseRes.failed();
}
public static ParseRes<Boolean> parseDeclarationType(Source src, int i) {
var res = Parsing.parseIdentifier(src, i);
if (!res.isSuccess()) return res.chainError();
if (res.result.equals("var")) return ParseRes.res(true, res.n);
return ParseRes.failed();
}
public static Node[] parse(Environment env, Filename filename, String raw) {
var src = new Source(env, filename, raw);
var list = new ArrayList<Node>();
int i = 0;
while (true) {
i += Parsing.skipEmpty(src, i);
if (i >= src.size()) break;
var res = parseStatement(src, i);
if (res.isError()) throw new SyntaxException(res.errorLocation, res.error);
else if (res.isFailed()) throw new SyntaxException(src.loc(i), "Unexpected syntax");
i += res.n;
i += Parsing.skipEmpty(src, i);
list.add(res.result);
}
return list.toArray(new Node[0]);
}
public static boolean checkVarName(String name) {
return !JavaScript.reserved.contains(name);
}
public static CompileResult compile(Environment env, boolean passthrough, Node ...statements) {
env = env.child();
env.add(COMPILE_ROOT, env);
var func = new FunctionValueNode(null, null, Arrays.asList(), new CompoundNode(null, statements), null);
var res = func.compileBody(env, new FunctionScope(passthrough), true, null);
return res;
}
public static CompileResult compile(Environment env, Filename filename, String raw, boolean passthrough) {
return JavaScript.compile(env, passthrough, JavaScript.parse(env, filename, raw));
}
public static CompileResult compile(Filename filename, String raw, boolean passthrough) {
var env = new Environment();
return JavaScript.compile(env, passthrough, JavaScript.parse(env, filename, raw));
}
public static ParseRes<String> parseLabel(Source src, int i) {
int n = Parsing.skipEmpty(src, i);
var nameRes = Parsing.parseIdentifier(src, i + n);
if (!nameRes.isSuccess()) return nameRes.chainError();
n += nameRes.n;
n += Parsing.skipEmpty(src, i + n);
if (!src.is(i + n, ":")) return ParseRes.failed();
n++;
return ParseRes.res(nameRes.result, n);
}
public static ParseRes<List<VariableNode>> parseParameters(Source src, int i) {
var n = Parsing.skipEmpty(src, i);
var openParen = Parsing.parseOperator(src, i + n, "(");
if (!openParen.isSuccess()) return openParen.chainError(src.loc(i + n), "Expected a parameter list");
n += openParen.n;
var params = new ArrayList<VariableNode>();
var closeParen = Parsing.parseOperator(src, i + n, ")");
n += closeParen.n;
if (!closeParen.isSuccess()) {
while (true) {
n += Parsing.skipEmpty(src, i + n);
var param = VariableNode.parse(src, i + n);
if (!param.isSuccess()) return ParseRes.error(src.loc(i + n), "Expected a parameter or a closing brace");
n += param.n;
n += Parsing.skipEmpty(src, i + n);
params.add(param.result);
if (src.is(i + n, ",")) {
n++;
n += Parsing.skipEmpty(src, i + n);
}
if (src.is(i + n, ")")) {
n++;
break;
}
}
}
return ParseRes.res(params, n);
}
}

View File

@@ -0,0 +1,120 @@
package me.topchetoeu.j2s.compilation;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.function.IntSupplier;
import me.topchetoeu.j2s.common.Instruction;
import me.topchetoeu.j2s.common.SyntaxException;
import me.topchetoeu.j2s.common.environment.Environment;
import me.topchetoeu.j2s.common.environment.Key;
import me.topchetoeu.j2s.common.parsing.Location;
public class LabelContext {
public static final Key<LabelContext> BREAK_CTX = new Key<>();
public static final Key<LabelContext> CONTINUE_CTX = new Key<>();
private final LinkedList<IntSupplier> list = new LinkedList<>();
private final HashMap<String, IntSupplier> map = new HashMap<>();
private final LinkedList<ArrayList<Runnable>> deferredList = new LinkedList<>();
private final HashMap<String, ArrayList<Runnable>> deferredMap = new HashMap<>();
public IntSupplier get() {
return list.peekLast();
}
public IntSupplier get(String name) {
return map.get(name);
}
public void flushAdders(String name) {
for (var adder : deferredList.peek()) {
adder.run();
}
deferredList.pop();
if (name != null) {
var adders = deferredMap.remove(name);
if (adders != null) {
for (var adder : adders) adder.run();
}
}
}
public boolean jump(CompileResult target) {
var res = get();
if (res != null) {
var tmp = target.temp();
this.deferredList.peek().add(() -> target.set(tmp, Instruction.jmp(res.getAsInt() - tmp)));
return true;
}
else return false;
}
public boolean jump(CompileResult target, String name) {
var res = name == null ? get() : get(name);
if (res != null) {
var tmp = target.temp();
Runnable task = () -> target.set(tmp, Instruction.jmp(res.getAsInt() - tmp));
if (name == null) this.deferredList.peekLast().add(task);
else if (deferredMap.containsKey(name)) this.deferredMap.get(name).add(task);
else return false;
return true;
}
else return false;
}
public void push(IntSupplier jumpTarget) {
list.add(jumpTarget);
}
public void push(Location loc, String name, IntSupplier jumpTarget) {
if (name == null) return;
if (map.containsKey(name)) throw new SyntaxException(loc, String.format("Label '%s' has already been declared", name));
map.put(name, jumpTarget);
}
public void pushLoop(Location loc, String name, IntSupplier jumpTarget) {
push(jumpTarget);
push(loc, name, jumpTarget);
deferredList.push(new ArrayList<>());
if (name != null) deferredMap.put(name, new ArrayList<>());
}
public void pop() {
list.removeLast();
}
public void pop(String name) {
if (name == null) return;
map.remove(name);
}
public void popLoop(String name) {
pop();
pop(name);
flushAdders(name);
}
public static LabelContext getBreak(Environment env) {
return env.initFrom(BREAK_CTX, () -> new LabelContext());
}
public static LabelContext getCont(Environment env) {
return env.initFrom(CONTINUE_CTX, () -> new LabelContext());
}
public static void pushLoop(Environment env, Location loc, String name, IntSupplier breakTarget, int contTarget) {
LabelContext.getBreak(env).pushLoop(loc, name, breakTarget);
LabelContext.getCont(env).pushLoop(loc, name, () -> contTarget);
}
public static void pushLoop(Environment env, Location loc, String name, IntSupplier breakTarget, IntSupplier contTarget) {
LabelContext.getBreak(env).pushLoop(loc, name, breakTarget);
LabelContext.getCont(env).pushLoop(loc, name, contTarget);
}
public static void popLoop(Environment env, String name) {
LabelContext.getBreak(env).popLoop(name);
LabelContext.getCont(env).popLoop(name);
}
}

View File

@@ -0,0 +1,28 @@
package me.topchetoeu.j2s.compilation;
import me.topchetoeu.j2s.common.Instruction.BreakpointType;
import me.topchetoeu.j2s.common.parsing.Location;
public abstract class Node {
private Location loc;
public void resolve(CompileResult target) {}
public void compile(CompileResult target, boolean pollute, BreakpointType type) {
int start = target.size();
compile(target, pollute);
if (target.size() != start) target.setLocationAndDebug(start, loc(), type);
}
public void compile(CompileResult target, boolean pollute) {
compile(target, pollute, BreakpointType.NONE);
}
public abstract void compileFunctions(CompileResult target);
public Location loc() { return loc; }
public void setLoc(Location loc) { this.loc = loc; }
protected Node(Location loc) {
this.loc = loc;
}
}

View File

@@ -0,0 +1,93 @@
package me.topchetoeu.j2s.compilation;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.function.Function;
public final class NodeChildren implements Iterable<Node> {
public static final class Slot {
private Node node;
private final Function<Node, Node> replacer;
public final void replace(Node node) {
this.node = this.replacer.apply(node);
}
public Slot(Node nodes, Function<Node, Node> replacer) {
this.node = nodes;
this.replacer = replacer;
}
}
private final Slot[] slots;
private NodeChildren(Slot[] slots) {
this.slots = slots;
}
@Override public Iterator<Node> iterator() {
return new Iterator<Node>() {
private int i = 0;
private Slot[] arr = slots;
@Override public boolean hasNext() {
if (arr == null) return false;
else if (i >= arr.length) {
arr = null;
return false;
}
else return true;
}
@Override public Node next() {
if (!hasNext()) return null;
return arr[i++].node;
}
};
}
public Iterable<Slot> slots() {
return () -> new Iterator<Slot>() {
private int i = 0;
private Slot[] arr = slots;
@Override public boolean hasNext() {
if (arr == null) return false;
else if (i >= arr.length) {
arr = null;
return false;
}
else return true;
}
@Override public Slot next() {
if (!hasNext()) return null;
return arr[i++];
}
};
}
public static final class Builder {
private final ArrayList<Slot> slots = new ArrayList<>();
public final Builder add(Slot ...children) {
for (var child : children) {
this.slots.add(child);
}
return this;
}
public final Builder add(Iterable<Slot> children) {
for (var child : children) {
this.slots.add(child);
}
return this;
}
public final Builder add(Node child, Function<Node, Node> replacer) {
slots.add(new Slot(child, replacer));
return this;
}
public final NodeChildren build() {
return new NodeChildren(slots.toArray(new Slot[0]));
}
}
}

View File

@@ -0,0 +1,15 @@
package me.topchetoeu.j2s.compilation;
import me.topchetoeu.j2s.common.parsing.Location;
public final class Parameter {
public final Location loc;
public final String name;
public final Node node;
public Parameter(Location loc, String name, Node node) {
this.name = name;
this.node = node;
this.loc = loc;
}
}

View File

@@ -0,0 +1,115 @@
package me.topchetoeu.j2s.compilation;
import java.util.ArrayList;
import java.util.List;
import com.github.bsideup.jabel.Desugar;
import me.topchetoeu.j2s.common.Instruction;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.common.parsing.ParseRes;
import me.topchetoeu.j2s.common.parsing.Parsing;
import me.topchetoeu.j2s.common.parsing.Source;
import me.topchetoeu.j2s.compilation.values.VariableNode;
public class VariableDeclareNode extends Node {
@Desugar
public static record Pair(VariableNode var, Node value) { }
public final List<Pair> values;
@Override public void resolve(CompileResult target) {
for (var entry : values) {
target.scope.define(entry.var.name);
}
}
@Override public void compileFunctions(CompileResult target) {
for (var pair : values) {
if (pair.value != null) pair.value.compileFunctions(target);
}
}
@Override public void compile(CompileResult target, boolean pollute) {
for (var entry : values) {
var index = target.scope.get(entry.var.name, false);
if (entry.value != null) {
entry.value.compile(target, true);
}
if (index == null) {
if (entry.value == null) {
target.add(Instruction.globDef(entry.var.name));
}
else {
target.add(Instruction.globSet(entry.var.name, false, true));
}
}
else if (entry.value != null) {
target.add(index.index().toSet(false));
}
}
if (pollute) target.add(Instruction.pushUndefined());
}
public VariableDeclareNode(Location loc, List<Pair> values) {
super(loc);
this.values = values;
}
public static ParseRes<VariableDeclareNode> parse(Source src, int i) {
var n = Parsing.skipEmpty(src, i);
var loc = src.loc(i + n);
var declType = JavaScript.parseDeclarationType(src, i + n);
if (!declType.isSuccess()) return declType.chainError();
n += declType.n;
var res = new ArrayList<Pair>();
var end = JavaScript.parseStatementEnd(src, i + n);
if (end.isSuccess()) {
n += end.n;
return ParseRes.res(new VariableDeclareNode(loc, res), n);
}
while (true) {
var nameLoc = src.loc(i + n);
var name = Parsing.parseIdentifier(src, i + n);
if (!name.isSuccess()) return name.chainError(nameLoc, "Expected a variable name");
n += name.n;
Node val = null;
var endN = n;
n += Parsing.skipEmpty(src, i + n);
if (src.is(i + n, "=")) {
n++;
var valRes = JavaScript.parseExpression(src, i + n, 2);
if (!valRes.isSuccess()) return valRes.chainError(src.loc(i + n), "Expected a value after '='");
n += valRes.n;
endN = n;
n += Parsing.skipEmpty(src, i + n);
val = valRes.result;
}
res.add(new Pair(new VariableNode(nameLoc, name.result), val));
if (src.is(i + n, ",")) {
n++;
continue;
}
end = JavaScript.parseStatementEnd(src, i + endN);
if (end.isSuccess()) {
n += end.n + endN - n;
return ParseRes.res(new VariableDeclareNode(loc, res), n);
}
else return end.chainError(src.loc(i + n), "Expected a comma or end of statement");
}
}
}

View File

@@ -0,0 +1,58 @@
package me.topchetoeu.j2s.compilation.control;
import me.topchetoeu.j2s.common.Instruction;
import me.topchetoeu.j2s.common.SyntaxException;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.common.parsing.ParseRes;
import me.topchetoeu.j2s.common.parsing.Parsing;
import me.topchetoeu.j2s.common.parsing.Source;
import me.topchetoeu.j2s.compilation.CompileResult;
import me.topchetoeu.j2s.compilation.JavaScript;
import me.topchetoeu.j2s.compilation.LabelContext;
import me.topchetoeu.j2s.compilation.Node;
public class BreakNode extends Node {
public final String label;
@Override public void compileFunctions(CompileResult target) {
}
@Override public void compile(CompileResult target, boolean pollute) {
if (!LabelContext.getBreak(target.env).jump(target, label)) {
if (label != null) throw new SyntaxException(loc(), String.format("Undefined label '%s'", label));
else throw new SyntaxException(loc(), "Illegal break statement");
}
if (pollute) target.add(Instruction.pushUndefined());
}
public BreakNode(Location loc, String label) {
super(loc);
this.label = label;
}
public static ParseRes<BreakNode> parse(Source src, int i) {
var n = Parsing.skipEmpty(src, i);
var loc = src.loc(i + n);
if (!Parsing.isIdentifier(src, i + n, "break")) return ParseRes.failed();
n += 5;
var end = JavaScript.parseStatementEnd(src, i + n);
if (end.isSuccess()) {
n += end.n;
return ParseRes.res(new BreakNode(loc, null), n);
}
var label = Parsing.parseIdentifier(src, i + n);
if (label.isFailed()) return ParseRes.error(src.loc(i + n), "Expected a label name or an end of statement");
n += label.n;
end = JavaScript.parseStatementEnd(src, i + n);
if (end.isSuccess()) {
n += end.n;
return ParseRes.res(new BreakNode(loc, label.result), n);
}
else return end.chainError(src.loc(i + n), "Expected end of statement");
}
}

View File

@@ -0,0 +1,58 @@
package me.topchetoeu.j2s.compilation.control;
import me.topchetoeu.j2s.common.Instruction;
import me.topchetoeu.j2s.common.SyntaxException;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.common.parsing.ParseRes;
import me.topchetoeu.j2s.common.parsing.Parsing;
import me.topchetoeu.j2s.common.parsing.Source;
import me.topchetoeu.j2s.compilation.CompileResult;
import me.topchetoeu.j2s.compilation.JavaScript;
import me.topchetoeu.j2s.compilation.LabelContext;
import me.topchetoeu.j2s.compilation.Node;
public class ContinueNode extends Node {
public final String label;
@Override public void compileFunctions(CompileResult target) {
}
@Override public void compile(CompileResult target, boolean pollute) {
if (!LabelContext.getCont(target.env).jump(target)) {
if (label != null) throw new SyntaxException(loc(), String.format("Undefined label '%s'", label));
else throw new SyntaxException(loc(), "Illegal continue statement");
}
if (pollute) target.add(Instruction.pushUndefined());
}
public ContinueNode(Location loc, String label) {
super(loc);
this.label = label;
}
public static ParseRes<ContinueNode> parse(Source src, int i) {
var n = Parsing.skipEmpty(src, i);
var loc = src.loc(i + n);
if (!Parsing.isIdentifier(src, i + n, "continue")) return ParseRes.failed();
n += 8;
var end = JavaScript.parseStatementEnd(src, i + n);
if (end.isSuccess()) {
n += end.n;
return ParseRes.res(new ContinueNode(loc, null), n);
}
var label = Parsing.parseIdentifier(src, i + n);
if (label.isFailed()) return ParseRes.error(src.loc(i + n), "Expected a label name or an end of statement");
n += label.n;
end = JavaScript.parseStatementEnd(src, i + n);
if (end.isSuccess()) {
n += end.n;
return ParseRes.res(new ContinueNode(loc, label.result), n);
}
else return end.chainError(src.loc(i + n), "Expected end of statement");
}
}

View File

@@ -0,0 +1,40 @@
package me.topchetoeu.j2s.compilation.control;
import me.topchetoeu.j2s.common.Instruction;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.common.parsing.ParseRes;
import me.topchetoeu.j2s.common.parsing.Parsing;
import me.topchetoeu.j2s.common.parsing.Source;
import me.topchetoeu.j2s.compilation.CompileResult;
import me.topchetoeu.j2s.compilation.JavaScript;
import me.topchetoeu.j2s.compilation.Node;
public class DebugNode extends Node {
@Override public void compileFunctions(CompileResult target) {
}
@Override public void compile(CompileResult target, boolean pollute) {
target.add(Instruction.debug());
if (pollute) target.add(Instruction.pushUndefined());
}
public DebugNode(Location loc) {
super(loc);
}
public static ParseRes<DebugNode> parse(Source src, int i) {
var n = Parsing.skipEmpty(src, i);
var loc = src.loc(i + n);
if (!Parsing.isIdentifier(src, i + n, "debugger")) return ParseRes.failed();
n += 8;
var end = JavaScript.parseStatementEnd(src, i + n);
if (end.isSuccess()) {
n += end.n;
return ParseRes.res(new DebugNode(loc), n);
}
else return end.chainError(src.loc(i + n), "Expected end of statement");
}
}

View File

@@ -0,0 +1,57 @@
package me.topchetoeu.j2s.compilation.control;
import me.topchetoeu.j2s.common.Instruction;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.common.parsing.ParseRes;
import me.topchetoeu.j2s.common.parsing.Parsing;
import me.topchetoeu.j2s.common.parsing.Source;
import me.topchetoeu.j2s.compilation.CompileResult;
import me.topchetoeu.j2s.compilation.JavaScript;
import me.topchetoeu.j2s.compilation.Node;
import me.topchetoeu.j2s.compilation.values.VariableNode;
import me.topchetoeu.j2s.compilation.values.constants.BoolNode;
import me.topchetoeu.j2s.compilation.values.operations.IndexNode;
public class DeleteNode extends Node {
public final Node key;
public final Node value;
@Override public void compileFunctions(CompileResult target) {
key.compileFunctions(target);
value.compileFunctions(target);
}
@Override public void compile(CompileResult target, boolean pollute) {
value.compile(target, true);
key.compile(target, true);
target.add(Instruction.delete());
if (pollute) target.add(Instruction.pushValue(true));
}
public static ParseRes<? extends Node> parse(Source src, int i) {
var n = Parsing.skipEmpty(src, i);
var loc = src.loc(i + n);
if (!Parsing.isIdentifier(src, i + n, "delete")) return ParseRes.failed();
n += 6;
var valRes = JavaScript.parseExpression(src, i + n, 15);
if (!valRes.isSuccess()) return valRes.chainError(src.loc(i + n), "Expected a value after 'delete'");
n += valRes.n;
if (valRes.result instanceof IndexNode) {
var index = (IndexNode)valRes.result;
return ParseRes.res(new DeleteNode(loc, index.index, index.object), n);
}
else if (valRes.result instanceof VariableNode) {
return ParseRes.error(src.loc(i + n), "A variable may not be deleted");
}
else return ParseRes.res(new BoolNode(loc, true), n);
}
public DeleteNode(Location loc, Node key, Node value) {
super(loc);
this.key = key;
this.value = value;
}
}

View File

@@ -0,0 +1,88 @@
package me.topchetoeu.j2s.compilation.control;
import me.topchetoeu.j2s.common.Instruction;
import me.topchetoeu.j2s.common.Instruction.BreakpointType;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.common.parsing.ParseRes;
import me.topchetoeu.j2s.common.parsing.Parsing;
import me.topchetoeu.j2s.common.parsing.Source;
import me.topchetoeu.j2s.compilation.CompileResult;
import me.topchetoeu.j2s.compilation.DeferredIntSupplier;
import me.topchetoeu.j2s.compilation.JavaScript;
import me.topchetoeu.j2s.compilation.LabelContext;
import me.topchetoeu.j2s.compilation.Node;
public class DoWhileNode extends Node {
public final Node condition, body;
public final String label;
@Override public void compileFunctions(CompileResult target) {
condition.compileFunctions(target);
body.compileFunctions(target);
}
@Override public void resolve(CompileResult target) {
body.resolve(target);
}
@Override public void compile(CompileResult target, boolean pollute) {
int start = target.size();
var end = new DeferredIntSupplier();
LabelContext.pushLoop(target.env, loc(), label, end, start);
body.compile(target, false, BreakpointType.STEP_OVER);
condition.compile(target, true, BreakpointType.STEP_OVER);
int endI = target.size();
end.set(endI + 1);
LabelContext.popLoop(target.env, label);
target.add(Instruction.jmpIf(start - endI));
}
public DoWhileNode(Location loc, String label, Node condition, Node body) {
super(loc);
this.label = label;
this.condition = condition;
this.body = body;
}
public static ParseRes<DoWhileNode> parse(Source src, int i) {
var n = Parsing.skipEmpty(src, i);
var loc = src.loc(i + n);
var labelRes = JavaScript.parseLabel(src, i + n);
n += labelRes.n;
if (!Parsing.isIdentifier(src, i + n, "do")) return ParseRes.failed();
n += 2;
var bodyRes = JavaScript.parseStatement(src, i + n);
if (!bodyRes.isSuccess()) return bodyRes.chainError(src.loc(i + n), "Expected a do-while body.");
n += bodyRes.n;
n += Parsing.skipEmpty(src, i + n);
if (!Parsing.isIdentifier(src, i + n, "while")) return ParseRes.failed();
n += 5;
n += Parsing.skipEmpty(src, i + n);
if (!src.is(i + n, "(")) return ParseRes.error(src.loc(i + n), "Expected a open paren after 'while'.");
n++;
var condRes = JavaScript.parseExpression(src, i + n, 0);
if (!condRes.isSuccess()) return condRes.chainError(src.loc(i + n), "Expected a do-while condition.");
n += condRes.n;
n += Parsing.skipEmpty(src, i + n);
if (!src.is(i + n, ")")) return ParseRes.error(src.loc(i + n), "Expected a closing paren after do-while condition.");
n++;
var end = JavaScript.parseStatementEnd(src, i + n);
if (end.isSuccess()) {
n += end.n;
return ParseRes.res(new DoWhileNode(loc, labelRes.result, condRes.result, bodyRes.result), n);
}
else return end.chainError(src.loc(i + n), "Expected end of statement");
}
}

View File

@@ -0,0 +1,116 @@
package me.topchetoeu.j2s.compilation.control;
import me.topchetoeu.j2s.common.Instruction;
import me.topchetoeu.j2s.common.Instruction.BreakpointType;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.common.parsing.ParseRes;
import me.topchetoeu.j2s.common.parsing.Parsing;
import me.topchetoeu.j2s.common.parsing.Source;
import me.topchetoeu.j2s.compilation.CompileResult;
import me.topchetoeu.j2s.compilation.DeferredIntSupplier;
import me.topchetoeu.j2s.compilation.JavaScript;
import me.topchetoeu.j2s.compilation.LabelContext;
import me.topchetoeu.j2s.compilation.Node;
import me.topchetoeu.j2s.compilation.values.VariableNode;
public class ForInNode extends Node {
public final boolean isDecl;
public final VariableNode binding;
public final Node object, body;
public final String label;
@Override public void resolve(CompileResult target) {
body.resolve(target);
if (isDecl) {
target.scope.define(binding.name);
}
}
@Override public void compileFunctions(CompileResult target) {
object.compileFunctions(target);
body.compileFunctions(target);
}
@Override public void compile(CompileResult target, boolean pollute) {
object.compile(target, true, BreakpointType.STEP_OVER);
target.add(Instruction.keys(false, true));
int start = target.size();
target.add(Instruction.dup());
int mid = target.temp();
target.add(Instruction.loadMember("value")).setLocation(binding.loc());
target.add(VariableNode.toSet(target, loc(), binding.name, false, true)).setLocation(binding.loc());
target.setLocationAndDebug(object.loc(), BreakpointType.STEP_OVER);
var end = new DeferredIntSupplier();
LabelContext.pushLoop(target.env, loc(), label, end, start);
body.compile(target, false, BreakpointType.STEP_OVER);
int endI = target.size();
target.add(Instruction.jmp(start - endI));
target.add(Instruction.discard());
target.set(mid, Instruction.jmpIfNot(endI - mid + 1));
end.set(endI + 1);
LabelContext.popLoop(target.env, label);
if (pollute) target.add(Instruction.pushUndefined());
}
public ForInNode(Location loc, String label, VariableNode binding, boolean isDecl, Node object, Node body) {
super(loc);
this.label = label;
this.binding = binding;
this.isDecl = isDecl;
this.object = object;
this.body = body;
}
public static ParseRes<ForInNode> parse(Source src, int i) {
var n = Parsing.skipEmpty(src, i);
var loc = src.loc(i + n);
var label = JavaScript.parseLabel(src, i + n);
n += label.n;
n += Parsing.skipEmpty(src, i + n);
if (!Parsing.isIdentifier(src, i + n, "for")) return ParseRes.failed();
n += 3;
n += Parsing.skipEmpty(src, i + n);
if (!src.is(i + n, "(")) return ParseRes.error(src.loc(i + n), "Expected an opening paren");
n++;
n += Parsing.skipEmpty(src, i + n);
var varKw = JavaScript.parseDeclarationType(src, i + n);
n += varKw.n;
n += Parsing.skipEmpty(src, i + n);
var bindingLoc = src.loc(i + n);
var name = Parsing.parseIdentifier(src, i + n);
if (!name.isSuccess()) return name.chainError(src.loc(i + n), "Expected a variable name");
n += name.n;
n += Parsing.skipEmpty(src, i + n);
if (!Parsing.isIdentifier(src, i + n, "in")) return ParseRes.error(src.loc(i + n), "Expected 'in' keyword after variable declaration");
n += 2;
var obj = JavaScript.parseExpression(src, i + n, 0);
if (!obj.isSuccess()) return obj.chainError(src.loc(i + n), "Expected a value");
n += obj.n;
n += Parsing.skipEmpty(src, i + n);
if (!src.is(i + n, ")")) return ParseRes.error(src.loc(i + n), "Expected a closing paren");
n++;
var bodyRes = JavaScript.parseStatement(src, i + n);
if (!bodyRes.isSuccess()) return bodyRes.chainError(src.loc(i + n), "Expected a for-in body");
n += bodyRes.n;
return ParseRes.res(new ForInNode(loc, label.result, new VariableNode(bindingLoc, name.result), varKw.isSuccess(), obj.result, bodyRes.result), n);
}
}

View File

@@ -0,0 +1,132 @@
package me.topchetoeu.j2s.compilation.control;
import me.topchetoeu.j2s.common.Instruction;
import me.topchetoeu.j2s.common.Instruction.BreakpointType;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.common.parsing.ParseRes;
import me.topchetoeu.j2s.common.parsing.Parsing;
import me.topchetoeu.j2s.common.parsing.Source;
import me.topchetoeu.j2s.compilation.CompileResult;
import me.topchetoeu.j2s.compilation.DeferredIntSupplier;
import me.topchetoeu.j2s.compilation.JavaScript;
import me.topchetoeu.j2s.compilation.LabelContext;
import me.topchetoeu.j2s.compilation.Node;
import me.topchetoeu.j2s.compilation.VariableDeclareNode;
public class ForNode extends Node {
public final Node declaration, assignment, condition, body;
public final String label;
@Override public void resolve(CompileResult target) {
if (declaration != null) declaration.resolve(target);
body.resolve(target);
}
@Override public void compileFunctions(CompileResult target) {
if (declaration != null) declaration.compileFunctions(target);
if (assignment != null) assignment.compileFunctions(target);
if (condition != null) condition.compileFunctions(target);
body.compileFunctions(target);
}
@Override public void compile(CompileResult target, boolean pollute) {
if (declaration != null) declaration.compile(target, false, BreakpointType.STEP_OVER);
var continueTarget = new DeferredIntSupplier();
int start = target.size();
int mid = -1;
if (condition != null) {
condition.compile(target, true, BreakpointType.STEP_OVER);
mid = target.temp();
}
var end = new DeferredIntSupplier();
LabelContext.pushLoop(target.env, loc(), label, end, continueTarget);
body.compile(target, false, BreakpointType.STEP_OVER);
continueTarget.set(target.size());
if (assignment != null) assignment.compile(target, false, BreakpointType.STEP_OVER);
int endI = target.size();
end.set(endI + 1);
LabelContext.popLoop(target.env, label);
target.add(Instruction.jmp(start - endI));
if (condition != null) target.set(mid, Instruction.jmpIfNot(endI - mid + 1));
if (pollute) target.add(Instruction.pushUndefined());
}
public ForNode(Location loc, String label, Node declaration, Node condition, Node assignment, Node body) {
super(loc);
this.label = label;
this.declaration = declaration;
this.condition = condition;
this.assignment = assignment;
this.body = body;
}
private static ParseRes<Node> parseSemicolon(Source src, int i) {
var n = Parsing.skipEmpty(src, i);
if (!src.is(i + n, ";")) return ParseRes.failed();
else return ParseRes.res(null, n + 1);
}
private static ParseRes<Node> parseCondition(Source src, int i) {
var n = Parsing.skipEmpty(src, i);
var res = JavaScript.parseExpression(src, i + n, 0);
if (!res.isSuccess()) return res.chainError();
n += res.n;
n += Parsing.skipEmpty(src, i + n);
if (!src.is(i + n, ";")) return ParseRes.error(src.loc(i + n), "Expected a semicolon");
else return ParseRes.res(res.result, n + 1);
}
private static ParseRes<? extends Node> parseUpdater(Source src, int i) {
return JavaScript.parseExpression(src, i, 0);
}
public static ParseRes<ForNode> parse(Source src, int i) {
var n = Parsing.skipEmpty(src, i);
var loc = src.loc(i + n);
var labelRes = JavaScript.parseLabel(src, i + n);
n += labelRes.n;
n += Parsing.skipEmpty(src, i + n);
if (!Parsing.isIdentifier(src, i + n, "for")) return ParseRes.failed();
n += 3;
n += Parsing.skipEmpty(src, i + n);
if (!src.is(i + n, "(")) return ParseRes.error(src.loc(i + n), "Expected a open paren after 'for'");
n++;
ParseRes<Node> decl = ParseRes.first(src, i + n,
ForNode::parseSemicolon,
VariableDeclareNode::parse,
ForNode::parseCondition
);
if (!decl.isSuccess()) return decl.chainError(src.loc(i + n), "Expected a declaration or an expression");
n += decl.n;
ParseRes<Node> cond = ParseRes.first(src, i + n,
ForNode::parseSemicolon,
ForNode::parseCondition
);
if (!cond.isSuccess()) return cond.chainError(src.loc(i + n), "Expected a condition");
n += cond.n;
var update = parseUpdater(src, i + n);
if (update.isError()) return update.chainError();
n += update.n;
n += Parsing.skipEmpty(src, i + n);
if (!src.is(i + n, ")")) return ParseRes.error(src.loc(i + n), "Expected a close paren after for updater");
n++;
var body = JavaScript.parseStatement(src, i + n);
if (!body.isSuccess()) return body.chainError(src.loc(i + n), "Expected a for body.");
n += body.n;
return ParseRes.res(new ForNode(loc, labelRes.result, decl.result, cond.result, update.result, body.result), n);
}
}

View File

@@ -0,0 +1,135 @@
package me.topchetoeu.j2s.compilation.control;
import me.topchetoeu.j2s.common.Instruction;
import me.topchetoeu.j2s.common.Instruction.BreakpointType;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.common.parsing.ParseRes;
import me.topchetoeu.j2s.common.parsing.Parsing;
import me.topchetoeu.j2s.common.parsing.Source;
import me.topchetoeu.j2s.compilation.CompileResult;
import me.topchetoeu.j2s.compilation.DeferredIntSupplier;
import me.topchetoeu.j2s.compilation.JavaScript;
import me.topchetoeu.j2s.compilation.LabelContext;
import me.topchetoeu.j2s.compilation.Node;
public class IfNode extends Node {
public final Node condition, body, elseBody;
public final String label;
@Override public void resolve(CompileResult target) {
body.resolve(target);
if (elseBody != null) elseBody.resolve(target);
}
@Override public void compileFunctions(CompileResult target) {
condition.compileFunctions(target);
body.compileFunctions(target);
if (elseBody != null) elseBody.compileFunctions(target);
}
@Override public void compile(CompileResult target, boolean pollute, BreakpointType breakpoint) {
condition.compile(target, true, breakpoint);
if (elseBody == null) {
int start = target.temp();
var end = new DeferredIntSupplier();
LabelContext.getBreak(target.env).push(loc(), label, end);
body.compile(target, pollute, BreakpointType.STEP_OVER);
LabelContext.getBreak(target.env).pop(label);
int endI = target.size();
end.set(endI);
target.set(start, Instruction.jmpIfNot(endI - start));
}
else {
int start = target.temp();
var end = new DeferredIntSupplier();
LabelContext.getBreak(target.env).push(loc(), label, end);
body.compile(target, pollute, BreakpointType.STEP_OVER);
int mid = target.temp();
elseBody.compile(target, pollute, BreakpointType.STEP_OVER);
LabelContext.getBreak(target.env).pop(label);
int endI = target.size();
end.set(endI);
target.set(start, Instruction.jmpIfNot(mid - start + 1));
target.set(mid, Instruction.jmp(endI - mid));
}
}
@Override public void compile(CompileResult target, boolean pollute) {
compile(target, pollute, BreakpointType.STEP_IN);
}
public IfNode(Location loc, Node condition, Node body, Node elseBody, String label) {
super(loc);
this.condition = condition;
this.body = body;
this.elseBody = elseBody;
this.label = label;
}
public static ParseRes<IfNode> parseTernary(Source src, int i, Node prev, int precedence) {
if (precedence > 2) return ParseRes.failed();
var n = Parsing.skipEmpty(src, i);
if (!src.is(i + n, "?")) return ParseRes.failed();
var loc = src.loc(i + n);
n++;
var a = JavaScript.parseExpression(src, i + n, 2);
if (!a.isSuccess()) return a.chainError(src.loc(i + n), "Expected a value after the ternary operator.");
n += a.n;
n += Parsing.skipEmpty(src, i + n);
if (!src.is(i + n, ":")) return ParseRes.failed();
n++;
var b = JavaScript.parseExpression(src, i + n, 2);
if (!b.isSuccess()) return b.chainError(src.loc(i + n), "Expected a second value after the ternary operator.");
n += b.n;
return ParseRes.res(new IfNode(loc, prev, a.result, b.result, null), n);
}
public static ParseRes<IfNode> parse(Source src, int i) {
var n = Parsing.skipEmpty(src, i);
var loc = src.loc(i + n);
var label = JavaScript.parseLabel(src, i + n);
n += label.n;
n += Parsing.skipEmpty(src, i + n);
if (!Parsing.isIdentifier(src, i + n, "if")) return ParseRes.failed();
n += 2;
n += Parsing.skipEmpty(src, i + n);
if (!src.is(i + n, "(")) return ParseRes.error(src.loc(i + n), "Expected a open paren after 'if'.");
n++;
var condRes = JavaScript.parseExpression(src, i + n, 0);
if (!condRes.isSuccess()) return condRes.chainError(src.loc(i + n), "Expected an if condition.");
n += condRes.n;
n += Parsing.skipEmpty(src, i + n);
if (!src.is(i + n, ")")) return ParseRes.error(src.loc(i + n), "Expected a closing paren after if condition.");
n++;
var res = JavaScript.parseStatement(src, i + n);
if (!res.isSuccess()) return res.chainError(src.loc(i + n), "Expected an if body.");
n += res.n;
var elseKw = Parsing.parseIdentifier(src, i + n, "else");
if (!elseKw.isSuccess()) return ParseRes.res(new IfNode(loc, condRes.result, res.result, null, label.result), n);
n += elseKw.n;
var elseRes = JavaScript.parseStatement(src, i + n);
if (!elseRes.isSuccess()) return elseRes.chainError(src.loc(i + n), "Expected an else body.");
n += elseRes.n;
return ParseRes.res(new IfNode(loc, condRes.result, res.result, elseRes.result, label.result), n);
}
}

View File

@@ -0,0 +1,53 @@
package me.topchetoeu.j2s.compilation.control;
import me.topchetoeu.j2s.common.Instruction;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.common.parsing.ParseRes;
import me.topchetoeu.j2s.common.parsing.Parsing;
import me.topchetoeu.j2s.common.parsing.Source;
import me.topchetoeu.j2s.compilation.CompileResult;
import me.topchetoeu.j2s.compilation.JavaScript;
import me.topchetoeu.j2s.compilation.Node;
public class ReturnNode extends Node {
public final Node value;
@Override public void compileFunctions(CompileResult target) {
if (value != null) value.compileFunctions(target);
}
@Override public void compile(CompileResult target, boolean pollute) {
if (value == null) target.add(Instruction.pushUndefined());
else value.compile(target, true);
target.add(Instruction.ret()).setLocation(loc());
}
public ReturnNode(Location loc, Node value) {
super(loc);
this.value = value;
}
public static ParseRes<ReturnNode> parse(Source src, int i) {
var n = Parsing.skipEmpty(src, i);
var loc = src.loc(i + n);
if (!Parsing.isIdentifier(src, i + n, "return")) return ParseRes.failed();
n += 6;
var end = JavaScript.parseStatementEnd(src, i + n);
if (end.isSuccess()) {
n += end.n;
return ParseRes.res(new ReturnNode(loc, null), n);
}
var val = JavaScript.parseExpression(src, i + n, 0);
if (val.isError()) return val.chainError();
n += val.n;
end = JavaScript.parseStatementEnd(src, i + n);
if (end.isSuccess()) {
n += end.n;
return ParseRes.res(new ReturnNode(loc, val.result), n);
}
else return end.chainError(src.loc(i + n), "Expected end of statement or a return value");
}
}

View File

@@ -0,0 +1,198 @@
package me.topchetoeu.j2s.compilation.control;
import java.util.ArrayList;
import java.util.HashMap;
import me.topchetoeu.j2s.common.Instruction;
import me.topchetoeu.j2s.common.Operation;
import me.topchetoeu.j2s.common.Instruction.BreakpointType;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.common.parsing.ParseRes;
import me.topchetoeu.j2s.common.parsing.Parsing;
import me.topchetoeu.j2s.common.parsing.Source;
import me.topchetoeu.j2s.compilation.CompileResult;
import me.topchetoeu.j2s.compilation.DeferredIntSupplier;
import me.topchetoeu.j2s.compilation.JavaScript;
import me.topchetoeu.j2s.compilation.LabelContext;
import me.topchetoeu.j2s.compilation.Node;
public class SwitchNode extends Node {
public static class SwitchCase {
public final Node value;
public final int statementI;
public SwitchCase(Node value, int statementI) {
this.value = value;
this.statementI = statementI;
}
}
public final Node value;
public final SwitchCase[] cases;
public final Node[] body;
public final int defaultI;
public final String label;
@Override public void resolve(CompileResult target) {
for (var stm : body) stm.resolve(target);
}
@Override public void compileFunctions(CompileResult target) {
value.compileFunctions(target);
for (var _case : cases) {
_case.value.compileFunctions(target);
}
for (var stm : body) {
stm.compileFunctions(target);
}
}
@Override public void compile(CompileResult target, boolean pollute) {
var caseToStatement = new HashMap<Integer, Integer>();
var statementToIndex = new HashMap<Integer, Integer>();
value.compile(target, true, BreakpointType.STEP_OVER);
// TODO: create a jump map
for (var ccase : cases) {
target.add(Instruction.dup());
ccase.value.compile(target, true);
target.add(Instruction.operation(Operation.EQUALS));
caseToStatement.put(target.temp(), ccase.statementI);
}
int start = target.temp();
var end = new DeferredIntSupplier();
LabelContext.getBreak(target.env).pushLoop(loc(), label, end);
for (var stm : body) {
statementToIndex.put(statementToIndex.size(), target.size());
stm.compile(target, false, BreakpointType.STEP_OVER);
}
int endI = target.size();
end.set(endI);
LabelContext.getBreak(target.env).popLoop(label);
target.add(Instruction.discard());
if (pollute) target.add(Instruction.pushUndefined());
if (defaultI < 0 || defaultI >= body.length) target.set(start, Instruction.jmp(endI - start));
else target.set(start, Instruction.jmp(statementToIndex.get(defaultI) - start));
for (var el : caseToStatement.entrySet()) {
var i = statementToIndex.get(el.getValue());
if (i == null) i = endI;
target.set(el.getKey(), Instruction.jmpIf(i - el.getKey()));
}
}
public SwitchNode(Location loc, String label, Node value, int defaultI, SwitchCase[] cases, Node[] body) {
super(loc);
this.label = label;
this.value = value;
this.defaultI = defaultI;
this.cases = cases;
this.body = body;
}
private static ParseRes<Node> parseSwitchCase(Source src, int i) {
var n = Parsing.skipEmpty(src, i);
if (!Parsing.isIdentifier(src, i + n, "case")) return ParseRes.failed();
n += 4;
var val = JavaScript.parseExpression(src, i + n, 0);
if (!val.isSuccess()) return val.chainError(src.loc(i + n), "Expected a value after 'case'");
n += val.n;
n += Parsing.skipEmpty(src, i + n);
if (!src.is(i + n, ":")) return ParseRes.error(src.loc(i + n), "Expected colons after 'case' value");
n++;
return ParseRes.res(val.result, n);
}
private static ParseRes<Void> parseDefaultCase(Source src, int i) {
var n = Parsing.skipEmpty(src, i);
if (!Parsing.isIdentifier(src, i + n, "default")) return ParseRes.failed();
n += 7;
n += Parsing.skipEmpty(src, i + n);
if (!src.is(i + n, ":")) return ParseRes.error(src.loc(i + n), "Expected colons after 'default'");
n++;
return ParseRes.res(null, n);
}
public static ParseRes<SwitchNode> parse(Source src, int i) {
var n = Parsing.skipEmpty(src, i);
var loc = src.loc(i + n);
var label = JavaScript.parseLabel(src, i + n);
n += label.n;
n += Parsing.skipEmpty(src, i + n);
if (!Parsing.isIdentifier(src, i + n, "switch")) return ParseRes.failed();
n += 6;
n += Parsing.skipEmpty(src, i + n);
if (!src.is(i + n, "(")) return ParseRes.error(src.loc(i + n), "Expected a open paren after 'switch'");
n++;
var val = JavaScript.parseExpression(src, i + n, 0);
if (!val.isSuccess()) return val.chainError(src.loc(i + n), "Expected a switch value");
n += val.n;
n += Parsing.skipEmpty(src, i + n);
if (!src.is(i + n, ")")) return ParseRes.error(src.loc(i + n), "Expected a closing paren after switch value");
n++;
n += Parsing.skipEmpty(src, i + n);
if (!src.is(i + n, "{")) return ParseRes.error(src.loc(i + n), "Expected an opening brace after switch value");
n++;
n += Parsing.skipEmpty(src, i + n);
var statements = new ArrayList<Node>();
var cases = new ArrayList<SwitchCase>();
var defaultI = -1;
while (true) {
n += Parsing.skipEmpty(src, i + n);
if (src.is(i + n, "}")) {
n++;
break;
}
if (src.is(i + n, ";")) {
n++;
continue;
}
ParseRes<Node> caseRes = ParseRes.first(src, i + n,
SwitchNode::parseDefaultCase,
SwitchNode::parseSwitchCase
);
if (caseRes.isSuccess()) {
n += caseRes.n;
if (caseRes.result == null) defaultI = statements.size();
else cases.add(new SwitchCase(caseRes.result, statements.size()));
continue;
}
if (caseRes.isError()) return caseRes.chainError();
var stm = JavaScript.parseStatement(src, i + n);
if (stm.isSuccess()) {
n += stm.n;
statements.add(stm.result);
continue;
}
else stm.chainError(src.loc(i + n), "Expected a statement, 'case' or 'default'");
}
return ParseRes.res(new SwitchNode(
loc, label.result, val.result, defaultI,
cases.toArray(new SwitchCase[0]),
statements.toArray(new Node[0])
), n);
}
}

View File

@@ -0,0 +1,46 @@
package me.topchetoeu.j2s.compilation.control;
import me.topchetoeu.j2s.common.Instruction;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.common.parsing.ParseRes;
import me.topchetoeu.j2s.common.parsing.Parsing;
import me.topchetoeu.j2s.common.parsing.Source;
import me.topchetoeu.j2s.compilation.CompileResult;
import me.topchetoeu.j2s.compilation.JavaScript;
import me.topchetoeu.j2s.compilation.Node;
public class ThrowNode extends Node {
public final Node value;
@Override public void compileFunctions(CompileResult target) {
value.compileFunctions(target);
}
@Override public void compile(CompileResult target, boolean pollute) {
value.compile(target, true);
target.add(Instruction.throwInstr()).setLocation(loc());
}
public ThrowNode(Location loc, Node value) {
super(loc);
this.value = value;
}
public static ParseRes<ThrowNode> parse(Source src, int i) {
var n = Parsing.skipEmpty(src, i);
var loc = src.loc(i + n);
if (!Parsing.isIdentifier(src, i + n, "throw")) return ParseRes.failed();
n += 5;
var val = JavaScript.parseExpression(src, i + n, 0);
if (val.isFailed()) return ParseRes.error(src.loc(i + n), "Expected a throw value");
n += val.n;
var end = JavaScript.parseStatementEnd(src, i + n);
if (end.isSuccess()) {
n += end.n;
return ParseRes.res(new ThrowNode(loc, val.result), n);
}
else return end.chainError(src.loc(i + n), "Expected end of statement");
}
}

View File

@@ -0,0 +1,148 @@
package me.topchetoeu.j2s.compilation.control;
import me.topchetoeu.j2s.common.Instruction;
import me.topchetoeu.j2s.common.Instruction.BreakpointType;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.common.parsing.ParseRes;
import me.topchetoeu.j2s.common.parsing.Parsing;
import me.topchetoeu.j2s.common.parsing.Source;
import me.topchetoeu.j2s.compilation.CompileResult;
import me.topchetoeu.j2s.compilation.CompoundNode;
import me.topchetoeu.j2s.compilation.DeferredIntSupplier;
import me.topchetoeu.j2s.compilation.JavaScript;
import me.topchetoeu.j2s.compilation.LabelContext;
import me.topchetoeu.j2s.compilation.Node;
public class TryNode extends Node {
public final CompoundNode tryBody;
public final CompoundNode catchBody;
public final CompoundNode finallyBody;
public final String captureName;
public final String label;
@Override public void resolve(CompileResult target) {
tryBody.resolve(target);
if (catchBody != null) catchBody.resolve(target);
if (finallyBody != null) finallyBody.resolve(target);
}
@Override public void compileFunctions(CompileResult target) {
tryBody.compileFunctions(target);
if (catchBody != null) {
if (captureName != null) {
var index = target.scope.defineCatch(captureName);
target.catchBindings.put(this, index);
}
catchBody.compileFunctions(target);
if (captureName != null) target.scope.undefineCatch();
}
if (finallyBody != null) finallyBody.compileFunctions(target);
}
@Override public void compile(CompileResult target, boolean pollute, BreakpointType bpt) {
int replace = target.temp();
var endSuppl = new DeferredIntSupplier();
int start = replace + 1, catchStart = -1, finallyStart = -1;
LabelContext.getBreak(target.env).push(loc(), label, endSuppl);
tryBody.compile(target, false);
target.add(Instruction.tryEnd());
if (catchBody != null) {
catchStart = target.size() - start;
if (captureName != null) {
var catchVar = target.catchBindings.get(this);
target.scope.defineCatch(captureName, catchVar);
target.add(Instruction.loadError()).setLocation(catchBody.loc());
target.add(catchVar.index().toSet(false)).setLocation(catchBody.loc());
catchBody.compile(target, false);
target.scope.undefineCatch();
}
else catchBody.compile(target, false);
target.add(Instruction.tryEnd());
}
if (finallyBody != null) {
finallyStart = target.size() - start;
finallyBody.compile(target, false);
target.add(Instruction.tryEnd());
}
LabelContext.getBreak(target.env).pop(label);
endSuppl.set(target.size());
target.set(replace, Instruction.tryStart(catchStart, finallyStart, target.size() - start));
target.setLocationAndDebug(replace, loc(), BreakpointType.STEP_OVER);
if (pollute) target.add(Instruction.pushUndefined());
}
public TryNode(Location loc, String label, CompoundNode tryBody, CompoundNode catchBody, CompoundNode finallyBody, String captureName) {
super(loc);
this.tryBody = tryBody;
this.catchBody = catchBody;
this.finallyBody = finallyBody;
this.captureName = captureName;
this.label = label;
}
public static ParseRes<TryNode> parse(Source src, int i) {
var n = Parsing.skipEmpty(src, i);
var loc = src.loc(i + n);
var labelRes = JavaScript.parseLabel(src, i + n);
n += labelRes.n;
n += Parsing.skipEmpty(src, i + n);
if (!Parsing.isIdentifier(src, i + n, "try")) return ParseRes.failed();
n += 3;
var tryBody = CompoundNode.parse(src, i + n);
if (!tryBody.isSuccess()) return tryBody.chainError(src.loc(i + n), "Expected a try body");
n += tryBody.n;
n += Parsing.skipEmpty(src, i + n);
String capture = null;
CompoundNode catchBody = null, finallyBody = null;
if (Parsing.isIdentifier(src, i + n, "catch")) {
n += 5;
n += Parsing.skipEmpty(src, i + n);
if (src.is(i + n, "(")) {
n++;
var nameRes = Parsing.parseIdentifier(src, i + n);
if (!nameRes.isSuccess()) return nameRes.chainError(src.loc(i + n), "xpected a catch variable name");
capture = nameRes.result;
n += nameRes.n;
n += Parsing.skipEmpty(src, i + n);
if (!src.is(i + n, ")")) return ParseRes.error(src.loc(i + n), "Expected a closing paren after catch variable name");
n++;
}
var bodyRes = CompoundNode.parse(src, i + n);
if (!bodyRes.isSuccess()) return tryBody.chainError(src.loc(i + n), "Expected a catch body");
n += bodyRes.n;
n += Parsing.skipEmpty(src, i + n);
catchBody = bodyRes.result;
}
if (Parsing.isIdentifier(src, i + n, "finally")) {
n += 7;
var bodyRes = CompoundNode.parse(src, i + n);
if (!bodyRes.isSuccess()) return tryBody.chainError(src.loc(i + n), "Expected a finally body");
n += bodyRes.n;
n += Parsing.skipEmpty(src, i + n);
finallyBody = bodyRes.result;
}
if (finallyBody == null && catchBody == null) ParseRes.error(src.loc(i + n), "Expected catch or finally");
return ParseRes.res(new TryNode(loc, labelRes.result, tryBody.result, catchBody, finallyBody, capture), n);
}
}

View File

@@ -0,0 +1,82 @@
package me.topchetoeu.j2s.compilation.control;
import me.topchetoeu.j2s.common.Instruction;
import me.topchetoeu.j2s.common.Instruction.BreakpointType;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.common.parsing.ParseRes;
import me.topchetoeu.j2s.common.parsing.Parsing;
import me.topchetoeu.j2s.common.parsing.Source;
import me.topchetoeu.j2s.compilation.CompileResult;
import me.topchetoeu.j2s.compilation.DeferredIntSupplier;
import me.topchetoeu.j2s.compilation.JavaScript;
import me.topchetoeu.j2s.compilation.LabelContext;
import me.topchetoeu.j2s.compilation.Node;
public class WhileNode extends Node {
public final Node condition, body;
public final String label;
@Override public void resolve(CompileResult target) {
body.resolve(target);
}
@Override public void compileFunctions(CompileResult target) {
condition.compileFunctions(target);
body.compileFunctions(target);
}
@Override public void compile(CompileResult target, boolean pollute) {
int start = target.size();
condition.compile(target, true);
int mid = target.temp();
var end = new DeferredIntSupplier();
LabelContext.pushLoop(target.env, loc(), label, end, start);
body.compile(target, false, BreakpointType.STEP_OVER);
var endI = target.size();
end.set(endI + 1);
LabelContext.popLoop(target.env, label);
target.add(Instruction.jmp(start - endI));
target.set(mid, Instruction.jmpIfNot(endI - mid + 1));
if (pollute) target.add(Instruction.pushUndefined());
}
public WhileNode(Location loc, String label, Node condition, Node body) {
super(loc);
this.label = label;
this.condition = condition;
this.body = body;
}
public static ParseRes<WhileNode> parse(Source src, int i) {
var n = Parsing.skipEmpty(src, i);
var loc = src.loc(i + n);
var label = JavaScript.parseLabel(src, i + n);
n += label.n;
n += Parsing.skipEmpty(src, i + n);
if (!Parsing.isIdentifier(src, i + n, "while")) return ParseRes.failed();
n += 5;
n += Parsing.skipEmpty(src, i + n);
if (!src.is(i + n, "(")) return ParseRes.error(src.loc(i + n), "Expected a open paren after 'while'.");
n++;
var cond = JavaScript.parseExpression(src, i + n, 0);
if (!cond.isSuccess()) return cond.chainError(src.loc(i + n), "Expected a while condition.");
n += cond.n;
n += Parsing.skipEmpty(src, i + n);
if (!src.is(i + n, ")")) return ParseRes.error(src.loc(i + n), "Expected a closing paren after while condition.");
n++;
var body = JavaScript.parseStatement(src, i + n);
if (!body.isSuccess()) return body.chainError(src.loc(i + n), "Expected a while body.");
n += body.n;
return ParseRes.res(new WhileNode(loc, label.result, cond.result, body.result), n);
}
}

View File

@@ -0,0 +1,59 @@
package me.topchetoeu.j2s.compilation.members;
import me.topchetoeu.j2s.common.Instruction;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.common.parsing.ParseRes;
import me.topchetoeu.j2s.common.parsing.Parsing;
import me.topchetoeu.j2s.common.parsing.Source;
import me.topchetoeu.j2s.compilation.CompileResult;
import me.topchetoeu.j2s.compilation.JavaScript;
import me.topchetoeu.j2s.compilation.Node;
import me.topchetoeu.j2s.compilation.values.ObjectNode;
public class FieldMemberNode implements Member {
public final Location loc;
public final Node key;
public final Node value;
@Override public Location loc() { return loc; }
@Override public void compileFunctions(CompileResult target) {
key.compileFunctions(target);
value.compileFunctions(target);
}
@Override public void compile(CompileResult target, boolean pollute) {
if (pollute) target.add(Instruction.dup());
key.compile(target, true);
if (value == null) target.add(Instruction.pushUndefined());
else value.compile(target, true);
target.add(Instruction.storeMember());
}
public FieldMemberNode(Location loc, Node key, Node value) {
this.loc = loc;
this.key = key;
this.value = value;
}
public static ParseRes<FieldMemberNode> parse(Source src, int i) {
var n = Parsing.skipEmpty(src, i);
var loc = src.loc(i + n);
var name = ObjectNode.parsePropName(src, i + n);
if (!name.isSuccess()) return name.chainError();
n += name.n;
n += Parsing.skipEmpty(src, i + n);
if (!src.is(i + n, ":")) return ParseRes.failed();
n++;
var value = JavaScript.parseExpression(src, i + n, 2);
if (!value.isSuccess()) return value.chainError(src.loc(i + n), "Expected a value");
n += value.n;
return ParseRes.res(new FieldMemberNode(loc, name.result, value.result), n);
}
}

View File

@@ -0,0 +1,11 @@
package me.topchetoeu.j2s.compilation.members;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.compilation.CompileResult;
public interface Member {
Location loc();
void compileFunctions(CompileResult target);
void compile(CompileResult target, boolean pollute);
}

View File

@@ -0,0 +1,83 @@
package me.topchetoeu.j2s.compilation.members;
import java.util.Arrays;
import me.topchetoeu.j2s.common.Instruction;
import me.topchetoeu.j2s.common.Instruction.BreakpointType;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.common.parsing.ParseRes;
import me.topchetoeu.j2s.common.parsing.Parsing;
import me.topchetoeu.j2s.common.parsing.Source;
import me.topchetoeu.j2s.compilation.CompileResult;
import me.topchetoeu.j2s.compilation.CompoundNode;
import me.topchetoeu.j2s.compilation.FunctionNode;
import me.topchetoeu.j2s.compilation.JavaScript;
import me.topchetoeu.j2s.compilation.Node;
import me.topchetoeu.j2s.compilation.values.ObjectNode;
import me.topchetoeu.j2s.compilation.values.VariableNode;
import me.topchetoeu.j2s.compilation.values.constants.StringNode;
public final class PropertyMemberNode extends FunctionNode implements Member {
public final Node key;
public final VariableNode argument;
@Override public String name() {
if (key instanceof StringNode str) {
if (isGetter()) return "get " + str.value;
else return "set " + str.value;
}
else return null;
}
public boolean isGetter() { return argument == null; }
public boolean isSetter() { return argument != null; }
@Override public void compileFunctions(CompileResult target) {
key.compileFunctions(target);
target.addChild(this, compileBody(target, null));
}
@Override public void compile(CompileResult target, boolean pollute, String name, BreakpointType bp) {
if (pollute) target.add(Instruction.dup());
key.compile(target, true);
target.add(Instruction.loadFunc(target.childrenIndices.get(this), name(name), captures(target))).setLocation(loc());
target.add(Instruction.defProp(isSetter()));
}
public PropertyMemberNode(Location loc, Location end, Node key, VariableNode argument, CompoundNode body) {
super(loc, end, argument == null ? Arrays.asList() : Arrays.asList(argument), body);
this.key = key;
this.argument = argument;
}
public static ParseRes<PropertyMemberNode> parse(Source src, int i) {
var n = Parsing.skipEmpty(src, i);
var loc = src.loc(i + n);
var access = Parsing.parseIdentifier(src, i + n);
if (!access.isSuccess()) return ParseRes.failed();
if (!access.result.equals("get") && !access.result.equals("set")) return ParseRes.failed();
n += access.n;
var name = ObjectNode.parsePropName(src, i + n);
if (!name.isSuccess()) return name.chainError(src.loc(i + n), "Expected a property name after '" + access + "'");
n += name.n;
var params = JavaScript.parseParameters(src, i + n);
if (!params.isSuccess()) return params.chainError(src.loc(i + n), "Expected an argument list");
if (access.result.equals("get") && params.result.size() != 0) return ParseRes.error(src.loc(i + n), "Getter must not have any parameters");
if (access.result.equals("set") && params.result.size() != 1) return ParseRes.error(src.loc(i + n), "Setter must have exactly one parameter");
n += params.n;
var body = CompoundNode.parse(src, i + n);
if (!body.isSuccess()) return body.chainError(src.loc(i + n), "Expected a compound statement for property accessor.");
n += body.n;
var end = src.loc(i + n - 1);
return ParseRes.res(new PropertyMemberNode(
loc, end, name.result, access.result.equals("get") ? null : params.result.get(0), body.result
), n);
}
}

View File

@@ -0,0 +1,27 @@
package me.topchetoeu.j2s.compilation.patterns;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.compilation.CompileResult;
/**
* Represents all nodes that can be assign targets
*/
public interface AssignTarget extends AssignTargetLike {
Location loc();
/**
* Called to perform calculations before the assigned value is calculated
*/
default void beforeAssign(CompileResult target) {}
/**
* Called to perform the actual assignemnt. Between the `beforeAssign` and this call a single value will have been pushed to the stack
* @param pollute Whether or not to leave the original value on the stack
*/
void afterAssign(CompileResult target, boolean pollute);
default void assign(CompileResult target, boolean pollute) {
afterAssign(target, pollute);
}
@Override default AssignTarget toAssignTarget() { return this; }
}

View File

@@ -0,0 +1,8 @@
package me.topchetoeu.j2s.compilation.patterns;
/**
* Represents all nodes that can be converted to assign targets
*/
public interface AssignTargetLike {
AssignTarget toAssignTarget();
}

View File

@@ -0,0 +1,7 @@
package me.topchetoeu.j2s.compilation.patterns;
import me.topchetoeu.j2s.compilation.CompileResult;
public interface ChangeTarget extends AssignTarget {
void beforeChange(CompileResult target);
}

View File

@@ -0,0 +1,221 @@
package me.topchetoeu.j2s.compilation.scope;
import java.util.ArrayList;
import java.util.HashMap;
public final class FunctionScope {
protected final VariableList locals = new VariableList(VariableIndex.IndexType.LOCALS);
protected final VariableList capturables = new VariableList(VariableIndex.IndexType.CAPTURABLES, this.locals);
private final VariableList captures = new VariableList(VariableIndex.IndexType.CAPTURES);
private final HashMap<String, Variable> localsMap = new HashMap<>();
private final HashMap<String, Variable> capturesMap = new HashMap<>();
private final ArrayList<Variable> catchesMap = new ArrayList<>();
private final HashMap<Variable, Variable> childToParent = new HashMap<>();
private final HashMap<Variable, Variable> parentToChild = new HashMap<>();
public final FunctionScope parent;
public final boolean passthrough;
private Variable addCaptured(Variable var, boolean captured) {
if (captured && !this.capturables.has(var) && !this.captures.has(var)) this.capturables.add(var);
return var;
}
private Variable getCatchVar(String name) {
for (var el : catchesMap) {
if (el.name.equals(name)) return el;
}
return null;
}
/**
* @returns If a variable with the same name exists, the old variable. Otherwise, the given variable
*/
public Variable define(Variable var) {
if (passthrough) return null;
else {
var catchVar = getCatchVar(var.name);
if (catchVar != null) return catchVar;
if (localsMap.containsKey(var.name)) return localsMap.get(var.name);
if (capturesMap.containsKey(var.name)) throw new RuntimeException("HEY!");
localsMap.put(var.name, var);
return locals.add(var);
}
}
/**
* @returns A variable with the given name, or null if a global variable
*/
public Variable define(String name) {
return define(new Variable(name, false));
}
/**
* Creates a catch variable and returns it
*/
public Variable defineCatch(String name) {
var var = new Variable(name, false);
this.locals.add(var);
this.catchesMap.add(var);
return var;
}
/**
* Creates a catch variable, using a specific variable instance
* Used in the second pass
*/
public Variable defineCatch(String name, Variable var) {
this.locals.add(var);
this.catchesMap.add(var);
return var;
}
/**
* Removes the last catch variable.
* NOTE: the variable is still in the internal list. It just won't be findable by its name
*/
public void undefineCatch() {
this.catchesMap.remove(this.catchesMap.size() - 1);
}
/**
* Gets the index supplier of the given variable name, or null if it is a global
*
* @param capture If true, the variable is being captured by a function
*/
public Variable get(String name, boolean capture) {
var catchVar = getCatchVar(name);
if (catchVar != null) return addCaptured(catchVar, capture);
if (localsMap.containsKey(name)) return addCaptured(localsMap.get(name), capture);
if (capturesMap.containsKey(name)) return addCaptured(capturesMap.get(name), capture);
if (parent == null) return null;
var parentVar = parent.get(name, true);
if (parentVar == null) return null;
var childVar = captures.add(parentVar.clone().setIndexSupplier(null));
capturesMap.put(childVar.name, childVar);
childToParent.put(childVar, parentVar);
parentToChild.put(parentVar, childVar);
return childVar;
}
/**
* If the variable given is contained in this function, just returns the variable itself.
* However, this function is important to handle cases in which you might want to access
* a captured variable. In such cases, this function will return a capture to the given variable.
*
* @param capture Whether or not to execute this capturing logic
*/
public Variable get(Variable var, boolean capture) {
if (captures.has(var)) return addCaptured(var, capture);
if (locals.has(var)) return addCaptured(var, capture);
if (capture) {
if (parentToChild.containsKey(var)) return addCaptured(parentToChild.get(var), capture);
if (parent == null) return null;
var parentVar = parent.get(var, true);
if (parentVar == null) return null;
var childVar = captures.add(parentVar.clone());
childToParent.put(childVar, parentVar);
parentToChild.put(parentVar, childVar);
return childVar;
}
else return null;
}
/**
* Checks if the given variable name is accessible
*
* @param capture If true, will check beyond this function's scope
*/
public boolean has(String name, boolean capture) {
if (localsMap.containsKey(name)) return true;
if (capture) {
if (capturesMap.containsKey(name)) return true;
if (parent != null) return parent.has(name, true);
}
return false;
}
public int localsCount() {
return locals.size();
}
public int capturesCount() {
return captures.size();
}
public int capturablesCount() {
return capturables.size();
}
public int[] getCaptureIndices() {
var res = new int[captures.size()];
var i = 0;
for (var el : captures) {
assert childToParent.containsKey(el);
res[i] = childToParent.get(el).index().toCaptureIndex();
i++;
}
return res;
}
public Iterable<Variable> capturables() {
return capturables;
}
public Iterable<Variable> locals() {
return locals;
}
public String[] captureNames() {
var res = new String[this.captures.size()];
var i = 0;
for (var el : this.captures) {
res[i++] = el.name;
}
return res;
}
public String[] localNames() {
var res = new String[this.locals.size()];
var i = 0;
for (var el : this.locals) {
res[i++] = el.name;
}
return res;
}
public String[] capturableNames() {
var res = new String[this.capturables.size()];
var i = 0;
for (var el : this.capturables) {
res[i++] = el.name;
}
return res;
}
public FunctionScope(FunctionScope parent) {
this.parent = parent;
this.passthrough = false;
}
public FunctionScope(boolean passthrough) {
this.parent = null;
this.passthrough = passthrough;
}
}

View File

@@ -0,0 +1,35 @@
package me.topchetoeu.j2s.compilation.scope;
import java.util.function.Supplier;
public final class Variable {
private Supplier<VariableIndex> indexSupplier;
public final boolean readonly;
public final String name;
public final VariableIndex index() {
return indexSupplier.get();
}
public final Variable setIndexSupplier(Supplier<VariableIndex> index) {
this.indexSupplier = index;
return this;
}
public final Supplier<VariableIndex> indexSupplier() {
return indexSupplier;
}
public final Variable clone() {
return new Variable(name, readonly).setIndexSupplier(indexSupplier);
}
public Variable(String name, boolean readonly) {
this.name = name;
this.readonly = readonly;
}
public static Variable of(String name, boolean readonly, VariableIndex index) {
return new Variable(name, readonly).setIndexSupplier(() -> index);
}
}

View File

@@ -0,0 +1,53 @@
package me.topchetoeu.j2s.compilation.scope;
import me.topchetoeu.j2s.common.Instruction;
public final class VariableIndex {
public static enum IndexType {
/**
* A simple variable that is only ever used within the function
*/
LOCALS,
/**
* A variable that has the ability to be captured by children functions
*/
CAPTURABLES,
/**
* A variable that has been captured from the parent function
*/
CAPTURES,
}
public final IndexType type;
public final int index;
public final int toCaptureIndex() {
switch (type) {
case CAPTURES: return ~index;
case CAPTURABLES: return index;
default: throw new UnsupportedOperationException("Index type " + type + " may not be captured");
}
}
public final Instruction toGet() {
switch (type) {
case CAPTURES: return Instruction.loadVar(~index);
case CAPTURABLES: return Instruction.loadVar(index);
case LOCALS: return Instruction.loadVar(index);
default: throw new UnsupportedOperationException("Unknown index type " + type);
}
}
public final Instruction toSet(boolean keep) {
switch (type) {
case CAPTURES: return Instruction.storeVar(~index, keep, false);
case CAPTURABLES: return Instruction.storeVar(index, keep, false);
case LOCALS: return Instruction.storeVar(index, keep, false);
default: throw new UnsupportedOperationException("Unknown index type " + type);
}
}
public VariableIndex(VariableIndex.IndexType type, int index) {
this.type = type;
this.index = index;
}
}

View File

@@ -0,0 +1,191 @@
package me.topchetoeu.j2s.compilation.scope;
import java.util.HashMap;
import java.util.Iterator;
import java.util.function.IntSupplier;
import java.util.function.Supplier;
import me.topchetoeu.j2s.compilation.scope.VariableIndex.IndexType;
public final class VariableList implements Iterable<Variable> {
private final class VariableNode implements Supplier<VariableIndex> {
public Variable var;
public VariableNode next;
public VariableNode prev;
public int index;
public int indexIteration = -1;
public VariableList list() { return VariableList.this; }
private int getIndex() {
if (this.indexIteration != VariableList.this.indexIteration) {
this.indexIteration = VariableList.this.indexIteration;
if (prev == null) this.index = 0;
else this.index = prev.getIndex() + 1;
}
return this.index;
}
@Override public VariableIndex get() {
if (offset == null) return new VariableIndex(indexType, this.getIndex());
else return new VariableIndex(indexType, offset.getAsInt() + this.getIndex());
}
public VariableNode(Variable var, VariableNode next, VariableNode prev) {
this.var = var;
this.next = next;
this.prev = prev;
}
}
private VariableNode first, last;
private HashMap<Variable, VariableNode> varMap = new HashMap<>();
private final IntSupplier offset;
/**
* Increased when indices need recalculation. VariableNode will check if
* its internal indexIteration is up to date with this, and if not, will
* recalculate its index
*/
private int indexIteration = 0;
public final VariableIndex.IndexType indexType;
/**
* Adds the given variable to this list. If it already exists, does nothing
* @return val
*/
public Variable add(Variable val) {
if (this.varMap.containsKey(val)) return val;
this.indexIteration++;
if (val.indexSupplier() instanceof VariableNode prevNode) {
prevNode.list().remove(val);
}
var node = new VariableNode(val, null, last);
if (last != null) {
assert first != null;
last.next = node;
node.prev = last;
last = node;
}
else {
first = last = node;
}
varMap.put(val, node);
val.setIndexSupplier(node);
return val;
}
/**
* If the variable is not in the list, does nothing. Otherwise, removes the variable from the list
* @return null if nothing was done, else the deleted variable (should be var)
*/
public Variable remove(Variable var) {
if (var == null) return null;
var node = varMap.get(var);
if (node == null) return null;
this.indexIteration++;
if (node.prev != null) {
assert node != first;
node.prev.next = node.next;
}
else {
assert node == first;
first = first.next;
}
if (node.next != null) {
assert node != last;
node.next.prev = node.prev;
}
else {
assert node == last;
last = last.prev;
}
node.next = null;
node.prev = null;
varMap.remove(node.var);
node.var.setIndexSupplier(null);
return node.var;
}
/**
* Checks if the list has the given variable
*/
public boolean has(Variable var) {
return varMap.containsKey(var);
}
/**
* Returns an indexer for the given variable
*/
public Supplier<VariableIndex> indexer(Variable var) {
return varMap.get(var);
}
public int size() {
return varMap.size();
}
public Iterator<Variable> iterator() {
return new Iterator<Variable>() {
private VariableNode curr = first;
@Override public boolean hasNext() {
return curr != null;
}
@Override public Variable next() {
if (curr == null) return null;
var res = curr;
curr = curr.next;
return res.var;
}
};
}
/**
* @param offset Will offset the indices by the given amount from the supplier
*/
public VariableList(IndexType type, IntSupplier offset) {
this.indexType = type;
this.offset = offset;
}
/**
* @param offset Will offset the indices by the given amount
*/
public VariableList(IndexType type, int offset) {
this.indexType = type;
this.offset = () -> offset;
}
/**
* @param offset Will offset the indices by the size of the given list
*/
public VariableList(IndexType type, VariableList prev) {
this.indexType = type;
this.offset = () -> {
if (prev.offset != null) return prev.offset.getAsInt() + prev.size();
else return prev.size();
};
}
public VariableList(IndexType type) {
this.indexType = type;
this.offset = null;
}
}

View File

@@ -0,0 +1,20 @@
package me.topchetoeu.j2s.compilation.values;
import me.topchetoeu.j2s.common.Instruction;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.compilation.CompileResult;
import me.topchetoeu.j2s.compilation.Node;
public class ArgumentsNode extends Node {
@Override public void compileFunctions(CompileResult target) {
}
@Override public void compile(CompileResult target, boolean pollute) {
if (pollute) target.add(Instruction.loadArgs());
}
public ArgumentsNode(Location loc) {
super(loc);
}
}

View File

@@ -0,0 +1,87 @@
package me.topchetoeu.j2s.compilation.values;
import java.util.ArrayList;
import me.topchetoeu.j2s.common.Instruction;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.common.parsing.ParseRes;
import me.topchetoeu.j2s.common.parsing.Parsing;
import me.topchetoeu.j2s.common.parsing.Source;
import me.topchetoeu.j2s.compilation.CompileResult;
import me.topchetoeu.j2s.compilation.JavaScript;
import me.topchetoeu.j2s.compilation.Node;
public class ArrayNode extends Node {
public final Node[] statements;
@Override public void compileFunctions(CompileResult target) {
for (var stm : statements) {
if (stm != null) stm.compileFunctions(target);
}
}
@Override public void compile(CompileResult target, boolean pollute) {
target.add(Instruction.loadArr(statements.length));
for (var i = 0; i < statements.length; i++) {
var el = statements[i];
if (el != null) {
target.add(Instruction.dup());
el.compile(target, true);
target.add(Instruction.storeMember(i));
}
}
if (!pollute) target.add(Instruction.discard());
}
public ArrayNode(Location loc, Node[] statements) {
super(loc);
this.statements = statements;
}
public static ParseRes<ArrayNode> parse(Source src, int i) {
var n = Parsing.skipEmpty(src, i);
var loc = src.loc(i + n);
if (!src.is(i + n, "[")) return ParseRes.failed();
n++;
var values = new ArrayList<Node>();
loop: while (true) {
n += Parsing.skipEmpty(src, i + n);
if (src.is(i + n, "]")) {
n++;
break;
}
while (src.is(i + n, ",")) {
n++;
n += Parsing.skipEmpty(src, i + n);
values.add(null);
if (src.is(i + n, "]")) {
n++;
break loop;
}
}
var res = JavaScript.parseExpression(src, i + n, 2);
if (!res.isSuccess()) return res.chainError(src.loc(i + n), "Expected an array element.");
n += res.n;
n += Parsing.skipEmpty(src, i + n);
values.add(res.result);
if (src.is(i + n, ",")) n++;
else if (src.is(i + n, "]")) {
n++;
break;
}
}
return ParseRes.res(new ArrayNode(loc, values.toArray(new Node[0])), n);
}
}

View File

@@ -0,0 +1,20 @@
package me.topchetoeu.j2s.compilation.values;
import me.topchetoeu.j2s.common.Instruction;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.compilation.CompileResult;
import me.topchetoeu.j2s.compilation.Node;
public class GlobalThisNode extends Node {
@Override public void compileFunctions(CompileResult target) {
}
@Override public void compile(CompileResult target, boolean pollute) {
if (pollute) target.add(Instruction.loadGlob());
}
public GlobalThisNode(Location loc) {
super(loc);
}
}

View File

@@ -0,0 +1,114 @@
package me.topchetoeu.j2s.compilation.values;
import java.util.LinkedList;
import java.util.List;
import me.topchetoeu.j2s.common.Instruction;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.common.parsing.ParseRes;
import me.topchetoeu.j2s.common.parsing.Parsing;
import me.topchetoeu.j2s.common.parsing.Source;
import me.topchetoeu.j2s.compilation.CompileResult;
import me.topchetoeu.j2s.compilation.JavaScript;
import me.topchetoeu.j2s.compilation.Node;
import me.topchetoeu.j2s.compilation.members.FieldMemberNode;
import me.topchetoeu.j2s.compilation.members.Member;
import me.topchetoeu.j2s.compilation.members.PropertyMemberNode;
import me.topchetoeu.j2s.compilation.values.constants.NumberNode;
import me.topchetoeu.j2s.compilation.values.constants.StringNode;
public class ObjectNode extends Node {
public final List<Member> members;
@Override public void compileFunctions(CompileResult target) {
for (var member : members) member.compileFunctions(target);
}
@Override public void compile(CompileResult target, boolean pollute) {
target.add(Instruction.loadObj());
for (var el : members) el.compile(target, true);
}
public ObjectNode(Location loc, List<Member> map) {
super(loc);
this.members = map;
}
private static ParseRes<Node> parseComputePropName(Source src, int i) {
var n = Parsing.skipEmpty(src, i);
if (!src.is(i + n, "[")) return ParseRes.failed();
n++;
var val = JavaScript.parseExpression(src, i, 0);
if (!val.isSuccess()) return val.chainError(src.loc(i + n), "Expected an expression in compute property");
n += val.n;
n += Parsing.skipEmpty(src, i + n);
if (!src.is(i + n, "]")) return ParseRes.error(src.loc(i + n), "Expected a closing bracket after compute property");
n++;
return ParseRes.res(val.result, n);
}
public static ParseRes<Node> parsePropName(Source src, int i) {
return ParseRes.first(src, i,
(s, j) -> {
var m = Parsing.skipEmpty(s, j);
var l = s.loc(j + m);
var r = Parsing.parseIdentifier(s, j + m);
if (r.isSuccess()) return ParseRes.res(new StringNode(l, r.result), r.n);
else return r.chainError();
},
StringNode::parse,
NumberNode::parse,
ObjectNode::parseComputePropName
);
}
public static ParseRes<ObjectNode> parse(Source src, int i) {
var n = Parsing.skipEmpty(src, i);
var loc = src.loc(i + n);
if (!src.is(i + n, "{")) return ParseRes.failed();
n++;
n += Parsing.skipEmpty(src, i + n);
var members = new LinkedList<Member>();
if (src.is(i + n, "}")) {
n++;
return ParseRes.res(new ObjectNode(loc, members), n);
}
while (true) {
ParseRes<Member> prop = ParseRes.first(src, i + n,
PropertyMemberNode::parse,
FieldMemberNode::parse
);
if (!prop.isSuccess()) return prop.chainError(src.loc(i + n), "Expected a member in object literal");
n += prop.n;
members.add(prop.result);
n += Parsing.skipEmpty(src, i + n);
if (src.is(i + n, ",")) {
n++;
n += Parsing.skipEmpty(src, i + n);
if (src.is(i + n, "}")) {
n++;
break;
}
continue;
}
else if (src.is(i + n, "}")) {
n++;
break;
}
else ParseRes.error(src.loc(i + n), "Expected a comma or a closing brace.");
}
return ParseRes.res(new ObjectNode(loc, members), n);
}
}

View File

@@ -0,0 +1,86 @@
package me.topchetoeu.j2s.compilation.values;
import me.topchetoeu.j2s.common.Instruction;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.common.parsing.ParseRes;
import me.topchetoeu.j2s.common.parsing.Parsing;
import me.topchetoeu.j2s.common.parsing.Source;
import me.topchetoeu.j2s.compilation.CompileResult;
import me.topchetoeu.j2s.compilation.Node;
public class RegexNode extends Node {
public final String pattern, flags;
@Override public void compileFunctions(CompileResult target) {
}
@Override public void compile(CompileResult target, boolean pollute) {
target.add(Instruction.loadRegex(pattern, flags));
if (!pollute) target.add(Instruction.discard());
}
public static ParseRes<RegexNode> parse(Source src, int i) {
var n = Parsing.skipEmpty(src, i);
if (!src.is(i + n, '/')) return ParseRes.failed();
var loc = src.loc(i + n);
n++;
var source = new StringBuilder();
var flags = new StringBuilder();
var inBrackets = false;
loop: while (true) {
if (i + n >= src.size()) break;
switch (src.at(i + n)) {
case '[':
inBrackets = true;
source.append('[');
n++;
continue;
case ']':
inBrackets = false;
source.append(']');
n++;
continue;
case '/':
n++;
if (inBrackets) {
source.append('/');
continue;
}
else break loop;
case '\\':
source.append('\\');
source.append(src.at(i + n + 1));
n += 2;
break;
default:
source.append(src.at(i + n));
n++;
break;
}
}
while (true) {
char c = src.at(i + n, '\0');
if (src.is(i + n, v -> Parsing.isAny(c, "dgimsuy"))) {
if (flags.indexOf(c + "") >= 0) return ParseRes.error(src.loc(i + n), "The flags of a regular expression may not be repeated");
flags.append(c);
}
else break;
n++;
}
return ParseRes.res(new RegexNode(loc, source.toString(), flags.toString()), n);
}
public RegexNode(Location loc, String pattern, String flags) {
super(loc);
this.pattern = pattern;
this.flags = flags;
}
}

View File

@@ -0,0 +1,20 @@
package me.topchetoeu.j2s.compilation.values;
import me.topchetoeu.j2s.common.Instruction;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.compilation.CompileResult;
import me.topchetoeu.j2s.compilation.Node;
public class ThisNode extends Node {
@Override public void compileFunctions(CompileResult target) {
}
@Override public void compile(CompileResult target, boolean pollute) {
if (pollute) target.add(Instruction.loadThis());
}
public ThisNode(Location loc) {
super(loc);
}
}

View File

@@ -0,0 +1,73 @@
package me.topchetoeu.j2s.compilation.values;
import me.topchetoeu.j2s.common.Instruction;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.common.parsing.ParseRes;
import me.topchetoeu.j2s.common.parsing.Parsing;
import me.topchetoeu.j2s.common.parsing.Source;
import me.topchetoeu.j2s.compilation.CompileResult;
import me.topchetoeu.j2s.compilation.JavaScript;
import me.topchetoeu.j2s.compilation.Node;
import me.topchetoeu.j2s.compilation.patterns.ChangeTarget;
public class VariableNode extends Node implements ChangeTarget {
public final String name;
@Override public void compileFunctions(CompileResult target) {
}
public String assignName() { return name; }
@Override public void beforeChange(CompileResult target) {
target.add(VariableNode.toGet(target, loc(), name));
}
@Override public void afterAssign(CompileResult target, boolean pollute) {
target.add(VariableNode.toSet(target, loc(), name, pollute, false)).setLocation(loc());
}
@Override public void compile(CompileResult target, boolean pollute) {
target.add(toGet(target, loc(), name, true, false)).setLocation(loc());
}
public static Instruction toGet(CompileResult target, Location loc, String name, boolean keep, boolean forceGet) {
var i = target.scope.get(name, false);
if (i != null) {
if (keep) return i.index().toGet();
else return Instruction.nop();
}
else return Instruction.globGet(name, forceGet);
}
public static Instruction toGet(CompileResult target, Location loc, String name) {
return toGet(target, loc, name, true, false);
}
public static Instruction toSet(CompileResult target, Location loc, String name, boolean keep, boolean init) {
var i = target.scope.get(name, false);
if (i != null) return i.index().toSet(keep);
else return Instruction.globSet(name, keep, init);
}
public VariableNode(Location loc, String name) {
super(loc);
this.name = name;
}
public static ParseRes<VariableNode> parse(Source src, int i) {
var n = Parsing.skipEmpty(src, i);
var loc = src.loc(i + n);
var literal = Parsing.parseIdentifier(src, i);
if (!literal.isSuccess()) return literal.chainError();
n += literal.n;
if (!JavaScript.checkVarName(literal.result)) {
if (literal.result.equals("await")) return ParseRes.error(src.loc(i + n), "'await' expressions are not supported");
return ParseRes.error(src.loc(i + n), String.format("Unexpected keyword '%s'", literal.result));
}
return ParseRes.res(new VariableNode(loc, literal.result), n);
}
}

View File

@@ -0,0 +1,22 @@
package me.topchetoeu.j2s.compilation.values.constants;
import me.topchetoeu.j2s.common.Instruction;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.compilation.CompileResult;
import me.topchetoeu.j2s.compilation.Node;
public class BoolNode extends Node {
public final boolean value;
@Override public void compileFunctions(CompileResult target) {
}
@Override public void compile(CompileResult target, boolean pollute) {
if (pollute) target.add(Instruction.pushValue(value));
}
public BoolNode(Location loc, boolean value) {
super(loc);
this.value = value;
}
}

View File

@@ -0,0 +1,17 @@
package me.topchetoeu.j2s.compilation.values.constants;
import me.topchetoeu.j2s.common.Instruction;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.compilation.CompileResult;
import me.topchetoeu.j2s.compilation.Node;
public class NullNode extends Node {
@Override public void compileFunctions(CompileResult target) {
}
@Override public void compile(CompileResult target, boolean pollute) {
if (pollute) target.add(Instruction.pushNull());
}
public NullNode(Location loc) { super(loc); }
}

View File

@@ -0,0 +1,34 @@
package me.topchetoeu.j2s.compilation.values.constants;
import me.topchetoeu.j2s.common.Instruction;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.common.parsing.ParseRes;
import me.topchetoeu.j2s.common.parsing.Parsing;
import me.topchetoeu.j2s.common.parsing.Source;
import me.topchetoeu.j2s.compilation.CompileResult;
import me.topchetoeu.j2s.compilation.Node;
public class NumberNode extends Node {
public final double value;
@Override public void compileFunctions(CompileResult target) {
}
@Override public void compile(CompileResult target, boolean pollute) {
if (pollute) target.add(Instruction.pushValue(value));
}
public NumberNode(Location loc, double value) {
super(loc);
this.value = value;
}
public static ParseRes<NumberNode> parse(Source src, int i) {
var n = Parsing.skipEmpty(src, i);
var loc = src.loc(i + n);
var res = Parsing.parseNumber(src, i + n, false);
if (res.isSuccess()) return ParseRes.res(new NumberNode(loc, res.result), n + res.n);
else return res.chainError();
}
}

View File

@@ -0,0 +1,34 @@
package me.topchetoeu.j2s.compilation.values.constants;
import me.topchetoeu.j2s.common.Instruction;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.common.parsing.ParseRes;
import me.topchetoeu.j2s.common.parsing.Parsing;
import me.topchetoeu.j2s.common.parsing.Source;
import me.topchetoeu.j2s.compilation.CompileResult;
import me.topchetoeu.j2s.compilation.Node;
public class StringNode extends Node {
public final String value;
@Override public void compileFunctions(CompileResult target) {
}
@Override public void compile(CompileResult target, boolean pollute) {
if (pollute) target.add(Instruction.pushValue(value));
}
public StringNode(Location loc, String value) {
super(loc);
this.value = value;
}
public static ParseRes<StringNode> parse(Source src, int i) {
var n = Parsing.skipEmpty(src, i);
var loc = src.loc(i + n);
var res = Parsing.parseString(src, i + n);
if (res.isSuccess()) return ParseRes.res(new StringNode(loc, res.result), n + res.n);
else return res.chainError();
}
}

View File

@@ -0,0 +1,51 @@
package me.topchetoeu.j2s.compilation.values.operations;
import me.topchetoeu.j2s.common.Instruction;
import me.topchetoeu.j2s.common.Operation;
import me.topchetoeu.j2s.common.SyntaxException;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.compilation.CompileResult;
import me.topchetoeu.j2s.compilation.Node;
import me.topchetoeu.j2s.compilation.patterns.AssignTarget;
public class AssignNode extends Node implements AssignTarget {
public final AssignTarget assignable;
public final Node value;
@Override public void compileFunctions(CompileResult target) {
((Node)assignable).compileFunctions(target);
value.compileFunctions(target);
}
@Override public void compile(CompileResult target, boolean pollute) {
if (assignable instanceof AssignNode other) throw new SyntaxException(other.loc(), "Assign deconstructor not allowed here");
assignable.beforeAssign(target);
value.compile(target, true);
assignable.afterAssign(target, pollute);
}
@Override public void afterAssign(CompileResult target, boolean pollute) {
if (assignable instanceof AssignNode other) throw new SyntaxException(other.loc(), "Double assign deconstructor not allowed");
if (pollute) target.add(Instruction.dup(2, 0));
else target.add(Instruction.dup());
target.add(Instruction.pushUndefined());
target.add(Instruction.operation(Operation.EQUALS));
var start = target.temp();
target.add(Instruction.discard());
value.compile(target, true);
target.set(start, Instruction.jmpIfNot(target.size() - start));
assignable.assign(target, false);
if (!pollute) target.add(Instruction.discard());
}
public AssignNode(Location loc, AssignTarget assignable, Node value) {
super(loc);
this.assignable = assignable;
this.value = value;
}
}

View File

@@ -0,0 +1,115 @@
package me.topchetoeu.j2s.compilation.values.operations;
import java.util.ArrayList;
import me.topchetoeu.j2s.common.Instruction;
import me.topchetoeu.j2s.common.Instruction.BreakpointType;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.common.parsing.ParseRes;
import me.topchetoeu.j2s.common.parsing.Parsing;
import me.topchetoeu.j2s.common.parsing.Source;
import me.topchetoeu.j2s.compilation.CompileResult;
import me.topchetoeu.j2s.compilation.JavaScript;
import me.topchetoeu.j2s.compilation.Node;
public class CallNode extends Node {
public final Node func;
public final Node[] args;
public final boolean isNew;
@Override public void compileFunctions(CompileResult target) {
func.compileFunctions(target);
for (var arg : args) arg.compileFunctions(target);
}
@Override public void compile(CompileResult target, boolean pollute, BreakpointType type) {
if (!isNew && func instanceof IndexNode indexNode) {
var obj = indexNode.object;
var index = indexNode.index;
obj.compile(target, true);
target.add(Instruction.dup());
IndexNode.indexLoad(target, index, true);
for (var arg : args) arg.compile(target, true);
target.add(Instruction.call(args.length, true));
target.setLocationAndDebug(loc(), type);
}
else {
func.compile(target, true);
for (var arg : args) arg.compile(target, true);
if (isNew) target.add(Instruction.callNew(args.length)).setLocationAndDebug(loc(), type);
else target.add(Instruction.call(args.length, false)).setLocationAndDebug(loc(), type);
}
if (!pollute) target.add(Instruction.discard());
}
@Override public void compile(CompileResult target, boolean pollute) {
compile(target, pollute, BreakpointType.STEP_IN);
}
public CallNode(Location loc, boolean isNew, Node func, Node ...args) {
super(loc);
this.isNew = isNew;
this.func = func;
this.args = args;
}
public static ParseRes<CallNode> parseCall(Source src, int i, Node prev, int precedence) {
if (precedence > 17) return ParseRes.failed();
var n = Parsing.skipEmpty(src, i);
var loc = src.loc(i + n);
if (!src.is(i + n, "(")) return ParseRes.failed();
n++;
var args = new ArrayList<Node>();
boolean prevArg = false;
while (true) {
var argRes = JavaScript.parseExpression(src, i + n, 2);
n += argRes.n;
n += Parsing.skipEmpty(src, i + n);
if (argRes.isSuccess()) {
args.add(argRes.result);
prevArg = true;
}
else if (argRes.isError()) return argRes.chainError();
if (prevArg && src.is(i + n, ",")) {
prevArg = false;
n++;
}
else if (src.is(i + n, ")")) {
n++;
break;
}
else if (prevArg) return ParseRes.error(src.loc(i + n), "Expected a comma or a closing paren");
else return ParseRes.error(src.loc(i + n), "Expected an expression or a closing paren");
}
return ParseRes.res(new CallNode(loc, false, prev, args.toArray(new Node[0])), n);
}
public static ParseRes<CallNode> parseNew(Source src, int i) {
var n = Parsing.skipEmpty(src, i);
var loc = src.loc(i + n);
if (!Parsing.isIdentifier(src, i + n, "new")) return ParseRes.failed();
n += 3;
var valRes = JavaScript.parseExpression(src, i + n, 18);
if (!valRes.isSuccess()) return valRes.chainError(src.loc(i + n), "Expected a value after 'new' keyword.");
n += valRes.n;
var callRes = CallNode.parseCall(src, i + n, valRes.result, 0);
if (callRes.isFailed()) return ParseRes.res(new CallNode(loc, true, valRes.result), n);
if (callRes.isError()) return callRes.chainError();
n += callRes.n;
return ParseRes.res(new CallNode(loc, true, callRes.result.func, callRes.result.args), n);
}
}

View File

@@ -0,0 +1,65 @@
package me.topchetoeu.j2s.compilation.values.operations;
import me.topchetoeu.j2s.common.Instruction;
import me.topchetoeu.j2s.common.Operation;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.common.parsing.ParseRes;
import me.topchetoeu.j2s.common.parsing.Parsing;
import me.topchetoeu.j2s.common.parsing.Source;
import me.topchetoeu.j2s.compilation.CompileResult;
import me.topchetoeu.j2s.compilation.JavaScript;
import me.topchetoeu.j2s.compilation.Node;
import me.topchetoeu.j2s.compilation.patterns.ChangeTarget;
import me.topchetoeu.j2s.compilation.values.constants.NumberNode;
public class ChangeNode extends Node {
public final ChangeTarget changable;
public final Node value;
public final Operation op;
@Override public void compileFunctions(CompileResult target) {
((Node)changable).compileFunctions(target);
value.compileFunctions(target);
}
@Override public void compile(CompileResult target, boolean pollute) {
changable.beforeChange(target);
value.compile(target, true);
target.add(Instruction.operation(op));
changable.afterAssign(target, pollute);
}
public ChangeNode(Location loc, ChangeTarget changable, Node value, Operation op) {
super(loc);
this.changable = changable;
this.value = value;
this.op = op;
}
public static ParseRes<ChangeNode> parsePrefixIncrease(Source src, int i) {
var n = Parsing.skipEmpty(src, i);
var loc = src.loc(i + n);
if (!src.is(i + n, "++")) return ParseRes.failed();
n += 2;
var res = JavaScript.parseExpression(src, i + n, 15);
if (!res.isSuccess()) return res.chainError(src.loc(i + n), "Expected assignable value after prefix operator.");
else if (!(res.result instanceof ChangeTarget)) return ParseRes.error(src.loc(i + n), "Expected assignable value after prefix operator.");
return ParseRes.res(new ChangeNode(loc, (ChangeTarget)res.result, new NumberNode(loc, -1), Operation.SUBTRACT), n + res.n);
}
public static ParseRes<ChangeNode> parsePrefixDecrease(Source src, int i) {
var n = Parsing.skipEmpty(src, i);
var loc = src.loc(i + n);
if (!src.is(i + n, "--")) return ParseRes.failed();
n += 2;
var res = JavaScript.parseExpression(src, i + n, 15);
if (!res.isSuccess()) return res.chainError(src.loc(i + n), "Expected assignable value after prefix operator.");
else if (!(res.result instanceof ChangeTarget)) return ParseRes.error(src.loc(i + n), "Expected assignable value after prefix operator.");
return ParseRes.res(new ChangeNode(loc, (ChangeTarget)res.result, new NumberNode(loc, 1), Operation.SUBTRACT), n + res.n);
}
}

View File

@@ -0,0 +1,43 @@
package me.topchetoeu.j2s.compilation.values.operations;
import me.topchetoeu.j2s.common.Instruction;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.common.parsing.ParseRes;
import me.topchetoeu.j2s.common.parsing.Parsing;
import me.topchetoeu.j2s.common.parsing.Source;
import me.topchetoeu.j2s.compilation.CompileResult;
import me.topchetoeu.j2s.compilation.JavaScript;
import me.topchetoeu.j2s.compilation.Node;
public class DiscardNode extends Node {
public final Node value;
@Override public void compileFunctions(CompileResult target) {
if (value != null) value.compileFunctions(target);
}
@Override public void compile(CompileResult target, boolean pollute) {
if (value != null) value.compile(target, false);
if (pollute) target.add(Instruction.pushUndefined());
}
public DiscardNode(Location loc, Node val) {
super(loc);
this.value = val;
}
public static ParseRes<DiscardNode> parse(Source src, int i) {
var n = Parsing.skipEmpty(src, i);
var loc = src.loc(i + n);
if (!Parsing.isIdentifier(src, i + n, "void")) return ParseRes.failed();
n += 4;
var valRes = JavaScript.parseExpression(src, i + n, 14);
if (!valRes.isSuccess()) return valRes.chainError(src.loc(i + n), "Expected a value after 'void' keyword.");
n += valRes.n;
return ParseRes.res(new DiscardNode(loc, valRes.result), n);
}
}

View File

@@ -0,0 +1,154 @@
package me.topchetoeu.j2s.compilation.values.operations;
import me.topchetoeu.j2s.common.Instruction;
import me.topchetoeu.j2s.common.Instruction.BreakpointType;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.common.parsing.ParseRes;
import me.topchetoeu.j2s.common.parsing.Parsing;
import me.topchetoeu.j2s.common.parsing.Source;
import me.topchetoeu.j2s.compilation.CompileResult;
import me.topchetoeu.j2s.compilation.JavaScript;
import me.topchetoeu.j2s.compilation.Node;
import me.topchetoeu.j2s.compilation.patterns.ChangeTarget;
import me.topchetoeu.j2s.compilation.values.constants.NumberNode;
import me.topchetoeu.j2s.compilation.values.constants.StringNode;
public class IndexNode extends Node implements ChangeTarget {
public final Node object;
public final Node index;
@Override public void compileFunctions(CompileResult target) {
object.compileFunctions(target);
index.compileFunctions(target);
}
@Override public void beforeAssign(CompileResult target) {
object.compile(target, true);
indexStorePushKey(target, index);
}
@Override public void beforeChange(CompileResult target) {
object.compile(target, true);
if (index instanceof NumberNode num && (int)num.value == num.value) {
target.add(Instruction.dup());
target.add(Instruction.loadMember((int)num.value));
}
else if (index instanceof StringNode str) {
target.add(Instruction.dup());
target.add(Instruction.loadMember(str.value));
}
else {
index.compile(target, true);
target.add(Instruction.dup(1, 1));
target.add(Instruction.dup(1, 1));
target.add(Instruction.loadMember());
}
}
@Override public void assign(CompileResult target, boolean pollute) {
object.compile(target, true);
target.add(Instruction.dup(1, 1));
indexStorePushKey(target, index);
indexStore(target, index, pollute);
}
@Override public void afterAssign(CompileResult target, boolean pollute) {
indexStore(target, index, pollute);
}
public void compile(CompileResult target, boolean dupObj, boolean pollute) {
object.compile(target, true);
if (dupObj) target.add(Instruction.dup());
if (index instanceof NumberNode num && (int)num.value == num.value) {
target.add(Instruction.loadMember((int)num.value));
}
else if (index instanceof StringNode str) {
target.add(Instruction.loadMember(str.value));
}
else {
index.compile(target, true);
target.add(Instruction.loadMember());
}
target.setLocationAndDebug(loc(), BreakpointType.STEP_IN);
if (!pollute) target.add(Instruction.discard());
}
@Override public void compile(CompileResult target, boolean pollute) {
compile(target, false, pollute);
}
public IndexNode(Location loc, Node object, Node index) {
super(loc);
this.object = object;
this.index = index;
}
public static ParseRes<IndexNode> parseIndex(Source src, int i, Node prev, int precedence) {
if (precedence > 18) return ParseRes.failed();
var n = Parsing.skipEmpty(src, i);
var loc = src.loc(i + n);
if (!src.is(i + n, "[")) return ParseRes.failed();
n++;
var valRes = JavaScript.parseExpression(src, i + n, 0);
if (!valRes.isSuccess()) return valRes.chainError(src.loc(i + n), "Expected a value in index expression");
n += valRes.n;
n += Parsing.skipEmpty(src, i + n);
if (!src.is(i + n, "]")) return ParseRes.error(src.loc(i + n), "Expected a closing bracket");
n++;
return ParseRes.res(new IndexNode(loc, prev, valRes.result), n);
}
public static ParseRes<IndexNode> parseMember(Source src, int i, Node prev, int precedence) {
if (precedence > 18) return ParseRes.failed();
var n = Parsing.skipEmpty(src, i);
var loc = src.loc(i + n);
if (!src.is(i + n, ".")) return ParseRes.failed();
n++;
var literal = Parsing.parseIdentifier(src, i + n);
if (!literal.isSuccess()) return literal.chainError(src.loc(i + n), "Expected an identifier after member access.");
n += literal.n;
return ParseRes.res(new IndexNode(loc, prev, new StringNode(loc, literal.result)), n);
}
public static void indexStorePushKey(CompileResult target, Node index) {
if (index instanceof NumberNode num && (int)num.value == num.value) return;
if (index instanceof StringNode) return;
index.compile(target, true);
}
public static void indexStore(CompileResult target, Node index, boolean pollute) {
if (index instanceof NumberNode num && (int)num.value == num.value) {
target.add(Instruction.storeMember((int)num.value, pollute));
}
else if (index instanceof StringNode str) {
target.add(Instruction.storeMember(str.value, pollute));
}
else {
target.add(Instruction.storeMember(pollute));
}
}
public static void indexLoad(CompileResult target, Node index, boolean pollute) {
if (index instanceof NumberNode num && (int)num.value == num.value) {
target.add(Instruction.loadMember((int)num.value));
}
else if (index instanceof StringNode str) {
target.add(Instruction.loadMember(str.value));
}
else {
index.compile(target, true);
target.add(Instruction.loadMember());
}
if (!pollute) target.add(Instruction.discard());
}
}

View File

@@ -0,0 +1,50 @@
package me.topchetoeu.j2s.compilation.values.operations;
import me.topchetoeu.j2s.common.Instruction;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.common.parsing.ParseRes;
import me.topchetoeu.j2s.common.parsing.Parsing;
import me.topchetoeu.j2s.common.parsing.Source;
import me.topchetoeu.j2s.compilation.CompileResult;
import me.topchetoeu.j2s.compilation.JavaScript;
import me.topchetoeu.j2s.compilation.Node;
public class LazyAndNode extends Node {
public final Node first, second;
@Override public void compileFunctions(CompileResult target) {
first.compileFunctions(target);
second.compileFunctions(target);
}
@Override public void compile(CompileResult target, boolean pollute) {
first.compile(target, true);
if (pollute) target.add(Instruction.dup());
int start = target.temp();
if (pollute) target.add(Instruction.discard());
second.compile(target, pollute);
target.set(start, Instruction.jmpIfNot(target.size() - start));
}
public LazyAndNode(Location loc, Node first, Node second) {
super(loc);
this.first = first;
this.second = second;
}
public static ParseRes<LazyAndNode> parse(Source src, int i, Node prev, int precedence) {
if (precedence < 4) return ParseRes.failed();
var n = Parsing.skipEmpty(src, i);
if (!src.is(i + n, "&&")) return ParseRes.failed();
var loc = src.loc(i + n);
n += 2;
var res = JavaScript.parseExpression(src, i + n, 4);
if (!res.isSuccess()) return res.chainError(src.loc(i + n), "Expected a value after the '&&' operator.");
n += res.n;
return ParseRes.res(new LazyAndNode(loc, prev, res.result), n);
}
}

View File

@@ -0,0 +1,51 @@
package me.topchetoeu.j2s.compilation.values.operations;
import me.topchetoeu.j2s.common.Instruction;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.common.parsing.ParseRes;
import me.topchetoeu.j2s.common.parsing.Parsing;
import me.topchetoeu.j2s.common.parsing.Source;
import me.topchetoeu.j2s.compilation.CompileResult;
import me.topchetoeu.j2s.compilation.JavaScript;
import me.topchetoeu.j2s.compilation.Node;
public class LazyOrNode extends Node {
public final Node first, second;
@Override public void compileFunctions(CompileResult target) {
first.compileFunctions(target);
second.compileFunctions(target);
}
@Override public void compile(CompileResult target, boolean pollute) {
first.compile(target, true);
if (pollute) target.add(Instruction.dup());
int start = target.temp();
if (pollute) target.add(Instruction.discard());
second.compile(target, pollute);
target.set(start, Instruction.jmpIf(target.size() - start));
}
public LazyOrNode(Location loc, Node first, Node second) {
super(loc);
this.first = first;
this.second = second;
}
public static ParseRes<LazyOrNode> parse(Source src, int i, Node prev, int precedence) {
if (precedence < 3) return ParseRes.failed();
var n = Parsing.skipEmpty(src, i);
if (!src.is(i + n, "||")) return ParseRes.failed();
var loc = src.loc(i + n);
n += 2;
var res = JavaScript.parseExpression(src, i + n, 4);
if (!res.isSuccess()) return res.chainError(src.loc(i + n), "Expected a value after the '||' operator.");
n += res.n;
return ParseRes.res(new LazyOrNode(loc, prev, res.result), n);
}
}

View File

@@ -0,0 +1,237 @@
package me.topchetoeu.j2s.compilation.values.operations;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import me.topchetoeu.j2s.common.Instruction;
import me.topchetoeu.j2s.common.Operation;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.common.parsing.ParseRes;
import me.topchetoeu.j2s.common.parsing.Parsing;
import me.topchetoeu.j2s.common.parsing.Source;
import me.topchetoeu.j2s.compilation.CompileResult;
import me.topchetoeu.j2s.compilation.JavaScript;
import me.topchetoeu.j2s.compilation.Node;
import me.topchetoeu.j2s.compilation.patterns.AssignTargetLike;
import me.topchetoeu.j2s.compilation.patterns.ChangeTarget;
public class OperationNode extends Node {
private static interface OperatorFactory {
String token();
int precedence();
ParseRes<Node> construct(Source src, int i, Node prev);
}
private static class NormalOperatorFactory implements OperatorFactory {
public final String token;
public final int precedence;
public final Operation operation;
@Override public int precedence() { return precedence; }
@Override public String token() { return token; }
@Override public ParseRes<Node> construct(Source src, int i, Node prev) {
var loc = src.loc(i);
var other = JavaScript.parseExpression(src, i, precedence + 1);
if (!other.isSuccess()) return other.chainError(src.loc(i + other.n), String.format("Expected a value after '%s'", token));
return ParseRes.res(new OperationNode(loc, operation, prev, (Node)other.result), other.n);
}
public NormalOperatorFactory(String token, int precedence, Operation operation) {
this.token = token;
this.precedence = precedence;
this.operation = operation;
}
}
private static class AssignmentOperatorFactory implements OperatorFactory {
public final String token;
public final int precedence;
public final Operation operation;
@Override public int precedence() { return precedence; }
@Override public String token() { return token; }
@Override public ParseRes<Node> construct(Source src, int i, Node prev) {
var loc = src.loc(i);
if (operation == null) {
if (!(prev instanceof AssignTargetLike target)) return ParseRes.error(loc, String.format("Expected an assignable expression before '%s'", token));
var other = JavaScript.parseExpression(src, i, precedence);
if (!other.isSuccess()) return other.chainError(src.loc(i + other.n), String.format("Expected a value after '%s'", token));
return ParseRes.res(new AssignNode(loc, target.toAssignTarget(), other.result), other.n);
}
else {
if (!(prev instanceof ChangeTarget target)) return ParseRes.error(loc, String.format("Expected a changeable expression before '%s'", token));
var other = JavaScript.parseExpression(src, i, precedence);
if (!other.isSuccess()) return other.chainError(src.loc(i + other.n), String.format("Expected a value after '%s'", token));
return ParseRes.res(new ChangeNode(loc, target, other.result, operation), other.n);
}
}
public AssignmentOperatorFactory(String token, int precedence, Operation operation) {
this.token = token;
this.precedence = precedence;
this.operation = operation;
}
}
private static class LazyAndFactory implements OperatorFactory {
@Override public int precedence() { return 5; }
@Override public String token() { return "&&"; }
@Override public ParseRes<Node> construct(Source src, int i, Node prev) {
var loc = src.loc(i);
var other = JavaScript.parseExpression(src, i, 6);
if (!other.isSuccess()) return other.chainError(src.loc(i + other.n), "Expected a value after '&&'");
return ParseRes.res(new LazyAndNode(loc, prev, (Node)other.result), other.n);
}
}
private static class LazyOrFactory implements OperatorFactory {
@Override public int precedence() { return 4; }
@Override public String token() { return "||"; }
@Override public ParseRes<Node> construct(Source src, int i, Node prev) {
var loc = src.loc(i);
var other = JavaScript.parseExpression(src, i, 5);
if (!other.isSuccess()) return other.chainError(src.loc(i + other.n), "Expected a value after '||'");
return ParseRes.res(new LazyOrNode(loc, prev, (Node)other.result), other.n);
}
}
public final Node[] args;
public final Operation operation;
@Override public void compileFunctions(CompileResult target) {
for (var arg : args) arg.compileFunctions(target);
}
@Override public void compile(CompileResult target, boolean pollute) {
for (var arg : args) {
arg.compile(target, true);
}
target.add(Instruction.operation(operation));
if (!pollute) target.add(Instruction.discard());
}
public OperationNode(Location loc, Operation operation, Node ...args) {
super(loc);
this.operation = operation;
this.args = args;
}
private static final Map<String, OperatorFactory> factories = Arrays.asList(
new NormalOperatorFactory("*", 13, Operation.MULTIPLY),
new NormalOperatorFactory("/", 12, Operation.DIVIDE),
new NormalOperatorFactory("%", 12, Operation.MODULO),
new NormalOperatorFactory("-", 11, Operation.SUBTRACT),
new NormalOperatorFactory("+", 11, Operation.ADD),
new NormalOperatorFactory(">>", 10, Operation.SHIFT_RIGHT),
new NormalOperatorFactory("<<", 10, Operation.SHIFT_LEFT),
new NormalOperatorFactory(">>>", 10, Operation.USHIFT_RIGHT),
new NormalOperatorFactory(">", 9, Operation.GREATER),
new NormalOperatorFactory("<", 9, Operation.LESS),
new NormalOperatorFactory(">=", 9, Operation.GREATER_EQUALS),
new NormalOperatorFactory("<=", 9, Operation.LESS_EQUALS),
new NormalOperatorFactory("!=", 8, Operation.LOOSE_NOT_EQUALS),
new NormalOperatorFactory("!==", 8, Operation.NOT_EQUALS),
new NormalOperatorFactory("==", 8, Operation.LOOSE_EQUALS),
new NormalOperatorFactory("===", 8, Operation.EQUALS),
new NormalOperatorFactory("&", 7, Operation.AND),
new NormalOperatorFactory("^", 6, Operation.XOR),
new NormalOperatorFactory("|", 5, Operation.OR),
new AssignmentOperatorFactory("=", 2, null),
new AssignmentOperatorFactory("*=", 2, Operation.MULTIPLY),
new AssignmentOperatorFactory("/=", 2, Operation.DIVIDE),
new AssignmentOperatorFactory("%=", 2, Operation.MODULO),
new AssignmentOperatorFactory("-=", 2, Operation.SUBTRACT),
new AssignmentOperatorFactory("+=", 2, Operation.ADD),
new AssignmentOperatorFactory(">>=", 2, Operation.SHIFT_RIGHT),
new AssignmentOperatorFactory("<<=", 2, Operation.SHIFT_LEFT),
new AssignmentOperatorFactory(">>>=", 2, Operation.USHIFT_RIGHT),
new AssignmentOperatorFactory("&=", 2, Operation.AND),
new AssignmentOperatorFactory("^=", 2, Operation.XOR),
new AssignmentOperatorFactory("|=", 2, Operation.OR),
new LazyAndFactory(),
new LazyOrFactory()
).stream().collect(Collectors.toMap(v -> v.token(), v -> v));
private static final List<String> operatorsByLength = factories.keySet().stream().sorted((a, b) -> -a.compareTo(b)).collect(Collectors.toList());
public static ParseRes<OperationNode> parsePrefix(Source src, int i) {
var n = Parsing.skipEmpty(src, i);
var loc = src.loc(i + n);
Operation operation = null;
String op;
if (src.is(i + n, op = "+")) operation = Operation.POS;
else if (src.is(i + n, op = "-")) operation = Operation.NEG;
else if (src.is(i + n, op = "~")) operation = Operation.INVERSE;
else if (src.is(i + n, op = "!")) operation = Operation.NOT;
else return ParseRes.failed();
n++;
var res = JavaScript.parseExpression(src, i + n, 14);
if (res.isSuccess()) return ParseRes.res(new OperationNode(loc, operation, res.result), n + res.n);
else return res.chainError(src.loc(i + n), String.format("Expected a value after the unary operator '%s'.", op));
}
public static ParseRes<OperationNode> parseInstanceof(Source src, int i, Node prev, int precedence) {
if (precedence > 9) return ParseRes.failed();
var n = Parsing.skipEmpty(src, i);
var loc = src.loc(i + n);
var kw = Parsing.parseIdentifier(src, i + n, "instanceof");
if (!kw.isSuccess()) return kw.chainError();
n += kw.n;
var valRes = JavaScript.parseExpression(src, i + n, 10);
if (!valRes.isSuccess()) return valRes.chainError(src.loc(i + n), "Expected a value after 'instanceof'.");
n += valRes.n;
return ParseRes.res(new OperationNode(loc, Operation.INSTANCEOF, prev, valRes.result), n);
}
public static ParseRes<OperationNode> parseIn(Source src, int i, Node prev, int precedence) {
if (precedence > 9) return ParseRes.failed();
var n = Parsing.skipEmpty(src, i);
var loc = src.loc(i + n);
var kw = Parsing.parseIdentifier(src, i + n, "in");
if (!kw.isSuccess()) return kw.chainError();
n += kw.n;
var valRes = JavaScript.parseExpression(src, i + n, 10);
if (!valRes.isSuccess()) return valRes.chainError(src.loc(i + n), "Expected a value after 'in'.");
n += valRes.n;
return ParseRes.res(new OperationNode(loc, Operation.IN, valRes.result, prev), n);
}
public static ParseRes<? extends Node> parseOperator(Source src, int i, Node prev, int precedence) {
var n = Parsing.skipEmpty(src, i);
for (var token : operatorsByLength) {
var factory = factories.get(token);
if (!src.is(i + n, token)) continue;
if (factory.precedence() < precedence) return ParseRes.failed();
n += token.length();
n += Parsing.skipEmpty(src, i + n);
var res = factory.construct(src, i + n, prev);
return res.addN(n);
}
return ParseRes.failed();
}
}

View File

@@ -0,0 +1,56 @@
package me.topchetoeu.j2s.compilation.values.operations;
import me.topchetoeu.j2s.common.Instruction;
import me.topchetoeu.j2s.common.Operation;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.common.parsing.ParseRes;
import me.topchetoeu.j2s.common.parsing.Parsing;
import me.topchetoeu.j2s.common.parsing.Source;
import me.topchetoeu.j2s.compilation.CompileResult;
import me.topchetoeu.j2s.compilation.Node;
import me.topchetoeu.j2s.compilation.patterns.ChangeTarget;
import me.topchetoeu.j2s.compilation.values.constants.NumberNode;
public class PostfixNode extends ChangeNode {
@Override public void compileFunctions(CompileResult target) {
((Node)changable).compileFunctions(target);
}
@Override public void compile(CompileResult target, boolean pollute) {
super.compile(target, pollute);
if (pollute) {
value.compile(target, true);
target.add(Instruction.operation(Operation.ADD));
}
}
public PostfixNode(Location loc, ChangeTarget value, double addAmount) {
super(loc, value, new NumberNode(loc, -addAmount), Operation.SUBTRACT);
}
public static ParseRes<ChangeNode> parsePostfixIncrease(Source src, int i, Node prev, int precedence) {
if (precedence > 15) return ParseRes.failed();
var n = Parsing.skipEmpty(src, i);
var loc = src.loc(i + n);
if (!src.is(i + n, "++")) return ParseRes.failed();
if (!(prev instanceof ChangeTarget)) return ParseRes.error(src.loc(i + n), "Expected assignable value before suffix operator.");
n += 2;
return ParseRes.res(new PostfixNode(loc, (ChangeTarget)prev, 1), n);
}
public static ParseRes<ChangeNode> parsePostfixDecrease(Source src, int i, Node prev, int precedence) {
if (precedence > 15) return ParseRes.failed();
var n = Parsing.skipEmpty(src, i);
var loc = src.loc(i + n);
if (!src.is(i + n, "--")) return ParseRes.failed();
if (!(prev instanceof ChangeTarget)) return ParseRes.error(src.loc(i + n), "Expected assignable value before suffix operator.");
n += 2;
return ParseRes.res(new PostfixNode(loc, (ChangeTarget)prev, -1), n);
}
}

View File

@@ -0,0 +1,51 @@
package me.topchetoeu.j2s.compilation.values.operations;
import me.topchetoeu.j2s.common.Instruction;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.common.parsing.ParseRes;
import me.topchetoeu.j2s.common.parsing.Parsing;
import me.topchetoeu.j2s.common.parsing.Source;
import me.topchetoeu.j2s.compilation.CompileResult;
import me.topchetoeu.j2s.compilation.JavaScript;
import me.topchetoeu.j2s.compilation.Node;
import me.topchetoeu.j2s.compilation.values.VariableNode;
public class TypeofNode extends Node {
public final Node value;
@Override public void compileFunctions(CompileResult target) {
value.compileFunctions(target);
}
@Override public void compile(CompileResult target, boolean pollute) {
if (value instanceof VariableNode varNode) {
target.add(VariableNode.toGet(target, varNode.loc(), varNode.name, true, true));
if (pollute) target.add(Instruction.typeof());
else target.add(Instruction.discard());
return;
}
value.compile(target, pollute);
if (pollute) target.add(Instruction.typeof());
}
public TypeofNode(Location loc, Node value) {
super(loc);
this.value = value;
}
public static ParseRes<TypeofNode> parse(Source src, int i) {
var n = Parsing.skipEmpty(src, i);
var loc = src.loc(i + n);
if (!Parsing.isIdentifier(src, i + n, "typeof")) return ParseRes.failed();
n += 6;
var valRes = JavaScript.parseExpression(src, i + n, 15);
if (!valRes.isSuccess()) return valRes.chainError(src.loc(i + n), "Expected a value after 'typeof' keyword.");
n += valRes.n;
return ParseRes.res(new TypeofNode(loc, valRes.result), n);
}
}

View File

@@ -0,0 +1,39 @@
package me.topchetoeu.j2s.compilation.values.operations;
import me.topchetoeu.j2s.common.Instruction;
import me.topchetoeu.j2s.common.Operation;
import me.topchetoeu.j2s.common.parsing.Location;
import me.topchetoeu.j2s.compilation.CompileResult;
import me.topchetoeu.j2s.compilation.FunctionNode;
import me.topchetoeu.j2s.compilation.Node;
import me.topchetoeu.j2s.compilation.values.VariableNode;
public class VariableAssignNode extends Node {
public final String name;
public final Node value;
public final Operation operation;
@Override public void compileFunctions(CompileResult target) {
value.compileFunctions(target);
}
@Override public void compile(CompileResult target, boolean pollute) {
if (operation != null) {
target.add(VariableNode.toGet(target, loc(), name));
FunctionNode.compileWithName(value, target, true, name);
target.add(Instruction.operation(operation));
target.add(VariableNode.toSet(target, loc(), name, pollute, false)).setLocation(loc());
}
else {
FunctionNode.compileWithName(value, target, true, name);
target.add(VariableNode.toSet(target, loc(), name, pollute, false)).setLocation(loc());
}
}
public VariableAssignNode(Location loc, String name, Node val, Operation operation) {
super(loc);
this.name = name;
this.value = val;
this.operation = operation;
}
}

View File

@@ -0,0 +1,14 @@
package me.topchetoeu.j2s;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class TestHelloWorld {
@Test
public void testHelloWorld() {
final String message = "Hello World!";
assertEquals("Hello World!", message);
}
}