Advertisement
ice09

7l7w io day 3

Nov 21st, 2011
1,047
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Enhance the XML program to add spaces to show the indentation structure
  2.  
  3. Builder := Object clone
  4. Builder num := 0
  5. Builder forward := method(
  6.     self num := self num+1
  7.     indent(num, "  ")
  8.   writeln("<", call message name, ">")
  9.   call message arguments foreach(
  10.     arg,
  11.     content := self doMessage(arg);
  12.     if(content type == "Sequence",
  13.     indent(num+1, "  ")
  14.     writeln(content)))
  15.     indent(num, "  ")
  16.     self num := self num-1
  17.   writeln("</", call message name, ">"))
  18.  
  19. Builder indent := method(cnt, identifier,
  20.     for(i, 1, cnt, 1, write(identifier))
  21. )
  22.  
  23. # Create a list syntax that uses brackets
  24.  
  25. list := List clone
  26. squareBrackets := method(
  27.   call message arguments foreach(arg,
  28.        call sender list append(arg)
  29.   )
  30. )
  31.  
  32. # Enhance the XML program to handle attribute
  33.  
  34. num := 0
  35.  
  36. OperatorTable addAssignOperator(":", "atPutNumber")
  37. curlyBrackets := method(
  38.   r := Map clone
  39.   call message arguments foreach(arg,
  40.        r doMessage(arg)
  41.        )
  42.   r
  43. )
  44. Map atPutNumber := method(
  45.   self atPut(
  46.        call evalArgAt(0) asMutable removePrefix("\"") removeSuffix("\""),
  47.        "\"" .. call evalArgAt(1) .. "\"")
  48. )
  49. forward := method(
  50.     self num := self num+1
  51.     indent(num, "**")
  52.     argument := ""
  53.  
  54. # this is not nice, but I don't find another way to avoid evaluation
  55.  
  56.     if (call argAt(0) asString exSlice(0, 13) == "curlyBrackets",
  57.         key := call evalArgAt(0) keys at(0)
  58.         argument := " " .. key .. "=" .. call evalArgAt(0) at (key)
  59.         call message arguments remove(0)
  60.     )
  61.   writeln("<", call message name, argument, ">")
  62.   call message arguments foreach(
  63.     arg,
  64.     content := self doMessage(arg);
  65.     if(content type == "Sequence",
  66.     indent(num+1, "**")
  67.     writeln(content)))
  68.     indent(num, "**")
  69.     self num := self num-1
  70.   writeln("</", call message name, ">"))
  71.  
  72. indent := method(cnt, identifier,
  73.     for(i, 1, cnt, 1, write(identifier))
  74. )
  75.  
  76. # Sample
  77.  
  78. ul({"o":"p"},li("Io"),li("JS"),li(a(href({"url":"google.de"},"Google"))))
  79.  
  80. # Result
  81.  
  82. **<ul o="p">
  83. ****<li>
  84. ******Io
  85. ****</li>
  86. ****<li>
  87. ******JS
  88. ****</li>
  89. ****<li>
  90. ******<a>
  91. ********<href url="google.de">
  92. **********Google
  93. ********</href>
  94. ******</a>
  95. ****</li>
  96. **</ul>
  97. ==> nil
Advertisement
RAW Paste Data Copied
Advertisement