comparison 2-Io/day3-DSL.io @ 43:d2764720ea17

Add the Day 3 'Domain Specific Language'
author IBBoard <dev@ibboard.co.uk>
date Mon, 18 Sep 2017 20:45:32 +0100
parents
children 6347bacb3be7
comparison
equal deleted inserted replaced
42:54608c26bda2 43:d2764720ea17
1 # Creating a simple "domain-specific language"
2 # (Except it's not really - it's just a little extension to support
3 # the "{ key:value[, key:value[…]] }" format of specifying dictionaries)
4
5 # Because this is a string then we can do it at any time!
6 OperatorTable addAssignOperator(":", "atPutThing")
7
8 # curlyBrackets is an automatically invoked function from the parser whenever
9 # it encounters curly brackets.
10 # This is hidden black magic that you need to know about to override it.
11 # It appears to call the method for all of the content within the brackets.
12 curlyBrackets := method(
13 map := Map clone
14 call message arguments foreach(arg,
15 map doMessage(arg)
16 )
17 map
18 )
19
20 Map atPutThing := method(
21 self atPut(
22 call evalArgAt(0) asMutable removePrefix("\"") removeSuffix("\""),
23 call evalArgAt(1)
24 )
25 )
26
27 the_string := "{
28 \"foo\": \"12345\",
29 \"bar\": \"7890\"
30 }"
31
32 phoneNumbers := doString(the_string)
33 phoneNumbers keys println
34 phoneNumbers values println
35
36 # But presumably this will break, because there's no "atPutThing" method
37 a := 1
38 b := 5
39 range := a : b
40 # Yep - "Exception: Number does not respond to ':'"
41 # So you've got to be careful for reusable operators