theta 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "shoulda", ">= 0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.5.2"
12
+ gem "rcov", ">= 0"
13
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,20 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.5.2)
6
+ bundler (~> 1.0.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ rake (0.8.7)
10
+ rcov (0.9.9)
11
+ shoulda (2.11.3)
12
+
13
+ PLATFORMS
14
+ x86-mingw32
15
+
16
+ DEPENDENCIES
17
+ bundler (~> 1.0.0)
18
+ jeweler (~> 1.5.2)
19
+ rcov
20
+ shoulda
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Chris O'Neal
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.
data/README.rdoc ADDED
@@ -0,0 +1,23 @@
1
+ = Theta
2
+
3
+ Theta is a Lisp interpreter for Ruby. It was heavily influenced by {lis.py}[http://norvig.com/lispy.html] and {flea}[https://github.com/aarongough/flea]. It was primarily made as a learning exercise.
4
+
5
+ == How to use
6
+
7
+ Theta is distributed as a gem, so install by typing:
8
+
9
+ % gem install theta
10
+
11
+ After installation, simply type theta to get to the interactive interpreter prompt.
12
+
13
+ % theta
14
+ theta> (define a 2)
15
+ 2
16
+ theta> (+ a 3)
17
+ 5
18
+ theta> (- 6 a)
19
+ 4
20
+
21
+ To exit, simply press enter on a blank line.
22
+
23
+ It's a little low on features currently, but I'll be adding more eventually!
data/Rakefile ADDED
@@ -0,0 +1,55 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+
4
+ begin
5
+ Bundler.setup(:default, :development)
6
+ rescue Bundler::BundlerError => e
7
+ $stderr.puts e.message
8
+ $stderr.puts "Run `bundle install` to install missing gems"
9
+ exit e.status_code
10
+ end
11
+ require 'rake'
12
+
13
+ require 'jeweler'
14
+ Jeweler::Tasks.new do |gem|
15
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
16
+ gem.name = "theta"
17
+ gem.homepage = "http://github.com/ctoneal/theta"
18
+ gem.license = "MIT"
19
+ gem.summary = %Q{Theta is a Lisp interpreter in Ruby}
20
+ gem.description = %Q{Theta was created as a learning project based off of lis.py and flea}
21
+ gem.email = "ctoneal@gmail.com"
22
+ gem.authors = ["Chris O'Neal"]
23
+ gem.executables << 'theta'
24
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
25
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
26
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
27
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
28
+ end
29
+ Jeweler::RubygemsDotOrgTasks.new
30
+
31
+ require 'rake/testtask'
32
+ Rake::TestTask.new(:test) do |test|
33
+ test.libs << 'lib' << 'test'
34
+ test.pattern = 'test/**/test_*.rb'
35
+ test.verbose = true
36
+ end
37
+
38
+ require 'rcov/rcovtask'
39
+ Rcov::RcovTask.new do |test|
40
+ test.libs << 'test'
41
+ test.pattern = 'test/**/test_*.rb'
42
+ test.verbose = true
43
+ end
44
+
45
+ task :default => :test
46
+
47
+ require 'rake/rdoctask'
48
+ Rake::RDocTask.new do |rdoc|
49
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
50
+
51
+ rdoc.rdoc_dir = 'rdoc'
52
+ rdoc.title = "theta #{version}"
53
+ rdoc.rdoc_files.include('README*')
54
+ rdoc.rdoc_files.include('lib/**/*.rb')
55
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/bin/theta ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "lib", "theta.rb"))
4
+
5
+ theta = Theta::Base.new
6
+ # if a file was passed in, run it
7
+ # otherwise, enter the repl
8
+ if ARGV.length > 0
9
+ theta.run(ARGV[1])
10
+ else
11
+ theta.repl
12
+ end
@@ -0,0 +1,30 @@
1
+ module Theta
2
+ # An environment, stores defined items for a
3
+ # particular level
4
+ class Environment
5
+ def initialize(parent = nil)
6
+ @parent = parent
7
+ @table = {}
8
+ if @parent.nil?
9
+ define(":#t", true)
10
+ define(":#f", false)
11
+ end
12
+ end
13
+
14
+ # find an item in the environment
15
+ def find(name)
16
+ if @table.has_key?(name)
17
+ return @table[name]
18
+ elsif @parent.nil?
19
+ return nil
20
+ else
21
+ @parent.find(name)
22
+ end
23
+ end
24
+
25
+ # define an item in the environment
26
+ def define(name, value)
27
+ @table[name] = value
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,61 @@
1
+ module Theta
2
+ # interpret scheme code
3
+ class Interpreter
4
+
5
+ #initialize the environment and the parser
6
+ def initialize
7
+ @global_environment = Environment.new
8
+ @parser = Parser.new
9
+ load_library
10
+ end
11
+
12
+ # load any predefined library files
13
+ def load_library
14
+ library_directory = File.expand_path(File.join(File.dirname(__FILE__), "library"))
15
+ Dir.foreach(library_directory) do |file|
16
+ if file != "." && file != ".."
17
+ f = File.open(File.join(library_directory, file))
18
+ run(f.read)
19
+ end
20
+ end
21
+ end
22
+
23
+ # run a line of code
24
+ def run(program)
25
+ expressions = parse(program)
26
+ return evaluate(expressions)
27
+ end
28
+
29
+ # call the parser to make a string interpretable
30
+ def parse(string)
31
+ @parser.parse(string)
32
+ end
33
+
34
+ # call the parser to make something readable
35
+ def make_readable(value)
36
+ @parser.to_string(value)
37
+ end
38
+
39
+ # evaluate the scheme expression
40
+ def evaluate(expression, environment=@global_environment)
41
+ if expression.is_a? Symbol
42
+ return environment.find(expression)
43
+ elsif not expression.is_a? Array
44
+ return expression
45
+ end
46
+ case expression[0]
47
+ when :define
48
+ return environment.define(expression [1], evaluate(expression[2], environment))
49
+ when :ruby_func
50
+ return eval expression[1]
51
+ else
52
+ function = evaluate(expression[0], environment)
53
+ if function.lambda?
54
+ arguments = expression[1, expression.length]
55
+ function.call(arguments, self)
56
+ else
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,8 @@
1
+ ( define +
2
+ (ruby_func "
3
+ lambda { |arguments, interpreter|
4
+ temp = arguments.map { |item| interpreter.evaluate(item) };
5
+ temp.inject { |result, n| result + n};
6
+ }
7
+ ")
8
+ )
@@ -0,0 +1,8 @@
1
+ (define /
2
+ (ruby_func "
3
+ lambda { |arguments, interpreter|
4
+ temp = arguments.map { |item| interpreter.evaluate(item) };
5
+ temp.inject { |result, n| result / n};
6
+ }
7
+ ")
8
+ )
@@ -0,0 +1,8 @@
1
+ (define *
2
+ (ruby_func "
3
+ lambda { |arguments, interpreter|
4
+ temp = arguments.map { |item| interpreter.evaluate(item) };
5
+ temp.inject { |result, n| result * n};
6
+ }
7
+ ")
8
+ )
@@ -0,0 +1,8 @@
1
+ (define -
2
+ (ruby_func "
3
+ lambda { |arguments, interpreter|
4
+ temp = arguments.map { |item| interpreter.evaluate(item) };
5
+ temp.inject { |result, n| result - n};
6
+ }
7
+ ")
8
+ )
@@ -0,0 +1,66 @@
1
+ module Theta
2
+ # parses strings into arrays of items understandable by the interpreter
3
+ class Parser
4
+
5
+ # returns the scheme interpretation of the string
6
+ def parse(string)
7
+ return read_from(tokenize(string))
8
+ end
9
+
10
+ # converts a string into an array of tokens
11
+ def tokenize(string)
12
+ return string.gsub("(", " ( ").gsub(")", " ) ").split
13
+ end
14
+
15
+ # reads an expression from an array of tokens (created
16
+ # by tokenize)
17
+ def read_from(tokens)
18
+ if tokens.length == 0
19
+ raise SyntaxError, "unexpected EOF while reading"
20
+ end
21
+ token = tokens.shift
22
+ if "(" == token
23
+ l = []
24
+ until tokens[0] == ")"
25
+ l << read_from(tokens)
26
+ end
27
+ tokens.shift
28
+ return l
29
+ elsif ")" == token
30
+ raise SyntaxError, "unexpected )"
31
+ elsif "\"" == token
32
+ string = ""
33
+ until tokens[0] == "\""
34
+ string += " " + tokens.shift
35
+ end
36
+ tokens.shift
37
+ return string.gsub(/\n/, "")
38
+ else
39
+ return atom(token)
40
+ end
41
+ end
42
+
43
+ # returns appropriate numeric object if a number,
44
+ # otherwise returns a symbol
45
+ def atom(token)
46
+ begin
47
+ return Integer(token)
48
+ rescue ArgumentError
49
+ begin
50
+ return Float(token)
51
+ rescue ArgumentError
52
+ return token.to_sym
53
+ end
54
+ end
55
+ end
56
+
57
+ # convert an expression back to a readable string
58
+ def to_string(expression)
59
+ if expression.is_a? Array
60
+ return "(" + " ".join(expression.map { |exp| to_string(exp) }) + ")"
61
+ else
62
+ return expression.to_s
63
+ end
64
+ end
65
+ end
66
+ end
data/lib/theta.rb ADDED
@@ -0,0 +1,36 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "theta", "interpreter.rb"))
2
+ require File.expand_path(File.join(File.dirname(__FILE__), "theta", "environment.rb"))
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "theta", "parser.rb"))
4
+
5
+ module Theta
6
+ # runs the interpreter
7
+ class Base
8
+
9
+ def initialize
10
+ @interpreter = Interpreter.new
11
+ end
12
+
13
+ # run a preexisting program
14
+ def run(fileName)
15
+ program = File.open(fileName) { |f| f.read }
16
+ puts @interpreter.run(program)
17
+ end
18
+
19
+ # start an interactive interpreter
20
+ def repl
21
+ while true
22
+ print "theta> "
23
+ input = gets
24
+ input.strip!
25
+ if input.empty?
26
+ puts "Exiting..."
27
+ break
28
+ end
29
+ value = @interpreter.run(input)
30
+ unless value.nil?
31
+ puts @interpreter.make_readable(value)
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'theta'
16
+
17
+ class Test::Unit::TestCase
18
+ end
data/theta.gemspec ADDED
@@ -0,0 +1,70 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{theta}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Chris O'Neal"]
12
+ s.date = %q{2011-03-28}
13
+ s.description = %q{Theta was created as a learning project based off of lis.py and flea}
14
+ s.email = %q{ctoneal@gmail.com}
15
+ s.executables = ["theta", "theta"]
16
+ s.extra_rdoc_files = [
17
+ "LICENSE.txt",
18
+ "README.rdoc"
19
+ ]
20
+ s.files = [
21
+ ".document",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "bin/theta",
29
+ "lib/theta.rb",
30
+ "lib/theta/environment.rb",
31
+ "lib/theta/interpreter.rb",
32
+ "lib/theta/library/add.scm",
33
+ "lib/theta/library/divide.scm",
34
+ "lib/theta/library/multiply.scm",
35
+ "lib/theta/library/subtract.scm",
36
+ "lib/theta/parser.rb",
37
+ "test/helper.rb",
38
+ "theta.gemspec"
39
+ ]
40
+ s.homepage = %q{http://github.com/ctoneal/theta}
41
+ s.licenses = ["MIT"]
42
+ s.require_paths = ["lib"]
43
+ s.rubygems_version = %q{1.6.2}
44
+ s.summary = %q{Theta is a Lisp interpreter in Ruby}
45
+ s.test_files = [
46
+ "test/helper.rb"
47
+ ]
48
+
49
+ if s.respond_to? :specification_version then
50
+ s.specification_version = 3
51
+
52
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
53
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
54
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
55
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
56
+ s.add_development_dependency(%q<rcov>, [">= 0"])
57
+ else
58
+ s.add_dependency(%q<shoulda>, [">= 0"])
59
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
60
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
61
+ s.add_dependency(%q<rcov>, [">= 0"])
62
+ end
63
+ else
64
+ s.add_dependency(%q<shoulda>, [">= 0"])
65
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
66
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
67
+ s.add_dependency(%q<rcov>, [">= 0"])
68
+ end
69
+ end
70
+
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: theta
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Chris O'Neal
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-03-28 00:00:00.000000000 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: shoulda
17
+ requirement: &7590828 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: *7590828
26
+ - !ruby/object:Gem::Dependency
27
+ name: bundler
28
+ requirement: &7589724 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *7589724
37
+ - !ruby/object:Gem::Dependency
38
+ name: jeweler
39
+ requirement: &7588884 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: 1.5.2
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *7588884
48
+ - !ruby/object:Gem::Dependency
49
+ name: rcov
50
+ requirement: &7588152 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *7588152
59
+ description: Theta was created as a learning project based off of lis.py and flea
60
+ email: ctoneal@gmail.com
61
+ executables:
62
+ - theta
63
+ - theta
64
+ extensions: []
65
+ extra_rdoc_files:
66
+ - LICENSE.txt
67
+ - README.rdoc
68
+ files:
69
+ - .document
70
+ - Gemfile
71
+ - Gemfile.lock
72
+ - LICENSE.txt
73
+ - README.rdoc
74
+ - Rakefile
75
+ - VERSION
76
+ - bin/theta
77
+ - lib/theta.rb
78
+ - lib/theta/environment.rb
79
+ - lib/theta/interpreter.rb
80
+ - lib/theta/library/add.scm
81
+ - lib/theta/library/divide.scm
82
+ - lib/theta/library/multiply.scm
83
+ - lib/theta/library/subtract.scm
84
+ - lib/theta/parser.rb
85
+ - test/helper.rb
86
+ - theta.gemspec
87
+ has_rdoc: true
88
+ homepage: http://github.com/ctoneal/theta
89
+ licenses:
90
+ - MIT
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ segments:
102
+ - 0
103
+ hash: -350340689
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 1.6.2
113
+ signing_key:
114
+ specification_version: 3
115
+ summary: Theta is a Lisp interpreter in Ruby
116
+ test_files:
117
+ - test/helper.rb