flea 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (113) hide show
  1. data/.gitignore +1 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +274 -0
  4. data/Rakefile +36 -0
  5. data/VERSION +1 -0
  6. data/bin/flea +39 -0
  7. data/examples/guess-the-number.scm +18 -0
  8. data/flea-language-spec/README.rdoc +3 -0
  9. data/flea-language-spec/flea-language-spec.yaml +2 -0
  10. data/flea-language-spec/test-cases/01-display-and-basic-literals/01-display-string-literal.scm +5 -0
  11. data/flea-language-spec/test-cases/01-display-and-basic-literals/02-display-integer-literal.scm +5 -0
  12. data/flea-language-spec/test-cases/01-display-and-basic-literals/03-display-boolean-true-literal.scm +5 -0
  13. data/flea-language-spec/test-cases/01-display-and-basic-literals/04-display-boolean-false-literal.scm +5 -0
  14. data/flea-language-spec/test-cases/01-display-and-basic-literals/05-display-list-literal-using-quote.scm +5 -0
  15. data/flea-language-spec/test-cases/01-display-and-basic-literals/06-display-identifier-quoted.scm +5 -0
  16. data/flea-language-spec/test-cases/02-variables/01-define-with-string.scm +6 -0
  17. data/flea-language-spec/test-cases/02-variables/02-define-with-integer.scm +6 -0
  18. data/flea-language-spec/test-cases/02-variables/03-define-with-boolean-true.scm +6 -0
  19. data/flea-language-spec/test-cases/02-variables/04-define-with-boolean-false.scm +6 -0
  20. data/flea-language-spec/test-cases/02-variables/05-define-with-list.scm +6 -0
  21. data/flea-language-spec/test-cases/03-basic-built-in-procedures/01-addition.scm +5 -0
  22. data/flea-language-spec/test-cases/03-basic-built-in-procedures/02-subtraction.scm +5 -0
  23. data/flea-language-spec/test-cases/03-basic-built-in-procedures/03-multiplication.scm +5 -0
  24. data/flea-language-spec/test-cases/03-basic-built-in-procedures/04-division.scm +5 -0
  25. data/flea-language-spec/test-cases/03-basic-built-in-procedures/05-equality-true-integer.scm +5 -0
  26. data/flea-language-spec/test-cases/03-basic-built-in-procedures/06-equality-false-integer.scm +5 -0
  27. data/flea-language-spec/test-cases/03-basic-built-in-procedures/07-equal?/01-equal?-true-integer.scm +5 -0
  28. data/flea-language-spec/test-cases/03-basic-built-in-procedures/07-equal?/02-equal?-false-integer.scm +5 -0
  29. data/flea-language-spec/test-cases/03-basic-built-in-procedures/07-equal?/03-equal?-true-string.scm +5 -0
  30. data/flea-language-spec/test-cases/03-basic-built-in-procedures/07-equal?/04-equal?-false-string.scm +5 -0
  31. data/flea-language-spec/test-cases/03-basic-built-in-procedures/07-equal?/05-equal?-true-boolean.scm +5 -0
  32. data/flea-language-spec/test-cases/03-basic-built-in-procedures/07-equal?/06-equal?-false-boolean.scm +5 -0
  33. data/flea-language-spec/test-cases/03-basic-built-in-procedures/07-equal?/07-equal?-true-list.scm +7 -0
  34. data/flea-language-spec/test-cases/03-basic-built-in-procedures/07-equal?/08-equal?-false-list.scm +7 -0
  35. data/flea-language-spec/test-cases/04-comma-quoting/01-comma-quoting.scm +5 -0
  36. data/flea-language-spec/test-cases/05-lambda/01-lambda.scm +5 -0
  37. data/flea-language-spec/test-cases/05-lambda/02-call-in-place-lambda.scm +5 -0
  38. data/flea-language-spec/test-cases/05-lambda/03-define-with-lambda.scm +6 -0
  39. data/flea-language-spec/test-cases/05-lambda/04-define-and-call-lambda.scm +8 -0
  40. data/flea-language-spec/test-cases/05-lambda/05-lambda-repeated-argument.scm +9 -0
  41. data/flea-language-spec/test-cases/05-lambda/06-lambda-list-argument.scm +8 -0
  42. data/flea-language-spec/test-cases/05-lambda/07-lambda-n-or-more-arguments.scm +12 -0
  43. data/flea-language-spec/test-cases/05-lambda/08-lambda-should-return-last-result.scm +16 -0
  44. data/flea-language-spec/test-cases/06-if/01-if-true-single-arg.scm +6 -0
  45. data/flea-language-spec/test-cases/06-if/02-if-false-single-arg.scm +6 -0
  46. data/flea-language-spec/test-cases/06-if/03-if-true-two-args.scm +7 -0
  47. data/flea-language-spec/test-cases/06-if/04-if-false-two-args.scm +7 -0
  48. data/flea-language-spec/test-cases/07-set/01-should-not-set-undefined-variable.scm +5 -0
  49. data/flea-language-spec/test-cases/07-set/02-set-previously-defined-variable.scm +8 -0
  50. data/flea-language-spec/test-cases/08-derived-expressions/01-begin/01-begin.scm +8 -0
  51. data/flea-language-spec/test-cases/09-list-manipulation/01-car.scm +7 -0
  52. data/flea-language-spec/test-cases/09-list-manipulation/02-cdr.scm +7 -0
  53. data/flea-language-spec/test-cases/09-list-manipulation/03-list-tail.scm +7 -0
  54. data/flea-language-spec/test-cases/09-list-manipulation/04-append.scm +10 -0
  55. data/flea-language-spec/test-cases/09-list-manipulation/05-list.scm +5 -0
  56. data/flea-language-spec/test-cases/10-functional-examples/countdown.scm +15 -0
  57. data/lib/flea.rb +5 -0
  58. data/lib/flea/environment.rb +38 -0
  59. data/lib/flea/interpreter.rb +67 -0
  60. data/lib/flea/standard_library/addition_operator.scm +7 -0
  61. data/lib/flea/standard_library/append.scm +7 -0
  62. data/lib/flea/standard_library/begin.scm +11 -0
  63. data/lib/flea/standard_library/car.scm +7 -0
  64. data/lib/flea/standard_library/cdr.scm +7 -0
  65. data/lib/flea/standard_library/cons.scm +13 -0
  66. data/lib/flea/standard_library/display.scm +8 -0
  67. data/lib/flea/standard_library/division_operator.scm +7 -0
  68. data/lib/flea/standard_library/equality_operator.scm +8 -0
  69. data/lib/flea/standard_library/gets.scm +6 -0
  70. data/lib/flea/standard_library/greater_than.scm +8 -0
  71. data/lib/flea/standard_library/if.scm +10 -0
  72. data/lib/flea/standard_library/lambda.scm +57 -0
  73. data/lib/flea/standard_library/less_than.scm +8 -0
  74. data/lib/flea/standard_library/list.scm +8 -0
  75. data/lib/flea/standard_library/list_predicate.scm +6 -0
  76. data/lib/flea/standard_library/list_tail.scm +5 -0
  77. data/lib/flea/standard_library/multiplication_operator.scm +7 -0
  78. data/lib/flea/standard_library/null.scm +3 -0
  79. data/lib/flea/standard_library/quote.scm +6 -0
  80. data/lib/flea/standard_library/rand.scm +6 -0
  81. data/lib/flea/standard_library/read.scm +6 -0
  82. data/lib/flea/standard_library/set.scm +9 -0
  83. data/lib/flea/standard_library/string_to_num.scm +6 -0
  84. data/lib/flea/standard_library/subtraction_operator.scm +7 -0
  85. data/spec/flea/environment_spec.rb +114 -0
  86. data/spec/flea/interpreter_spec.rb +85 -0
  87. data/spec/flea/standard_library/addition_operator_spec.rb +23 -0
  88. data/spec/flea/standard_library/append_spec.rb +17 -0
  89. data/spec/flea/standard_library/begin_spec.rb +20 -0
  90. data/spec/flea/standard_library/car_spec.rb +17 -0
  91. data/spec/flea/standard_library/cdr_spec.rb +17 -0
  92. data/spec/flea/standard_library/cons_spec.rb +25 -0
  93. data/spec/flea/standard_library/display_spec.rb +36 -0
  94. data/spec/flea/standard_library/division_operator_spec.rb +29 -0
  95. data/spec/flea/standard_library/equality_operator_spec.rb +45 -0
  96. data/spec/flea/standard_library/gets_spec.rb +23 -0
  97. data/spec/flea/standard_library/greater_than_spec.rb +29 -0
  98. data/spec/flea/standard_library/if_spec.rb +59 -0
  99. data/spec/flea/standard_library/lambda_spec.rb +70 -0
  100. data/spec/flea/standard_library/less_than_spec.rb +29 -0
  101. data/spec/flea/standard_library/list_predicate_spec.rb +25 -0
  102. data/spec/flea/standard_library/list_spec.rb +24 -0
  103. data/spec/flea/standard_library/list_tail_spec.rb +17 -0
  104. data/spec/flea/standard_library/mutiplication_operator_spec.rb +23 -0
  105. data/spec/flea/standard_library/null_spec.rb +26 -0
  106. data/spec/flea/standard_library/quote_spec.rb +20 -0
  107. data/spec/flea/standard_library/rand_spec.rb +25 -0
  108. data/spec/flea/standard_library/read_spec.rb +23 -0
  109. data/spec/flea/standard_library/set_spec.rb +23 -0
  110. data/spec/flea/standard_library/string_to_num_spec.rb +28 -0
  111. data/spec/flea/standard_library/subtraction_operator_spec.rb +24 -0
  112. data/spec/spec_helper.rb +10 -0
  113. metadata +231 -0
@@ -0,0 +1 @@
1
+ *.swp
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Aaron Gough (http://thingsaaronmade.com/)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,274 @@
1
+ = Flea
2
+
3
+ Flea is a tiny Lisp interpreter implemented in Ruby. Flea is not designed to be a production language, instead it is designed to be an example of how simple it can be to bootstrap the core of a small flexible language. Flea essentially defines an informal subset of Scheme, just enough to be fun and interesting.
4
+
5
+ === A Quick Example
6
+
7
+ This is the classic 'guess the number' program implemented in Flea:
8
+
9
+ (define number (+ (rand 9) 1))
10
+
11
+ (display "\n\nI'm thinking of a number between 1 and 10,\n")
12
+ (display "try to guess it!\n\n")
13
+
14
+ (define user-guess
15
+ (lambda ()
16
+ (display "Take a guess - ")
17
+ (define guess (string-to-num (gets)))
18
+ (if (equal? guess number)
19
+ (display "Good guess!\n")
20
+ (begin
21
+ (if (greater-than? guess number)
22
+ (display "Lower!\n")
23
+ (display "Higher!\n"))
24
+ (user-guess)))))
25
+
26
+ (user-guess)
27
+
28
+ === Installation
29
+
30
+ For ease of use the Flea is packaged as a RubyGem. Providing you already have Ruby and RubyGems installing Flea is as easy as entering the following command in a terminal:
31
+
32
+ gem install flea
33
+
34
+ Mac OS X and most Unix/Linux distributions come with an installation of Ruby and RubyGems. If you do not have Ruby and RubyGems installed please check the {Ruby website for instructions}[http://www.ruby-lang.org/en/downloads/].
35
+
36
+ === Usage
37
+
38
+ After Flea is installed you can run a program by typing the following on the command line:
39
+
40
+ flea /path/to/program
41
+
42
+ You can also launch Flea's interactive shell by simply calling `flea` with no arguments.
43
+
44
+ === Author & Credits
45
+
46
+ Author:: {Aaron Gough}[mailto:aaron@aarongough.com]
47
+
48
+ Copyright (c) 2010 {Aaron Gough}[http://thingsaaronmade.com/] ({thingsaaronmade.com}[http://thingsaaronmade.com/]), released under the MIT license
49
+
50
+ ---
51
+
52
+ == API Documentation and More Info
53
+
54
+ === More Info
55
+
56
+ Flea's core is only about 100 lines of code, and provides a simple interface that is used to build the rest of the language. Flea is highly extensible because of its foreign function interface which allows Ruby Proc objects to be defined and then executed within the context of the Flea interpreter. The entire standard library of the language is implemented using this foreign function interface. For example, here is the implementation of the multiplication operator:
57
+
58
+ (define *
59
+ (native_function "
60
+ Proc.new() do |arguments, interpreter|
61
+ tmp = arguments.map {|item| interpreter.evaluate(item)}
62
+ tmp.inject {|sum, n| sum * n}
63
+ end
64
+ "))
65
+
66
+ === API
67
+
68
+ Flea comes with a small but thoughtful standard library that includes functions for:
69
+
70
+ ==== Output
71
+
72
+ (display) Outputs data to STDOUT:
73
+
74
+ (display "test")
75
+ # => test
76
+
77
+ (display '(1 2 3))
78
+ # => (1 2 3)
79
+
80
+ ==== Input
81
+
82
+ (read) Reads and parses an s-expression from STDIN:
83
+
84
+ (read) # user enters '(1 2 3)'
85
+ # => (1 2 3)
86
+
87
+ (gets) Reads a string from STDIN:
88
+
89
+ (gets) # user enters '(1 2 3)'
90
+ # => "(1 2 3)"
91
+
92
+ ==== Variables
93
+
94
+ (define) Sets a variable:
95
+
96
+ (define test 1)
97
+ (display test)
98
+ # => 1
99
+
100
+ (set!) Re-defines an existing variable:
101
+
102
+ (define test 1)
103
+ (set! test 2)
104
+ (display test)
105
+ # => 2
106
+
107
+ ==== Numeric operations
108
+
109
+ (+) Add one or more numbers:
110
+
111
+ (+ 1 2 3)
112
+ # => 6
113
+
114
+ (-) Subtract one or more numbers:
115
+
116
+ (- 10 2 1)
117
+ # => 7
118
+
119
+ (*) Multiply one or more numbers:
120
+
121
+ (* 10 2 4)
122
+ # => 80
123
+
124
+ (/) Divide one or more numbers:
125
+
126
+ (/ 100 2 2)
127
+ # => 25
128
+
129
+ (greater-than?) Returns true if it's first argument is greater than all the others, false otherwise:
130
+
131
+ (greater-than? 10 2 3 4)
132
+ # => #t
133
+
134
+ (greater-than? 1 2 3 4)
135
+ # => #f
136
+
137
+ (less-than?) Returns true if it's first argument is smaller than all the others, false otherwise:
138
+
139
+ (less-than? 1 3 4 5)
140
+ # => #t
141
+
142
+ (less-than? 50 2 45 100)
143
+ # => #f
144
+
145
+ (equal?) and (=) Return true if all their arguments are the same, false otherwise:
146
+
147
+ (equal? 1 1 1)
148
+ # => #t
149
+
150
+ (= 1 2 3)
151
+ # => #f
152
+
153
+ ==== List creation and manipulation
154
+
155
+ (list) Creates a new list from it's arguments:
156
+
157
+ (list (+ 1 2) "test" (rand 10))
158
+ # => (3 "test" 5)
159
+
160
+ (list?) Returns true if it's first argument is a list, false otherwise:
161
+
162
+ (list? '())
163
+ # => #t
164
+
165
+ (list? 1)
166
+ # => #f
167
+
168
+ (list-tail) Returns a new list created by removing the first n elements of the provided list:
169
+
170
+ (define a '(1 2 3 4 5 6))
171
+ (list-tail a 3)
172
+ # => (4 5 6)
173
+
174
+ (car) Returns the first item of a list:
175
+
176
+ (car '(1 2 3))
177
+ # => 1
178
+
179
+ (cdr) Returns the remainder of a list:
180
+
181
+ (cdr '(1 2 3))
182
+ # => (2 3)
183
+
184
+ (cons) Creates a new list by using it's first argument as the CAR it's second argument as the CDR:
185
+
186
+ (cons '(1 2 3) 3)
187
+ # => ((1 2 3) 3)
188
+
189
+ (cons 1 2)
190
+ # => (1 2)
191
+
192
+ (cons 1 (2 3 4))
193
+ # => (1 2 3 4)
194
+
195
+ (append) Creates a new list by concatenating it's arguments, it's first argument must be a list:
196
+
197
+ (append '(1 2) '(3 4) 5)
198
+ # => (1 2 3 4 5)
199
+
200
+ (null?) Returns true if it's first argument is null (the empty list):
201
+
202
+ (null '())
203
+ # => #t
204
+
205
+ (null '(1 2 3))
206
+ # => #f
207
+
208
+ (null? 1)
209
+ # => #f
210
+
211
+ ==== Conditionals
212
+
213
+ (if) If it's first argument is true then it will execute it's first code block, otherwise it will execute the second code block (if any):
214
+
215
+ (if (equal? 1 1)
216
+ (display "true")
217
+ (display "false"))
218
+ # => true
219
+
220
+ (if (equal? 2 1)
221
+ (display "true")
222
+ (display "false"))
223
+ # => false
224
+
225
+ ==== Function creation
226
+
227
+ (lambda) Creates a new function:
228
+
229
+ (define adder
230
+ (lambda (a)
231
+ (+ a 10)))
232
+
233
+ (adder 5)
234
+ # => 15
235
+
236
+ For more info on lambda syntax read the {R5RS Scheme specification}[http://schemers.org/Documents/Standards/R5RS/HTML/r5rs-Z-H-7.html#%_sec_4.1.4]
237
+
238
+ ==== Type conversion
239
+
240
+ (string-to-num) Converts a string containing a number into a numeric literal:
241
+
242
+ (string-to-num "123")
243
+ # => 123
244
+
245
+ ==== Misc
246
+
247
+ (rand) Returns a random number less than or equal to it's first argument:
248
+
249
+ (rand 10)
250
+ # => 1
251
+
252
+ (rand 10)
253
+ # => 6
254
+
255
+ ==== Foreign function interface
256
+
257
+ (native_function) Takes a string representing a Ruby Proc object and returns it in a form that is able to be called like any other Flea function. The proc must take two arguments (arguments and interpreter):
258
+
259
+ (define test
260
+ (native_function "
261
+ Proc.new() do |arguments, interpreter|
262
+ puts "Arguments class: " + arguments.class
263
+ puts "Arguments: " + arguments.inspect
264
+ puts "Interpreter class: " + interpreter.class
265
+ puts "foo"
266
+ end
267
+ ")
268
+
269
+ (test 1 2 3 4)
270
+ # => Arguments class: Array
271
+ Arguments: [1, 2, 3, 4]
272
+ Interpreter class: Flea::Interpreter
273
+ foo
274
+
@@ -0,0 +1,36 @@
1
+ require 'rubygems'
2
+ require 'any-spec'
3
+ require 'rspec/core/rake_task'
4
+
5
+ begin
6
+ require 'jeweler'
7
+ Jeweler::Tasks.new do |gemspec|
8
+ gemspec.name = "flea"
9
+ gemspec.summary = "A tiny but flexible Lisp interpreter written in Ruby"
10
+ gemspec.description = "Flea is an extremely simple, but extremely extensible Lisp interpreter written in Ruby."
11
+ gemspec.email = "aaron@aarongough.com"
12
+ gemspec.homepage = "http://github.com/aarongough/flea"
13
+ gemspec.authors = ["Aaron Gough"]
14
+ gemspec.rdoc_options << '--line-numbers' << '--inline-source'
15
+ gemspec.extra_rdoc_files = ['README.rdoc', 'MIT-LICENSE']
16
+ gemspec.add_dependency "sexpistol"
17
+ gemspec.add_development_dependency "any-spec"
18
+ gemspec.executables << 'flea'
19
+ end
20
+ rescue LoadError
21
+ puts "Jeweler not available. Install it with: gem install jeweler"
22
+ end
23
+
24
+ task :test => [:spec, :anyspec]
25
+
26
+ task :anyspec do
27
+ test_runner = AnySpec::TestRunner.new("./bin/flea", "./flea-language-spec/flea-language-spec.yaml")
28
+ test_runner.run_tests
29
+ end
30
+
31
+ desc "Run the specs for Flea"
32
+ RSpec::Core::RakeTask.new do |t|
33
+ t.rspec_opts = "-c"
34
+ t.fail_on_error = false
35
+ t.verbose = false
36
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,39 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'stringio'
4
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'flea.rb'))
5
+
6
+ interpreter = Flea::Interpreter.new
7
+
8
+ if ARGV.length > 0
9
+ interpreter.run(ARGF.read)
10
+ exit
11
+ end
12
+
13
+ loop do
14
+ program = []
15
+ indent = 0
16
+ loop do
17
+ print "> " + (" " * indent)
18
+ program << gets
19
+
20
+ tmp_program = program.join(" ")
21
+ open_p_count = tmp_program.count("(")
22
+ close_p_count = tmp_program.count(")")
23
+
24
+ break if open_p_count == close_p_count
25
+ indent += 1 if program.last.count("(") > program.last.count(")")
26
+ indent -= 1 if program.last.count("(") < program.last.count(")")
27
+ end
28
+ program = program.join(" ")
29
+ exit if program.strip == "quit"
30
+
31
+ old_stdout = $stdout
32
+ buffer = StringIO.new
33
+ $stdout = buffer
34
+ result = interpreter.run program
35
+ $stdout = old_stdout
36
+ print buffer.string
37
+ print "\n" unless buffer.string.empty?
38
+ puts " => #{result.inspect}"
39
+ end
@@ -0,0 +1,18 @@
1
+ (define number (+ (rand 9) 1))
2
+
3
+ (display "\n\nI'm thinking of a number between 1 and 10,\n")
4
+ (display "try to guess it!\n\n")
5
+
6
+ (define user-guess
7
+ (lambda ()
8
+ (display "Take a guess - ")
9
+ (define guess (string-to-num (gets)))
10
+ (if (equal? guess number)
11
+ (display "Good guess!\n")
12
+ (begin
13
+ (if (greater-than? guess number)
14
+ (display "Lower!\n")
15
+ (display "Higher!\n"))
16
+ (user-guess)))))
17
+
18
+ (user-guess)
@@ -0,0 +1,3 @@
1
+ = Flea Language Specifcation
2
+
3
+ This is a small executable language specification for Flea written using {AnySpec}[https://github.com/aarongough/any-spec]. It basically serves as an integration test-suite for the language, and provides an easy way of doing test-first development of new language features.
@@ -0,0 +1,2 @@
1
+ specification_root: test-cases
2
+ specification_extension: .scm
@@ -0,0 +1,5 @@
1
+ (display "Hello World!")
2
+
3
+ ----
4
+
5
+ assert_output "Hello World!"
@@ -0,0 +1,5 @@
1
+ (display (quote ( 1 "Hello!" #t #f )))
2
+
3
+ ----
4
+
5
+ assert_output "(1 Hello! #t #f)"
@@ -0,0 +1,5 @@
1
+ (display (quote test))
2
+
3
+ ----
4
+
5
+ assert_output "test"