lisp-interpreter 0.1.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 +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +75 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +173 -0
- data/Rakefile +6 -0
- data/bin/console.bat +2 -0
- data/bin/setup +8 -0
- data/bin/start.rb +2 -0
- data/lib/lisp/interpreter.rb +9 -0
- data/lib/lisp/interpreter/boolean.rb +42 -0
- data/lib/lisp/interpreter/checker.rb +45 -0
- data/lib/lisp/interpreter/errors.rb +13 -0
- data/lib/lisp/interpreter/functional.rb +272 -0
- data/lib/lisp/interpreter/list.rb +162 -0
- data/lib/lisp/interpreter/numbers.rb +161 -0
- data/lib/lisp/interpreter/object.rb +51 -0
- data/lib/lisp/interpreter/parser.rb +72 -0
- data/lib/lisp/interpreter/run.rb +4 -0
- data/lib/lisp/interpreter/strings.rb +131 -0
- data/lib/lisp/interpreter/tokenizer.rb +185 -0
- data/lib/lisp/interpreter/validator.rb +53 -0
- data/lib/lisp/interpreter/value_finder.rb +60 -0
- data/lib/lisp/interpreter/version.rb +5 -0
- data/lisp-interpreter.gemspec +35 -0
- data/spec/lisp/interpreter_spec.rb +987 -0
- data/spec/spec_helper.rb +14 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b05bfd3f510fb947e037a1de118289cd36f77f37
|
4
|
+
data.tar.gz: de9c23f86e55ef264e44a1e54bee0282dc83ee51
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b83a05bd09ad2d0f92b23b47e10f35810dfff583281357bf14980b299c10877d1cf91ec565bb58cab8c3611dc475353603ebd4571bdde65260f3d81b740595cd
|
7
|
+
data.tar.gz: f6b0962fec13104184464a03c60663393ef6858e8eb2f8aeb31dfa4c482cb9605a7868c682cb2cbca8c3954aa8445ea238be5f066ab6a9aef6aea38085658a14
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at zaki1993@abv.bg
|
59
|
+
. All
|
60
|
+
complaints will be reviewed and investigated and will result in a response that
|
61
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
62
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
63
|
+
Further details of specific enforcement policies may be posted separately.
|
64
|
+
|
65
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
66
|
+
faith may face temporary or permanent repercussions as determined by other
|
67
|
+
members of the project's leadership.
|
68
|
+
|
69
|
+
## Attribution
|
70
|
+
|
71
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
72
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
73
|
+
|
74
|
+
[homepage]: http://contributor-covenant.org
|
75
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Zaki Petrov
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,173 @@
|
|
1
|
+
# Lisp::Interpreter
|
2
|
+
Lisp interpreter implemented in Ruby.
|
3
|
+
|
4
|
+
## Installation
|
5
|
+
|
6
|
+
Add this line to your application's Gemfile:
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
gem 'lisp-interpreter'
|
10
|
+
```
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install lisp-interpreter
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
booleans:
|
22
|
+
|
23
|
+
#t; #t
|
24
|
+
|
25
|
+
#f; #f
|
26
|
+
|
27
|
+
(not #t); #f
|
28
|
+
|
29
|
+
(equal? 5 5); #t
|
30
|
+
|
31
|
+
(if (<= 2 3) 'TRUE 'FALSE); TRUE
|
32
|
+
|
33
|
+
numbers:
|
34
|
+
|
35
|
+
(+ 1 2); 3
|
36
|
+
|
37
|
+
(- 1 2); -1
|
38
|
+
|
39
|
+
(* 1 2); 2
|
40
|
+
|
41
|
+
(/ 1 2); 0.5
|
42
|
+
|
43
|
+
(quotient 10 3); 3
|
44
|
+
|
45
|
+
(remainder 10 3); 1
|
46
|
+
|
47
|
+
(modulo 10 3); 1
|
48
|
+
|
49
|
+
(numerator 5); 5
|
50
|
+
|
51
|
+
(denominator 5); 1
|
52
|
+
|
53
|
+
(abs -10); 10
|
54
|
+
|
55
|
+
(add1 1); 2
|
56
|
+
|
57
|
+
(sub1 1); 0
|
58
|
+
|
59
|
+
(min 5 3 1 2 4); 1
|
60
|
+
|
61
|
+
(max 5 3 1 2 4); 5
|
62
|
+
|
63
|
+
(< 1 2); #t
|
64
|
+
|
65
|
+
(<= 2 2); #t
|
66
|
+
|
67
|
+
(> 1 2); #f
|
68
|
+
|
69
|
+
(>= 2 2); #t
|
70
|
+
|
71
|
+
strings:
|
72
|
+
|
73
|
+
(string-length "Hello world"); 11
|
74
|
+
|
75
|
+
(substring "Hello world" 4); "o world"
|
76
|
+
|
77
|
+
(string-upcase "sample"); "SAMPLE"
|
78
|
+
|
79
|
+
(string-downcase "SAMPLE"); "sample"
|
80
|
+
|
81
|
+
(string-contains? "Racket" "Rac"); #t
|
82
|
+
|
83
|
+
(string->list "Sample"); (#\S #\a #\m #\p #\l #\e)
|
84
|
+
|
85
|
+
(string-split " foo bar baz \r\n\t"); ("foo" "bar" "baz")
|
86
|
+
|
87
|
+
(string? "str"); #t
|
88
|
+
|
89
|
+
(string-replace "foo bar baz" "bar" "blah"); "foo blah baz"
|
90
|
+
|
91
|
+
(string-prefix? "Racket" "Rac"); #t
|
92
|
+
|
93
|
+
(string-sufix? "Racket" "cket"); #t
|
94
|
+
|
95
|
+
(string-join '(1 2) "potato"); "1potato2"
|
96
|
+
|
97
|
+
list and pairs:
|
98
|
+
|
99
|
+
(null? \'()); #t
|
100
|
+
|
101
|
+
(cons 1 2); (1 . 2)
|
102
|
+
|
103
|
+
null; ()
|
104
|
+
|
105
|
+
(list 1 "s"); (1 "s")
|
106
|
+
|
107
|
+
(car (list #f 2 3 4)); #f
|
108
|
+
|
109
|
+
(cdr (list #f 2 3 4)); (2 3 4)
|
110
|
+
|
111
|
+
(cadr (list 1 2 3 4)); 2
|
112
|
+
|
113
|
+
(cdar (list (list 1 2) 2 3 4)); (2)
|
114
|
+
|
115
|
+
(list? (list 1 2)); #t
|
116
|
+
|
117
|
+
(pair? (list 1 2)); #t
|
118
|
+
|
119
|
+
(length (list 1 2 3)); 3
|
120
|
+
|
121
|
+
(reverse (list 1 2 3)); (3 2 1)
|
122
|
+
|
123
|
+
(remove 1 (list 1 2 3)); (2 3)
|
124
|
+
|
125
|
+
(shuffle (list 1 2 3)); permutation of [1, 2, 3]
|
126
|
+
|
127
|
+
(map (lambda (n)(+ 1 n)) '(1 2 3 4)); (2 3 4 5)
|
128
|
+
|
129
|
+
(foldl cons '() '(1 2 3 4 5)); (5 4 3 2 1)
|
130
|
+
|
131
|
+
(foldr cons '() '(1 2 3 4 5)); (1 2 3 4 5)
|
132
|
+
|
133
|
+
(filter (<= 3) '(1 2 3 4 5)); (1 2 3)
|
134
|
+
|
135
|
+
(member 2 (list 1 2 3 4)); (2 3 4
|
136
|
+
|
137
|
+
functions and procedures:
|
138
|
+
|
139
|
+
(lambda (x) (* 2 x)); <Proc>
|
140
|
+
|
141
|
+
((lambda (x) (* 2 x)) 5); 10
|
142
|
+
|
143
|
+
(apply + '(1 2 3)); 6
|
144
|
+
|
145
|
+
((compose fn1 fn2) val); (fn1 (fn2 val))
|
146
|
+
|
147
|
+
other:
|
148
|
+
|
149
|
+
(define x 5) ; 5
|
150
|
+
|
151
|
+
(define (even? x) (if (equal? (remainder x 2) 0) #t #f))
|
152
|
+
|
153
|
+
(even? 4); #t
|
154
|
+
|
155
|
+
(even? 3); #f
|
156
|
+
|
157
|
+
## Development
|
158
|
+
|
159
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
160
|
+
|
161
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
162
|
+
|
163
|
+
## Contributing
|
164
|
+
|
165
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/lisp-interpreter. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
166
|
+
|
167
|
+
## License
|
168
|
+
|
169
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
170
|
+
|
171
|
+
## Code of Conduct
|
172
|
+
|
173
|
+
Everyone interacting in the Lisp::Interpreter project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/lisp-interpreter/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console.bat
ADDED
data/bin/setup
ADDED
data/bin/start.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# Scheme booleans helper
|
2
|
+
module SchemeBooleansHelper
|
3
|
+
def if_idx_helper(other)
|
4
|
+
if other[0] == '('
|
5
|
+
idx = find_bracket_idx other, 0
|
6
|
+
(find_next_value other[idx + 1..-1])[0]
|
7
|
+
else
|
8
|
+
_, other = find_next_value other
|
9
|
+
(find_next_value other)[0]
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def if_helper(expr, other)
|
14
|
+
if expr == '#f'
|
15
|
+
if_idx_helper other
|
16
|
+
else
|
17
|
+
(find_next_value other)[0]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# Scheme booleans module
|
23
|
+
module SchemeBooleans
|
24
|
+
include SchemeBooleansHelper
|
25
|
+
def equal?(other)
|
26
|
+
raise 'Incorrect number of arguments' if other.size != 2
|
27
|
+
other[0].to_s == other[1].to_s ? '#t' : '#f'
|
28
|
+
end
|
29
|
+
|
30
|
+
def not(other)
|
31
|
+
raise 'Incorrect number of arguments' if other.size != 1
|
32
|
+
raise 'Invalid data type' unless check_for_bool other[0]
|
33
|
+
other[0] == '#t' ? '#f' : '#t'
|
34
|
+
end
|
35
|
+
|
36
|
+
def if(other)
|
37
|
+
raise 'Incorrect number of arguments' if other.empty?
|
38
|
+
expr, other = find_next_value other
|
39
|
+
raise 'Incorrect number of arguments' if other.empty?
|
40
|
+
if_helper expr, other
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# Check if variable is specific type
|
2
|
+
module SchemeChecker
|
3
|
+
def check_for_bool(token)
|
4
|
+
return true if ['#t', '#f'].include? token
|
5
|
+
is_instance_var = check_instance_var token
|
6
|
+
return true if is_instance_var && (check_for_bool get_var token)
|
7
|
+
false
|
8
|
+
end
|
9
|
+
|
10
|
+
def check_for_string(token)
|
11
|
+
return true if token.string?
|
12
|
+
is_instance_var = check_instance_var token
|
13
|
+
return true if is_instance_var && (check_for_string get_var token)
|
14
|
+
false
|
15
|
+
end
|
16
|
+
|
17
|
+
def check_for_number(token)
|
18
|
+
return true if token.to_s.number?
|
19
|
+
is_instance_var = check_instance_var token
|
20
|
+
return true if is_instance_var && (check_for_number get_var token)
|
21
|
+
false
|
22
|
+
end
|
23
|
+
|
24
|
+
def check_for_quote(token)
|
25
|
+
return true if token[0].quote?
|
26
|
+
is_instance_var = check_instance_var token
|
27
|
+
return true if is_instance_var && (check_for_number get_var token)
|
28
|
+
false
|
29
|
+
end
|
30
|
+
|
31
|
+
def check_instance_var(var)
|
32
|
+
return false if var.is_a? Proc
|
33
|
+
return false unless valid_var_name var
|
34
|
+
instance_variable_defined?("@#{var}")
|
35
|
+
end
|
36
|
+
|
37
|
+
def check_for_symbol(var)
|
38
|
+
var = var.join('') if var.is_a? Array
|
39
|
+
return true if var == '#\space'
|
40
|
+
return true if var.character?
|
41
|
+
is_instance_var = check_instance_var var
|
42
|
+
return true if is_instance_var && (check_for_character get_var var)
|
43
|
+
false
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# ErrorMessages module contains different error messages
|
2
|
+
module ErrorMessages
|
3
|
+
def unbalanced_brackets_error
|
4
|
+
'error signaled: unbalanced brackets'
|
5
|
+
end
|
6
|
+
|
7
|
+
def unbalanced_quotes_error
|
8
|
+
'error signaled: unbalanced quotes'
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class SchemeException < RuntimeError
|
13
|
+
end
|