Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Basic Typhon REPL.

  • Loading branch information...
commit 2fa28149fb34542eccbad795e5f7c87fe8859371 1 parent 29cf493
@vic authored
View
3  lib/typhon/code_loader.rb
@@ -18,8 +18,7 @@ def self.pythonize_literals(cm)
cm
end
- def self.execute_code(code, binding, from_module)
- print = Compiler::Print.new
+ def self.execute_code(code, binding, from_module, print = Compiler::Print.new)
cm = pythonize_literals(Compiler.compile_for_eval(code, binding.variables,
"(eval)", 1, print))
cm.scope = binding.static_scope.dup
View
27 lib/typhon/main.rb
@@ -1,3 +1,5 @@
+require 'typhon/version'
+
module Typhon
# Command line interface to Typhon.
@@ -19,9 +21,9 @@ def initialize
def main(argv=ARGV)
options(argv)
+ return repl if @rest.empty? && @evals.empty? && !@compile_only
evals unless @evals.empty?
- script unless @compile_only || @rest.empty?
- repl if !@compile_only && @rest.empty?
+ script unless @rest.empty?
compile if @compile_only
end
@@ -38,7 +40,13 @@ def compile
# Evaluate code given on command line
def evals
- raise "Eval not implemented yet."
+ bnd = Object.new
+ def bnd.get; binding; end
+ bnd = bnd.get
+ mod = Environment::PythonModule.new(nil, :__eval__, "Eval expression", "(eval)")
+ @evals.each do |code|
+ CodeLoader.execute_code code, bnd, mod, @print
+ end
end
# Run the given script if any
@@ -48,7 +56,8 @@ def script
# Run the Typhon REPL unless we were given an script
def repl
- raise "REPL not implemented yet"
+ require 'typhon/repl'
+ ReadEvalPrintLoop.new.main(@print)
end
# Parse command line options
@@ -88,6 +97,16 @@ def options(argv)
@evals << e
end
+ options.on "-V", "Print Python version and exit" do
+ puts VERSION.python_string
+ exit 0
+ end
+
+ options.on "--version", "Print version and exit" do
+ puts VERSION.full_string
+ exit 0
+ end
+
options.on "-h", "--help", "Display this help" do
puts options
exit 0
View
45 lib/typhon/repl.rb
@@ -0,0 +1,45 @@
+require 'readline'
+
+module Typhon
+ class ReadEvalPrintLoop
+ def prompt(lineno = 1, level = 1, print_line = false)
+ line = ("%03i " % [line] if print_line).to_s
+ prompt = if level > 1; "..."; else ">>>"; end
+ space = " " * level
+ [line, prompt, space].join
+ end
+
+ def header
+ puts VERSION.full_string
+ puts 'Type "help", "copyright", "credits" or "license" for more information.'
+ end
+
+ def main(print = Compiler::Print.new)
+ trap("INT") { exit 0 }
+
+ bnd = Object.new
+ def bnd.bnd; binding; end
+ bnd = bnd.bnd
+ mod = Environment::PythonModule.new(nil, :__repl__, "REPL", "(repl)")
+
+ lineno = 1
+ level = 1
+ header
+ while line = Readline.readline(prompt(lineno, level),true)
+ Readline::HISTORY.pop() if double_or_empty?(line)
+
+ begin
+ p CodeLoader.execute_code line, bnd, mod, print
+ rescue => e
+ puts e.message
+ end
+
+ lineno += 1
+ end
+ end
+
+ def double_or_empty?(line)
+ line =~ /^\s*$/ || Readline::HISTORY.to_a[-2] == line
+ end
+ end
+end
View
75 lib/typhon/version.rb
@@ -1,10 +1,73 @@
+require 'rbconfig'
+
module Typhon
- class VERSION < Struct.new(:major, :minor, :patch, :commit)
- def to_s
- [major, minor, patch].join(".")
- end
- end
- VERSION = VERSION.new(0, 0, 1)
+ module VERSION
+ extend self
+
+ attr_accessor :major, :minor, :tiny, :commit, :codename, :tagline
+
+ self.codename = "Snakes on rbx-head"
+ self.tagline = "Snakes on rbx-head"
+
+ self.major = 0
+ self.minor = 0
+ self.tiny = 1
+
+ def commit
+ @commit ||= `git rev-parse HEAD`[0..7]
+ end
+
+ def to_s
+ [major, minor, tiny].join(".")
+ end
+
+ def to_str
+ to_s
+ end
+
+ def full_string(sep = "\n")
+ [typhon_string, rbx_string].join(sep)
+ end
+
+ def typhon_string
+ "Typhon #{to_s} (#{commit}) \"#{codename}\""
+ end
+
+ def python_string
+ `python -V 2>&1`.chomp
+ end
+
+ # Returns a partial Ruby version string based on +which+. For example,
+ # if RUBY_VERSION = 8.2.3 and RUBY_PATCHLEVEL = 71:
+ #
+ # :major => "8"
+ # :minor => "8.2"
+ # :tiny => "8.2.3"
+ # :teeny => "8.2.3"
+ # :full => "8.2.3.71"
+ def self.ruby_version(which = :minor)
+ case which
+ when :major
+ n = 1
+ when :minor
+ n = 2
+ when :tiny, :teeny
+ n = 3
+ else
+ n = 4
+ end
+
+ patch = RUBY_PATCHLEVEL.to_i
+ patch = 0 if patch < 0
+ version = "#{RUBY_VERSION}.#{patch}"
+ version.split('.')[0,n].join('.')
+ end
+
+ def rbx_string
+ "Rubinius #{Rubinius::VERSION} (#{ruby_version(:tiny)} "+
+ "#{Rubinius::BUILD_REV[0..7]} #{Rubinius::RELEASE_DATE})"
+ end
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.