Permalink
Browse files

CLI.

  • Loading branch information...
1 parent 74f3055 commit 7cd249768db2f7a7eabc311147a8f5ab487145e7 Brian Ford committed Nov 21, 2010
Showing with 89 additions and 0 deletions.
  1. +89 −0 bin/syme
View
@@ -0,0 +1,89 @@
+#!/usr/bin/env rbx
+#
+# vim: filetype=ruby
+
+$:.unshift File.expand_path('../../lib', __FILE__)
+
+require 'syme'
+
+class SymeScript
+ def initialize
+ @evals = []
+ @ast = false
+ @sexp = false
+ @script = nil
+ end
+
+ def options(argv=ARGV)
+ options = Rubinius::Options.new "Usage: syme [options] [script]", 20
+
+ options.on "-", "Read and evaluate code from STDIN" do
+ @evals << STDIN.read
+ end
+
+ options.on "-c", "FILE", "Check the syntax of FILE" do |f|
+ begin
+ begin
+ Syme::Parser.new.parse_file f
+ rescue Syme::Syntax::SyntaxError => e
+ e.render
+ exit 1
+ end
+
+ puts "Syntax OK"
+ exit 0
+ end
+ end
+
+ options.on "-A", "Print the AST" do
+ @ast = true
+ end
+
+ options.on "-S", "Print the S-expression" do
+ @sexp = true
+ end
+
+ options.on "-e", "CODE", "Execute CODE" do |e|
+ @evals << e
+ end
+
+ options.on "-h", "--help", "Display this help" do
+ puts options
+ exit 0
+ end
+
+ options.doc ""
+
+ rest = options.parse(argv)
+ @script ||= rest.first
+ end
+
+ def evals
+ return if @evals.empty?
+ end
+
+ def script
+ return unless @script
+
+ if File.exists? @script
+
+ Syme::CodeLoader.execute_file @script
+ else
+ STDERR.puts "Unable to find '#{@script}' to run"
+ exit 1
+ end
+ end
+
+ def repl
+ return if @script
+ end
+
+ def main
+ options
+ evals
+ script
+ repl
+ end
+end
+
+SymeScript.new.main

0 comments on commit 7cd2497

Please sign in to comment.