lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Genius :)

-----Original Message----- From: Axel Kittenberger
Sent: Monday, August 22, 2011 7:00 PM
To: Lua mailing list
Subject: [ANN] Smileua 1^.^0, a language that smiles

Inspired by MoonScript I too made a compiles-to-lua-language to
sweeten your Monday. Decades over debates about syntax(1) of languages
and the rise and fall of languages was actually pendulum between the
two antagonists expressivity vs. error-prevention. Smileua  on the
other hand opens a neglected new dimension! While it might be worse
than Lua on both of this old two dimensions it has this warm cozy
feeling of being smiled at and being appreciated when looking at the
code. The compiler(2) is of course(!) written in Smileua itself and
added to this email, so you can see how to code smiles at you. It
might look gibberish to the untrained eye, but isn't the smiley worth
it!? Since email tends to mangle source, you can get it here along
with the language definitions and an already self-compiled Lua-script
for boot strapping:
http://homepage.univie.ac.at/axel.kittenberger/smileua/index.html

(1) but not semantics.
(2) compiler is an overstatement, actually preprocessor as it works on
token basis only

(^.^)

-------------------------------------------------------
#!/usr/bin/lua
smileua = {:
["{:"]   = {: r = "{"         :},
[":}"]   = {: r = "}"         :},
[":~)"]  = {: r = "not"       :},
[":a)"]  = {: r = "and"       :},
[":o)"]  = {: r = "or"        :},
[":+)"]  = {: r = "+"         :},
[":-)"]  = {: r = "-", nw = 1 :}, --:) no warn
[":*)"]  = {: r = "*"         :},
["/:)"]  = {: r = "/"         :},
[":=)"]  = {: r = "=", nw = 1 :},
[":==)"] = {: r = "=="        :},
[":~=)"] = {: r = "~="        :},
[":>"]   = {: r = ">"         :},
["<:"]   = {: r = "<"         :},
[":=>"]  = {: r = ">="        :},
["<=:"]  = {: r = "<="        :},
["[:?"]  = {: r = "if"        :},
["[^:"]  = {: r = "then"      :},
[":^]"]  = {: r = "else"      :},
[":^]?"] = {: r = "elseif"    :},
["[:"]   = {: r = "do"        :},
[":]"]   = {: r = "end"       :},
["(:"]   = {: r = "("         :},
[":)"]   = {: r = ")"         :},
["(:)"]  = {: r = "()"        :},
["?:)"]  = {: r = "for"       :},
["?:]"]  = {: r = "while"     :},
[":?}"]  = {: r = "repeat"    :},
["{?:"]  = {: r = "until"     :},
["=:}"]  = {: r = "local"     :},
["^.^"]  = {: r = "function"  :},
["\\o/"] = {: r = "return"    :},
[":^)"]  = {: r = "break"     :},
:}
--:) copies reverses as warnings into the language table
[:
=:} warns = {: :}
?:) k, v in pairs(:smileua:) [:
[:? :~) warns[v.r] :a) :~) v.nw [^: warns[v.r] = {: r = k, warn = 1 :} :]
:]
?:) k, v in pairs(:warns:) [:
[:? :~) smileua[k] [^: smileua[k] = v :]
:]
:]
--:) argument handling
^.^ err(:...:) io.stderr:write(:...:); os.exit(:1:) :]
[:? #arg :==) 1 :a) arg[1]:sub(:-3:) :==) ".8p" [^: arg[2] =
arg[1]:sub(:0, -3:).."lua" :]
[:? #arg :~=) 2 [^: err(:"Usage: ",arg[0],"  [SMILEUA-IN] [LUA-OUT]\n":) :]
--:) load input
fi = io.open(:arg[1]:)
[:? :~) fi [^: err(:"Cannot read "..arg[1]:) :]
si = fi:read(:"*a":) --:) input string
[:? si :==) null [^: err(: "Cannot load "..arg[1] :) :]
fi:close(:)
pi = 1     --:) input pointer
ao = {: :} --:) output array
po = 1     --:) output pointer
ln = 1     --:) line nunmber
wc = 0     --:) number of warnings
alphanum = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"
c = si:sub(:1, 1:) --:) current character
^.^ string:has(:s:) \o/ self:find(:s, 1, true:) :]
--:) increments input pointer :a) get current char
^.^ pinc(:n:)
[:? :~) n [^: n = 1 :]
pi = pi :+) n
c = si:sub(:pi, pi:)
cc = si:sub(:pi, pi :+) 1:)
[:? c :==) "\n" [^: ln = ln :+) 1 :]
:]
^.^ opush(:o:) ao[po] = o; po = po :+) 1 :]
[:? c :==) "#" [^:                                             --:) hashbang
:?} pinc(:); {?: c :==) "\n" :o) pi :> #si
pinc(:);
opush(:si:sub(:1, pi :-) 1:):)
:]
?:] pi <=: #si [:
[:? (:" \t\n":):has(:c:) [^:  --:) space
opush(:c:); pinc(:)
:^]? alphanum:has(:c:) [^:       --:) normal token or number
=:} pis = pi
:?}
pinc(:)
{?: :~) alphanum:has(:c:) :o) pi :> #si
=:} t = si:sub(:pis, pi :-) 1:)
=:} ts = smileua[t]
[:? ts :a) ts.warn [^:
io.stderr:write(:ln,":) missed a smile. use ",ts.r," instead of ",t,"\n":)
wc = wc :+) 1
:]
opush(:t:)
:^]? c :==) "'" :o) c :==) '"' [^:            --:) string constant
=:} pis = pi; =:} cs = c
:?} pinc(:); [:? c :==) "\\" [^: pinc(:) :] {?: c :==) cs :o) pi :> #si
pinc(:);
opush(:si:sub(:pis, pi :-) 1:):)
:^]? cc :==) "--" [^:
=:} pis = pi
:?} pinc(:) {?: c :==) "\n" :o) pi :> #si
opush(:si:sub(:pis, pi :-) 1:):)
:^]? cc :==) "[[" [^:
=:} pis = pi; :?} pinc(:) {?: cc :==) "]]" :o) pi :> #si
opush(:si:sub(:pis, pi :-) 1:):)
:^]                                         --:) control chars
=:} tl = 4
?:] tl :> 0 [:
=:} t = si:sub(:pi, pi :+) tl :-) 1:)
=:} v = smileua[t]
[:? v [^:
[:? v.warn [^:
io.stderr:write(:ln,":) missed a smile. use ",v.r," instead of ",t,"\n":)
opush(:t:); wc = wc :+) 1
:^]
opush(:v.r:)
:]
pinc(:tl:)
:^)
:]
tl = tl :-) 1
:]
[:? tl :==) 0 [^: opush(:c:); pinc(:) :]
:]
:]
[:? arg[2] :==) "-" [^:
fo = io.stdout
:^]
fo = io.open(:arg[2],"w":)
[:? :~) fo [^: err(: "Cannot write "..arg[2] :) :]
:]
fo:write(:table.concat(:ao:):)
fo:close(:)
[:? wc :> 0 [^: io.stderr:write(: "Missed ",wc," opportunities to
smiley\n" :) :]
---------Eo)F-------