asmrb 0.0.2.1 → 0.0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/asmrb.rb +53 -49
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c8b8ea4a6ef9586d90b45b4fbea0d27c3fe25d2c
|
4
|
+
data.tar.gz: 457793531698daf895a1f3b878a437570fce2b51
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 57207355602fa8b5440d45d64de9e62644e2b3e30ec3f97458b98858ea7698a31ec814289e4fa88a2b8ddf05c7ef2d565c34f9d618c4563f90898d9382a1844f
|
7
|
+
data.tar.gz: 3b82fb7f49b70dc03be8adbd223d7dd253522d1ab1f2a90a55eae70063244a7e947677c6d48ed04a52ef9e31cea8285fd69a7ab1484e2e2af594d61b6c4ce333
|
data/lib/asmrb.rb
CHANGED
@@ -2,7 +2,35 @@ require 'benchmark'
|
|
2
2
|
require 'colorize'
|
3
3
|
require 'pry'
|
4
4
|
|
5
|
+
# You can make an instruction-based function with high level ruby by this Asm class.
|
5
6
|
class Asm
|
7
|
+
# Sample with recursion:
|
8
|
+
#
|
9
|
+
# Asm.new do
|
10
|
+
# defn :rec_print # define a function, also a label to jump.
|
11
|
+
# arg arr # arguments
|
12
|
+
# len arr # get length of arr as array
|
13
|
+
# pop l # get value to l variable
|
14
|
+
# cmp l , 0 # is l > 0 ?
|
15
|
+
# jge :print # then jump to :print block
|
16
|
+
# jmp :exit # else, jump to :exit block
|
17
|
+
# label :print # start of :print
|
18
|
+
# car arr # get out first element of arr
|
19
|
+
# call :puts # call ruby's puts
|
20
|
+
# cdr arr # get rest of arr Array
|
21
|
+
# jmp :rec_print # jump back to :rec_print ( or start of this function )
|
22
|
+
# label :exit # ending label.
|
23
|
+
# end
|
24
|
+
# => $rec_print
|
25
|
+
#
|
26
|
+
# debug:
|
27
|
+
# => $rec_print.is_debug = true
|
28
|
+
#
|
29
|
+
# calling:
|
30
|
+
# => $rec_print.invoke [1,2,3]
|
31
|
+
#
|
32
|
+
#
|
33
|
+
|
6
34
|
attr_reader :variables, :stack, :name, :is_debug, :result, :functions, :params
|
7
35
|
attr_writer :is_debug
|
8
36
|
|
@@ -12,10 +40,6 @@ class Asm
|
|
12
40
|
assemble &block
|
13
41
|
end
|
14
42
|
|
15
|
-
def export
|
16
|
-
|
17
|
-
end
|
18
|
-
|
19
43
|
def new_scope
|
20
44
|
@variables = {}
|
21
45
|
@program = []
|
@@ -27,20 +51,33 @@ class Asm
|
|
27
51
|
@name = "it"
|
28
52
|
end
|
29
53
|
|
30
|
-
def new_var(dest)
|
31
|
-
eval("def #{dest}; #{@variables[dest]}; end")
|
32
|
-
end
|
33
|
-
|
34
54
|
def refvar(val)
|
35
55
|
val.is_a?(Symbol) ? @variables[val] : val
|
36
56
|
end
|
37
57
|
|
58
|
+
def req_type(f,types)
|
59
|
+
s = types.is_a?(Array) ? types.join(', ') : types
|
60
|
+
raise Exception.new "[#{f}] got problem with #{lst}. need to be #{s}."
|
61
|
+
end
|
62
|
+
|
38
63
|
def req_args(func,amount)
|
39
64
|
raise Exception.new "[#{func}]: require #{amount} args to compare." if @stack.length < amount
|
40
65
|
end
|
41
66
|
|
42
67
|
OPS = {
|
43
68
|
|
69
|
+
"len" => lambda { | lst |
|
70
|
+
@stack.push @variables[lst].length
|
71
|
+
},
|
72
|
+
|
73
|
+
"cdr" => lambda { | lst |
|
74
|
+
@stack.push @variables[lst].drop(@variables[lst].first)
|
75
|
+
},
|
76
|
+
|
77
|
+
"car" => lambda { | lst |
|
78
|
+
@stack.push @variables[lst].first
|
79
|
+
},
|
80
|
+
|
44
81
|
"rdu" => lambda { | ops |
|
45
82
|
e = eval "@stack.reduce(:#{ops})"
|
46
83
|
@stack.push e
|
@@ -107,7 +144,6 @@ class Asm
|
|
107
144
|
|
108
145
|
"pop" => lambda {|val|
|
109
146
|
@variables[val] = @stack.pop
|
110
|
-
#new_var val
|
111
147
|
},
|
112
148
|
|
113
149
|
"push" => lambda {|src|
|
@@ -131,7 +167,7 @@ class Asm
|
|
131
167
|
|
132
168
|
"dbg" => lambda {
|
133
169
|
puts "#{@stack} > #{@program[@pc][0]}".light_green
|
134
|
-
binding.pry
|
170
|
+
binding.pry if @is_debug
|
135
171
|
},
|
136
172
|
|
137
173
|
"func" => lambda {|name|
|
@@ -156,15 +192,15 @@ class Asm
|
|
156
192
|
#binding.pry
|
157
193
|
},
|
158
194
|
|
159
|
-
"ivok" => lambda { |obj, f |
|
160
|
-
req_args "ivok",
|
161
|
-
args =
|
162
|
-
|
195
|
+
"ivok" => lambda { |obj, f , n|
|
196
|
+
req_args "ivok",n # obj.send(:func, args)
|
197
|
+
args = []
|
198
|
+
n.times { args << @stack.pop } if n > 0
|
199
|
+
result = @variables[obj].send(f, eval("args.join(', ')"))
|
163
200
|
@stack.push result
|
164
201
|
},
|
165
202
|
|
166
203
|
"call" => lambda {|fname|
|
167
|
-
#binding.pry
|
168
204
|
_method = method(fname)
|
169
205
|
if _method.parameters.length > 0
|
170
206
|
args = []
|
@@ -175,7 +211,6 @@ class Asm
|
|
175
211
|
else
|
176
212
|
invoke = _method.call
|
177
213
|
end
|
178
|
-
#binding.pry
|
179
214
|
@stack.push invoke if !invoke.nil?
|
180
215
|
},
|
181
216
|
|
@@ -279,7 +314,6 @@ class Asm
|
|
279
314
|
|
280
315
|
# ranging...
|
281
316
|
(debug_start..debug_end).each do | i |
|
282
|
-
|
283
317
|
instr = @program[i]
|
284
318
|
color = i != @pc ? :light_green : :magenta
|
285
319
|
puts "#{i}: #{instr[0]} #{instr[1]}".colorize color #if !instr.nil?
|
@@ -321,41 +355,11 @@ class Asm
|
|
321
355
|
def self.fac(acc, n)
|
322
356
|
if(n > 0)
|
323
357
|
fac(
|
324
|
-
|
325
|
-
|
358
|
+
(acc * n),
|
359
|
+
(n - 1))
|
326
360
|
else
|
327
361
|
acc
|
328
362
|
end
|
329
363
|
end
|
330
364
|
|
331
|
-
def self.compare_speed
|
332
|
-
# recursive fac:
|
333
|
-
Asm.assemble do
|
334
|
-
defn :factorial
|
335
|
-
arg acc, n
|
336
|
-
cmp n, 1
|
337
|
-
jge :cont
|
338
|
-
|
339
|
-
push acc
|
340
|
-
jmp :exit
|
341
|
-
|
342
|
-
label :cont
|
343
|
-
mul acc, n
|
344
|
-
sub n, 1
|
345
|
-
jmp :factorial
|
346
|
-
|
347
|
-
label :exit
|
348
|
-
|
349
|
-
#dbg
|
350
|
-
end
|
351
|
-
|
352
|
-
$factorial.is_debug = false
|
353
|
-
|
354
|
-
puts "Asm version vs primitive factorial (both recursive):".light_green
|
355
|
-
Benchmark.bm do |x|
|
356
|
-
x.report { $factorial.invoke 1, 8000 }
|
357
|
-
x.report { fac 1, 8000 }
|
358
|
-
end
|
359
|
-
puts "Run: 1 -> 8710, which is primitive version limitation".light_green
|
360
|
-
end
|
361
365
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: asmrb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.2.
|
4
|
+
version: 0.0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- The Trung
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-02-04 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Inteprete your assembly style code inside ruby.
|
14
14
|
email: deulamco@gmail.com
|