From b13f970f966c850e0155091806b3fbad6bdbbddc Mon Sep 17 00:00:00 2001 From: Jacques Wagener Date: Thu, 27 Jun 2019 14:06:51 +0200 Subject: [PATCH] Add support for only handling annoted public functions. --- eth_abi_utils.nim | 5 +++++ eth_macros.nim | 27 ++++++++++++++++++--------- examples/wrc202.nim | 11 +++++++++-- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/eth_abi_utils.nim b/eth_abi_utils.nim index afb102b..ee61aa5 100644 --- a/eth_abi_utils.nim +++ b/eth_abi_utils.nim @@ -23,6 +23,7 @@ type payable*: bool method_id*: string method_sig*: string + is_private*: bool proc generate_method_sig*(func_sig: FunctionSignature, v2_sig: bool = false): string = @@ -59,6 +60,10 @@ proc generate_function_signature*(proc_def: NimNode): FunctionSignature = case child.kind: of nnkIdent: func_sig.name = strVal(child) + func_sig.is_private = true + of nnkPostfix: + func_sig.name = strVal(child[1]) + func_sig.is_private = false of nnkFormalParams: for param in child: case param.kind diff --git a/eth_macros.nim b/eth_macros.nim index e161fd3..47e1492 100644 --- a/eth_macros.nim +++ b/eth_macros.nim @@ -4,7 +4,7 @@ import system import strformat import tables import endians - +import sequtils import ./eth_abi_utils, ./builtin_keywords @@ -24,14 +24,18 @@ type # Global temp variables create at beginning of the function. keyword_define_stmts: NimNode # Map of temp variables that have to be replaced by. - global_keyword_map: Table[string, string] + global_keyword_map: Table[string, string] proc get_func_name(proc_def: NimNode): string = var func_name = "" for child in proc_def: + if child.kind == nnkPostfix: + func_name = strVal(child[1]) + break if child.kind == nnkIdent: func_name = strVal(child) + break return func_name @@ -150,6 +154,14 @@ proc handle_contract_interface(stmts: NimNode): NimNode = discard # raise newException(ParserError, ">> Invalid stmt \"" & getTypeInst(child) & "\" not supported in contract block") + echo function_signatures + + if filter(function_signatures, proc(x: FunctionSignature): bool = x.is_private).len == 0: + raise newException( + ParserError, + "No public functions have defined, use * postfix to annotate public functions." + ) + # Build Main Entrypoint. var out_stmts = newStmtList() out_stmts.add( @@ -188,15 +200,12 @@ proc handle_contract_interface(stmts: NimNode): NimNode = newIdentNode("selector") ) - # selector_CaseStmt.add(newIdentNode("selector")) - # nnkStmtList.newTree( - # nnkCall.newTree( - # newIdentNode("hello"), - # newIdentNode("a") - # ) - # ) + # Build function selector. for func_sig in function_signatures: + if func_sig.is_private: + echo "!!!" + continue echo "Building " & func_sig.method_sig var call_and_copy_block = nnkStmtList.newTree() var call_to_func = nnkCall.newTree( diff --git a/examples/wrc202.nim b/examples/wrc202.nim index 73a9ae4..bac136e 100644 --- a/examples/wrc202.nim +++ b/examples/wrc202.nim @@ -7,8 +7,8 @@ import endians import macros import stint - -contract("MyContract"): +expandMacros: + contract("MyContract"): # proc becomeKing(): uint256 = # var a: int32 @@ -16,10 +16,17 @@ contract("MyContract"): # a += 1.int32 # return a.stuint(256) + proc get_sender222(): address = + if true: + return msg.sender + proc get_sender(): address = if true: return msg.sender + proc publicGetSender*(): address = + return msg.sender + # getCaller(addr tmp_addr) # proc addition(a: uint256, b: uint256): uint256 = # return a + b