Skip to content
Permalink
Browse files
Move static final function names to function classes
- Move static final function names to the static final `NAME` field in their corresponding function classes.
- Add a test that validates that when a `NAME` field exists in a function class, it will return the same value `func.getName()` returns.
  • Loading branch information
Pieter12345 committed Jul 25, 2020
1 parent bcf811d commit bf4419a9b07e5031559f03fc251e3a0bf813db3a
Show file tree
Hide file tree
Showing 23 changed files with 310 additions and 219 deletions.
@@ -46,12 +46,18 @@
import com.laytonsmith.core.extensions.ExtensionManager;
import com.laytonsmith.core.extensions.ExtensionTracker;
import com.laytonsmith.core.functions.Compiler;
import com.laytonsmith.core.functions.Compiler.__autoconcat__;
import com.laytonsmith.core.functions.Compiler.__cbrace__;
import com.laytonsmith.core.functions.Compiler.p;
import com.laytonsmith.core.functions.Compiler.smart_string;
import com.laytonsmith.core.functions.Math.neg;
import com.laytonsmith.core.functions.ControlFlow;
import com.laytonsmith.core.functions.DataHandling;
import com.laytonsmith.core.functions.Function;
import com.laytonsmith.core.functions.FunctionBase;
import com.laytonsmith.core.functions.FunctionList;
import com.laytonsmith.core.functions.IncludeCache;
import com.laytonsmith.core.functions.ArrayHandling.array_get;
import com.laytonsmith.core.natives.interfaces.Mixed;
import com.laytonsmith.persistence.DataSourceException;
import java.io.File;
@@ -1298,7 +1304,7 @@ public static ParseTree compile(TokenStream stream, Environment environment,
constructCount.push(new AtomicInteger(0));
parents.push(tree);

tree.addChild(new ParseTree(new CFunction(Function.__AUTOCONCAT__, unknown), fileOptions));
tree.addChild(new ParseTree(new CFunction(__autoconcat__.NAME, unknown), fileOptions));
parents.push(tree.getChildAt(0));
tree = tree.getChildAt(0);
constructCount.push(new AtomicInteger(0));
@@ -1331,7 +1337,7 @@ public static ParseTree compile(TokenStream stream, Environment environment,
// Brace handling
if(t.type == TType.LCURLY_BRACKET) {
inObjectDefinition = false;
ParseTree b = new ParseTree(new CFunction(Function.__CBRACE__, t.getTarget()), fileOptions);
ParseTree b = new ParseTree(new CFunction(__cbrace__.NAME, t.getTarget()), fileOptions);
tree.addChild(b);
tree = b;
parents.push(b);
@@ -1349,7 +1355,7 @@ public static ParseTree compile(TokenStream stream, Environment environment,
//We need to autoconcat some stuff
int stacks = constructCount.peek().get();
int replaceAt = tree.getChildren().size() - stacks;
ParseTree c = new ParseTree(new CFunction(Function.__AUTOCONCAT__, tree.getTarget()), fileOptions);
ParseTree c = new ParseTree(new CFunction(__autoconcat__.NAME, tree.getTarget()), fileOptions);
List<ParseTree> subChildren = new ArrayList<>();
for(int b = replaceAt; b < tree.numberOfChildren(); b++) {
subChildren.add(tree.getChildAt(b));
@@ -1426,7 +1432,7 @@ public static ParseTree compile(TokenStream stream, Environment environment,
ParseTree myArray = tree.getChildAt(array);
ParseTree myIndex;
if(!emptyArray) {
myIndex = new ParseTree(new CFunction(Function.__AUTOCONCAT__, myArray.getTarget()), fileOptions);
myIndex = new ParseTree(new CFunction(__autoconcat__.NAME, myArray.getTarget()), fileOptions);

for(int j = index; j < tree.numberOfChildren(); j++) {
myIndex.addChild(tree.getChildAt(j));
@@ -1435,14 +1441,14 @@ public static ParseTree compile(TokenStream stream, Environment environment,
myIndex = new ParseTree(new CSlice("0..-1", t.target), fileOptions);
}
tree.setChildren(tree.getChildren().subList(0, array));
ParseTree arrayGet = new ParseTree(new CFunction(Function.ARRAY_GET, t.target), fileOptions);
ParseTree arrayGet = new ParseTree(new CFunction(array_get.NAME, t.target), fileOptions);
arrayGet.addChild(myArray);
arrayGet.addChild(myIndex);

// Check if the @var[...] had a negating "-" in front. If so, add a neg().
if(!minusArrayStack.isEmpty() && arrayStack.size() + 1 == minusArrayStack.peek().get()) {
if(!next1.type.equals(TType.LSQUARE_BRACKET)) { // Wait if there are more array_get's comming.
ParseTree negTree = new ParseTree(new CFunction(Function.NEG, unknown), fileOptions);
ParseTree negTree = new ParseTree(new CFunction(neg.NAME, unknown), fileOptions);
negTree.addChild(arrayGet);
tree.addChild(negTree);
minusArrayStack.pop();
@@ -1461,7 +1467,7 @@ public static ParseTree compile(TokenStream stream, Environment environment,
if(t.type == TType.SMART_STRING) {
if(t.val().contains("@")) {
ParseTree function = new ParseTree(fileOptions);
function.setData(new CFunction(Function.SMART_STRING, t.target));
function.setData(new CFunction(smart_string.NAME, t.target));
ParseTree string = new ParseTree(fileOptions);
string.setData(new CString(t.value, t.target));
function.addChild(string);
@@ -1518,7 +1524,7 @@ public static ParseTree compile(TokenStream stream, Environment environment,
//We need to autoconcat some stuff
int stacks = constructCount.peek().get();
int replaceAt = tree.getChildren().size() - stacks;
ParseTree c = new ParseTree(new CFunction(Function.__AUTOCONCAT__, tree.getTarget()), fileOptions);
ParseTree c = new ParseTree(new CFunction(__autoconcat__.NAME, tree.getTarget()), fileOptions);
List<ParseTree> subChildren = new ArrayList<>();
for(int b = replaceAt; b < tree.numberOfChildren(); b++) {
subChildren.add(tree.getChildAt(b));
@@ -1554,7 +1560,7 @@ public static ParseTree compile(TokenStream stream, Environment environment,
minusArrayStack.push(new AtomicInteger(arrayStack.size() + 1)); // +1 because the bracket isn't counted yet.
} else {
// Negate this function.
ParseTree negTree = new ParseTree(new CFunction(Function.NEG, unknown), fileOptions);
ParseTree negTree = new ParseTree(new CFunction(neg.NAME, unknown), fileOptions);
negTree.addChild(tree.getChildAt(tree.numberOfChildren() - 1));
tree.removeChildAt(tree.numberOfChildren() - 1);
tree.addChildAt(tree.numberOfChildren(), negTree);
@@ -1572,7 +1578,7 @@ public static ParseTree compile(TokenStream stream, Environment environment,
if(constructCount.peek().get() > 1) {
int stacks = constructCount.peek().get();
int replaceAt = tree.getChildren().size() - stacks;
ParseTree c = new ParseTree(new CFunction(Function.__AUTOCONCAT__, unknown), fileOptions);
ParseTree c = new ParseTree(new CFunction(__autoconcat__.NAME, unknown), fileOptions);
List<ParseTree> subChildren = new ArrayList<>();
for(int b = replaceAt; b < tree.numberOfChildren(); b++) {
subChildren.add(tree.getChildAt(b));
@@ -1732,7 +1738,7 @@ public static ParseTree compile(TokenStream stream, Environment environment,
} else if(next1.type.equals(TType.FUNC_NAME)) {
minusFuncStack.push(new AtomicInteger(parens + 1)); // +1 because the function isn't counted yet.
} else {
ParseTree negTree = new ParseTree(new CFunction(Function.NEG, unknown), fileOptions);
ParseTree negTree = new ParseTree(new CFunction(neg.NAME, unknown), fileOptions);
negTree.addChild(new ParseTree(new IVariable(next1.value, next1.target), fileOptions));
tree.addChild(negTree);
constructCount.peek().incrementAndGet();
@@ -2106,7 +2112,7 @@ private static void rewriteAutoconcats(ParseTree root, Environment env,
rewriteAutoconcats(child, env, envs, compilerExceptions);
}
}
if(root.getData() instanceof CFunction && root.getData().val().equals(Function.__AUTOCONCAT__)) {
if(root.getData() instanceof CFunction && root.getData().val().equals(__autoconcat__.NAME)) {
try {
ParseTree ret = ((Compiler.__autoconcat__) ((CFunction) root.getData()).getFunction())
.rewrite(root.getChildren(), true, envs);
@@ -2404,7 +2410,7 @@ private static void optimize(ParseTree tree, Environment env,
//Let's see.
try {
ParseTree root = new ParseTree(
new CFunction(Function.__AUTOCONCAT__, Target.UNKNOWN), tree.getFileOptions());
new CFunction(__autoconcat__.NAME, Target.UNKNOWN), tree.getFileOptions());
Script fakeScript = Script.GenerateScript(root, "*");
// Environment env = null;
// try {
@@ -2464,7 +2470,7 @@ private static void optimize(ParseTree tree, Environment env,
}
}
if(tempNode == Optimizable.REMOVE_ME) {
tree.setData(new CFunction(Function.P, Target.UNKNOWN));
tree.setData(new CFunction(p.NAME, Target.UNKNOWN));
tree.removeChildren();
} else if(tempNode != null) {
tree.setData(tempNode.getData());
@@ -6,7 +6,7 @@
import com.laytonsmith.core.constructs.CFunction;
import com.laytonsmith.core.constructs.CNull;
import com.laytonsmith.core.exceptions.ConfigCompileException;
import com.laytonsmith.core.functions.Function;
import com.laytonsmith.core.functions.Compiler.__cbrace__;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
@@ -92,7 +92,7 @@ protected boolean isValidCodeBlock(ParseTree node) {
* @return
*/
protected static boolean isCodeBlock(ParseTree node) {
return node.getData() instanceof CFunction && node.getData().val().equals(Function.__CBRACE__);
return node.getData() instanceof CFunction && node.getData().val().equals(__cbrace__.NAME);
}

/**
@@ -7,7 +7,8 @@
import com.laytonsmith.core.constructs.CFunction;
import com.laytonsmith.core.constructs.Target;
import com.laytonsmith.core.exceptions.ConfigCompileException;
import com.laytonsmith.core.functions.Function;
import com.laytonsmith.core.functions.ControlFlow._dowhile;
import com.laytonsmith.core.functions.ControlFlow._while;

import java.util.List;

@@ -26,13 +27,14 @@ public int process(List<ParseTree> list, int keywordPosition) throws ConfigCompi
ParseTree codeTree = list.get(keywordPosition + 1);
ParseTree whileTree = list.get(keywordPosition + 2);
this.validateCodeBlock(codeTree, "Missing brace following \"do\" keyword");
if(!(whileTree.getData() instanceof CFunction) || !whileTree.getData().val().equals(Function.WHILE)) {
if(!(whileTree.getData() instanceof CFunction) || !whileTree.getData().val().equals(_while.NAME)) {
throw new ConfigCompileException("Missing while clause following \"do\" keyword", t);
}
if(whileTree.getChildren().isEmpty()) {
throw new ConfigCompileException("Missing argument to while clause", whileTree.getTarget());
}
ParseTree dowhile = new ParseTree(new CFunction(Function.DOWHILE, t), list.get(keywordPosition).getFileOptions());
ParseTree dowhile = new ParseTree(
new CFunction(_dowhile.NAME, t), list.get(keywordPosition).getFileOptions());
dowhile.addChild(Keyword.getArgumentOrNull(codeTree));
dowhile.addChild(whileTree.getChildAt(0));
list.set(keywordPosition, dowhile);
@@ -10,7 +10,7 @@
import com.laytonsmith.core.constructs.CKeyword;
import com.laytonsmith.core.constructs.Target;
import com.laytonsmith.core.exceptions.ConfigCompileException;
import com.laytonsmith.core.functions.Function;
import com.laytonsmith.core.functions.ControlFlow.forelse;

/**
*
@@ -43,7 +43,7 @@ public int process(List<ParseTree> list, int keywordPosition) throws ConfigCompi
forTree.addChild(getArgumentOrNull(codeBlock));
}
// We also have to refactor this into a foreachelse, instead of a foreach.
list.get(keywordPosition).setData(new CFunction(Function.FORELSE, t));
list.get(keywordPosition).setData(new CFunction(forelse.NAME, t));
list.remove(keywordPosition + 1);
}
}
@@ -10,7 +10,7 @@
import com.laytonsmith.core.constructs.CKeyword;
import com.laytonsmith.core.constructs.Target;
import com.laytonsmith.core.exceptions.ConfigCompileException;
import com.laytonsmith.core.functions.Function;
import com.laytonsmith.core.functions.ControlFlow.ifelse;

/**
*
@@ -36,7 +36,7 @@ public int process(List<ParseTree> list, int keywordPosition) throws ConfigCompi
if(nodeIsElseKeyword(list.get(keywordPosition + 2))
&& nodeIsIfFunction(list.get(keywordPosition + 3))) {
// It is, convert this into an ifelse
ParseTree newNode = new ParseTree(new CFunction(Function.IFELSE, t), node.getFileOptions());
ParseTree newNode = new ParseTree(new CFunction(ifelse.NAME, t), node.getFileOptions());
newNode.setChildren(node.getChildren());
node = newNode;
}
@@ -8,7 +8,7 @@
import com.laytonsmith.core.compiler.Keyword;
import com.laytonsmith.core.constructs.CFunction;
import com.laytonsmith.core.exceptions.ConfigCompileException;
import com.laytonsmith.core.functions.Function;
import com.laytonsmith.core.functions.DataHandling._instanceof;

/**
*
@@ -28,7 +28,8 @@ public int process(List<ParseTree> list, int keywordPosition) throws ConfigCompi
if(list.size() <= keywordPosition + 1) {
throw new ConfigCompileException("Expected type to follow \"instanceof\" keyword, but no type was found.", list.get(keywordPosition).getTarget());
}
ParseTree node = new ParseTree(new CFunction(Function.INSTANCEOF, list.get(keywordPosition).getTarget()), list.get(keywordPosition).getFileOptions());
ParseTree node = new ParseTree(new CFunction(
_instanceof.NAME, list.get(keywordPosition).getTarget()), list.get(keywordPosition).getFileOptions());
node.addChild(list.get(keywordPosition - 1));
node.addChild(list.get(keywordPosition + 1));
list.set(keywordPosition - 1, node); // Overwrite the LHS
@@ -8,7 +8,8 @@
import com.laytonsmith.core.compiler.Keyword;
import com.laytonsmith.core.constructs.CFunction;
import com.laytonsmith.core.exceptions.ConfigCompileException;
import com.laytonsmith.core.functions.Function;
import com.laytonsmith.core.functions.BasicLogic.not;
import com.laytonsmith.core.functions.DataHandling._instanceof;

/**
*
@@ -24,14 +25,14 @@ public int process(List<ParseTree> list, int keywordPosition) throws ConfigCompi
if(list.size() <= keywordPosition + 1) {
throw new ConfigCompileException("Expected type to follow \"notinstanceof\" keyword, but no type was found.", list.get(keywordPosition).getTarget());
}
ParseTree node = new ParseTree(new CFunction(Function.INSTANCEOF,
ParseTree node = new ParseTree(new CFunction(_instanceof.NAME,
list.get(keywordPosition).getTarget()), list.get(keywordPosition).getFileOptions());
node.addChild(list.get(keywordPosition - 1));
node.addChild(list.get(keywordPosition + 1));
ParseTree not = new ParseTree(new CFunction(Function.NOT,
ParseTree notNode = new ParseTree(new CFunction(not.NAME,
list.get(keywordPosition).getTarget()), list.get(keywordPosition).getFileOptions());
not.addChild(node);
list.set(keywordPosition - 1, not); // Overwrite the LHS
notNode.addChild(node);
list.set(keywordPosition - 1, notNode); // Overwrite the LHS
list.remove(keywordPosition); // Remove the keyword
list.remove(keywordPosition); // Remove the RHS
return keywordPosition;
@@ -10,7 +10,7 @@
import com.laytonsmith.core.constructs.CKeyword;
import com.laytonsmith.core.constructs.CString;
import com.laytonsmith.core.exceptions.ConfigCompileException;
import com.laytonsmith.core.functions.Function;
import com.laytonsmith.core.functions.DataHandling.proc;

/**
*
@@ -23,23 +23,24 @@ public int process(List<ParseTree> list, int keywordPosition) throws ConfigCompi
if(list.get(keywordPosition).getData() instanceof CKeyword) {
// It's a lone keyword, so we expect some function to follow, which is the proc name + variables
if(list.get(keywordPosition + 1).getData() instanceof CFunction) {
ParseTree proc = new ParseTree(new CFunction(Function.PROC, list.get(keywordPosition).getTarget()), list.get(keywordPosition).getFileOptions());
proc.addChild(new ParseTree(new CString(list.get(keywordPosition + 1).getData().val(),
ParseTree procNode = new ParseTree(new CFunction(
proc.NAME, list.get(keywordPosition).getTarget()), list.get(keywordPosition).getFileOptions());
procNode.addChild(new ParseTree(new CString(list.get(keywordPosition + 1).getData().val(),
list.get(keywordPosition + 1).getTarget()), list.get(keywordPosition + 1).getFileOptions()));
// Grab the functions children, and put them on the stack
for(ParseTree child : list.get(keywordPosition + 1).getChildren()) {
proc.addChild(child);
procNode.addChild(child);
}
if(list.size() > keywordPosition + 2) {
validateCodeBlock(list.get(keywordPosition + 2), "Expected braces to follow proc definition");
proc.addChild(getArgumentOrNull(list.get(keywordPosition + 2)));
procNode.addChild(getArgumentOrNull(list.get(keywordPosition + 2)));
} else {
throw new ConfigCompileException("Expected braces to follow proc definition", list.get(keywordPosition + 1).getTarget());
}
list.remove(keywordPosition); // Remove the keyword
list.remove(keywordPosition); // Remove the function definition
list.remove(keywordPosition); // Remove the cbrace
list.add(keywordPosition, proc); // Add in the new proc definition
list.add(keywordPosition, procNode); // Add in the new proc definition
} else {
throw new ConfigCompileException("Unexpected use of \"proc\" keyword", list.get(keywordPosition).getTarget());
}
@@ -60,7 +61,7 @@ public int process(List<ParseTree> list, int keywordPosition) throws ConfigCompi
}

private boolean nodeIsProcFunction(ParseTree node) {
return node.getData() instanceof CFunction && node.getData().val().equals(Function.PROC);
return node.getData() instanceof CFunction && node.getData().val().equals(proc.NAME);
}

@Override
@@ -7,7 +7,8 @@
import com.laytonsmith.core.constructs.CFunction;
import com.laytonsmith.core.constructs.CKeyword;
import com.laytonsmith.core.exceptions.ConfigCompileException;
import com.laytonsmith.core.functions.Function;
import com.laytonsmith.core.functions.Exceptions._try;
import com.laytonsmith.core.functions.Exceptions.complex_try;

import java.util.List;

@@ -40,13 +41,13 @@ public int process(List<ParseTree> list, int keywordPosition) throws ConfigCompi

// If it's the old version, and a function
if(list.get(keywordPosition).getData() instanceof CFunction
&& list.get(keywordPosition).getData().val().equals(Function.TRY)) {
&& list.get(keywordPosition).getData().val().equals(_try.NAME)) {
return keywordPosition;
}
// Otherwise it's not, and we can continue on, assuming keyword usage.
this.validateCodeBlock(list.get(keywordPosition + 1), "Expecting braces after try keyword");

ParseTree complexTry = new ParseTree(new CFunction(Function.COMPLEX_TRY,
ParseTree complexTry = new ParseTree(new CFunction(complex_try.NAME,
list.get(keywordPosition).getTarget()), list.get(keywordPosition).getFileOptions());
complexTry.addChild(getArgumentOrNull(list.get(keywordPosition + 1)));

@@ -78,7 +78,6 @@
import com.laytonsmith.core.exceptions.EventException;
import com.laytonsmith.core.exceptions.PrefilterNonMatchException;
import com.laytonsmith.core.functions.EventBinding.modify_event;
import com.laytonsmith.core.functions.Function;
import com.laytonsmith.core.functions.StringHandling;
import com.laytonsmith.core.natives.interfaces.Mixed;

@@ -1624,7 +1623,7 @@ public boolean modifyEvent(String key, Mixed value, BindableEvent event) {
if("format".equals(key)) {
String format = Construct.nval(value);
if(format == null) {
throw new CRENullPointerException("The \"format\" key in " + Function.MODIFY_EVENT
throw new CRENullPointerException("The \"format\" key in " + modify_event.NAME
+ " for the " + this.getName()
+ " event may not be null.", value.getTarget());
}

0 comments on commit bf4419a

Please sign in to comment.