akaza 0.2.0 → 0.3.0

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,80 @@
1
+ class Array
2
+ def first
3
+ self[0]
4
+ end
5
+
6
+ def last
7
+ self[size-1]
8
+ end
9
+
10
+ def empty?
11
+ size == 0
12
+ end
13
+
14
+ def zip(other)
15
+ res = []
16
+ idx = 0
17
+ while idx < size
18
+ res.push(idx < other.size ? [self[idx], other[idx]] : [self[idx], nil])
19
+ idx = idx + 1
20
+ end
21
+ res
22
+ end
23
+ end
24
+
25
+ class Integer
26
+ def <(right)
27
+ (self <=> right) == -1
28
+ end
29
+
30
+ def >(right)
31
+ (self <=> right) == 1
32
+ end
33
+
34
+ def <=(right)
35
+ (self <=> right) < 1
36
+ end
37
+
38
+ def >=(right)
39
+ (self <=> right) > -1
40
+ end
41
+ end
42
+
43
+ def p(obj)
44
+ if obj.is_a?(Integer)
45
+ put_as_number obj
46
+ elsif obj.is_a?(Array)
47
+ size = obj.size
48
+ idx = 0
49
+ put_as_char '['
50
+ while idx < size
51
+ p obj[idx]
52
+ idx = idx + 1
53
+ if idx != size
54
+ put_as_char ','
55
+ put_as_char ' '
56
+ end
57
+ end
58
+ put_as_char ']'
59
+ elsif obj.is_a?(Hash)
60
+ raise "p(Hash) does not supported yet."
61
+ elsif obj == nil
62
+ put_as_char 'n'
63
+ put_as_char 'i'
64
+ put_as_char 'l'
65
+ elsif obj == true
66
+ put_as_char 't'
67
+ put_as_char 'r'
68
+ put_as_char 'u'
69
+ put_as_char 'e'
70
+ elsif obj == false
71
+ put_as_char 'f'
72
+ put_as_char 'a'
73
+ put_as_char 'l'
74
+ put_as_char 's'
75
+ put_as_char 'e'
76
+ else
77
+ raise "Unknown"
78
+ end
79
+ obj
80
+ end
@@ -1,3 +1,3 @@
1
1
  module Akaza
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -15,67 +15,67 @@ module Akaza
15
15
 
16
16
  def eval
17
17
  while true
18
+ # p @stack
19
+ # p @heap
20
+ # puts '-' * 100
21
+ # p @commands[@index]
18
22
  case @commands[@index]
19
- in [:stack, :push, number]
23
+ in [:stack_push, number]
20
24
  @stack.push number
21
- in [:stack, :dup]
25
+ in [:stack_dup]
22
26
  @stack.push @stack.last
23
- in [:stack, :swap]
27
+ in [:stack_swap]
24
28
  @stack[-1], @stack[-2] = @stack[-2], @stack[-1]
25
- in [:stack, :pop]
29
+ in [:stack_pop]
26
30
  @stack.pop
27
- in [:calc, :add]
28
- r = @stack.pop
29
- l = @stack.pop
30
- @stack.push l + r
31
- in [:calc, :sub]
32
- r = @stack.pop
33
- l = @stack.pop
34
- @stack.push l - r
35
- in [:calc, :multi]
36
- r = @stack.pop
37
- l = @stack.pop
38
- @stack.push l * r
39
- in [:calc, :div]
40
- r = @stack.pop
41
- l = @stack.pop
42
- @stack.push l / r
43
- in [:calc, :mod]
44
- r = @stack.pop
45
- l = @stack.pop
46
- @stack.push l % r
47
- in [:heap, :save]
31
+ in [:calc_add]
32
+ @stack[-2] = @stack[-2] + @stack[-1]
33
+ @stack.pop
34
+ in [:calc_sub]
35
+ @stack[-2] = @stack[-2] - @stack[-1]
36
+ @stack.pop
37
+ in [:calc_multi]
38
+ @stack[-2] = @stack[-2] * @stack[-1]
39
+ @stack.pop
40
+ in [:calc_div]
41
+ @stack[-2] = @stack[-2] / @stack[-1]
42
+ @stack.pop
43
+ in [:calc_mod]
44
+ @stack[-2] = @stack[-2] % @stack[-1]
45
+ @stack.pop
46
+ in [:heap_save]
48
47
  val = @stack.pop
49
48
  addr = @stack.pop
50
49
  @heap[addr] = val
51
- in [:heap, :load]
52
- addr = @stack.pop
53
- @stack.push @heap[addr]
54
- in [:flow, :def, label]
50
+ in [:heap_load]
51
+ val = @heap[@stack[-1]]
52
+ raise "Heap #{addr} is not initialized" unless val
53
+ @stack[-1] = val
54
+ in [:flow_def, label]
55
55
  # skip
56
- in [:flow, :call, label]
56
+ in [:flow_call, label]
57
57
  raise "unknwon label:#{label.inspect}" unless @labels.key?(label)
58
58
  @call_stack.push @index
59
59
  @index = @labels[label]
60
- in [:flow, :jump, label]
60
+ in [:flow_jump, label]
61
61
  raise "unknwon label:#{label.inspect}" unless @labels.key?(label)
62
62
  @index = @labels[label]
63
- in [:flow, :jump_if_zero, label]
63
+ in [:flow_jump_if_zero, label]
64
64
  @index = @labels[label] if @stack.pop == 0
65
- in [:flow, :jump_if_neg, label]
65
+ in [:flow_jump_if_neg, label]
66
66
  @index = @labels[label] if @stack.pop < 0
67
- in [:flow, :end]
67
+ in [:flow_end]
68
68
  @index = @call_stack.pop
69
- in [:flow, :exit]
69
+ in [:flow_exit]
70
70
  return
71
- in [:io, :write_char]
71
+ in [:io_write_char]
72
72
  @output.write @stack.pop.chr
73
- in [:io, :write_num]
73
+ in [:io_write_num]
74
74
  @output.write @stack.pop.to_s
75
- in [:io, :read_char]
75
+ in [:io_read_char]
76
76
  addr = @stack.pop
77
77
  @heap[addr] = @input.read(1).ord
78
- in [:io, :read_num]
78
+ in [:io_read_num]
79
79
  addr = @stack.pop
80
80
  @heap[addr] = @input.readline.to_i
81
81
  end
@@ -86,7 +86,7 @@ module Akaza
86
86
  def prepare_label
87
87
  @commands.each.with_index do |command, index|
88
88
  case command
89
- in [:flow, :def, label]
89
+ in [:flow_def, label]
90
90
  @labels[label] = index
91
91
  else
92
92
  # skip
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: akaza
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masataka Pocke Kuwabara
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-04-30 00:00:00.000000000 Z
11
+ date: 2019-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,10 +52,25 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: m
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  description: ''
56
70
  email:
57
71
  - kuwabara@pocke.me
58
- executables: []
72
+ executables:
73
+ - akaza
59
74
  extensions: []
60
75
  extra_rdoc_files: []
61
76
  files:
@@ -67,11 +82,14 @@ files:
67
82
  - bin/console
68
83
  - bin/setup
69
84
  - bin/str_to_ws.rb
85
+ - doc/ruby2ws.md
86
+ - exe/akaza
70
87
  - lib/akaza.rb
71
88
  - lib/akaza/annotation.rb
72
89
  - lib/akaza/ast_ext.rb
73
90
  - lib/akaza/parser.rb
74
91
  - lib/akaza/ruby2ws.rb
92
+ - lib/akaza/ruby2ws/prelude.rb
75
93
  - lib/akaza/version.rb
76
94
  - lib/akaza/vm.rb
77
95
  homepage: https://github.com/pocke/akaza