Skip to content

Commit

Permalink
Small tinkerings and additions.
Browse files Browse the repository at this point in the history
  • Loading branch information
greggirwin committed Apr 26, 2018
1 parent bb04346 commit eb71941
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 9 deletions.
18 changes: 16 additions & 2 deletions composite.red
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ composite-ctx: context [
ctx [none! object!] "Evaluate expr in the given context; none means use global context"
/local res
][
expr: load expr
if error? set/any 'res try [expr: load expr][
;return any [err-val form reduce [" *** Error: Invalid expression Where:" expr "*** "]]
cause-error 'syntax 'invalid [arg1: 'composite-expression arg2: expr]
]
; If expression evaluates to a non-block value that is anything other than a
; word, we can't bind it.
if all [ctx any [block? :expr word? :expr]][bind expr ctx]
Expand All @@ -34,12 +37,23 @@ composite-ctx: context [
; with a refinement, and then just have to choose the default.
; Putting the colons on the outside gives you a clean paren expression
; on the inside.
; `Compose` could be extended, to work as-is for blocks, but add support for
; this behavior for strings. The extra refinements are an issue, though.
; They don't conflict with the existing `compose` refinements, but we
; have to see how they might cause confusion given the different behaviors.
set 'composite func [
"Replace :( ... ): sections in a string with their evaluated results."
"Returns a copy of a string, evaluating :( ... ): sections"
;"Replace :( ... ): sections in a string with their evaluated results"
;"Returns a copy of a string, replacing :( ... ): sections with their evaluated results"
data [string! file! url!]
/marks markers [block!] "Use custom expression markers in place of :( and ):"
/with ctx [object!] "Evaluate the expressions in the given context"
/err-val e "Use instead of formed error info from eval error"
; /into might be useful, but it also complicates things, given the current implementation.
; Need to weigh the value. If we always create or use the out buffer, rather than changing
; the input data in place, it won't add much complexity.
;/into "Put results in `out`, instead of creating a new string"
; out [string!] "Target for results, when /into is used"
/local expr expr-beg= expr-end= pos
][
if all [marks not parse markers [2 [char! | string! | tag!]]][
Expand Down
4 changes: 4 additions & 0 deletions format-date-time.red
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,10 @@ date-time-formatting: context [
case [
string? fmt [format-date-time-via-mask value fmt]
block? fmt [format-date-time-via-accessors value fmt]
;!! If we check for named formats first, and just set the format
; to the string format associated with the name, we eliminate
; the recursive call. There are a few exception cases, where we
; just FORM the value, and where we force it to be a UTC time.
'else [
; named formats
switch/default fmt [
Expand Down
9 changes: 7 additions & 2 deletions format.red
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ Red [
- mold-logic (DONE)
- Composite (DONE)
- as-ordinal (DONE)
- block interpolation
- string interpolation
- date format
- format-number-via-masks
}
TBD: {
- Determine exact format-by-width/short-form+prec behavior! It seems like the
Expand Down Expand Up @@ -565,6 +570,7 @@ formatting: context [
rejoin [round/to size scale any [ch ""] units/1]
]
]

; Should this also support integers, so format-number doesn't have to call this
; func? Really, it could support any value that can be converted to logic, but
; is that more helpful to the user, or will it make things more confusing for
Expand Down Expand Up @@ -984,8 +990,7 @@ formatting: context [
C-hex [join "0x" to-hex to integer! n]
;VB-hex [join "&H" to-hex to integer! n]
;octal [] ; maybe useful for things like `chmod 755` viz ; no enbase for octal yet
bin [num-to-bin-str n]
binary [num-to-bin-str n]
bin binary [num-to-bin-str n]
min-bin [ ; No leading zeros
either zero? n [""] [
form find num-to-bin-str n complement charset "0"
Expand Down
2 changes: 1 addition & 1 deletion formatting-functions.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ details start to pop up.
- Format a block of values
- Support formats suited to each datatype
- Interpolate formatted values into a block
- Interpolate values in a string
- Interpolate formatted values in a string
- Create a formatted row
- Create a formatted column
- Create a table
Expand Down
9 changes: 5 additions & 4 deletions short-format.red
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ short-format-ctx: context [
value: case [ ; Reassign 'value to string result for later padding
spec/style [apply-format-style value spec/style] ; A named format style was used
not number? :value [form any [:value ""]] ; Coerce none to ""; form to prevent arg modifcation
; ?? What about FALSE?
'else [
suffix: either all [integer? value flag? spec #"º"] [ordinal-suffix value][""]
append mold absolute value suffix ; Note: absolute; no sign here
Expand Down Expand Up @@ -274,10 +275,10 @@ short-format-ctx: context [
; then want a way to skip to a new index in the values.
case [
none? spec/key [if series? data [take data]] ; unkeyed field, take sequentially from data
integer? spec/key [pick-val data spec/key] ; index key
paren? spec/key [do-paren spec/key] ; expression to evaluate
path? spec/key [get-path-key data spec/key] ; deep key
'else [ ; simple key name
integer? spec/key [pick-val data spec/key] ; index key
paren? spec/key [do-paren spec/key] ; expression to evaluate
path? spec/key [get-path-key data spec/key] ; deep key
'else [ ; simple key name
;?? Do we want to allow functions? I'm not so sure.
val: select data spec/key
either any-function? :val [val][val]
Expand Down
4 changes: 4 additions & 0 deletions test-composite.red
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ foreach val [
")::("
":(1):beg):"
"asdf:(1):beg):"
":(1/a/b/c):"
":(2abc):"
][test-bad-composite val]

print "^/Composite/Marks"
Expand All @@ -83,6 +85,8 @@ foreach val [
"a:(a + b):b"
][test-composite-with val o]



print ""

halt
1 change: 1 addition & 0 deletions test-format.red
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

do %format.red
do %short-format.red
do %format-date-time.red

;-------------------------------------------------------------------------------

Expand Down

0 comments on commit eb71941

Please sign in to comment.