Module: Nydp

Extended by:
Converter
Defined in:
lib/nydp/readline_history.rb,
lib/nydp.rb,
lib/nydp/cond.rb,
lib/nydp/core.rb,
lib/nydp/date.rb,
lib/nydp/loop.rb,
lib/nydp/error.rb,
lib/nydp/truth.rb,
lib/nydp/helper.rb,
lib/nydp/parser.rb,
lib/nydp/plugin.rb,
lib/nydp/runner.rb,
lib/nydp/closure.rb,
lib/nydp/literal.rb,
lib/nydp/version.rb,
lib/nydp/compiler.rb,
lib/nydp/namespace.rb,
lib/nydp/tokeniser.rb,
lib/nydp/assignment.rb,
lib/nydp/string_atom.rb,
lib/nydp/builtin/sort.rb,
lib/nydp/string_token.rb,
lib/nydp/symbol_lookup.rb,
lib/nydp/context_symbol.rb,
lib/nydp/lexical_context.rb,
lib/nydp/function_invocation.rb,
lib/nydp/interpreted_function.rb

Overview

Defined Under Namespace

Modules: AutoWrap, Builtin, Converter, Helper, Invocation, LexicalContextBuilder, PluginHelper, ReadlineHistory Classes: Assignment, Closure, Compiler, Cond, CondBase, Cond_LEX, Cond_LEX_CND_LIT, Cond_LEX_LEX_LIT, Cond_LEX_LIT_LIT, Cond_LEX_NVB_LEX, Cond_LEX_NVB_LIT, Cond_SYM, ContextSymbol, Core, Date, Error, Evaluator, FileReader, Fn, FunctionInvocation, InterpretedFunction, InterpretedFunctionWithClosure, InterpretedFunctionWithoutClosure, InvocationFailed, LexicalContext, Literal, Loop, Namespace, OR_LEX_LEX, OR_LEX_LIT, OR_LEX_XXX, Pair, Parser, ReadlineReader, Runner, StreamReader, StringAtom, StringFragmentCloseToken, StringFragmentToken, StringReader, Struct, Symbol, SymbolLookup, Tokeniser

Constant Summary collapse

GENERATED_CLASS_PREFIX =
"NydpGenerated"
NIL =
nil
T =
true
PLUGINS =
[]
COMMENT_RX =
/^.*##> /
GENERATED_CLASS_PREFIX_REGEXP =
/#{Nydp::GENERATED_CLASS_PREFIX}/
VERSION =
"0.6.0"

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Converter

n2r, r2n, rubify

Class Attribute Details

.loggerObject

set this if you plan on using ‘log



8
9
10
# File 'lib/nydp.rb', line 8

def logger
  @logger
end

Class Method Details

.all_filesObject



65
# File 'lib/nydp/plugin.rb', line 65

def self.all_files ; PLUGINS.each_with_object([]) { |plg, list|  plg.loadfiles.each { |f| list << f } } ; end

.apply_function(ns, name, *args) ⇒ Object



11
# File 'lib/nydp.rb', line 11

def self.apply_function      ns, name, *args ; ns.apply name, *args                                 ; end

.base_gen_pathObject



49
# File 'lib/nydp/plugin.rb', line 49

def self.base_gen_path   ; File.expand_path("rubycode/")       ; end

.build_nydp(&block) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/nydp/plugin.rb', line 67

def self.build_nydp &block
  rc = base_gen_path
  $LOAD_PATH.unshift rc unless $LOAD_PATH.include?(rc)

  digest   = Digest::SHA256.hexdigest(all_files.map { |f| f.read }.join("\n"))
  mname    = "Manifest_#{digest}"

  ns = ::Nydp::Namespace.new
  setup(ns)

  digest = ""

  PLUGINS.each { |plugin|
    digest = install_plugin ns, plugin, digest, &block
  }

  ns
end

.enhance_backtrace(bt) ⇒ Object



45
46
47
# File 'lib/nydp/plugin.rb', line 45

def self.enhance_backtrace bt
  bt.map { |s| nydp_from_backtrace s }
end

.eval_src(ns, src_txt, name = nil) ⇒ Object



13
# File 'lib/nydp.rb', line 13

def self.eval_src      ns, src_txt, name=nil ; eval_with Nydp::Runner, ns, src_txt, name            ; end

.eval_with(runner, ns, src_txt, name) ⇒ Object



14
# File 'lib/nydp.rb', line 14

def self.eval_with runner, ns, src_txt, name ; runner.new(ns, reader(name, src_txt), nil, name).run ; end

.handle_run_error(e, indent = "") ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/nydp.rb', line 23

def self.handle_run_error e, indent=""
  puts "#{indent}#{e.class.name}"
  puts "#{indent_message indent, e.message}"
  if e.cause
    puts "#{indent}#{e.backtrace.first}"
    puts "\n#{indent}Caused by:"
    handle_run_error e.cause, "#{indent}    "
  else
    Nydp.enhance_backtrace(e.backtrace).each do |b|
      puts "#{indent}#{b}"
    end
  end
end

.indent_message(indent, msg) ⇒ Object



19
20
21
# File 'lib/nydp.rb', line 19

def self.indent_message indent, msg
  msg.split(/\n/).map { |line| "#{indent}#{line}" }.join("\n")
end

.indent_text(txt) ⇒ Object



2
3
4
# File 'lib/nydp/error.rb', line 2

def self.indent_text txt
  txt.split(/\n/).map { |line| "  #{line}"}.join("\n")
end

.install_plugin(ns, plugin, digest, &block) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/nydp/plugin.rb', line 86

def self.install_plugin ns, plugin, digest, &block
  f0       = plugin.loadfiles.map { |f| f.read }.join("\n")
  f1       = plugin.testfiles.map { |f| f.read }.join("\n")
  digest   = Digest::SHA256.hexdigest([digest, f0, f1].join("\n"))
  mname    = "Manifest_#{digest}"

  if Nydp.logger
    Nydp.logger.info "manifest name for plugin #{plugin.name.inspect} is #{mname.inspect}"
  end

  begin
    require mname
    const_get(mname).build ns

  rescue LoadError => e
    manifest = []
    loadall ns, plugin, plugin.loadfiles, manifest, &block
    loadall ns, plugin, plugin.testfiles, manifest, &block
    Nydp::Evaluator.mk_manifest "Manifest_#{digest}", manifest
  end

  digest
end

.load_rake_tasksObject



51
# File 'lib/nydp/plugin.rb', line 51

def self.load_rake_tasks ; PLUGINS.each &:load_rake_tasks      ; end

.loadall(ns, plugin, files, manifest) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/nydp/plugin.rb', line 55

def self.loadall ns, plugin, files, manifest
  ns.apply :"script-run", :"plugin-start", plugin.name if plugin
  files.each { |f|
    Nydp::Runner.new(ns, f, nil, f.name, manifest).run
    yield f.name if block_given?
  }
ensure
  ns.apply :"script-run", :"plugin-end", plugin.name if plugin
end

.ms(t1, t0) ⇒ Object



15
# File 'lib/nydp.rb', line 15

def self.ms                           t1, t0 ; ((t1 - t0) * 1000).to_i                              ; end

.new_parserObject



17
# File 'lib/nydp.rb', line 17

def self.new_parser                          ; Nydp::Parser.new                                     ; end

.new_tokeniser(reader) ⇒ Object



16
# File 'lib/nydp.rb', line 16

def self.new_tokeniser                reader ; Nydp::Tokeniser.new reader                           ; end

.nydp_from_backtrace(str) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/nydp/plugin.rb', line 21

def self.nydp_from_backtrace str
  file, original_line, meth = str.split(/:/)
  line = original_line.to_i - 2 # -1 to convert from 1-based index to zero-based index ; -1 to start looking backwards from previous line
  filepath = File.expand_path file

  if filepath =~ Nydp::GENERATED_CLASS_PREFIX_REGEXP
    code = File.read filepath
    lines = code.split /\n/

    while line > 0 && !(lines[line] =~ COMMENT_RX)
      line = line - 1
    end

    if (line >= 0 && (lines[line] =~ COMMENT_RX))
      comment = lines[line].sub(COMMENT_RX, '').gsub(/\\n/, "\n")
      return [(filepath.sub(base_gen_path + '/', '') + ":" + original_line), comment].join("\n")
    else
      return str
    end
  end

  str
end

.plug_in(plugin) ⇒ Object



50
# File 'lib/nydp/plugin.rb', line 50

def self.plug_in  plugin ; PLUGINS << plugin                   ; end

.plugin_namesObject



53
# File 'lib/nydp/plugin.rb', line 53

def self.plugin_names    ; PLUGINS.map(&:name)                 ; end

.reader(name, txt) ⇒ Object



12
# File 'lib/nydp.rb', line 12

def self.reader                    name, txt ; Nydp::StringReader.new name, txt                     ; end

.repl(options = { }) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/nydp.rb', line 45

def self.repl options={ }
  launch_time      = Time.now
  silent           = options.delete :silent
  ns               = options.delete :ns
  last_script_time = Time.now
  puts "welcome to nydp #{options.inspect}" unless silent
  reader = Nydp::ReadlineReader.new $stdin, "nydp > "
  ns   ||= build_nydp do |script|
    this_script_time = Time.now
    puts "script #{script} time #{ms this_script_time, last_script_time}ms" if options[:verbose]
    last_script_time = this_script_time
  end
  load_time = Time.now
  puts "nydp v#{Nydp::VERSION} repl ready in #{ms(load_time, launch_time)}ms" unless silent
  puts "^D to exit" unless silent
  while !options[:exit]
    toplevel do
      Nydp::Runner.new(ns, reader, $stdout, "<stdin>").run
      options[:exit] = true
    end
  end
  # Nydp::Invocation.whazzup
end

.setup(ns) ⇒ Object



52
# File 'lib/nydp/plugin.rb', line 52

def self.setup        ns ; PLUGINS.each { |plg| plg.setup ns } ; end

.tests(*options) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/nydp.rb', line 69

def self.tests *options
  toplevel do
    verbose = options.include?(:verbose) ? true : nil
    puts "welcome to nydp : running tests"
    build_nydp.apply :"run-all-tests", verbose
  end
end

.toplevelObject



37
38
39
40
41
42
43
# File 'lib/nydp.rb', line 37

def self.toplevel
  begin
    yield
  rescue StandardError => e
    handle_run_error e
  end
end