Module: Opal
Overview
Opal
A neat language directly into Ruby code.
This is Opal. It has cool syntax. It uses all of the same classes, base, etc. as Ruby!
-
Shell like
-
As fast as Ruby
-
Programs are easily translated
-
Interactive Shell
And much more… Here’s a comparison: Ruby code…
"Hello World".gsub("l", "d") #=> Heddo Wordd
vs. Opal code…
"Hello World" gsub "l" "d" -- => Heddo Wordd
But just to tell you, the Ruby code:
exit 1
or something similar is equivlant to the Opal code:
Kernel exit 1 -- or (System quit)
Why? Because Opal would process the Ruby code as
exit.send(:'1')
‘cause it thinks it’s a method.
I forgot to mention variables. They’re different than your general Ruby variables. No, these are special:
monkey = Animal.new("Monkey")
That’s Ruby, but what about Opal?
%monkey= Animal new "Monkey"
And to get it again:
%monkey
Don’t forget to use this exact syntax,
%monkey = Animal new "Monkey"
or
% monkey
won’t work.
Defined Under Namespace
Modules: Env Classes: OpalCompileError, OpalError, OpalRuntimeError, OpalSyntaxError, OpalSystemError, Version
Instance Method Summary collapse
-
#compile(s, head = true) ⇒ Object
old! The Opal Compiler (to ruby).
-
#interactive ⇒ Object
updated! The Opal interactive mode.
-
#joins(a, sreg = /^"/, ereg = /"$/) ⇒ Object
new! Joins an array based on delimiters.
-
#opal_eval(s, args = [], fname = "(opal-eval)") ⇒ Object
updated! Evaluate!.
-
#run(linz, vars = {}) ⇒ Object
new! The parser.
-
#transform(line) ⇒ Object
old! Transform Opal code into Ruby code.
-
#version ⇒ Object
Get the version.
Instance Method Details
#compile(s, head = true) ⇒ Object
old! The Opal Compiler (to ruby)
270 271 272 273 274 275 276 277 |
# File 'lib/opal.rb', line 270 def compile(s, head=true) rbcode = "" rbcode << "#!/usr/bin/env ruby\n# Compiled by DCWare Opal #{Opal.version.to_s}\nrequire 'opal'\ninclude Opal::Env\ncmdargs = ARGV\n" if head s.split(/;|\n/).each do |line| rbcode << Opal.transform(line) + "\n" end return rbcode end |
#interactive ⇒ Object
updated! The Opal interactive mode. Requires “colored” and “readline”.
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
# File 'lib/opal.rb', line 314 def interactive require 'colored' unless defined?(Colored) require 'readline' unless defined?(Readline) yexit = false v = {:cmdargs => [], :file => "(opal-interactive)"} trap "INT" do yexit = true end until yexit $useglobal = true print "(opal-interactive)".cyan.bold + ">> ".magenta.bold s = (Readline.readline rescue $stdin.readline.chomp) begin rets = [] s.split(';').each do |cm| rets << Opal.run(cm, v).inspect.green.bold end puts "=> ".magenta.bold + rets.join("; ".magenta.bold) rescue SystemExit yexit = true rescue Exception puts ($!.class.name.sub(/^Opal::/, "") + ": ").magenta.bold + $!..red.bold end end $useglobal = nil end |
#joins(a, sreg = /^"/, ereg = /"$/) ⇒ Object
new! Joins an array based on delimiters.
397 398 399 400 401 402 403 404 405 406 407 408 |
# File 'lib/opal.rb', line 397 def joins(a, sreg=/^"/, ereg=/"$/) inst = false a.each do |pt| inst = a.index(pt) if (pt =~ sreg) and not (inst) if (pt =~ ereg) and (inst) rng = inst..(a.index(pt)) a[rng] = a[rng].join(" ") inst = false end end return a end |
#opal_eval(s, args = [], fname = "(opal-eval)") ⇒ Object
updated! Evaluate!
342 343 344 345 346 347 348 |
# File 'lib/opal.rb', line 342 def opal_eval(s, args=[], fname="(opal-eval)") v = {:cmdargs => args, :file => fname} lns = s.split(/\r|\n|;/) lns.each do |line| Opal.run(line, v) end end |
#run(linz, vars = {}) ⇒ Object
new! The parser. may contain traces of regular expressions.
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
# File 'lib/opal.rb', line 352 def run(linz, vars={}) line = linz.gsub(/[ \t]*--[^\n]+/, "") if line =~ /^%[A-Za-z0-9]+\=\ / eq = line.split /\=\ / eq[0].sub! /^%/, "" vars[eq[0].intern] = Opal.run(eq[1]) return true elsif line =~ /^[\ ]*$/ return true else ln = line.split(" ") ln.each do |pt| pt.gsub! "`", " " vars.each do |nm, vl| pt.sub! "%#{nm.to_s}", "vars[#{nm.inspect}]" end end Opal.joins(ln) Opal.joins(ln, /^'/, /'$/) Opal.joins(ln, /^\[/, /\]$/) Opal.joins(ln, /^\{/, /\}$/) on = eval(ln[0], Opal::Env.bind(vars)) rescue nil raise OpalCompileError, "Cannot work on \"#{ln[0]}\" because it does not exist." unless on mt = (ln[1] or "me").intern rgs = [] (ln[2..-1] or []).each do |r| rgs << eval(r) end begin return on.send(mt, *rgs) rescue NameError raise OpalCompileError, $!. rescue TypeError raise OpalSyntaxError, $!. rescue NoMethodError raise OpalCompileError, $!. rescue SyntaxError raise OpalSyntaxError, $!. rescue RuntimeError raise OpalRuntimeError, $!. end end end |
#transform(line) ⇒ Object
old! Transform Opal code into Ruby code.
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/opal.rb', line 280 def transform(line) ret = "" ln = line.split(" ") ln.each do |pt| pt.gsub! "`", " " if pt =~ /^%/ pt[0] = '' ln[ln.index(pt)] = "$vars[:#{pt}]" if $useglobal end end if line =~ /--*/ line[0..1] = '' ret = "##{line}" elsif line =~ /^#!/ elsif line =~ /%*=\ / enil = line.split(/%*=\ /) enil[0][0] = '' c = Opal.transform(enil[1]) if $useglobal ret = "$vars[:#{enil[0]}] = #{c}" else ret = "#{enil[0]} = #{c}" end elsif ln.length > 2 ret = "#{ln[0]}.#{ln[1]}(#{ln[2..-1].join(", ")})" elsif ln.length > 1 ret = "#{ln[0]}.#{ln[1]}" elsif ln.length == 1 ret = ln[0] end return ret end |