Skip to content

Commit

Permalink
Merge pull request #1842 from arturo-lang/add-using-stdlib-function
Browse files Browse the repository at this point in the history
[Core] add new `using` stdlib function
  • Loading branch information
drkameleon authored Dec 30, 2024
2 parents c247cb8 + c9fffef commit 09ce194
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
32 changes: 32 additions & 0 deletions src/library/Core.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1421,6 +1421,38 @@ proc defineLibrary*() =
do:
discard

builtin "using",
alias = unaliased,
op = opNop,
rule = PrefixPrecedence,
description = "execute block with a pre-defined given `this` value",
args = {
"this" : {Any},
"body" : {Block}
},
attrs = NoAttrs,
returns = {Nothing},
example = """
p: #[name: "John" surname: "Doe" age: 38]
using p [
print \name ; access our
print \age ; fields directly
\surname: "Smith" ; or change their value
]
; John
; 38
""":
#=======================================================
prepareLeaklessOne("this")

SetSym("this", x, safe=true)

let evaled: Translation = doEval(y, useStored=false)
execUnscoped(evaled)

finalizeLeaklessOne()

builtin "var",
alias = unaliased,
op = opNop,
Expand Down
2 changes: 1 addition & 1 deletion src/vm/parse.nim
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import lexbase, os, streams
import strutils, tables, unicode

import vm/[errors, profiler, values/value]
import vm/[errors, values/value]
import vm/values/custom/[vquantity, vsymbol]

#=======================================
Expand Down
2 changes: 1 addition & 1 deletion version/build
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3331
3334

0 comments on commit 09ce194

Please sign in to comment.