Skip to content

Instantly share code, notes, and snippets.

@meijeru
Last active June 10, 2018 19:51
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 meijeru/6f4d37547a2ec16b9502812eb11c2dcd to your computer and use it in GitHub Desktop.
Save meijeru/6f4d37547a2ec16b9502812eb11c2dcd to your computer and use it in GitHub Desktop.
Red program to show application of to-string, form and mold(/all) on values of all types
Red [
Title: "String conversion explorer"
Purpose: {Show application of to-string, form and mold(/all) on all types}
Author: "Rudolf W. MEIJER"
File: %form-explorer.red
Date: 8-Jun-2018
Notes: {Baed on earlier attempt for Rebol}
Language: 'English
Tabs: 4
]
;---|----1----|----2----|----3----|----4----|----5----|----6----|----7----|-
; not yet (fully) implemented: point!
; not having any literals nor make constructors: handle! event!
form-file: %form.adoc
test-values: [
; type loadable expression for a typical value
unset! {( )}
none! {#[none]}
logic! {#[true]}
char! {#"a"}
integer! {123}
float! {123.456}
percent! {50%}
pair! {1x2}
tuple! {1.2.3}
date! {now/date}
time! {now/time}
word! {'abc}
set-word! {quote abc:}
lit-word! {quote 'abc}
get-word! {quote :abc}
refinement! {/abc}
issue! {#abc}
block! {[1 2 3]}
paren! {quote (1 2 3)}
hash! {make hash! [1 2 3]}
path! {'a/b/c}
lit-path! {quote 'a/b/c}
set-path! {quote a/b/c:}
get-path! {quote :a/b/c}
string! {"abc"}
file! {%abc.def}
url! {http://abc.def}
tag! {<a href="red-lang.org">}
email! {user@host.com}
vector! {make vector! [1 2 3]}
image! {make image! 2x2}
binary! {#{313233}}
bitset! {charset "abc"}
map! {#(a: 1 b: 2)}
object! {object [a: 1 b: 2]}
error! {try [1 / 0]}
native! {:if}
action! {:add}
op! {:+}
function! {func [a b][a + b]}
routine! {:event?}
datatype! {action!}
typeset! {number!}
; point!
; handle!
; event!
]
context [
funcs: [to-string form mold mold/all]
set 'generate-form-table function [
][
;-- auxiliary functions
emit: func [line] [append buff rejoin [reduce line]]
html-ize: func [value] [
replace/all value "<" "&lt;"
replace/all value ">" "&gt;"
]
buff: copy ""
;-- create table and fill in headers
emit "### String conversions^/"
emit {[options="header"]^/}
emit "|===^/^/"
emit "|type |expression"
foreach fnc funcs [emit ["|" form fnc]]
emit "^/"
;-- loop over types and expressions
foreach [type expr] test-values [
emit ["|*" form type "*"]
emit ["|*" html-ize copy expr "*"]
set/any 'val reduce load expr
;-- multi-literal expressions are enclosed in a block by load
;-- even if they result in only one value
if all [
block? :val
type <> 'block!
][
val: first val
]
;-- loop over the four functions
foreach fnc funcs [
either error? res: try [do reduce [fnc first [:val]]]
[
emit "|_error_"
][
;-- shorten the representation to show the essentials
case [
"make" = copy/part res 4 [
append clear find/tail res "!" " ..."
]
"routine" = copy/part res 7 [
append clear at res 8 "[...][...]"
]
"func" = copy/part res 4 [
append clear at res 5 "[...][...]"
]
type == 'error! [
append clear find/tail res "Error:" " ..."
]
]
;-- HTML will not show the empty string as such
either empty? res
[
emit "|_empty string_"
][
emit ["|" html-ize res]
]
]
]
emit "^/"
]
emit "|===^/"
buff
]
]
attempt [delete form-file]
write form-file generate-form-table
ask "OK"
halt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment