flea 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/build.yml +30 -0
  3. data/.gitignore +3 -0
  4. data/.rspec +2 -0
  5. data/.rubocop.yml +30 -0
  6. data/Gemfile +5 -0
  7. data/Gemfile.lock +65 -0
  8. data/MIT-LICENSE +1 -1
  9. data/README.md +345 -0
  10. data/bin/flea +17 -15
  11. data/flea-language-spec/{README.rdoc → README.md} +2 -2
  12. data/flea-language-spec/test-cases/01-display-and-basic-literals/05-display-list-literal-using-quote.scm +1 -1
  13. data/flea-language-spec/test-cases/02-variables/05-define-with-list.scm +1 -1
  14. data/flea-language-spec/test-cases/04-quoting/01-quoting.scm +5 -0
  15. data/flea.gemspec +34 -0
  16. data/lib/flea/environment.rb +20 -18
  17. data/lib/flea/interpreter.rb +44 -44
  18. data/lib/flea/standard_library/display.scm +7 -1
  19. data/lib/flea/standard_library/null.scm +1 -1
  20. data/lib/flea/standard_library/read.scm +1 -1
  21. data/lib/flea/version.rb +5 -0
  22. data/lib/flea.rb +6 -4
  23. metadata +110 -138
  24. data/README.rdoc +0 -274
  25. data/Rakefile +0 -36
  26. data/VERSION +0 -1
  27. data/flea-language-spec/test-cases/04-comma-quoting/01-comma-quoting.scm +0 -5
  28. data/spec/flea/environment_spec.rb +0 -114
  29. data/spec/flea/interpreter_spec.rb +0 -85
  30. data/spec/flea/standard_library/addition_operator_spec.rb +0 -23
  31. data/spec/flea/standard_library/append_spec.rb +0 -17
  32. data/spec/flea/standard_library/begin_spec.rb +0 -20
  33. data/spec/flea/standard_library/car_spec.rb +0 -17
  34. data/spec/flea/standard_library/cdr_spec.rb +0 -17
  35. data/spec/flea/standard_library/cons_spec.rb +0 -25
  36. data/spec/flea/standard_library/display_spec.rb +0 -36
  37. data/spec/flea/standard_library/division_operator_spec.rb +0 -29
  38. data/spec/flea/standard_library/equality_operator_spec.rb +0 -45
  39. data/spec/flea/standard_library/gets_spec.rb +0 -23
  40. data/spec/flea/standard_library/greater_than_spec.rb +0 -29
  41. data/spec/flea/standard_library/if_spec.rb +0 -59
  42. data/spec/flea/standard_library/lambda_spec.rb +0 -70
  43. data/spec/flea/standard_library/less_than_spec.rb +0 -29
  44. data/spec/flea/standard_library/list_predicate_spec.rb +0 -25
  45. data/spec/flea/standard_library/list_spec.rb +0 -24
  46. data/spec/flea/standard_library/list_tail_spec.rb +0 -17
  47. data/spec/flea/standard_library/mutiplication_operator_spec.rb +0 -23
  48. data/spec/flea/standard_library/null_spec.rb +0 -26
  49. data/spec/flea/standard_library/quote_spec.rb +0 -20
  50. data/spec/flea/standard_library/rand_spec.rb +0 -25
  51. data/spec/flea/standard_library/read_spec.rb +0 -23
  52. data/spec/flea/standard_library/set_spec.rb +0 -23
  53. data/spec/flea/standard_library/string_to_num_spec.rb +0 -28
  54. data/spec/flea/standard_library/subtraction_operator_spec.rb +0 -24
  55. data/spec/spec_helper.rb +0 -10
@@ -1,67 +1,67 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Flea
2
4
  class Interpreter
3
-
4
- attr_accessor :base_environment,
5
- :current_environment,
6
- :parser
7
-
8
- def initialize(options = {})
9
- options = {
10
- :base_environment => Environment.new,
11
- :load_standard_library => true
12
- }.merge(options)
13
-
14
- @base_environment = @current_environment = options[:base_environment]
15
- @parser = Sexpistol.new
16
- @parser.ruby_keyword_literals = false
17
- @parser.scheme_compatability = true
18
-
19
- load_standard_library unless options[:load_standard_library] == false
5
+ attr_accessor :base_environment, :current_environment
6
+
7
+ def initialize(environment: Environment.new, standard_library: true)
8
+ @base_environment = @current_environment = environment
9
+
10
+ load_standard_library if standard_library
20
11
  end
21
-
12
+
22
13
  def run(program)
23
- expressions = parse(program)
14
+ program = parse(program)
24
15
  result = nil
25
- expressions.each do |expression|
26
- result = evaluate(expression)
27
- end
28
16
 
29
- return result
30
- end
31
-
32
- def parse(string)
33
- return @parser.parse_string(string)
17
+
18
+
19
+ if program.is_a?(Sexpistol::SExpressionArray)
20
+ program.each do |expression|
21
+ result = evaluate(expression)
22
+ end
23
+
24
+ else
25
+ result = evaluate(program)
26
+ end
27
+
28
+ result
34
29
  end
35
-
30
+
36
31
  def evaluate(expression)
37
32
  return @current_environment.find(expression) if expression.is_a? Symbol
38
33
  return expression unless expression.is_a? Array
39
-
40
- if expression[0] == :define
41
- return @current_environment.define expression[1], evaluate(expression[2])
42
-
43
- elsif expression[0] == :native_function
44
- return eval expression[1]
45
-
46
- else # function call
34
+
35
+ case expression[0]
36
+ when :define then @current_environment.define(expression[1], evaluate(expression[2]))
37
+ when :native_function then eval expression[1]
38
+ else
47
39
  function = evaluate(expression[0])
48
- raise RuntimeError, "\n#{@parser.to_sexp(expression)}\n ^\n\n#{expression[0]} is not a function" unless function.is_a? Proc
40
+ raise "\n#{to_sexp(expression)}\n ^\n\n#{expression[0]} is not a function\n\n#{@current_environment.table.keys}" unless function.is_a? Proc
41
+
49
42
  arguments = expression.slice(1, expression.length)
50
- return function.call(arguments, self)
43
+ function.call(arguments, self)
51
44
  end
52
45
  end
53
-
54
- private
55
-
46
+
47
+ def parse(string)
48
+ Sexpistol.parse(string, parse_ruby_keyword_literals: true)
49
+ end
50
+
51
+ def to_sexp(expression)
52
+ Sexpistol.to_sexp(expression, scheme_compatability: true)
53
+ end
54
+
55
+ private
56
+
56
57
  def load_standard_library
57
58
  library_pattern = File.join(File.dirname(__FILE__), 'standard_library', '*.scm')
58
-
59
+
59
60
  Dir[library_pattern].each do |item|
60
61
  File.open(item) do |file|
61
62
  run(file.read)
62
63
  end
63
64
  end
64
65
  end
65
-
66
66
  end
67
- end
67
+ end
@@ -2,7 +2,13 @@
2
2
  (native_function "
3
3
  Proc.new() do |arguments, interpreter|
4
4
  output = interpreter.evaluate(arguments[0])
5
- print interpreter.parser.to_sexp(output)
5
+
6
+ if output.is_a?(String)
7
+ print output
8
+ else
9
+ print interpreter.to_sexp(output)
10
+ end
11
+
6
12
  output
7
13
  end
8
14
  "))
@@ -1,3 +1,3 @@
1
1
  (define null?
2
2
  (lambda (arg)
3
- (equal? arg '())))
3
+ (equal? arg (quote ()))))
@@ -1,6 +1,6 @@
1
1
  (define read
2
2
  (native_function "
3
3
  Proc.new() do |arguments, interpreter|
4
- interpreter.parser.parse_string($stdin.gets)
4
+ interpreter.parse($stdin.gets)
5
5
  end
6
6
  "))
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Flea
4
+ VERSION = '0.1.1'
5
+ end
data/lib/flea.rb CHANGED
@@ -1,5 +1,7 @@
1
- require "rubygems"
2
- require "sexpistol"
1
+ # frozen_string_literal: true
3
2
 
4
- require File.expand_path(File.join(File.dirname(__FILE__), "flea", "environment.rb"))
5
- require File.expand_path(File.join(File.dirname(__FILE__), "flea", "interpreter.rb"))
3
+ require 'sexpistol'
4
+ require 'stringio'
5
+
6
+ require 'flea/environment'
7
+ require 'flea/interpreter'
metadata CHANGED
@@ -1,67 +1,104 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: flea
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 1
8
- - 0
9
- version: 0.1.0
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
10
5
  platform: ruby
11
- authors:
6
+ authors:
12
7
  - Aaron Gough
13
- autorequire:
14
- bindir: bin
8
+ autorequire:
9
+ bindir: exe
15
10
  cert_chain: []
16
-
17
- date: 2011-03-13 00:00:00 -05:00
18
- default_executable:
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
11
+ date: 2022-01-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
21
14
  name: sexpistol
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
24
- none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- segments:
29
- - 0
30
- version: "0"
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.2
31
20
  type: :runtime
32
- version_requirements: *id001
33
- - !ruby/object:Gem::Dependency
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.2
27
+ - !ruby/object:Gem::Dependency
34
28
  name: any-spec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.1.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.1.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 1.24.1
62
+ type: :development
35
63
  prerelease: false
36
- requirement: &id002 !ruby/object:Gem::Requirement
37
- none: false
38
- requirements:
39
- - - ">="
40
- - !ruby/object:Gem::Version
41
- segments:
42
- - 0
43
- version: "0"
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 1.24.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.17.1
44
76
  type: :development
45
- version_requirements: *id002
46
- description: Flea is an extremely simple, but extremely extensible Lisp interpreter written in Ruby.
47
- email: aaron@aarongough.com
48
- executables:
49
- - flea
50
- - flea
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.17.1
83
+ description: Flea is an extremely simple, but extremely extensible Lisp interpreter
84
+ written in Ruby.
85
+ email:
86
+ - aaron@aarongough.com
87
+ executables: []
51
88
  extensions: []
52
-
53
- extra_rdoc_files:
54
- - MIT-LICENSE
55
- - README.rdoc
56
- files:
57
- - .gitignore
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".github/workflows/build.yml"
92
+ - ".gitignore"
93
+ - ".rspec"
94
+ - ".rubocop.yml"
95
+ - Gemfile
96
+ - Gemfile.lock
58
97
  - MIT-LICENSE
59
- - README.rdoc
60
- - Rakefile
61
- - VERSION
98
+ - README.md
62
99
  - bin/flea
63
100
  - examples/guess-the-number.scm
64
- - flea-language-spec/README.rdoc
101
+ - flea-language-spec/README.md
65
102
  - flea-language-spec/flea-language-spec.yaml
66
103
  - flea-language-spec/test-cases/01-display-and-basic-literals/01-display-string-literal.scm
67
104
  - flea-language-spec/test-cases/01-display-and-basic-literals/02-display-integer-literal.scm
@@ -88,7 +125,7 @@ files:
88
125
  - flea-language-spec/test-cases/03-basic-built-in-procedures/07-equal?/06-equal?-false-boolean.scm
89
126
  - flea-language-spec/test-cases/03-basic-built-in-procedures/07-equal?/07-equal?-true-list.scm
90
127
  - flea-language-spec/test-cases/03-basic-built-in-procedures/07-equal?/08-equal?-false-list.scm
91
- - flea-language-spec/test-cases/04-comma-quoting/01-comma-quoting.scm
128
+ - flea-language-spec/test-cases/04-quoting/01-quoting.scm
92
129
  - flea-language-spec/test-cases/05-lambda/01-lambda.scm
93
130
  - flea-language-spec/test-cases/05-lambda/02-call-in-place-lambda.scm
94
131
  - flea-language-spec/test-cases/05-lambda/03-define-with-lambda.scm
@@ -110,6 +147,7 @@ files:
110
147
  - flea-language-spec/test-cases/09-list-manipulation/04-append.scm
111
148
  - flea-language-spec/test-cases/09-list-manipulation/05-list.scm
112
149
  - flea-language-spec/test-cases/10-functional-examples/countdown.scm
150
+ - flea.gemspec
113
151
  - lib/flea.rb
114
152
  - lib/flea/environment.rb
115
153
  - lib/flea/interpreter.rb
@@ -138,94 +176,28 @@ files:
138
176
  - lib/flea/standard_library/set.scm
139
177
  - lib/flea/standard_library/string_to_num.scm
140
178
  - lib/flea/standard_library/subtraction_operator.scm
141
- - spec/flea/environment_spec.rb
142
- - spec/flea/interpreter_spec.rb
143
- - spec/flea/standard_library/addition_operator_spec.rb
144
- - spec/flea/standard_library/append_spec.rb
145
- - spec/flea/standard_library/begin_spec.rb
146
- - spec/flea/standard_library/car_spec.rb
147
- - spec/flea/standard_library/cdr_spec.rb
148
- - spec/flea/standard_library/cons_spec.rb
149
- - spec/flea/standard_library/display_spec.rb
150
- - spec/flea/standard_library/division_operator_spec.rb
151
- - spec/flea/standard_library/equality_operator_spec.rb
152
- - spec/flea/standard_library/gets_spec.rb
153
- - spec/flea/standard_library/greater_than_spec.rb
154
- - spec/flea/standard_library/if_spec.rb
155
- - spec/flea/standard_library/lambda_spec.rb
156
- - spec/flea/standard_library/less_than_spec.rb
157
- - spec/flea/standard_library/list_predicate_spec.rb
158
- - spec/flea/standard_library/list_spec.rb
159
- - spec/flea/standard_library/list_tail_spec.rb
160
- - spec/flea/standard_library/mutiplication_operator_spec.rb
161
- - spec/flea/standard_library/null_spec.rb
162
- - spec/flea/standard_library/quote_spec.rb
163
- - spec/flea/standard_library/rand_spec.rb
164
- - spec/flea/standard_library/read_spec.rb
165
- - spec/flea/standard_library/set_spec.rb
166
- - spec/flea/standard_library/string_to_num_spec.rb
167
- - spec/flea/standard_library/subtraction_operator_spec.rb
168
- - spec/spec_helper.rb
169
- has_rdoc: true
170
- homepage: http://github.com/aarongough/flea
171
- licenses: []
172
-
173
- post_install_message:
174
- rdoc_options:
175
- - --charset=UTF-8
176
- - --line-numbers
177
- - --inline-source
178
- require_paths:
179
+ - lib/flea/version.rb
180
+ homepage: https://github.com/aarongough/flea
181
+ licenses:
182
+ - MIT
183
+ metadata: {}
184
+ post_install_message:
185
+ rdoc_options: []
186
+ require_paths:
179
187
  - lib
180
- required_ruby_version: !ruby/object:Gem::Requirement
181
- none: false
182
- requirements:
188
+ required_ruby_version: !ruby/object:Gem::Requirement
189
+ requirements:
183
190
  - - ">="
184
- - !ruby/object:Gem::Version
185
- segments:
186
- - 0
187
- version: "0"
188
- required_rubygems_version: !ruby/object:Gem::Requirement
189
- none: false
190
- requirements:
191
+ - !ruby/object:Gem::Version
192
+ version: 2.5.0
193
+ required_rubygems_version: !ruby/object:Gem::Requirement
194
+ requirements:
191
195
  - - ">="
192
- - !ruby/object:Gem::Version
193
- segments:
194
- - 0
195
- version: "0"
196
+ - !ruby/object:Gem::Version
197
+ version: '0'
196
198
  requirements: []
197
-
198
- rubyforge_project:
199
- rubygems_version: 1.3.7
200
- signing_key:
201
- specification_version: 3
199
+ rubygems_version: 3.0.8
200
+ signing_key:
201
+ specification_version: 4
202
202
  summary: A tiny but flexible Lisp interpreter written in Ruby
203
- test_files:
204
- - spec/flea/environment_spec.rb
205
- - spec/flea/interpreter_spec.rb
206
- - spec/flea/standard_library/addition_operator_spec.rb
207
- - spec/flea/standard_library/append_spec.rb
208
- - spec/flea/standard_library/begin_spec.rb
209
- - spec/flea/standard_library/car_spec.rb
210
- - spec/flea/standard_library/cdr_spec.rb
211
- - spec/flea/standard_library/cons_spec.rb
212
- - spec/flea/standard_library/display_spec.rb
213
- - spec/flea/standard_library/division_operator_spec.rb
214
- - spec/flea/standard_library/equality_operator_spec.rb
215
- - spec/flea/standard_library/gets_spec.rb
216
- - spec/flea/standard_library/greater_than_spec.rb
217
- - spec/flea/standard_library/if_spec.rb
218
- - spec/flea/standard_library/lambda_spec.rb
219
- - spec/flea/standard_library/less_than_spec.rb
220
- - spec/flea/standard_library/list_predicate_spec.rb
221
- - spec/flea/standard_library/list_spec.rb
222
- - spec/flea/standard_library/list_tail_spec.rb
223
- - spec/flea/standard_library/mutiplication_operator_spec.rb
224
- - spec/flea/standard_library/null_spec.rb
225
- - spec/flea/standard_library/quote_spec.rb
226
- - spec/flea/standard_library/rand_spec.rb
227
- - spec/flea/standard_library/read_spec.rb
228
- - spec/flea/standard_library/set_spec.rb
229
- - spec/flea/standard_library/string_to_num_spec.rb
230
- - spec/flea/standard_library/subtraction_operator_spec.rb
231
- - spec/spec_helper.rb
203
+ test_files: []