Skip to content

Commit

Permalink
Add funcitonality for special translation of certain method calls
Browse files Browse the repository at this point in the history
  • Loading branch information
sorig committed Feb 19, 2018
1 parent bb72427 commit bdf66e8
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 68 deletions.
5 changes: 4 additions & 1 deletion examples/meta/generator/targets/cpp.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@
"IntLiteral": "$number",
"RealLiteral": "$number",
"FloatLiteral": "${number}f",
"MethodCall": "$object->$method($arguments)",
"MethodCall": {
"Default": "$object->$method($arguments)",
"get_real": "$object->get<float64_t>($arguments)"
},
"StaticCall": "C$typeName::$method($arguments)",
"GlobalCall": "$method($arguments)",
"Identifier": "$identifier",
Expand Down
4 changes: 3 additions & 1 deletion examples/meta/generator/targets/csharp.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@
"IntLiteral": "$number",
"RealLiteral": "$number",
"FloatLiteral": "${number}f",
"MethodCall": "$object.$method($arguments)",
"MethodCall": {
"Default": "$object.$method($arguments)"
},
"StaticCall": "$typeName.$method($arguments)",
"GlobalCall": "shogun.$method($arguments)",
"Identifier": "$identifier",
Expand Down
41 changes: 39 additions & 2 deletions examples/meta/generator/targets/doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,41 @@
* $name: The name of the variable
* $typeName: The name of the variable's type
* $arguments: Arguments passed to the class constructor
* $kwargs: translated keyword arguments according to "KeywordArguments"
*/
"Construct": "auto $name = some<C$typeName>($arguments)",

/** Keywords:
* $name: The name of the variable
* $typeName: The name of the variable's type
* $expr: translated expression to assign
* $kwargs: translated keyword arguments according to "KeywordArguments"
*/
"Copy": "auto $name = $expr",

"KeywordArguments": {
/** Keywords:
* $elements: the list of elements according to the rules below
*/
"List": "\n$elements",

/** Keywords:
* $name: The name of the variable
* $keyword: The keyword identifier
* $expr: The translated expression associated with the keyword
*/
"Element": "$name.put(\"$keyword\", $expr)",

/** What string should go between the elements?
*/
"Separator": "\n",

/* Should there be an initial separator string when there are any
* normal arguments preceding the keyword arguments?
*/
"InitialSeperatorWhenArgs>0": false
},

/** SGVector and SGMatrix construction rule. Allows to use native types
* in the target languages.
* $name: The name of the variable
Expand Down Expand Up @@ -203,12 +228,15 @@
*/
"FloatLiteral": "${number}f",

/** Keywords:
/** Keywords for all rules in "MethodCall":
* $object: name of the object
* $method: name of the method
* $arguments: argument list passed to the method
*/
"MethodCall": "$object->$method($arguments)",
"MethodCall": {
"Default": "$object->$method($arguments)",
"get_real": "$object->prefix_$method_postfix($arguments)"
},

/** Keywords:
* $typeName: name of class to call the static method on
Expand All @@ -217,6 +245,15 @@
*/
"StaticCall": "C$typeName::$method($arguments)",

/** Keywords:
* $method: method name
* $arguments: argument list passed to the method
* $kwargs: translated keyword arguments (this only happens if the
global function is a factory function that constructs
objects)
*/
"GlobalCall": "$method($arguments)$kwargs",

// Keywords: $identifier
"Identifier": "$identifier",

Expand Down
4 changes: 3 additions & 1 deletion examples/meta/generator/targets/java.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@
"IntLiteral": "$number",
"RealLiteral": "$number",
"FloatLiteral": "${number}f",
"MethodCall": "$object.$method($arguments)",
"MethodCall": {
"Default": "$object.$method($arguments)"
},
"StaticCall": "$typeName.$method($arguments)",
"GlobalCall": "shogun.$method($arguments)",
"Identifier": "$identifier",
Expand Down
4 changes: 3 additions & 1 deletion examples/meta/generator/targets/lua.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
"IntLiteral": "$number",
"RealLiteral": "$number",
"FloatLiteral": "$number",
"MethodCall": "$object:$method($arguments)",
"MethodCall": {
"Default": "$object:$method($arguments)"
},
"StaticCall": "$typeName:$method($arguments)",
"GlobalCall": "$method($arguments)",
"Identifier": "$identifier",
Expand Down
4 changes: 3 additions & 1 deletion examples/meta/generator/targets/octave.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@
"IntLiteral": "$number",
"RealLiteral": "$number",
"FloatLiteral": "$number",
"MethodCall": "$object.$method($arguments)",
"MethodCall": {
"Default": "$object.$method($arguments)"
},
"StaticCall": "$typeName.$method($arguments)",
"GlobalCall": "$method($arguments)",
"Identifier": "$identifier",
Expand Down
4 changes: 3 additions & 1 deletion examples/meta/generator/targets/python.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@
"IntLiteral": "$number",
"RealLiteral": "$number",
"FloatLiteral": "$number",
"MethodCall": "$object.$method($arguments)",
"MethodCall": {
"Default": "$object.$method($arguments)"
},
"StaticCall": "$typeName.$method($arguments)",
"GlobalCall": "$method($arguments)",
"Identifier": "$identifier",
Expand Down
4 changes: 3 additions & 1 deletion examples/meta/generator/targets/r.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
"IntLiteral": "$number",
"RealLiteral": "$number",
"FloatLiteral": "$number",
"MethodCall": "$object$$$method($arguments)",
"MethodCall": {
"Default": "$object$$$method($arguments)"
},
"StaticCall": "$typeName$$$method($arguments)",
"GlobalCall": "$method($arguments)",
"Identifier": "$identifier",
Expand Down
4 changes: 3 additions & 1 deletion examples/meta/generator/targets/ruby.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@
"IntLiteral": "$number",
"RealLiteral": "$number",
"FloatLiteral": "$number",
"MethodCall": "$object.$method($arguments)",
"MethodCall": {
"Default": "$object.$method($arguments)"
},
"StaticCall": "Shogun::$typeName.$method($arguments)",
"GlobalCall": "Shogun::$method($arguments)",
"Identifier": "$identifier",
Expand Down
141 changes: 83 additions & 58 deletions examples/meta/generator/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,68 +543,13 @@ def translateExpr(self, expr, returnKwargs=False):
key = list(expr.keys())[0]

if key == "MethodCall":
template = Template(self.targetDict["Expr"]["MethodCall"])
object = expr[key][0]["Identifier"]
method = expr[key][1]["Identifier"]
argsList = None
try:
argsList = expr[key][2]
except IndexError:
pass
translatedArgsList = self.translateArgumentList(argsList)

assert object in self.variableTypes, \
"Variable {} not initialised".format(identifier)

return template.substitute(object=object,
method=method,
arguments=translatedArgsList)
return self.translateMethodCall(expr[key])

elif key == "StaticCall":
template = Template(self.targetDict["Expr"]["StaticCall"])
type_ = self.translateType(expr[key][0])
method = expr[key][1]["Identifier"]
argsList = None
try:
argsList = expr[key][2]
except IndexError:
pass
translatedArgsList = self.translateArgumentList(argsList)

return template.substitute(typeName=type_,
method=method,
arguments=translatedArgsList)
return self.translateStaticCall(expr[key])

elif key == "GlobalCall":
template = Template(self.targetDict["Expr"]["GlobalCall"])
method = expr[key][0]["Identifier"]
argsList = None
try:
argsList = expr[key][1]
except IndexError:
pass

translatedArgsList, kwargs = self.translateArgumentList(
argsList, returnKwargs=True
)

normalArgs = [
arg for arg in argsList["ArgumentList"]
if not "KeywordArgument" in arg
]
kwargsString = self.translateKwargs(
kwargs, argsGtZero=len(normalArgs) > 0
)

translation = template.substitute(
typeName=type,method=method, arguments=translatedArgsList,
kwargs=kwargsString
)

if returnKwargs:
return translation, kwargs

return translation
return self.translateGlobalCall(expr[key], returnKwargs=returnKwargs)

elif key == "ElementAccess":
return self.translateElementAccess(expr[key])
Expand Down Expand Up @@ -643,6 +588,86 @@ def translateExpr(self, expr, returnKwargs=False):

raise TranslationFailure("Unknown expression type: " + key)

def translateMethodCall(self, methodCall):
""" Translates a method call expression
Args:
methodCall: object like [identifierAST, identifierAST, argumentListAST]
"""

object = methodCall[0]["Identifier"]
method = methodCall[1]["Identifier"]
argsList = None
try:
argsList = methodCall[2]
except IndexError:
pass
translatedArgsList = self.translateArgumentList(argsList)

if object not in self.variableTypes:
raise TranslationFailure("Variable {} not initialised".format(identifier))

template = Template(self.targetDict["Expr"]["MethodCall"]["Default"])
if method in self.targetDict["Expr"]["MethodCall"]:
template = Template(self.targetDict["Expr"]["MethodCall"][method])

return template.substitute(object=object,
method=method,
arguments=translatedArgsList)

def translateStaticCall(self, staticCall):
""" Translates a method call expression
Args:
staticCall: object like [identifierAST, identifierAST, argumentListAST]
"""
template = Template(self.targetDict["Expr"]["StaticCall"])
type_ = self.translateType(staticCall[0])
method = staticCall[1]["Identifier"]
argsList = None
try:
argsList = staticCall[2]
except IndexError:
pass
translatedArgsList = self.translateArgumentList(argsList)

return template.substitute(typeName=type_,
method=method,
arguments=translatedArgsList)

def translateGlobalCall(self, globalCall, returnKwargs):
""" Translates a method call expression
Args:
staticCall: object like [identifierAST, argumentListAST]
"""
template = Template(self.targetDict["Expr"]["GlobalCall"])
method = globalCall[0]["Identifier"]
argsList = None
try:
argsList = globalCall[1]
except IndexError:
pass

translatedArgsList, kwargs = self.translateArgumentList(
argsList, returnKwargs=True
)

normalArgs = [
arg for arg in argsList["ArgumentList"]
if not "KeywordArgument" in arg
]
kwargsString = self.translateKwargs(
kwargs, argsGtZero=len(normalArgs) > 0
)

translation = template.substitute(
typeName=type,method=method, arguments=translatedArgsList,
kwargs=kwargsString
)

if returnKwargs:
return translation, kwargs

return translation

def translatePrint(self, printStmt):
template = Template(self.targetDict["Print"])
return template.substitute(expr=self.translateExpr(printStmt["Expr"]))
Expand Down
2 changes: 2 additions & 0 deletions examples/meta/src/meta_api/getters.sg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Kernel k = kernel("GaussianKernel")
real width = k.get_real("log_width")

0 comments on commit bdf66e8

Please sign in to comment.