Skip to content
This repository has been archived by the owner on May 13, 2019. It is now read-only.

Commit

Permalink
Avoid swaps in for loops.
Browse files Browse the repository at this point in the history
Better? Worse? Meh.
  • Loading branch information
matthewd committed Nov 21, 2010
1 parent dcfe4d7 commit 49f754d
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 31 deletions.
9 changes: 9 additions & 0 deletions README
@@ -0,0 +1,9 @@

rasp
=====

A VBScript runtime, running on the Rubinius VM.


Untested, undocumented, and ill-advised.

14 changes: 9 additions & 5 deletions bin/rasp
Expand Up @@ -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$/

14 changes: 10 additions & 4 deletions lib/rasp/ast/expression.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down
21 changes: 7 additions & 14 deletions lib/rasp/ast/flow.rb
Expand Up @@ -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
Expand All @@ -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, ...
Expand All @@ -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
Expand All @@ -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
Expand Down
16 changes: 8 additions & 8 deletions t/math.vbs
Expand Up @@ -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
Expand All @@ -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

0 comments on commit 49f754d

Please sign in to comment.