diff --git a/CodeGen/BMapBindings/ExpFctDecl.java b/CodeGen/BMapBindings/ExpFctDecl.java index abb4f90..9eecefb 100644 --- a/CodeGen/BMapBindings/ExpFctDecl.java +++ b/CodeGen/BMapBindings/ExpFctDecl.java @@ -6,9 +6,9 @@ public class ExpFctDecl { public VariableType mFctRetType; public Vector mFctParams; - public ExpFctDecl(String fct_name, VariableType ret_type) { - mFctName = fct_name; - mFctRetType = ret_type; + public ExpFctDecl() { + mFctName = ""; + mFctRetType = new VariableType(); mFctParams = new Vector(); } diff --git a/CodeGen/BMapBindings/ExpFctParamDecl.java b/CodeGen/BMapBindings/ExpFctParamDecl.java index c941b69..d4b6368 100644 --- a/CodeGen/BMapBindings/ExpFctParamDecl.java +++ b/CodeGen/BMapBindings/ExpFctParamDecl.java @@ -3,10 +3,12 @@ public class ExpFctParamDecl { public VariableType mVarType; public String mVarName; + public boolean mIsInput; - public ExpFctParamDecl(VariableType vtype, String vname) { - mVarType = vtype; - mVarName = vname; + public ExpFctParamDecl() { + mVarType = new VariableType(); + mVarName = ""; + mIsInput = true; } } diff --git a/CodeGen/BMapBindings/ExpFctsParser.g4 b/CodeGen/BMapBindings/ExpFctsParser.g4 index 9bbae20..0b7fb84 100644 --- a/CodeGen/BMapBindings/ExpFctsParser.g4 +++ b/CodeGen/BMapBindings/ExpFctsParser.g4 @@ -16,6 +16,6 @@ fctArg ; varType - : EXPFCTS_IDENTIFIER ('::' EXPFCTS_IDENTIFIER)* '*'? + : EXPFCTS_IDENTIFIER ('::' EXPFCTS_IDENTIFIER)* '*'* ; diff --git a/CodeGen/BMapBindings/ExpFctsWalker.java b/CodeGen/BMapBindings/ExpFctsWalker.java index 487ef2d..59b4127 100644 --- a/CodeGen/BMapBindings/ExpFctsWalker.java +++ b/CodeGen/BMapBindings/ExpFctsWalker.java @@ -1,6 +1,123 @@ +import java.util.Collections; +import java.util.Vector; +import java.util.stream.Collectors; + import org.antlr.v4.runtime.*; import org.antlr.v4.runtime.tree.*; public class ExpFctsWalker extends ExpFctsParserBaseListener { - + + public ExpFctsWalker() { + mFctList = new Vector(); + mCurrentFct = null; + mCurrentParam = null; + } + + public Vector getResult() { + return mFctList; + } + + private Vector mFctList; + private ExpFctDecl mCurrentFct; + private ExpFctParamDecl mCurrentParam; + + @Override + public void enterProgram(ExpFctsParser.ProgramContext ctx) { + mFctList.clear(); + } + + @Override + public void enterFctDecl(ExpFctsParser.FctDeclContext ctx) { + mCurrentFct = new ExpFctDecl(); + } + + @Override + public void exitFctDecl(ExpFctsParser.FctDeclContext ctx) { + mCurrentFct.mFctName = ctx.EXPFCTS_IDENTIFIER().getText(); + mFctList.add(mCurrentFct); + mCurrentFct = null; + } + + @Override + public void exitFctArgFileDecl(ExpFctsParser.FctArgFileDeclContext ctx) { + ExpFctParamDecl decl = new ExpFctParamDecl(); + decl.mVarName = ctx.EXPFCTS_IDENTIFIER().getText(); + decl.mIsInput = true; + decl.mVarType.fromCType("BMap::BMFile*"); + mCurrentFct.mFctParams.add(decl); + } + + @Override + public void exitFctArgMeshTransDecl(ExpFctsParser.FctArgMeshTransDeclContext ctx) { + ExpFctParamDecl decl = new ExpFctParamDecl(); + decl.mVarName = ctx.EXPFCTS_IDENTIFIER().getText(); + decl.mIsInput = true; + decl.mVarType.fromCType("BMap::BMMeshTransition*"); + mCurrentFct.mFctParams.add(decl); + } + + @Override + public void exitFctArgObjDecl(ExpFctsParser.FctArgObjDeclContext ctx) { + ExpFctParamDecl first_decl = new ExpFctParamDecl(); + first_decl.mVarName = ctx.EXPFCTS_IDENTIFIER(0).getText(); + first_decl.mIsInput = true; + first_decl.mVarType.fromCType("BMap::BMFile*"); + mCurrentFct.mFctParams.add(first_decl); + + ExpFctParamDecl second_decl = new ExpFctParamDecl(); + second_decl.mVarName = ctx.EXPFCTS_IDENTIFIER(1).getText(); + second_decl.mIsInput = true; + second_decl.mVarType.fromCType("LibCmo::CK2::CK_ID"); + mCurrentFct.mFctParams.add(second_decl); + } + + @Override + public void enterFctArgParamIn(ExpFctsParser.FctArgParamInContext ctx) { + mCurrentParam = new ExpFctParamDecl(); + } + + @Override + public void exitFctArgParamIn(ExpFctsParser.FctArgParamInContext ctx) { + mCurrentParam.mVarName = ctx.EXPFCTS_IDENTIFIER().getText(); + mCurrentParam.mIsInput = true; + + mCurrentFct.mFctParams.add(mCurrentParam); + mCurrentParam = null; + } + + @Override + public void enterFctArgParamOut(ExpFctsParser.FctArgParamOutContext ctx) { + mCurrentParam = new ExpFctParamDecl(); + } + + @Override + public void exitFctArgParamOut(ExpFctsParser.FctArgParamOutContext ctx) { + mCurrentParam.mVarName = ctx.EXPFCTS_IDENTIFIER().getText(); + mCurrentParam.mIsInput = false; + // set to its pointer type + //mCurrentParam.mVarType = mCurrentParam.mVarType.getPointerOfThis(); + + mCurrentFct.mFctParams.add(mCurrentParam); + mCurrentParam = null; + } + + @Override + public void exitVarType(ExpFctsParser.VarTypeContext ctx) { + // get namespace parts and join them + String ctype = ctx.EXPFCTS_IDENTIFIER().stream().map(value -> value.getText()) + .collect(Collectors.joining("::")); + // add star if necessary + if (ctx.EXPFCTS_STAR() != null) { + ctype += String.join("", Collections.nCopies(ctx.EXPFCTS_STAR().size(), "*")); + } + + if (!mCurrentFct.mFctRetType.isValid()) { + // fill function ret type first + mCurrentFct.mFctRetType.fromCType(ctype); + } else { + // otherwise, fill param data + mCurrentParam.mVarType.fromCType(ctype); + } + } + } diff --git a/CodeGen/BMapBindings/MainRunner.java b/CodeGen/BMapBindings/MainRunner.java index c989517..d6207a6 100644 --- a/CodeGen/BMapBindings/MainRunner.java +++ b/CodeGen/BMapBindings/MainRunner.java @@ -1,10 +1,28 @@ +import java.io.FileInputStream; +import java.nio.charset.StandardCharsets; +import java.util.Vector; + import org.antlr.v4.runtime.*; import org.antlr.v4.runtime.tree.*; public class MainRunner { public static void main(String[] args) throws Exception { + // get interface structture + FileInputStream fs = new FileInputStream("dest/BMExports.hpp"); + CharStream antlrfs = CharStreams.fromStream(fs, StandardCharsets.UTF_8); + ExpFctsLexer lexer = new ExpFctsLexer(antlrfs); + CommonTokenStream tokens = new CommonTokenStream(lexer); + ExpFctsParser parser = new ExpFctsParser(tokens); + ParseTree tree = parser.program(); + ParseTreeWalker walker = new ParseTreeWalker(); + ExpFctsWalker worker = new ExpFctsWalker(); + walker.walk(worker, tree); + fs.close(); + + Vector result = worker.getResult(); + // print message. System.out.println("DONE!"); } diff --git a/CodeGen/BMapBindings/VariableType.java b/CodeGen/BMapBindings/VariableType.java index d26aff9..ac61c84 100644 --- a/CodeGen/BMapBindings/VariableType.java +++ b/CodeGen/BMapBindings/VariableType.java @@ -1,21 +1,26 @@ import java.util.Collections; +import java.util.Vector; +import java.util.stream.Collectors; public class VariableType { /** - * The base type of this variable which remove all ending stars. + * The base type of this variable which remove all ending stars. Each item is a + * part of namespace string. If no namespace, this Vector will only have one + * item. */ - private String mBaseType; + private Vector mBaseType; /** * The pointer level of this type. It is equal with the count of stars. */ private int mPointerLevel; - public VariableType(String ctype) { - fromCType(ctype); + public VariableType() { + mBaseType = new Vector(); + mPointerLevel = 0; } - - private VariableType(String base_type, int pointer_level) { - mBaseType = base_type; + + private VariableType(Vector base_type, int pointer_level) { + mBaseType.addAll(base_type); mPointerLevel = pointer_level; } @@ -23,35 +28,48 @@ public class VariableType { if (ctype.isEmpty()) throw new IllegalArgumentException("empty string can not be parsed."); + // get pointer part and name part int len = ctype.length(); int star_pos = ctype.indexOf('*'); + String namepart; if (star_pos == -1) { // no star - mBaseType = ctype; + namepart = ctype; mPointerLevel = 0; } else { // has star if (star_pos == 0) throw new IllegalArgumentException("base type not found."); - mBaseType = ctype.substring(0, star_pos); + namepart = ctype.substring(0, star_pos); mPointerLevel = len - star_pos; } + + // resolve name part + mBaseType.clear(); + for (String item : namepart.split("::")) { + mBaseType.add(item); + } } public String toCType() { - return mBaseType + String.join("", Collections.nCopies(mPointerLevel, "*")); + return mBaseType.stream().collect(Collectors.joining("::")) + + String.join("", Collections.nCopies(mPointerLevel, "*")); } public String getBaseType() { - return mBaseType; + return mBaseType.lastElement(); } public boolean isPointer() { return mPointerLevel != 0; } + public boolean isValid() { + return mBaseType.size() != 0; + } + public VariableType getPointerOfThis() { return new VariableType(mBaseType, mPointerLevel); } - + }