karel-interpreter 0.1.0 → 0.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e536365aaca04663111b4b551fabe28846f3a850
4
- data.tar.gz: 8884b11c61f588d982c07d45e6297b3b3ce7a7f4
3
+ metadata.gz: 52f8e76bad41ecb3e6b368a62547e525c59ca1fa
4
+ data.tar.gz: 7d2a7d4092fbf057cf20ccf4fd5aba78e06ca474
5
5
  SHA512:
6
- metadata.gz: a27a642da2819853c99948a14e35dd00c8f719a3ea78eeb48bca40ba60e9839a7a2fcbe7aa5801d35fde63113b4b1b466481623ee322463f8544dee1486b3b55
7
- data.tar.gz: b9709c9ab0c17d66cf350d5e1fa277231178a53d44621321b002bcb0e4fe635852606f12e1bb7b8394ae2aa56eb8c5cad8eb6f7e57b6cce6741116a5b7d5d5b1
6
+ metadata.gz: 2f385b6ca371bd7e2c40fd485eaaaf926d81959ea2487e1f8ed49f38a8fa834c01d263737e7d9e3b3b3f81e3ef69a7fd5917e5e0d62b1988df62f48a9ae49a01
7
+ data.tar.gz: e5a651d3758d6e277baf7161689887ef3927e419e0734f89be96670560a19f2c1961b11b6f2bb02d9008b6ba17b2433442ab4f8d4686ed82dbc2dc3bbbff5ee3
data/.gitignore CHANGED
@@ -7,5 +7,8 @@
7
7
  /spec/reports/
8
8
  /tmp/
9
9
 
10
+ Gemfile.lock
11
+ *.gem
12
+
10
13
  # rspec failure tracking
11
14
  .rspec_status
@@ -0,0 +1,7 @@
1
+ Version 0.2.0, 2018-11-29
2
+ -------------------------
3
+ e60c95b Allow initialization of Karel board with command line option
4
+
5
+ Version 0.1.0, 2018-11-26
6
+ -------------------------
7
+ 17e434f Initial release
data/README.md CHANGED
@@ -114,7 +114,7 @@ if token?
114
114
  end
115
115
  ```
116
116
 
117
- #### Example: nested `while`
117
+ #### Example: a basic `while` statement
118
118
 
119
119
  ```
120
120
  # This will safely remove all tokens from a square.
@@ -125,8 +125,18 @@ end
125
125
 
126
126
  ### User-defined commands
127
127
 
128
- The Karel language supports user-defined commands, though these commands do not take
129
- arguments and do not return a value. Commands are defined as follows
128
+ The Karel language supports user-defined commands. These commands do not take
129
+ arguments and do not return a value. Commands are defined with the following syntax
130
+
131
+ ```
132
+ def <command-name>
133
+ <statement-1>
134
+ <statement-2>
135
+ ...
136
+ end
137
+ ```
138
+
139
+ #### Example
130
140
 
131
141
  ```
132
142
  def turn_around
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
21
21
  spec.require_paths = ["lib"]
22
22
 
23
23
  spec.add_development_dependency "bundler", "~> 1.16"
24
+ spec.add_development_dependency "json", "~> 2.1"
24
25
  spec.add_development_dependency "rake", "~> 10.0"
25
26
  spec.add_development_dependency "rspec", "~> 3.0"
26
27
  end
@@ -1,5 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
+ require 'json'
4
+
3
5
  require_relative 'karel/model/compass'
4
6
  require_relative 'karel/model/location'
5
7
  require_relative 'karel/model/token_container'
@@ -8,9 +10,13 @@ require_relative 'karel/utils/parser'
8
10
  module Karel
9
11
  def self.usage
10
12
  puts <<~USAGE
11
- Usage: #{$0} file
13
+ Usage: #{$0} [options] file
12
14
  Interpret a Karel file and print the final state
13
15
 
16
+ Where options include
17
+ -i FILE initalize the state of the Karel board before before
18
+ executing the Karel program file
19
+
14
20
  Example:
15
21
  % #{$0} move-up.krl
16
22
  location: (10, 10)
@@ -20,19 +26,51 @@ module Karel
20
26
  USAGE
21
27
  end
22
28
 
29
+ def self.initial_state(file)
30
+ compass = Model::Compass.new(:up)
31
+ location = Model::Location.new(0, 0)
32
+ tokens = Model::TokenContainer.new
33
+ if file
34
+ raise "Initialization file '#{file}' not found" if !File.exist?(file)
35
+ File.readlines(file).each do |raw_line|
36
+ line = raw_line.chomp
37
+ match = line.match(/^tokens: (.*)$/)
38
+ if match
39
+ token_config = JSON.parse(match[1])
40
+ token_config.each do |token|
41
+ location_match = token['location'].match(/\((-?\d+), (-?\d+)\)/)
42
+ tokens.put(
43
+ Model::Location.new(location_match[1].to_i, location_match[2].to_i),
44
+ token['count']
45
+ )
46
+ end
47
+ end
48
+ end
49
+ end
50
+ [compass, location, tokens]
51
+ end
52
+
23
53
  def self.run(args)
24
- if args.length != 1
54
+ init_file, karel_file = nil
55
+ while arg = args.shift
56
+ if arg == '-i'
57
+ init_file = args.shift
58
+ elsif karel_file.nil?
59
+ karel_file = arg
60
+ else
61
+ usage
62
+ exit(1)
63
+ end
64
+ end
65
+
66
+ if karel_file.nil?
25
67
  usage
26
68
  exit(1)
27
- elsif !File.exist?(args.first)
28
- puts "File '#{args.first}' not found"
69
+ elsif !File.exist?(karel_file)
70
+ raise "File '#{karel_file}' not found"
29
71
  else
30
- program = Utils::Parser.parse(args.first)
31
- response = program.execute(
32
- Model::Compass.new(:up),
33
- Model::Location.new(0, 0),
34
- Model::TokenContainer.new
35
- )
72
+ program = Utils::Parser.parse(karel_file)
73
+ response = program.execute(*initial_state(init_file))
36
74
  puts <<~STATE
37
75
  location: #{response.location.to_s}
38
76
  direction: #{response.compass.direction}
@@ -19,9 +19,9 @@ module Model
19
19
  return self
20
20
  end
21
21
 
22
- def put(location)
22
+ def put(location, value = 1)
23
23
  @tokens[location] ||= 0
24
- @tokens[location] += 1
24
+ @tokens[location] += value
25
25
  return self
26
26
  end
27
27
 
@@ -1,5 +1,5 @@
1
1
  module Karel
2
2
  module Interpreter
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: karel-interpreter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Collier
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-11-27 00:00:00.000000000 Z
11
+ date: 2018-11-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.1'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.1'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rake
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -63,6 +77,7 @@ files:
63
77
  - ".gitignore"
64
78
  - ".rspec"
65
79
  - ".travis.yml"
80
+ - CHANGELOG.txt
66
81
  - CODE_OF_CONDUCT.md
67
82
  - Gemfile
68
83
  - LICENSE.txt