nil 1.0.1 → 1.0.2

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,8 @@
1
+ # 1.0.2
2
+ - Fix bug where instructions were interpreted out of order
3
+
4
+ # 1.0.1
5
+ - Add executable for compiling/interpreting code
6
+
7
+ # 1.0
8
+ - Initial working release
@@ -6,21 +6,7 @@ module Nil
6
6
  eval(@compiled_ruby)
7
7
  end
8
8
 
9
- def handle_non_multi_command(char)
10
- case char
11
- when '['
12
- "while stack[sp] != 0\n"
13
- when ']'
14
- "end\n"
15
- when '.'
16
- "puts stack[sp]\n"
17
- when ','
18
- "stack[sp] = gets[0]\n"
19
- end
20
- end
21
-
22
- def handle_multi_command(command)
23
- count = command.length
9
+ def handle_command(command, count)
24
10
  case command[0]
25
11
  when '+'
26
12
  "stack[sp] += #{count}\n"
@@ -30,6 +16,14 @@ module Nil
30
16
  "sp += #{count}\n"
31
17
  when '<'
32
18
  "sp -= #{count}\n"
19
+ when '['
20
+ "while stack[sp] != 0\n"
21
+ when ']'
22
+ "end\n"
23
+ when '.'
24
+ "puts stack[sp]\n"
25
+ when ','
26
+ "stack[sp] = gets[0]\n"
33
27
  end
34
28
  end
35
29
 
@@ -45,17 +39,14 @@ RB
45
39
  end
46
40
  end
47
41
 
48
- while code != ""
49
- multi_command = code.match(/([><\-\+])(\1*)/)
42
+ commands = code.scan( /((.)\2*)/ ).collect { |match| match.first }
50
43
 
51
- if multi_command == nil
52
- ruby = handle_non_multi_command(code[0])
53
- @compiled_ruby << ruby
54
- code[0] = ""
55
- else
56
- @compiled_ruby << handle_multi_command(multi_command.to_s)
57
- code.sub!(multi_command.to_s, "")
58
- end
44
+ commands.each_index do |i|
45
+ commands[i] = [commands[i], commands[i].length]
46
+ end
47
+
48
+ commands.each do |c|
49
+ @compiled_ruby << handle_command(c[0], c[1])
59
50
  end
60
51
  @compiled_ruby
61
52
  end
@@ -7,8 +7,6 @@ module Nil
7
7
  end
8
8
 
9
9
  def compile_subroutines(code)
10
- # TODO: Throw an error when trying to define a subroutine that calls
11
- # itself.
12
10
  subroutines = {}
13
11
  sub_def_regex = Regexp.new ':(?<sub-name>[a-zA-Z0-9_]+)\{(?<sub-code>[><\-\+.,A-Za-z0-9:]+)\}'
14
12
  sub_call_regex = Regexp.new '([a-zA-Z0-9_]+):'
@@ -1,3 +1,3 @@
1
1
  module Nil
2
- VERSION = "1.0.1"
2
+ VERSION = "1.0.2"
3
3
  end
@@ -1,18 +1,18 @@
1
1
  require 'nil'
2
2
  require 'riot'
3
3
 
4
- Riot.pretty_dots
4
+ # Riot.pretty_dots
5
5
 
6
6
  BOILERPLATE = <<RB
7
7
  sp = 0
8
- stack = Array.new 10_000
8
+ stack = [0] * 10_000
9
9
  RB
10
10
 
11
11
  context "Interpreter" do
12
12
  setup { Nil::Interpreter.new }
13
13
  asserts("invalid characters in code cause failure") { topic.compile "a++" }.raises SyntaxError
14
14
  asserts("creates boilerplate code") { topic.compiled_ruby == BOILERPLATE }
15
- asserts("handles .") { topic.compile('.') == (BOILERPLATE + "print stack[sp]\n") }
15
+ asserts("handles .") { topic.compile('.') == (BOILERPLATE + "puts stack[sp]\n") }
16
16
  asserts("handles ,") { topic.compile(',') == (BOILERPLATE + "stack[sp] = gets[0]\n") }
17
17
  asserts("handles >") { topic.compile('>') == (BOILERPLATE + "sp += 1\n") }
18
18
  asserts("handles <") { topic.compile('<') == (BOILERPLATE + "sp -= 1\n") }
@@ -20,6 +20,7 @@ context "Interpreter" do
20
20
  asserts("handles -") { topic.compile('-') == (BOILERPLATE + "stack[sp] -= 1\n") }
21
21
  asserts("handles [") { topic.compile('[') == (BOILERPLATE + "while stack[sp] != 0\n" ) }
22
22
  asserts("handles ]") { topic.compile(']') == (BOILERPLATE + "end\n" ) }
23
+
23
24
  asserts("handles multiple operators smartly") do
24
25
  code = topic.compile "++++---->>>><<<<"
25
26
  code == BOILERPLATE + <<CODE
@@ -27,6 +28,18 @@ stack[sp] += 4
27
28
  stack[sp] -= 4
28
29
  sp += 4
29
30
  sp -= 4
31
+ CODE
32
+ end
33
+
34
+ asserts("handles multiple printing statements") do
35
+ code = "++++.>+++."
36
+ code = topic.compile(code)
37
+ code == BOILERPLATE + <<CODE
38
+ stack[sp] += 4
39
+ puts stack[sp]
40
+ sp += 1
41
+ stack[sp] += 3
42
+ puts stack[sp]
30
43
  CODE
31
44
  end
32
45
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nil
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-29 00:00:00.000000000 Z
12
+ date: 2012-05-31 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A brain**** macro language and interpreter
15
15
  email:
@@ -21,6 +21,7 @@ extra_rdoc_files: []
21
21
  files:
22
22
  - .gitignore
23
23
  - .rvmrc
24
+ - CHANGELOG.md
24
25
  - Gemfile
25
26
  - LICENSE
26
27
  - README.md