Skip to content

Commit

Permalink
fix macro bugs (the bug about the string may be replaced)
Browse files Browse the repository at this point in the history
  • Loading branch information
magiclu550 committed Feb 9, 2020
1 parent fc37906 commit 7988f53
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 deletions.
4 changes: 0 additions & 4 deletions src/main/java/cn/wenyan/compiler/CompileResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ public void setResult(List<String> result) {
this.result = result;
}

public void setSuccess(boolean success) {
this.success = success;
}

@Override
public String toString() {
return Utils.getWenyanFromArray(result.toArray(new String[0]));
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/cn/wenyan/compiler/PrepareCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import cn.wenyan.compiler.lib.Define;
import cn.wenyan.compiler.lib.Defines;
import cn.wenyan.compiler.streams.FunctionCompileStream;
import cn.wenyan.compiler.utils.GroovyUtils;
import cn.wenyan.compiler.utils.LexerUtils;
import cn.wenyan.compiler.utils.Utils;

Expand Down Expand Up @@ -90,6 +91,7 @@ private String getString(List<String> strs){
return builder.toString();
}

//TODO 替换字符串的问题
private void expansion(List<String> strs){
for(int i = 0;i<strs.size();i++){
int index = i;
Expand All @@ -101,7 +103,7 @@ private void expansion(List<String> strs){

String result = macroMapping.get(v);
for(int j = 0;j<values.size();j++){
result = result.replace(ns.get(j), values.get(j));
result = GroovyUtils.replaceWithOutString(result,ns.get(j), values.get(j));
}
strs.set(index,result);
return;
Expand All @@ -110,6 +112,8 @@ private void expansion(List<String> strs){
}
}



private List<String> getImportsWenYan(List<String> lists){
List<String> list = new ArrayList<>();
FunctionCompileStream stream = compiler.getStream(FunctionCompileStream.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,10 @@

public class VariableCompileStream extends CompileStream{


private Map<String,Integer> arrIndex = new HashMap<>();

private String nowName;

private List<String> nowNames = new ArrayList<>();

private Map<String,String> varMap = new HashMap<>();

private long varIndex = 0;

public VariableCompileStream(WenYanCompilerImpl compiler){
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/cn/wenyan/compiler/utils/GroovyUtils.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,41 @@ class GroovyUtils {
return maxIndex
}

static String replace(String before,String replaced,String after,List range){
int close = 0
StringBuilder afterBuilder = new StringBuilder()
StringBuilder now = new StringBuilder()
int afterStart = 0
for(s in before){
if(s == range[0]){
close ++
}
if(s == range[1]){
close --
}
now.append(s)
if(s == replaced[afterStart]){
afterStart++
}else{
afterStart = 0
afterBuilder.append(now)
now = new StringBuilder()
}
if(afterStart == replaced.size()){
if(close == 0){
afterBuilder.append(after)
}else{
afterBuilder.append(now)
}
afterStart = 0
now = new StringBuilder()
}
}
return afterBuilder
}

static String replaceWithOutString(String before,String replaced,String after){
replace(before,replaced,after,["",""])
}

}

0 comments on commit 7988f53

Please sign in to comment.