Skip to content

Instantly share code, notes, and snippets.

@chriseth
Created May 31, 2017 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chriseth/107c5202dd1ac47eab3fbc1434ea97b7 to your computer and use it in GitHub Desktop.
Save chriseth/107c5202dd1ac47eab3fbc1434ea97b7 to your computer and use it in GitHub Desktop.
Sketch of use of templating language for the julia code generator
bool ExpresionCompiler::visit(BinaryExpression const& _expr)
{
// noteResult returns the julia-equivalent of the expression.
// it also inserts a comment containing the source location of _expr
// Anything inside angle brackets is looked up in the table passed
// as third argument and a plain string replacement is performed.
// The convert function calls visit recursively (or instanciates a
// new AST walker) and returns what was supplied to "noteResult".
noteResult(
_expr.location(),
"<operation>(<left>, <right>)",
{
{"operation", operationToBuiltinFunction(_expr.operation())}
{"left", convert(_expr.leftOperand())},
{"right", convert(_expre.rightOperand())}
}
);
return false;
}
string JuliaCompiler::buildDispatcher(ContractDefinition const& _contract)
{
vector<map<string, string>> functions;
for (auto const& function: _contract.definedFunctions())
{
if (!function->isPartOfExternalInterface())
continue;
functions.push_back({});
functions.back()["identifier"] = toHex(FixedHash<4>::Arith(FixedHash<4>(dev::keccak256(function->externalSignature()))), HexPrefix::Add);
functions.back()["funname"] = function->name();
functions.back()["payablecheck"] = "";
if (!function->isPayable())
functions.back()["payablecheck"] = "ensureNoValueTransfer()";
}
// This template also employs array-replacement.
// The value of "functions" in the mapping (third argument)
// is a vector of mappings. Evaluating this template results
// in as many copies of the content inside [...] as there are
// elements in this vector. Template replacement is also applied
// to this content, but only the identifiers in this element
// are available.
evalTemplate(
_contract.location(),
R"(
switch extractCallSignature()
<functions[
case <identifier> { <payablecheck> _<funname>() }
]>
default { <fallback> }
)",
{
{"functions", functions},
{"fallback", _contract.fallbackFunction() ? "fallback()" : "revert()" }
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment