File: miku.rb

package info (click to toggle)
mikutter 5.1.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,780 kB
  • sloc: ruby: 22,912; sh: 186; makefile: 21
file content (47 lines) | stat: -rwxr-xr-x 1,203 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#! /usr/bin/ruby
# -*- coding: utf-8 -*-

if not defined? $loaded_miku
  $loaded_miku = true

  Dir.chdir(__dir__){
    require_relative 'array'
    require_relative 'hash'
    require_relative 'symbol'
    require_relative 'symboltable'
    require_relative 'nil'
    require_relative 'parser'
  }

  def miku(node, scope=MIKU::SymbolTable.new)
    if(node.is_a? MIKU::Node) then
      begin
        node.miku_eval(scope)
      rescue MIKU::MikuException => e
        warn e
      rescue Exception => e
        warn "[MIKU Bug] fatal error on code #{node.inspect}"
        raise e
      end
    else
      node end end

  def miku_stream(stream, scope)
    begin
      while(not stream.eof?) do
        miku(MIKU.parse(stream), scope) end
    rescue MIKU::EndofFile
    rescue MIKU::MikuException => e
      warn e end end

  if(__FILE__ == $0) then
    scope = MIKU::SymbolTable.new.run_init_script
    if ARGV.last
      miku_stream(open(ARGV.last, 'r'), scope)
    else
      require 'readline'
      while buf = Readline.readline('>>> ', true)
        begin
          puts MIKU.unparse(miku(MIKU.parse(buf), scope))
        rescue MIKU::MikuException => e
          puts e.to_s end end end end end