Skip to content
Permalink
Browse files
Centralize function name references
Replace all hard-coded string function names and `new funcClass().getName()` with a static final field in the `Function` class. This puts all 'static' function references in one place, allowing for shared function name caching and an easier way to find all references to a certain function.
  • Loading branch information
Pieter12345 committed Jul 24, 2020
1 parent 3512ac4 commit bcf811d987b79037d7d00a1286d5212e1ef26623
Show file tree
Hide file tree
Showing 18 changed files with 153 additions and 150 deletions.
@@ -45,7 +45,6 @@
import com.laytonsmith.core.exceptions.ProgramFlowManipulationException;
import com.laytonsmith.core.extensions.ExtensionManager;
import com.laytonsmith.core.extensions.ExtensionTracker;
import com.laytonsmith.core.functions.ArrayHandling;
import com.laytonsmith.core.functions.Compiler;
import com.laytonsmith.core.functions.ControlFlow;
import com.laytonsmith.core.functions.DataHandling;
@@ -1299,7 +1298,7 @@ public static ParseTree compile(TokenStream stream, Environment environment,
constructCount.push(new AtomicInteger(0));
parents.push(tree);

tree.addChild(new ParseTree(new CFunction(__autoconcat__, unknown), fileOptions));
tree.addChild(new ParseTree(new CFunction(Function.__AUTOCONCAT__, unknown), fileOptions));
parents.push(tree.getChildAt(0));
tree = tree.getChildAt(0);
constructCount.push(new AtomicInteger(0));
@@ -1332,7 +1331,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(__cbrace__, t.getTarget()), fileOptions);
ParseTree b = new ParseTree(new CFunction(Function.__CBRACE__, t.getTarget()), fileOptions);
tree.addChild(b);
tree = b;
parents.push(b);
@@ -1350,7 +1349,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(__autoconcat__, tree.getTarget()), fileOptions);
ParseTree c = new ParseTree(new CFunction(Function.__AUTOCONCAT__, tree.getTarget()), fileOptions);
List<ParseTree> subChildren = new ArrayList<>();
for(int b = replaceAt; b < tree.numberOfChildren(); b++) {
subChildren.add(tree.getChildAt(b));
@@ -1427,7 +1426,7 @@ public static ParseTree compile(TokenStream stream, Environment environment,
ParseTree myArray = tree.getChildAt(array);
ParseTree myIndex;
if(!emptyArray) {
myIndex = new ParseTree(new CFunction(__autoconcat__, myArray.getTarget()), fileOptions);
myIndex = new ParseTree(new CFunction(Function.__AUTOCONCAT__, myArray.getTarget()), fileOptions);

for(int j = index; j < tree.numberOfChildren(); j++) {
myIndex.addChild(tree.getChildAt(j));
@@ -1436,14 +1435,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(array_get, t.target), fileOptions);
ParseTree arrayGet = new ParseTree(new CFunction(Function.ARRAY_GET, 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("neg", unknown), fileOptions);
ParseTree negTree = new ParseTree(new CFunction(Function.NEG, unknown), fileOptions);
negTree.addChild(arrayGet);
tree.addChild(negTree);
minusArrayStack.pop();
@@ -1462,7 +1461,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(new Compiler.smart_string().getName(), t.target));
function.setData(new CFunction(Function.SMART_STRING, t.target));
ParseTree string = new ParseTree(fileOptions);
string.setData(new CString(t.value, t.target));
function.addChild(string);
@@ -1519,7 +1518,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("__autoconcat__", tree.getTarget()), fileOptions);
ParseTree c = new ParseTree(new CFunction(Function.__AUTOCONCAT__, tree.getTarget()), fileOptions);
List<ParseTree> subChildren = new ArrayList<>();
for(int b = replaceAt; b < tree.numberOfChildren(); b++) {
subChildren.add(tree.getChildAt(b));
@@ -1555,7 +1554,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("neg", unknown), fileOptions);
ParseTree negTree = new ParseTree(new CFunction(Function.NEG, unknown), fileOptions);
negTree.addChild(tree.getChildAt(tree.numberOfChildren() - 1));
tree.removeChildAt(tree.numberOfChildren() - 1);
tree.addChildAt(tree.numberOfChildren(), negTree);
@@ -1573,7 +1572,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("__autoconcat__", unknown), fileOptions);
ParseTree c = new ParseTree(new CFunction(Function.__AUTOCONCAT__, unknown), fileOptions);
List<ParseTree> subChildren = new ArrayList<>();
for(int b = replaceAt; b < tree.numberOfChildren(); b++) {
subChildren.add(tree.getChildAt(b));
@@ -1733,7 +1732,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("neg", unknown), fileOptions);
ParseTree negTree = new ParseTree(new CFunction(Function.NEG, unknown), fileOptions);
negTree.addChild(new ParseTree(new IVariable(next1.value, next1.target), fileOptions));
tree.addChild(negTree);
constructCount.peek().incrementAndGet();
@@ -2107,7 +2106,7 @@ private static void rewriteAutoconcats(ParseTree root, Environment env,
rewriteAutoconcats(child, env, envs, compilerExceptions);
}
}
if(root.getData() instanceof CFunction && root.getData().val().equals(__autoconcat__)) {
if(root.getData() instanceof CFunction && root.getData().val().equals(Function.__AUTOCONCAT__)) {
try {
ParseTree ret = ((Compiler.__autoconcat__) ((CFunction) root.getData()).getFunction())
.rewrite(root.getChildren(), true, envs);
@@ -2279,14 +2278,6 @@ private static void checkFunctionsExist(ParseTree tree, Set<ConfigCompileExcepti
}
}

// Variable is more clear when named after the function it represents.
@SuppressWarnings("checkstyle:constantname")
private static final String __autoconcat__ = new Compiler.__autoconcat__().getName();
@SuppressWarnings("checkstyle:constantname")
private static final String array_get = new ArrayHandling.array_get().getName();
@SuppressWarnings("checkstyle:constantname")
private static final String __cbrace__ = new Compiler.__cbrace__().getName();

/**
* Recurses down into the tree, attempting to optimize where possible. A few things have strong coupling, for
* information on these items, see the documentation included in the source.
@@ -2412,7 +2403,8 @@ private static void optimize(ParseTree tree, Environment env,
//However, as a special function, we *might* be able to get a const proc out of this
//Let's see.
try {
ParseTree root = new ParseTree(new CFunction(__autoconcat__, Target.UNKNOWN), tree.getFileOptions());
ParseTree root = new ParseTree(
new CFunction(Function.__AUTOCONCAT__, Target.UNKNOWN), tree.getFileOptions());
Script fakeScript = Script.GenerateScript(root, "*");
// Environment env = null;
// try {
@@ -2472,7 +2464,7 @@ private static void optimize(ParseTree tree, Environment env,
}
}
if(tempNode == Optimizable.REMOVE_ME) {
tree.setData(new CFunction("p", Target.UNKNOWN));
tree.setData(new CFunction(Function.P, Target.UNKNOWN));
tree.removeChildren();
} else if(tempNode != null) {
tree.setData(tempNode.getData());
@@ -6,6 +6,8 @@
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 java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@@ -22,11 +24,7 @@
*/
public abstract class Keyword implements Documentation {

@SuppressWarnings("checkstyle:constantname") // Variable is more clear when named after the function it represents.
private static final String __CBRACE__ = new com.laytonsmith.core.functions.Compiler.__cbrace__().getName();

protected Keyword() {
//
}

/**
@@ -94,7 +92,7 @@ protected boolean isValidCodeBlock(ParseTree node) {
* @return
*/
protected static boolean isCodeBlock(ParseTree node) {
return node.getData() instanceof CFunction && node.getData().val().equals(__CBRACE__);
return node.getData() instanceof CFunction && node.getData().val().equals(Function.__CBRACE__);
}

/**
@@ -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.ControlFlow;
import com.laytonsmith.core.functions.Function;

import java.util.List;

/**
@@ -16,9 +17,6 @@
@Keyword.keyword("do")
public class DoKeyword extends Keyword {

private static final String WHILE = new ControlFlow._while().getName();
private static final String DOWHILE = new ControlFlow._dowhile().getName();

@Override
public int process(List<ParseTree> list, int keywordPosition) throws ConfigCompileException {
// We expect the format to be "do" __cbracket__ while, so if this is not the case, we will
@@ -28,13 +26,13 @@ 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(WHILE)) {
if(!(whileTree.getData() instanceof CFunction) || !whileTree.getData().val().equals(Function.WHILE)) {
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(DOWHILE, t), list.get(keywordPosition).getFileOptions());
ParseTree dowhile = new ParseTree(new CFunction(Function.DOWHILE, t), list.get(keywordPosition).getFileOptions());
dowhile.addChild(Keyword.getArgumentOrNull(codeTree));
dowhile.addChild(whileTree.getChildAt(0));
list.set(keywordPosition, dowhile);
@@ -1,5 +1,7 @@
package com.laytonsmith.core.compiler.keywords;

import java.util.List;

import com.laytonsmith.PureUtilities.Version;
import com.laytonsmith.core.MSVersion;
import com.laytonsmith.core.ParseTree;
@@ -8,17 +10,14 @@
import com.laytonsmith.core.constructs.CKeyword;
import com.laytonsmith.core.constructs.Target;
import com.laytonsmith.core.exceptions.ConfigCompileException;
import com.laytonsmith.core.functions.ControlFlow;
import java.util.List;
import com.laytonsmith.core.functions.Function;

/**
*
*/
@Keyword.keyword("for")
public class ForKeyword extends Keyword {

private static final String FORELSE = new ControlFlow.forelse().getName();

@Override
public int process(List<ParseTree> list, int keywordPosition) throws ConfigCompileException {
ParseTree forTree = list.get(keywordPosition);
@@ -44,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(FORELSE, t));
list.get(keywordPosition).setData(new CFunction(Function.FORELSE, t));
list.remove(keywordPosition + 1);
}
}
@@ -1,5 +1,7 @@
package com.laytonsmith.core.compiler.keywords;

import java.util.List;

import com.laytonsmith.PureUtilities.Version;
import com.laytonsmith.core.MSVersion;
import com.laytonsmith.core.ParseTree;
@@ -8,17 +10,14 @@
import com.laytonsmith.core.constructs.CKeyword;
import com.laytonsmith.core.constructs.Target;
import com.laytonsmith.core.exceptions.ConfigCompileException;
import com.laytonsmith.core.functions.ControlFlow;
import java.util.List;
import com.laytonsmith.core.functions.Function;

/**
*
*/
@Keyword.keyword("if")
public class IfKeyword extends Keyword {

private static final String IFELSE = new ControlFlow.ifelse().getName();

@Override
public int process(List<ParseTree> list, int keywordPosition) throws ConfigCompileException {
ParseTree node = list.get(keywordPosition);
@@ -37,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(IFELSE, t), node.getFileOptions());
ParseTree newNode = new ParseTree(new CFunction(Function.IFELSE, t), node.getFileOptions());
newNode.setChildren(node.getChildren());
node = newNode;
}
@@ -1,22 +1,21 @@
package com.laytonsmith.core.compiler.keywords;

import java.util.List;

import com.laytonsmith.PureUtilities.Version;
import com.laytonsmith.core.MSVersion;
import com.laytonsmith.core.ParseTree;
import com.laytonsmith.core.compiler.Keyword;
import com.laytonsmith.core.constructs.CFunction;
import com.laytonsmith.core.exceptions.ConfigCompileException;
import com.laytonsmith.core.functions.DataHandling;
import java.util.List;
import com.laytonsmith.core.functions.Function;

/**
*
*/
@Keyword.keyword("instanceof")
public class InstanceofKeyword extends Keyword {

private static final String INSTANCEOF = new DataHandling._instanceof().getName();

@Override
public int process(List<ParseTree> list, int keywordPosition) throws ConfigCompileException {
if(list.get(keywordPosition).getData() instanceof CFunction) {
@@ -29,7 +28,7 @@ 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(INSTANCEOF, list.get(keywordPosition).getTarget()), list.get(keywordPosition).getFileOptions());
ParseTree node = new ParseTree(new CFunction(Function.INSTANCEOF, 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
@@ -1,24 +1,21 @@
package com.laytonsmith.core.compiler.keywords;

import java.util.List;

import com.laytonsmith.PureUtilities.Version;
import com.laytonsmith.core.MSVersion;
import com.laytonsmith.core.ParseTree;
import com.laytonsmith.core.compiler.Keyword;
import com.laytonsmith.core.constructs.CFunction;
import com.laytonsmith.core.exceptions.ConfigCompileException;
import com.laytonsmith.core.functions.BasicLogic;
import com.laytonsmith.core.functions.DataHandling;
import java.util.List;
import com.laytonsmith.core.functions.Function;

/**
*
*/
@Keyword.keyword("notinstanceof")
public class NotInstanceofKeyword extends Keyword {

private static final String INSTANCEOF = new DataHandling._instanceof().getName();
private static final String NOT = new BasicLogic.not().getName();

@Override
public int process(List<ParseTree> list, int keywordPosition) throws ConfigCompileException {
if(keywordPosition == 0) {
@@ -27,10 +24,12 @@ 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(INSTANCEOF, list.get(keywordPosition).getTarget()), list.get(keywordPosition).getFileOptions());
ParseTree node = new ParseTree(new CFunction(Function.INSTANCEOF,
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(NOT, list.get(keywordPosition).getTarget()), list.get(keywordPosition).getFileOptions());
ParseTree not = new ParseTree(new CFunction(Function.NOT,
list.get(keywordPosition).getTarget()), list.get(keywordPosition).getFileOptions());
not.addChild(node);
list.set(keywordPosition - 1, not); // Overwrite the LHS
list.remove(keywordPosition); // Remove the keyword

0 comments on commit bcf811d

Please sign in to comment.