Skip to content

Instantly share code, notes, and snippets.

@jomontanari
Created November 23, 2011 04:12
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 jomontanari/1387869 to your computer and use it in GitHub Desktop.
Save jomontanari/1387869 to your computer and use it in GitHub Desktop.
IO Day 3
Builder := Object clone
Builder indent := 0
Builder writeIndent := method(for(i, 1, indent, " " print))
Builder forward := method(
self writeIndent
writeln("<", call message name, ">")
indent = indent +1
call message arguments foreach(
arg,
content := self doMessage(arg);
if (content type == "Sequence", for(i, 1, indent, " " print; writeln(content))
)
indent = indent -1
self writeIndent
writeln("</", call message name, ">")
)
Builder body(
ul(
li("Io"),
li("Lua"),
li("JavaScript")
)
)
squareBrackets := method(
r := List clone
call message arguments foreach(arg,
r append(arg)
)
r
)
my_list := doString("[2, 4, 6]")
my_list println
OperatorTable addAssignOperator(":", "atPutAttribute")
curlyBrackets := method(
r := Map clone
call message arguments foreach(arg,
r doString(arg asString)
)
r
)
Map atPutAttribute := method(
self atPut(
call evalArgAt(0),
call evalArgAt(1)
)
)
Xml_Element := Object clone
Xml_Element write_xml := method(
write("<", name)
attributes foreach(key, val, write_attribute(key, val))
writeln(">")
contents foreach(c, writeln(c))
children foreach(write_xml)
writeln("</", name, ">")
)
Xml_Element write_attribute := method(key, val, write(" \"", key, "\"=\"", val, "\""))
Builder := Object clone
Builder writeIndent := method(for(i, 1, indent, " " print))
Builder xml_elements := List clone
Builder forward := method(
xml_element := Xml_Element clone
xml_element name := call message name
xml_element attributes := Map clone
xml_element contents := List clone
xml_element children := List clone
xml_elements append(xml_element)
call message arguments foreach(
arg,
content := self doMessage(arg);
if (content type == "Map", xml_element attributes mergeInPlace(content))
if (content type == "Sequence", xml_element contents append(content))
if (content type != "Map" and content type != "Sequence", xml_element children append(content))
)
if (xml_elements first == xml_element, xml_element write_xml)
xml_element
)
Builder xml(
book({ author : "Tate", Title : "Something" }),
book("A great read")
)
@sriki77
Copy link

sriki77 commented Dec 4, 2011

Hi Jo,
2-list-brackets - will not work for [1,2,3,[4,5],6,7]

Srikanth

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment