use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class CafeBabePyAstCreateVisitor method visitTerm.
@Override
public PyObject visitTerm(PythonParser.TermContext ctx) {
List<PythonParser.FactorContext> factorContextList = ctx.factor();
int factorIndex = 0;
PyObject factor = visitFactor(factorContextList.get(factorIndex));
factorIndex++;
int count = ctx.getChildCount();
for (int i = 1; i < count; i += 2) {
String op = ctx.getChild(i).getText();
PyObject operator;
switch(op) {
case "*":
operator = this.runtime.newPyObject("_ast.Mult");
break;
case "%":
operator = this.runtime.newPyObject("_ast.Mod");
break;
default:
throw this.runtime.newRaiseException("builtins.SyntaxError", "op '" + op + "' is not found");
}
PyObject rightFactor = visitFactor(factorContextList.get(factorIndex));
factorIndex++;
factor = this.runtime.newPyObject("_ast.BinOp", factor, operator, rightFactor);
}
return factor;
}
use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class CafeBabePyAstCreateVisitor method visitFuncdef.
@Override
public PyObject visitFuncdef(PythonParser.FuncdefContext ctx) {
PyObject name = this.runtime.str(ctx.NAME().getText());
PyObject args = this.runtime.None();
PythonParser.ParametersContext parametersContext = ctx.parameters();
if (parametersContext != null) {
args = visitParameters(parametersContext);
}
PyObject body = visitSuite(ctx.suite());
// FIXME 未実装
PyObject decorator_list = this.runtime.None();
// 無視
PyObject returns = this.runtime.None();
PythonParser.TestContext testContext = ctx.test();
if (testContext != null) {
returns = visitTest(testContext);
}
return this.runtime.newPyObject("_ast.FunctionDef", name, args, body, decorator_list, returns);
}
use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class CafeBabePyAstCreateVisitor method visitAtomToTestlist_comp.
private PyObject visitAtomToTestlist_comp(PyObject testList_Comp, String comp, String structure) {
PyObject load = this.runtime.newPyObject("_ast.Load");
List<PyObject> resultVisitList = this.runtime.toList(testList_Comp);
int comprehensionCount = 0;
PyObject type = this.runtime.typeOrThrow("_ast.comprehension");
for (int i = 1; i < resultVisitList.size(); i++) {
if (this.runtime.isInstance(resultVisitList.get(i), type)) {
comprehensionCount++;
}
}
if (0 < comprehensionCount) {
if (resultVisitList.size() - 1 != comprehensionCount) {
throw this.runtime.newRaiseException("builtins.SyntaxError", "Invalid comprehension");
}
PyObject elt = resultVisitList.get(0);
PyObject generators = this.runtime.list(resultVisitList.subList(1, resultVisitList.size()));
return this.runtime.newPyObject(comp, elt, generators);
} else {
return this.runtime.newPyObject(structure, testList_Comp, load);
}
}
use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class CafeBabePyAstCreateVisitor method visitTrailer.
@Override
public PyObject visitTrailer(PythonParser.TrailerContext ctx) {
if (ctx.NAME() != null) {
PyObject attr = this.runtime.str(ctx.NAME().getText());
PyObject load = this.runtime.newPyObject("_ast.Load");
return this.runtime.newPyObject("_ast.Attribute", this.runtime.None(), attr, load);
}
String firstText = ctx.getChild(0).getText();
String lastText = ctx.getChild(ctx.getChildCount() - 1).getText();
if ("(".equals(firstText) && ")".equals(lastText)) {
PythonParser.ArglistContext arglistContext = ctx.arglist();
PyObject arglist = this.runtime.None();
if (arglistContext != null) {
arglist = visitArglist(arglistContext);
}
// FIXME keywords
return this.runtime.newPyObject("_ast.Call", this.runtime.None(), arglist, this.runtime.None());
}
throw this.runtime.newRaiseException("builtins.SyntaxError", "Invalid ast");
}
use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class CafeBabePyAstCreateVisitor method visitTypedargslist.
@Override
public PyObject visitTypedargslist(PythonParser.TypedargslistContext ctx) {
List<PythonParser.TfpdefContext> tfpdefContextList = ctx.tfpdef();
PyObject[] argArray = new PyObject[tfpdefContextList.size()];
for (int i = 0; i < argArray.length; i++) {
argArray[i] = visitTfpdef(tfpdefContextList.get(i));
}
PyObject args = this.runtime.list(argArray);
PyObject vararg = this.runtime.None();
PyObject kwonlyargs = this.runtime.None();
PyObject kw_defaults = this.runtime.None();
PyObject kwarg = this.runtime.None();
PyObject defaults = this.runtime.None();
return this.runtime.newPyObject("_ast.arguments", args, vararg, kwonlyargs, kw_defaults, kwarg, defaults);
}
Aggregations