rVM 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,24 @@
1
+ module RVM
2
+ module Functions
3
+ class Split < Function
4
+ class << self
5
+ def execute params, env
6
+ if params.length == 2
7
+ RVM::Classes[:list].new(params[0].to_s,params[1])
8
+ else
9
+ RVM::Classes[:error].new(1,"#-1 FUNCTION (#{self.class.to_s}) EXPECTS 2 OR 3 ARGUMENTS BUT GOT #{params.length}")
10
+ end
11
+ end
12
+
13
+ def signature
14
+ [:string, :string]
15
+ end
16
+
17
+ def data_type
18
+ :list
19
+ end
20
+ end
21
+ register_for :split
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,25 @@
1
+ module RVM
2
+ module Functions
3
+ class And < RVM::Functions::Function
4
+ @@type=:any
5
+ class << self
6
+ def execute params, env
7
+ result = true
8
+ while result and not params.empty?
9
+ result = result && params.shift.execute(env).is_true?
10
+ end
11
+ result
12
+ end
13
+
14
+ def signature
15
+ [:any] # adjust the signature here
16
+ end
17
+
18
+ def execargs
19
+ false
20
+ end
21
+ end
22
+ register_for :and
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,18 @@
1
+ module RVM
2
+ module Functions
3
+ class Add < Function
4
+ def Add.execute params, env
5
+ a = RVM::Classes[:number].new 0
6
+ params.each do |p|
7
+ a += p
8
+ end
9
+ a
10
+ end
11
+
12
+ def Add.signature
13
+ [:number, :number]
14
+ end
15
+ register_for :add
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,21 @@
1
+ module RVM
2
+ module Functions
3
+ class Div < Function
4
+ def Div.execute params, env
5
+ a = RVM::Classes[:number].new params.shift
6
+ params.each do |p|
7
+ if p == 0
8
+ return RVM::Classes[:error].new(1,"DIVISION BY ZERO")
9
+ end
10
+ a /= p
11
+ end
12
+ a
13
+ end
14
+
15
+ def Div.signature
16
+ [:number, :number]
17
+ end
18
+ register_for :div
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,18 @@
1
+ module RVM
2
+ module Functions
3
+ class Mul < Function
4
+ def Mul.execute params, env
5
+ a = RVM::Classes[:number].new params.shift
6
+ params.each do |p|
7
+ a *= p
8
+ end
9
+ a
10
+ end
11
+
12
+ def Mul.signature
13
+ [:number, :number]
14
+ end
15
+ register_for :mul
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ module RVM
2
+ module Functions
3
+ class Neg < Function
4
+ def Neg.execute params, env
5
+ if params.length == 1
6
+ RVM::Classes[:number].new(params[0] * -1)
7
+ else
8
+ RVM::Classes[:error].new(1,"FUNCTION (#{self.name}) EXPECTS 1 ARGUMENTS BUT GOT #{params.length}")
9
+ end
10
+ end
11
+
12
+ def Neg.signature
13
+ [:number]
14
+ end
15
+ register_for :neg
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ module RVM
2
+ module Functions
3
+ class Power < Function
4
+ def Power.execute params, env
5
+ if params.length == 2
6
+ RVM::Classes[:number].new(params[0] ** params[1])
7
+ else
8
+ RVM::Classes[:error].new(1,"FUNCTION (#{self.name}) EXPECTS 2 ARGUMENTS BUT GOT #{params.length}")
9
+ end
10
+ end
11
+
12
+ def Power.signature
13
+ [:number, :number]
14
+ end
15
+ register_for :power
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ module RVM
2
+ module Functions
3
+ class Sub < Function
4
+ def Sub.execute params, env
5
+ a = RVM::Classes[:number].new params.shift
6
+ params.each do |p|
7
+ a -= p
8
+ end
9
+ a
10
+ end
11
+
12
+ def Sub.signature
13
+ [:number, :number]
14
+ end
15
+ register_for :sub
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,24 @@
1
+ module RVM
2
+ module Functions
3
+ class Ansi < Function
4
+ class << self
5
+ def execute params, env
6
+ if params.length == 2
7
+ params[1].ansi params[0]
8
+ else
9
+ RVM::Classes[:error].new(1,"FUNCTION (#{self.class.to_s}) EXPECTS 2 ARGUMENTS BUT GOT #{params.length}")
10
+ end
11
+ end
12
+
13
+ def signature
14
+ [:string, :string]
15
+ end
16
+
17
+ def data_type
18
+ :string
19
+ end
20
+ end
21
+ register_for :ansi
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,23 @@
1
+ module RVM
2
+ module Functions
3
+ class Capstr < RVM::Functions::Function
4
+ class << self
5
+ def execute params, env
6
+ if (params.length == 1)
7
+ RVM::Classes[:string].new(params.first.capitalize)
8
+ else
9
+ RVM::Classes[:error].new(1,"#-1 FUNCTION (#{self.class.to_s}) EXPECTS 1 ARGUMENT BUT GOT #{params.length}")
10
+ end
11
+ end
12
+
13
+ def signature
14
+ [:string]
15
+ end
16
+ def data_type
17
+ :string
18
+ end
19
+ end
20
+ register_for :capstr
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,25 @@
1
+ module RVM
2
+ module Functions
3
+ class Center < Function
4
+ class << self
5
+ def execute params, env
6
+ if params.length == 2
7
+ params[0].center(params[1])
8
+ elsif params.length == 3
9
+ params[0].center(params[1], params[2])
10
+ else
11
+ RVM::Classes[:error].new(1,"#-1 FUNCTION (#{self.class.to_s}) EXPECTS 2 OR 3 ARGUMENTS BUT GOT #{params.length}")
12
+ end
13
+ end
14
+
15
+ def signature
16
+ [:string, :number, :string]
17
+ end
18
+ def data_type
19
+ :string
20
+ end
21
+ end
22
+ register_for :center
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,26 @@
1
+ module RVM
2
+ module Functions
3
+ class Ljust < Function
4
+ class << self
5
+ def execute params, env
6
+ if params.length == 2
7
+ params[0].ljust params[1]
8
+ elsif params.length == 3
9
+ params[0].ljust params[1], params[2]
10
+ else
11
+ RVM::Classes[:error].new(1,"#-1 FUNCTION (#{self.class.to_s}) EXPECTS 2 OR 3 ARGUMENTS BUT GOT #{params.length}")
12
+ end
13
+ end
14
+
15
+ def signature
16
+ [:string, :number, :string]
17
+ end
18
+
19
+ def data_type
20
+ :string
21
+ end
22
+ end
23
+ register_for :ljust
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,34 @@
1
+ module RVM
2
+ module Functions
3
+ class Regmatch < RVM::Functions::Function
4
+ class << self
5
+ def execute params, env
6
+ if params.length == 2
7
+ str = params[0]
8
+ regexp = params[1].to_s
9
+ begin
10
+ regexp = Regexp.new(regexp)
11
+ rescue
12
+ return RVM::Classes[:error].new(1,"INVALID REGULAR EXPRESSION")
13
+ end
14
+ if regexp.match(str)
15
+ RVM::Classes[:boolean].new(true)
16
+ else
17
+ RVM::Classes[:boolean].new(false)
18
+ end
19
+ else
20
+ RVM::Classes[:error].new(1,"FUNCTION (#{self.class.to_s}) EXPECTS 2 ARGUMENTS BUT GOT #{params.length}")
21
+ end
22
+ end
23
+ def signature
24
+ [:string, :string] # adjust the signature here
25
+ end
26
+
27
+ def data_type
28
+ :boolean
29
+ end
30
+ end
31
+ register_for :regmatch
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,25 @@
1
+ module RVM
2
+ module Functions
3
+ class Rjust < Function
4
+ class << self
5
+ def execute params, env
6
+ if params.length == 2
7
+ params[0].rjust params[1]
8
+ elsif params.length == 3
9
+ params[0].rjust params[1], params[2]
10
+ else
11
+ RVM::Classes[:error].new(1,"#-1 FUNCTION (#{self.class.to_s}) EXPECTS 2 OR 3 ARGUMENTS BUT GOT #{params.length}")
12
+ end
13
+ end
14
+
15
+ def signature
16
+ [:string, :number, :string]
17
+ end
18
+ def data_type
19
+ :string
20
+ end
21
+ end
22
+ register_for :rjust
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,36 @@
1
+ module RVM
2
+ module Languages
3
+ module Math
4
+ class Compiler
5
+ include RVM::Interpreter
6
+ FUNCTION_MAP = {
7
+ '+' => :add,
8
+ '-' => :sub,
9
+ '/' => :div,
10
+ '*' => :mul,
11
+ '^' => :power
12
+ }
13
+ def Compiler.compile tree
14
+ if tree.is_a? Array
15
+ compile tree.first
16
+ elsif tree.is_a? Hash
17
+ case tree[:type]
18
+ when :number
19
+ Interpreter.const(:number, tree[:number])
20
+ when :function
21
+ params = tree[:params].map {|p| compile p}
22
+ case tree[:op]
23
+ when '-'
24
+ FunctionCall.new :neg, params
25
+ else
26
+ FunctionCall.new tree[:op], params
27
+ end
28
+ when :op
29
+ FunctionCall.new(FUNCTION_MAP[tree[:op]], [compile(tree[:left]),compile(tree[:right])])
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,47 @@
1
+ require 'strscan'
2
+ module RVM
3
+ module Languages
4
+ module Math
5
+ class Tokenizer
6
+ def Tokenizer.tokenize str
7
+ s = StringScanner.new str
8
+ tokens = []
9
+ state = :number
10
+ while not s.eos?
11
+ s.skip(/\s*/)
12
+ if state == :number
13
+ if s.scan(/[+-]+/)
14
+ t = s.matched.gsub('+','').gsub('--','')
15
+ if t == '-'
16
+ tokens << [t, :function]
17
+ end
18
+ elsif s.scan(/[a-z][a-z0-9]*/i)
19
+ tokens << [s.matched, :function]
20
+ elsif s.scan(/[\d]+(\.\d+)?/i)
21
+ tokens << [s.matched, :number]
22
+ state = :opperator
23
+ elsif s.scan(/\(/)
24
+ tokens << [s.matched, :paren_open]
25
+ else
26
+ return []
27
+ end
28
+ else
29
+ if s.scan(/\)/)
30
+ tokens << [s.matched, :paren_close]
31
+ elsif s.scan(/,/)
32
+ tokens << [s.matched, :function_sep]
33
+ state = :number
34
+ elsif s.scan(/[*+\/^-]/)
35
+ tokens << [s.matched, :opperator]
36
+ state = :number
37
+ else
38
+ return []
39
+ end
40
+ end
41
+ end
42
+ tokens
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,100 @@
1
+ module RVM
2
+ module Languages
3
+ module Math
4
+ class Tree
5
+ PRIORITIES = {
6
+ '(' => 0,
7
+ '+' => 1,
8
+ '-' => 1,
9
+ '*' => 2,
10
+ '/' => 2,
11
+ '^' => 3,
12
+ }
13
+ ASSOSICATIONS = {
14
+ '+' => :left,
15
+ '-' => :left,
16
+ '*' => :left,
17
+ '/' => :left,
18
+ '^' => :right
19
+ }
20
+ def Tree.generate tokens
21
+ output = []
22
+ dbgoutput = []
23
+ stack = []
24
+ dbgstack = []
25
+ while not tokens.empty?
26
+ token = tokens.shift
27
+ case token[1]
28
+ when :function
29
+ fun = token[0]
30
+ stack << {:type => :function, :op => fun}
31
+ output << {:type => :function_end}
32
+ dbgoutput << ')'
33
+ dbgstack << fun
34
+ when :function_sep
35
+ while stack.last && (stack.last[:op] != '(')
36
+ output << stack.pop
37
+ dbgoutput << dbgstack.pop
38
+ end
39
+ when :paren_open
40
+ op = token[0]
41
+ stack << {:type => :op, :op => op}
42
+ dbgstack << op
43
+ when :paren_close
44
+ while stack.last && (stack.last[:op] != '(')
45
+ output << stack.pop
46
+ dbgoutput << dbgstack.pop
47
+ end
48
+ stack.pop
49
+ dbgstack.pop
50
+ if stack.last && (stack.last[:type] == :function)
51
+ output << stack.pop
52
+ dbgoutput << dbgstack.pop
53
+ end
54
+ when :number
55
+ output << {:type => :number, :number => token[0]}
56
+ dbgoutput << token[0]
57
+ when :opperator
58
+ op = token[0]
59
+ ass = ASSOSICATIONS[op]
60
+ o1p = PRIORITIES[op]
61
+ while stack.last &&
62
+ (((ass == :left) && (o1p <= PRIORITIES[stack.last[:op]])) ||
63
+ ((ass == :right) && (o1p < PRIORITIES[stack.last[:op]])))
64
+ output << stack.pop
65
+ dbgoutput << dbgstack.pop
66
+ end
67
+ stack << {:type => :op, :op => op}
68
+ dbgstack << op
69
+ else
70
+ end
71
+ end
72
+ while not stack.empty?
73
+ output << stack.pop
74
+ end
75
+ gen_tree output
76
+ end
77
+
78
+ private
79
+ def Tree.gen_tree data
80
+ item = data.pop
81
+ case item[:type]
82
+ when :number
83
+ item
84
+ when :op
85
+ item[:right] = gen_tree(data)
86
+ item[:left] = gen_tree(data)
87
+ item
88
+ when :function
89
+ item[:params] = []
90
+ while data.last && data.last[:type] != :function_end
91
+ item[:params] << gen_tree(data)
92
+ end
93
+ data.pop
94
+ item
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,14 @@
1
+ require File.dirname(__FILE__) + '/math/tokenizer'
2
+ require File.dirname(__FILE__) + '/math/tree'
3
+ require File.dirname(__FILE__) + '/math/compiler'
4
+ module RVM
5
+ module Languages
6
+ class MathLanguage < Language
7
+ include Math
8
+ def compile text
9
+ Compiler.compile(Tree.generate(Tokenizer.tokenize(text)))
10
+ end
11
+ register_for :math
12
+ end
13
+ end
14
+ end
data/lib/rvm.rb ADDED
@@ -0,0 +1,33 @@
1
+ require 'rvm/base/interpreter'
2
+ require 'rvm/base/classes'
3
+ require 'rvm/base/functions'
4
+ require 'rvm/base/languages'
5
+ #This is the rVM Library
6
+ module RVM
7
+ class << self
8
+ @@strict = false
9
+
10
+ def strict
11
+ end
12
+
13
+ def compile language, code
14
+ compiler_for(language).compile(code)
15
+ end
16
+
17
+ def compiler_for language
18
+ if l = RVM::Languages[language]
19
+ l
20
+ else
21
+ raise "RVM Error: Unknown Language #{language}"
22
+ end
23
+ end
24
+
25
+ def strict= v
26
+ @@strict = v
27
+ end
28
+
29
+ def debug text
30
+ puts text if $DEBUG
31
+ end
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rVM
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Heinz N. Gies
8
+ autorequire: ""
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-02-28 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: heinz@licenser.net
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ files:
25
+ - lib/rvm
26
+ - lib/rvm/base
27
+ - lib/rvm/base/classes.rb
28
+ - lib/rvm/base/functions.rb
29
+ - lib/rvm/base/interpreter.rb
30
+ - lib/rvm/base/languages.rb
31
+ - lib/rvm/base/plugin.rb
32
+ - lib/rvm/classes
33
+ - lib/rvm/classes/block.rb
34
+ - lib/rvm/classes/boolean.rb
35
+ - lib/rvm/classes/error.rb
36
+ - lib/rvm/classes/list.rb
37
+ - lib/rvm/classes/number.rb
38
+ - lib/rvm/classes/string.rb
39
+ - lib/rvm/functions
40
+ - lib/rvm/functions/list
41
+ - lib/rvm/functions/list/align.rb
42
+ - lib/rvm/functions/list/join.rb
43
+ - lib/rvm/functions/list/map.rb
44
+ - lib/rvm/functions/list/split.rb
45
+ - lib/rvm/functions/logic
46
+ - lib/rvm/functions/logic/and.rb
47
+ - lib/rvm/functions/math
48
+ - lib/rvm/functions/math/add.rb
49
+ - lib/rvm/functions/math/div.rb
50
+ - lib/rvm/functions/math/mul.rb
51
+ - lib/rvm/functions/math/neg.rb
52
+ - lib/rvm/functions/math/power.rb
53
+ - lib/rvm/functions/math/sub.rb
54
+ - lib/rvm/functions/string
55
+ - lib/rvm/functions/string/ansi.rb
56
+ - lib/rvm/functions/string/capstr.rb
57
+ - lib/rvm/functions/string/center.rb
58
+ - lib/rvm/functions/string/ljust.rb
59
+ - lib/rvm/functions/string/regmatch.rb
60
+ - lib/rvm/functions/string/rjust.rb
61
+ - lib/rvm/languages
62
+ - lib/rvm/languages/math
63
+ - lib/rvm/languages/math/compiler.rb
64
+ - lib/rvm/languages/math/tokenizer.rb
65
+ - lib/rvm/languages/math/tree.rb
66
+ - lib/rvm/languages/math.rb
67
+ - lib/rvm.rb
68
+ - README
69
+ has_rdoc: true
70
+ homepage: http://code.licenser.net/projects/show/rvm
71
+ post_install_message:
72
+ rdoc_options: []
73
+
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: "0"
81
+ version:
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: "0"
87
+ version:
88
+ requirements: []
89
+
90
+ rubyforge_project: rmush
91
+ rubygems_version: 1.0.1
92
+ signing_key:
93
+ specification_version: 2
94
+ summary: A ruby based VM that lets one add secure scripting to ruby applications.
95
+ test_files: []
96
+