Skip to content

Commit

Permalink
Implemented classes with JavaTranspiler
Browse files Browse the repository at this point in the history
  • Loading branch information
ktaeyln committed Apr 9, 2017
1 parent d716934 commit 94f795d
Show file tree
Hide file tree
Showing 7 changed files with 914 additions and 417 deletions.
3 changes: 3 additions & 0 deletions src/net/nexustools/njs/ConstructableFunction.java
Expand Up @@ -21,6 +21,9 @@
*/
public abstract class ConstructableFunction extends AbstractFunction {

public ConstructableFunction(BaseFunction _super, Global global) {
super(_super, global);
}
public ConstructableFunction(Global global) {
super(global);
}
Expand Down
8 changes: 5 additions & 3 deletions src/net/nexustools/njs/compiler/CompiledFunction.java
Expand Up @@ -15,10 +15,7 @@
*/
package net.nexustools.njs.compiler;

import net.nexustools.njs.AbstractFunction;
import net.nexustools.njs.BaseObject;
import net.nexustools.njs.ConstructableFunction;
import net.nexustools.njs.GenericObject;
import net.nexustools.njs.Global;

/**
Expand All @@ -34,4 +31,9 @@ public CompiledFunction(Global global) {
this.global = global;
}

public CompiledFunction(net.nexustools.njs.BaseFunction _super, Global global) {
super(_super, global);
this.global = global;
}

}
8 changes: 8 additions & 0 deletions src/net/nexustools/njs/compiler/CompiledScript.java
Expand Up @@ -142,6 +142,14 @@ public BaseObject exec() {
public BaseObject exec(Global global) {
return exec(global, null);
}

public static BaseFunction lookupSuper(java.lang.String key, Scope scope) {
try {
return (BaseFunction)scope.get(key);
} catch(ClassCastException ex) {
throw new net.nexustools.njs.Error.JavaException("TypeError", "Class extends value " + key + " is not a function or null");
}
}

public static net.nexustools.njs.Number.Instance plusPlusLeft(Global global, BaseObject key, BaseObject _this) {
net.nexustools.njs.Number.Instance incremented = global.wrap(global.Number.fromValueOf(Utilities.get(_this, key)).value + 1);
Expand Down
1,041 changes: 633 additions & 408 deletions src/net/nexustools/njs/compiler/JavaTranspiler.java

Large diffs are not rendered by default.

218 changes: 212 additions & 6 deletions src/net/nexustools/njs/compiler/RegexCompiler.java
Expand Up @@ -134,9 +134,11 @@ public static class ScriptData {
final int rows, columns;
java.lang.Object optimizations;
java.lang.String methodName = null, source;
final Map<java.lang.String, Class> classes;
final Map<java.lang.String, Function> functions;

public ScriptData(Parsed[] impl, java.lang.String source, int rows, int columns) {
classes = new HashMap();
functions = new HashMap();
List<Parsed> imp = new ArrayList();
for (int i = 0; i < impl.length; i++) {
Expand Down Expand Up @@ -532,7 +534,6 @@ public Parsed transform(Parsed part) {
if (part instanceof Lambda) {
Function function = new Function();
function.arguments.add(ref);
function.name = function.uname = "<lambda>";
function.type = Function.Type.Lambda;
function.state = Function.State.InLambda;
function.storage = new LambdaReturn(function);
Expand Down Expand Up @@ -711,6 +712,9 @@ public Call(Parsed original, Parsed ref) {
rows = original.rows;
reference = ref;
}
public Call(Parsed ref) {
this(ref, ref);
}

@Override
public Parsed transform(Parsed part) {
Expand Down Expand Up @@ -874,6 +878,11 @@ public static class Yield extends Rh {
public Yield() {
}

@Override
public boolean isStandalone() {
return true;
}

@Override
public java.lang.String op() {
return "yield";
Expand Down Expand Up @@ -1080,7 +1089,6 @@ public Parsed transform(Parsed part) {
Function function = new Function();
for(Parsed _part : parts)
function.arguments.add(((Reference)_part).ref);
function.name = function.uname = "<lambda>";
function.type = Function.Type.Lambda;
function.state = Function.State.InLambda;
function.storage = new LambdaReturn(function);
Expand Down Expand Up @@ -1156,7 +1164,6 @@ public Parsed transform(Parsed part) {
Function function = new Function();
if(contents != null)
function.arguments.add(((Reference)contents).ref);
function.name = function.uname = "<lambda>";
function.type = Function.Type.Lambda;
function.state = Function.State.InLambda;
function.storage = new LambdaReturn(function);
Expand Down Expand Up @@ -2297,12 +2304,198 @@ public int precedence() {
return -1;
}
}

public static class ClassMethod extends Function {
public enum Type {
Normal,
Constructor,
Getter,
Setter
}
final Class clazz;
Type type = Type.Normal;
public ClassMethod(Class clazz) {
state = State.InArguments;
this.clazz = clazz;
}

@Override
public void complete() {
super.complete();
clazz.complete();
}

}

public static class Class extends Referency {
private enum State {
BeforeName,
AfterName,
AfterExtends,
AfterExtender,
InBody,
GetterStart,
SetterStart,
BeforeMethodArgs,
InMethod,
Complete
}

private State state = State.BeforeName;
ClassMethod currentClassMethod = new ClassMethod(this);
List<ClassMethod> methods = new ArrayList();
java.lang.String name, _extends;

public void complete() {
state = Class.State.InBody;
methods.add(currentClassMethod);
currentClassMethod = new ClassMethod(this);
}

@Override
public java.lang.String toSource() {
StringBuilder builder = new StringBuilder("class ");
builder.append(name);
if(_extends != null) {
builder.append(" extends ");
builder.append(_extends);
}
builder.append(" {\n");
builder.append("}");
return builder.toString();
}

@Override
public int precedence() {
return -1;
}

@Override
public boolean isIncomplete() {
return state != State.Complete;
}

@Override
public Parsed transform(Parsed part) {
switch(state) {
case BeforeName:
if(part instanceof Reference) {
name = ((Reference)part).ref;
state = State.AfterName;
return this;
}
break;
case AfterExtends:
if(part instanceof Reference) {
_extends = ((Reference)part).ref;
state = State.AfterExtender;
return this;
}
break;
case AfterName:
if(part instanceof ExtendsKeyword) {
state = State.AfterExtends;
return this;
}
case AfterExtender:
if(part instanceof OpenGroup) {
state = State.InBody;
return this;
}
break;
case InBody:
if(part instanceof Getter) {
currentClassMethod.type = ClassMethod.Type.Getter;
return this;
}
if(part instanceof Setter) {
currentClassMethod.type = ClassMethod.Type.Setter;
return this;
}
if(part instanceof Constructor) {
currentClassMethod.type = ClassMethod.Type.Constructor;
state = State.BeforeMethodArgs;
return this;
}
if(part instanceof Reference) {
currentClassMethod.name = ((Reference) part).ref;
state = State.BeforeMethodArgs;
return this;
}
if(part instanceof CloseGroup) {
state = State.Complete;
return this;
}
break;
case BeforeMethodArgs:
if(part instanceof OpenBracket) {
state = State.InMethod;
return this;
}
break;
case InMethod:
currentClassMethod.transform(part);
return this;
case Complete:
if(part instanceof NewLine || part instanceof SemiColon)
throw new CompleteException();
}

if(part instanceof NewLine)
return this;

if(state == State.Complete)
return super.transform(part);
throw new Error.JavaException("SyntaxError", "Unexpected " + part + " (" + state + ")");
}



}

public static class ClassKeyword extends Reference {

public ClassKeyword(java.lang.String ref) {
super(ref);
}

}

public static class Super extends ClassKeyword {
public Super() {
super("super");
}
}

public static class ExtendsKeyword extends ClassKeyword {
public ExtendsKeyword() {
super("extends");
}
}

public static class Constructor extends ClassKeyword {
public Constructor() {
super("constructor");
}
}

public static class Getter extends ClassKeyword {
public Getter() {
super("get");
}
}

public static class Setter extends ClassKeyword {
public Setter() {
super("set");
}
}

public static class Extends extends Referency {

@Override
public java.lang.String toSource() {
return "class ";
return "extends";
}

@Override
Expand All @@ -2313,6 +2506,10 @@ public int precedence() {
}

public static class Function extends Referency {

public void complete() {
state = Function.State.Complete;
}

@Override
public int precedence() {
Expand Down Expand Up @@ -2340,7 +2537,6 @@ public static enum State {
Parsed call;
Parsed storage;
java.lang.String name;
java.lang.String uname;
java.lang.String vararg;
List<java.lang.String> arguments = new ArrayList();
State state = State.BeforeName;
Expand Down Expand Up @@ -5134,7 +5330,7 @@ public final ScriptData parse(ParserReader reader, StringBuilder builder) throws
ex.function.impl = ce.impl;
ex.function.impl.callee = ex.function;
ex.function.source = ce.source;
ex.function.state = Function.State.Complete;
ex.function.complete();
builder.append(ce.source);
builder.append(reader.ltrim(1));
}
Expand Down Expand Up @@ -5346,6 +5542,16 @@ public void match(Pattern pattern, Matcher matcher, ParserReader reader) {
throw new PartExchange(new TypeOf(), ref.length());
} else if (ref.equals("case")) {
throw new PartExchange(new Case(), ref.length());
} else if (ref.equals("super")) {
throw new PartExchange(new Super(), ref.length());
} else if (ref.equals("get")) {
throw new PartExchange(new Getter(), ref.length());
} else if (ref.equals("constructor")) {
throw new PartExchange(new Constructor(), ref.length());
} else if (ref.equals("extends")) {
throw new PartExchange(new ExtendsKeyword(), ref.length());
} else if (ref.equals("set")) {
throw new PartExchange(new Setter(), ref.length());
} else if (ref.equals("class")) {
throw new PartExchange(new Class(), ref.length());
} else if (ref.equals("break")) {
Expand Down
5 changes: 5 additions & 0 deletions test/tests/Standards.java
Expand Up @@ -86,6 +86,11 @@ public void numbers() {
test("numbers");
}

@Test
public void _class() {
test("class");
}

@Test
public void group() {
test("group");
Expand Down

0 comments on commit 94f795d

Please sign in to comment.