Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Avoid swaps in for loops.

Better? Worse? Meh.
  • Loading branch information...
commit 49f754d3a16a5e8855921dc1d2bd375bb1cf84e2 1 parent dcfe4d7
@matthewd authored
View
9 README
@@ -0,0 +1,9 @@
+
+rasp
+=====
+
+A VBScript runtime, running on the Rubinius VM.
+
+
+Untested, undocumented, and ill-advised.
+
View
14 bin/rasp
@@ -6,15 +6,19 @@ $: << File.join(File.dirname(File.dirname(__FILE__)), "lib")
require 'rasp'
-filename = ARGV.shift || 't/math.vbs'
+filename = File.exist?(ARGV.first) ? ARGV.shift : 't/math.vbs'
parse = Rasp::Parser.parse(File.read(filename), :consume => true)
#puts parse.dump
-parse.value.graph
+parse.value.graph if ARGV.grep /^--graph$/
#p parse.value
-$DEBUG = true
-compiled = Rasp::Compiler.new.compile(parse.value, filename)
+old_debug, $DEBUG = $DEBUG, ARGV.grep(/^--decode$/)
+begin
+ compiled = Rasp::Compiler.new.compile(parse.value, filename)
+ensure
+ $DEBUG = old_debug
+end
-#compiled.call
+compiled.call if ARGV.grep /^--run$/
View
14 lib/rasp/ast/expression.rb
@@ -6,6 +6,10 @@ def initialize(inner)
@inner = inner
end
+ def bytecode(g)
+ @inner.bytecode(g)
+ op_bytecode g
+ end
def prescan(g)
@inner.prescan(g)
end
@@ -46,6 +50,9 @@ def op_bytecode(g)
end
end
class NotOp < UnaryOp
+ def op_bytecode(g)
+ g.send_vcall :~
+ end
end
class Comparison < BinaryOp
attr_accessor :operator
@@ -69,11 +76,10 @@ def bytecode(g)
case @lhs
when Rasp::AST::StringAppend
- # Already dupped
@lhs.bytecode(g)
when Rasp::AST::String
- # Was a literal
@lhs.bytecode(g)
+ g.string_dup
else
@lhs.bytecode(g)
g.send_vcall :to_s
@@ -95,12 +101,12 @@ def op_bytecode(g)
end
class UnaryPlus < UnaryOp
def op_bytecode(g)
- g.send_vcall :"@+"
+ g.send_vcall :+@
end
end
class UnaryMinus < UnaryOp
def op_bytecode(g)
- g.send_vcall :"@-"
+ g.send_vcall :-@
end
end
View
21 lib/rasp/ast/flow.rb
@@ -88,7 +88,6 @@ def bytecode(g)
unless Rasp::AST::Literal === @step
g.dup
g.meta_push_0
- g.swap
g.send :<, 1
end
end
@@ -110,7 +109,6 @@ def bytecode(g)
if @step
g.dup_many 2 # @step, @finish, ...
loop_var.get_bytecode(g)
- g.swap
g.send :+, 1
else
g.dup # @finish, ...
@@ -121,18 +119,16 @@ def bytecode(g)
if @step.nil? || @step.value > 0
# Always working forwards
- g.swap
- g.send :>, 1
+ g.send :<=, 1
elsif @step.value < 0
# Always working backwards
- g.swap
- g.send :<, 1
+ g.send :>=, 1
else
# Constant step of zero?! Twit.
raise "For loop cannot have a zero step"
end
- g.gif loop_body
+ g.git loop_body
if @step
g.pop_many 2
@@ -155,20 +151,17 @@ def bytecode(g)
g.dup_many 3 # loop_backwards, @step, @finish, ...
loop_var.get_bytecode(g)
- g.swap
g.send :+, 1
loop_var.set_bytecode(g)
g.git check_backwards
- g.swap
- g.send :>, 1
- g.gif loop_body
+ g.send :<=, 1
+ g.git loop_body
g.goto end_loop
check_backwards.set!
- g.swap
- g.send :<, 1
- g.gif loop_body
+ g.send :>=, 1
+ g.git loop_body
end_loop.set!
g.pop_many 3
View
16 t/math.vbs
@@ -5,13 +5,13 @@ Dim i,j
Dim fib, n
'Function fib(n)
- If True Then n = 2
- If 1 Then
- x = 1
- fib = 1
- Else
- fib = zfib(n - 2) + zfib(n - 1) + fizz(777)
- End If
+' If True Then n = 2
+' If 1 Then
+' x = 1
+' fib = 1
+' Else
+' fib = zfib(n - 2) + zfib(n - 1) + fizz(777)
+' End If
'End Function
Const MIN = -1
@@ -25,6 +25,6 @@ For i = MIN To MAX
WScript.Echo i & " Or " & j & " = " & (i Or j)
WScript.Echo i & " Xor " & j & " = " & (i Xor j)
Next
-' WScript.Echo "Not " & i & " = " & (Not i)
+ WScript.Echo "Not " & i & " = " & (Not i)
Next
Please sign in to comment.
Something went wrong with that request. Please try again.