Skip to content

Instantly share code, notes, and snippets.

@brikis98
Created February 6, 2012 08:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brikis98/1750650 to your computer and use it in GitHub Desktop.
Save brikis98/1750650 to your computer and use it in GitHub Desktop.
Seven Languages in Seven Weeks: Io, Day 3
OperatorTable addAssignOperator(":", "atParseHash")
Builder := Object clone do (
indent := ""
atParseHash := method(
key := call evalArgAt(0) asMutable removePrefix("\"") removeSuffix("\"")
value := call evalArgAt(1)
" #{key}=\"#{value}\"" interpolate
)
curlyBrackets := method(
call message arguments map(arg, self doMessage(arg)) join("")
)
forward := method(
arguments := call message arguments
name := call message name
attrs := ""
if(arguments size > 0 and arguments at(0) name == "curlyBrackets",
attrs = doMessage(arguments removeFirst)
)
writeln(indent, "<", name, attrs, ">")
arguments foreach(index, arg,
indent = indent .. " "
content := self doMessage(arg)
if (content type == "Sequence", writeln(indent, content))
indent = indent exclusiveSlice(2)
)
writeln(indent, "</", name, ">")
)
)
doFile("BuilderNewTest.io")
<html>
<head>
<title>
Test webpage
</title>
</head>
<body>
<h1>
Welcome to my test webpage!
</h1>
<div class="content" id="main">
<p>
Lots of fun to be had!
</p>
<p>
Don't forget to sign the guest book
</p>
</div>
</body>
</html>
Builder html(
head(
title("Test webpage")
),
body(
h1("Welcome to my test webpage!"),
div({"class": "content", "id": "main"},
p("Lots of fun to be had!"),
p("Don't forget to sign the guest book")
)
)
)
Builder := Object clone do (
forward := method(
writeln("<", call message name, ">")
call message arguments foreach(arg,
content := self doMessage(arg)
if (content type == "Sequence", writeln(content))
)
writeln("</", call message name, ">")
)
)
doFile("BuilderOriginalTest.io")
<html>
<head>
<title>
Test webpage
</title>
</head>
<body>
<h1>
Welcome to my test webpage!
</h1>
<div>
<p>
Lots of fun to be had!
</p>
<p>
Don't forget to sign the guest book
</p>
</div>
</body>
</html>
Builder html(
head(
title("Test webpage")
),
body(
h1("Welcome to my test webpage!"),
div(
p("Lots of fun to be had!"),
p("Don't forget to sign the guest book")
)
)
)
squareBrackets := method(
call message arguments
)
test := [1, 2, 3, 4]
test println // list(1, 2, 3, 4)
doInTransaction := Object clone do(
curlyBrackets := method(
"Starting transaction" println
call evalArgs
"Ending transaction" println
)
)
doInTransaction {
"do some first thing" println
"do some second thing" println
"do a third thing" println
}
/*
Starting transaction
do some first thing
do some second thing
do a third thing
Ending transaction
*/
@brikis98
Copy link
Author

brikis98 commented Feb 7, 2012

See Seven Languages in Seven Weeks: Io, Day 3 for more information.

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