Skip to content

Commit

Permalink
Merge pull request #1214 from arturo-lang/convert-System-module-const…
Browse files Browse the repository at this point in the history
…ants-to-methods

[System] Convert all inappropriately-considered constants to methods
  • Loading branch information
drkameleon authored Jun 14, 2023
2 parents 5fe5cff + 6675782 commit d5a70ba
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 53 deletions.
4 changes: 2 additions & 2 deletions examples/rosetta/call a function.art
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ sayHello: $[name][
]

printAll: $[args][
loop args [arg][
print arg
loop args [argv][
print argv
]
]

Expand Down
4 changes: 2 additions & 2 deletions examples/rosetta/variadic function.art
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
; a quasi-variadic function
;-------------------------------------------
variadic: function [args][
loop args 'arg [
print arg
loop args 'argv [
print argv
]
]

Expand Down
24 changes: 0 additions & 24 deletions src/library/Sets.nim
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,6 @@ import helpers/sets

import vm/lib

# proc intersection*[A](s1, s2: OrderedSet[A]): OrderedSet[A] =
# ## Returns the intersection of the sets `s1` and `s2`.
# ##
# ## The same as `s1 * s2 <#*,HashSet[A],HashSet[A]>`_.
# ##
# ## The intersection of two sets is represented mathematically as *A ∩ B* and
# ## is the set of all objects that are members of `s1` and `s2` at the same
# ## time.
# ##
# ## See also:
# ## * `union proc <#union,HashSet[A],HashSet[A]>`_
# ## * `difference proc <#difference,HashSet[A],HashSet[A]>`_
# ## * `symmetricDifference proc <#symmetricDifference,HashSet[A],HashSet[A]>`_

# result = initOrderedSet[A](max(min(s1.len, s2.len), 2))

# # iterate over the elements of the smaller set
# if s1.len < s2.len:
# for item in s1:
# if item in s2: incl(result, item)
# else:
# for item in s2:
# if item in s1: incl(result, item)

#=======================================
# Methods
#=======================================
Expand Down
112 changes: 88 additions & 24 deletions src/library/System.nim
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,81 @@ when not defined(WEB):

proc defineSymbols*() =

# TODO(System) Convert constants to methods
# None of the supposed constants here is actually a constant.
# All of them return something that doesn't change on one hand,
# but that doesn't mean they should be considered as such.
# labels: library, enhancement

constant "arg",
alias = unaliased,
description = "access command-line arguments as a list":
getCmdlineArgumentArray()

constant "args",
alias = unaliased,
description = "a dictionary with all command-line arguments parsed":
newDictionary(parseCmdlineArguments())
builtin "arg",
alias = unaliased,
op = opNop,
rule = PrefixPrecedence,
description = "get command-line arguments as a list",
args = NoArgs,
attrs = NoAttrs,
returns = {Block},
example = """
; called with no parameters
arg ; => []
; called with: 1 two 3
arg ; => ["1" "two" "3"]
""":
push(getCmdlineArgumentArray())

builtin "args",
alias = unaliased,
op = opNop,
rule = PrefixPrecedence,
description = "get all command-line arguments parsed as a dictionary",
args = NoArgs,
attrs = NoAttrs,
returns = {Dictionary},
example = """
; called with: 1 two 3
args
; => #[
; 1
; "two"
; 3
; ]
..........
; called with switches: -c -b
args
; => #[
; c : true
; b : true
; values: []
; ]
; called with switches: -c -b and values: 1 two 3
args
; => #[
; c : true
; b : true
; values: [1 "two" 3]
; ]
..........
; called with named parameters: -c:2 --name:newname myfile.txt
args
; => #[
; c : 2
; name : "newname"
; values: ["myfile.txt"]
; ]
""":
push(newDictionary(parseCmdlineArguments()))

when not defined(WEB):

constant "config",
alias = unaliased,
description = "access global configuration":
Config

# TODO(System\config) add documentation example
# labels: library, documentation, easy
builtin "config",
alias = unaliased,
op = opNop,
rule = PrefixPrecedence,
description = "get global configuration",
args = NoArgs,
attrs = NoAttrs,
returns = {Store},
example = """
""":
push(Config)

# TODO(System\env) could it be used for Web/JS builds too?
# and what type of environment variables could be served or would be useful serve?
Expand Down Expand Up @@ -308,10 +361,21 @@ proc defineSymbols*() =
# TODO(System/script) also add information about the current script being executed
# another location could also be Paths/path
# labels: library,enhancement
constant "script",
alias = unaliased,
description = "embedded information about the current script":
getScriptInfo()

# TODO(System\script) add documentation example
# labels: library, documentation, easy

builtin "script",
alias = unaliased,
op = opNop,
rule = PrefixPrecedence,
description = "get embedded information about the current script",
args = NoArgs,
attrs = NoAttrs,
returns = {Dictionary},
example = """
""":
push(getScriptInfo())

when not defined(WEB):
builtin "superuser?",
Expand Down
5 changes: 5 additions & 0 deletions src/vm/env.nim
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ proc parseCmdlineValue(v: string): Value =

# TODO(Env\parseCmdlineArguments) verify it's working right
# labels: vm,library,language,unit-test

# TODO(Env\parseCmdlineArguments) should return empty values array when no arguments are given
# currently, calling `args` from a script with no arguments returns a values block with the
# script name as the first and only "argument" (which it's not)
# labels: vm,library,language,enhancement
proc parseCmdlineArguments*(): ValueDict =
## parse command-line arguments and return
## result as a Dictionary value
Expand Down
14 changes: 14 additions & 0 deletions src/vm/exec.nim
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,20 @@ template callInternal*(fname: string, getValue: bool, args: varargs[Value]): unt
when getValue:
pop()

# TODO(VM/exec) Leakless blocks not working properly with pre-defined functions
# Let's say we have a pre-defined function (e.g. `arg`) and this symbol is used
# as an iterator variable - which is precisely where leakless blocks come into play -
# this creates a total mess, since their arities are not properly handled.
# e.g.
# ```
# for arr 'arg [
# ; do sth
# ]
# ```
# Also see: https://github.com/arturo-lang/arturo/blob/master/examples/rosetta/call%20a%20function.art
# and https://github.com/arturo-lang/arturo/blob/master/examples/rosetta/variadic%20function.art
# labels: bugs, critical, library, vm, execution

template prepareLeakless*(protected: seq[string] | ValueArray): untyped =
## Prepare for leak-less block execution

Expand Down
2 changes: 1 addition & 1 deletion version/build
Original file line number Diff line number Diff line change
@@ -1 +1 @@
858
861
4 changes: 4 additions & 0 deletions version/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ v0.9.84
- Numbers\clamp: made to work with Range values instead of lower/upper limits + better type support
- Statistics\median: fixed bug + better and more efficient implementation
- Strings\match: added support for Char values as needle
- System\arg: converted to built-in method
- System\args: converted to built-in method
- System\config: converted to built-in method
- System\script: converted to built-in method
- System\sys: added info about CPU endianess, current hostname and re-organized returned information

### Misc
Expand Down

0 comments on commit d5a70ba

Please sign in to comment.