diff options
Diffstat (limited to 'sample')
-rw-r--r-- | sample/biorhythm.rb | 4 | ||||
-rw-r--r-- | sample/cat2.rb | 4 | ||||
-rw-r--r-- | sample/getopts.rb | 2 | ||||
-rw-r--r-- | sample/io.rb | 2 | ||||
-rw-r--r-- | sample/newver.rb | 13 | ||||
-rw-r--r-- | sample/rcs.rb | 10 | ||||
-rw-r--r-- | sample/ruby-mode.el | 139 | ||||
-rw-r--r-- | sample/sieve.rb | 16 | ||||
-rw-r--r-- | sample/t2.rb | 7 | ||||
-rw-r--r-- | sample/trojan.rb | 12 | ||||
-rw-r--r-- | sample/tt.rb | 19 |
11 files changed, 151 insertions, 77 deletions
diff --git a/sample/biorhythm.rb b/sample/biorhythm.rb index 3261c4377f..eb14ca7365 100644 --- a/sample/biorhythm.rb +++ b/sample/biorhythm.rb @@ -41,7 +41,7 @@ def leapyear(y) return ta end -def bcalc(t, m, j) +def bcalc(tt, m, j) ta = 0 if (m <= 2) ta = (m - 1) * 31 @@ -51,7 +51,7 @@ def bcalc(t, m, j) end ta = ta + (j - 1) * 365 + ((j - 1) / 4.0).to_i ta = ta - ((j - 1) / 100) + ((j - 1) / 400.0).to_i - ta = ta + t + ta = ta + tt return ta end diff --git a/sample/cat2.rb b/sample/cat2.rb new file mode 100644 index 0000000000..f979dc53cb --- /dev/null +++ b/sample/cat2.rb @@ -0,0 +1,4 @@ +while gets() + if 1 ... /^\*/; print("--") end + printf("%5d: %s", $., $_) +end diff --git a/sample/getopts.rb b/sample/getopts.rb index 57a7db424f..01eddcea98 100644 --- a/sample/getopts.rb +++ b/sample/getopts.rb @@ -55,7 +55,7 @@ def getopts(single_opts, *opts) while ($ARGV.length != 0) compare = nil case $ARGV[0] - when /^-*$/ + when /^--?$/ $ARGV.shift break when /^--.*/ diff --git a/sample/io.rb b/sample/io.rb index 45d50d653a..b5591af7e4 100644 --- a/sample/io.rb +++ b/sample/io.rb @@ -13,7 +13,7 @@ end printf("%s:(%d)%s\n", $0, $ARGV.length, $ARGV[0]) passwd = open($ARGV[0], "r") -#printf("%s", do passwd.find using i; i =~ /\*/ end) +#printf("%s", passwd.find{i|i =~ /\*/}) n = 1 for i in passwd #.grep(/^\*/) diff --git a/sample/newver.rb b/sample/newver.rb new file mode 100644 index 0000000000..bbf03aebc2 --- /dev/null +++ b/sample/newver.rb @@ -0,0 +1,13 @@ +#! /usr/local/bin/ruby + +f = open("version.h", "r") +f.gets() +f.close + +if $_ =~ /"(\d)\.(\d+)"/; + f = open("version.h", "w") + i = $2.to_i + 1 + printf("ruby version %d.%0d\n", $1, i) + printf(f, "#define RUBY_VERSION \"%d.%0d\"\n", $1, i) + f.close +end diff --git a/sample/rcs.rb b/sample/rcs.rb index 6d7f10c6bc..0ed4a36c1e 100644 --- a/sample/rcs.rb +++ b/sample/rcs.rb @@ -14,12 +14,16 @@ while gets() while xr < hdw x = xr * (1 + y) - y * w / 2 i = (x / (1 + h) + sw /2) - c = if (1 < i && i < $_.length); $_[i, 1].to_i else 0 end + if (1 < i && i < $_.length); + c = $_[i, 1].to_i + else + c = 0 + end y = h - d * c xl = xr - w * y / (1 + y); if xl < -hdw || xl >= hdw || xl <= maxxl - t = rand(ss.length) - c = ss[t, 1] + tt = rand(ss.length) + c = ss[tt, 1] else c = s[xl + hdw, 1] maxxl = xl diff --git a/sample/ruby-mode.el b/sample/ruby-mode.el index 96915bdc5a..8f864d3846 100644 --- a/sample/ruby-mode.el +++ b/sample/ruby-mode.el @@ -8,22 +8,23 @@ ;;; (defconst ruby-block-beg-re - "class\\|module\\|def\\|if\\|case\\|while\\|do\\|for\\|protect" + "class\\|module\\|def\\|if\\|unless\\|case\\|while\\|until\\|for\\|protect" ) (defconst ruby-block-mid-re - "else\\|elsif\\|when\\|using\\|resque\\|ensure" + "else\\|elsif\\|when\\|resque\\|ensure" ) -(defconst ruby-block-end-re - (concat "\\(end\\([ \t]+\\(" ruby-block-beg-re "\\)\\)?\\)") - ) +(defconst ruby-block-end-re "end") (defconst ruby-delimiter - (concat "(\\|)\\|\\{\\|\\}\\|\"\\|\'\\|\\b\\(" ruby-block-beg-re "\\|" ruby-block-end-re "\\)\\b\\|#") + (concat "[/<(){}#\"'`]\\|\\[\\|\\]\\|\\b\\(" + ruby-block-beg-re "\\|" ruby-block-end-re "\\)\\b") ) + (defconst ruby-negative - (concat "^[ \t]*\\b\\(\\(" ruby-block-mid-re "\\)\\|\\(" ruby-block-end-re "\\)\\)\\b") + (concat "^[ \t]*\\(\\b\\(" ruby-block-mid-re "\\)\\|\\(" + ruby-block-end-re "\\)\\b\\|\\}\\|\\]\\)") ) (defvar ruby-mode-abbrev-table nil @@ -51,9 +52,10 @@ (setq ruby-mode-syntax-table (make-syntax-table)) (modify-syntax-entry ?\' "\"" ruby-mode-syntax-table) (modify-syntax-entry ?\" "\"" ruby-mode-syntax-table) - (modify-syntax-entry ?\n "> " ruby-mode-syntax-table) - (modify-syntax-entry ?\f "> " ruby-mode-syntax-table) - (modify-syntax-entry ?# "< " ruby-mode-syntax-table) +;;(modify-syntax-entry ?\n ">" ruby-mode-syntax-table) +;;(modify-syntax-entry ?\f ">" ruby-mode-syntax-table) + (modify-syntax-entry ?# "<" ruby-mode-syntax-table) + (modify-syntax-entry ?\\ "'" ruby-mode-syntax-table) (modify-syntax-entry ?_ "w" ruby-mode-syntax-table) (modify-syntax-entry ?< "." ruby-mode-syntax-table) (modify-syntax-entry ?> "." ruby-mode-syntax-table) @@ -68,9 +70,9 @@ (modify-syntax-entry ?- "." ruby-mode-syntax-table) (modify-syntax-entry ?\; "." ruby-mode-syntax-table) (modify-syntax-entry ?\( "()" ruby-mode-syntax-table) - (modify-syntax-entry ?) ")(" ruby-mode-syntax-table) - (modify-syntax-entry ?{ "(}" ruby-mode-syntax-table) - (modify-syntax-entry ?} "){" ruby-mode-syntax-table) + (modify-syntax-entry ?\) ")(" ruby-mode-syntax-table) + (modify-syntax-entry ?\{ "(}" ruby-mode-syntax-table) + (modify-syntax-entry ?\} "){" ruby-mode-syntax-table) (modify-syntax-entry ?\[ "(]" ruby-mode-syntax-table) (modify-syntax-entry ?\] ")[" ruby-mode-syntax-table) ) @@ -78,23 +80,6 @@ (defvar ruby-indent-level 2 "*Indentation of ruby statements.") -(defun ruby-mode-variables () - (setq local-abbrev-table ruby-mode-abbrev-table) - (make-local-variable 'indent-line-function) - (setq indent-line-function 'ruby-indent-line) - (make-local-variable 'require-final-newline) - (setq require-final-newline t) - (make-variable-buffer-local 'comment-start) - (setq comment-start "# ") - (make-variable-buffer-local 'comment-end) - (setq comment-end "") - (make-variable-buffer-local 'comment-column) - (setq comment-column 32) - (make-variable-buffer-local 'comment-start-skip) - (setq comment-start-skip "#+ *") - (make-local-variable 'parse-sexp-ignore-comments) - (setq parse-sexp-ignore-comments t)) - (defun ruby-mode () "Major mode for editing ruby scripts. \\[ruby-indent-command] properly indents subexpressions of multi-line @@ -109,7 +94,21 @@ The variable ruby-indent-level controls the amount of indentation. (setq mode-name "ruby") (setq major-mode 'ruby-mode) (set-syntax-table ruby-mode-syntax-table) - (ruby-mode-variables) + (setq local-abbrev-table ruby-mode-abbrev-table) + (make-local-variable 'indent-line-function) + (setq indent-line-function 'ruby-indent-line) + (make-local-variable 'require-final-newline) + (setq require-final-newline t) + (make-variable-buffer-local 'comment-start) + (setq comment-start "# ") + (make-variable-buffer-local 'comment-end) + (setq comment-end "") + (make-variable-buffer-local 'comment-column) + (setq comment-column 32) + (make-variable-buffer-local 'comment-start-skip) + (setq comment-start-skip "#+ *") + (make-local-variable 'parse-sexp-ignore-comments) + (setq parse-sexp-ignore-comments t) (run-hooks 'ruby-mode-hook)) (defun ruby-current-indentation () @@ -131,10 +130,8 @@ The variable ruby-indent-level controls the amount of indentation. (defun ruby-indent-line (&optional flag) "Correct indentation of the current ruby line." - (let - ((x (ruby-calculate-indent))) - (ruby-indent-to x))) - + (ruby-indent-to (ruby-calculate-indent))) + (defun ruby-indent-command () (interactive) (ruby-indent-line t)) @@ -166,14 +163,36 @@ The variable ruby-indent-level controls the amount of indentation. (ruby-beginning-of-defun)) (while (and (> indent-point (point)) (re-search-forward ruby-delimiter indent-point t)) - (let ((w (buffer-substring (match-beginning 0) (match-end 0)))) + (let ((w (buffer-substring (match-beginning 0) (match-end 0))) + (pnt (match-beginning 0))) (cond ((or (string= "\"" w) ;skip string - (string= "\'" w)) + (string= "'" w) + (string= "`" w)) (if (search-forward w indent-point t) nil (goto-char indent-point) (setq in-string t))) + ((or (string= "/" w) + (string= "<" w)) + (if (string= "<" w) (setq w ">")) + (let (c) + (save-excursion + (goto-char pnt) + (skip-chars-backward " \t") + (setq c (char-after (1- (point)))) + (if c + (setq c (char-syntax c)))) + (cond + ((or (eq c ?.) + (and (eq c ?w) + (save-excursion + (forward-word -1) + (looking-at ruby-block-beg-re)))) + (if (search-forward w indent-point t) + nil + (goto-char indent-point) + (setq in-string t)))))) ((string= "#" w) ;skip comment (forward-line 1)) ((string= "(" w) ;skip to matching paren @@ -189,7 +208,21 @@ The variable ruby-indent-level controls the amount of indentation. (t (setq nest (cdr nest)) (setq depth (1- depth))))) - (if (> depth orig) (setq in-paren t)))) + (if (> depth orig) (setq in-paren ?\()))) + ((string= "[" w) ;skip to matching paren + (let ((orig depth)) + (setq nest (cons (point) nest)) + (setq depth (1+ depth)) + (while (and (/= depth orig) + (re-search-forward "\\[\\|\\]" indent-point t)) + (cond + ((= (char-after (match-beginning 0)) ?\[ ) + (setq nest (cons (point) nest)) + (setq depth (1+ depth))) + (t + (setq nest (cdr nest)) + (setq depth (1- depth))))) + (if (> depth orig) (setq in-paren ?\[)))) ((string= "{" w) ;skip to matching paren (let ((orig depth)) (setq nest (cons (point) nest)) @@ -197,22 +230,28 @@ The variable ruby-indent-level controls the amount of indentation. (while (and (/= depth orig) (re-search-forward "[{}]" indent-point t)) (cond - ((= (char-after (match-beginning 0)) ?\{ ) + ((= (char-after (match-beginning 0)) ?{ ) (setq nest (cons (point) nest)) (setq depth (1+ depth))) (t (setq nest (cdr nest)) (setq depth (1- depth))))) - (if (> depth orig) (setq in-paren t)))) - ((string-match "^end" w) + (if (> depth orig) (setq in-paren ?{)))) + ((string-match ruby-block-end-re w) (setq nest (cdr nest)) (setq depth (1- depth))) ((string-match ruby-block-beg-re w) - (setq nest (cons (point) nest)) - (setq depth (1+ depth))) + (let (c) + (save-excursion + (goto-char pnt) + (skip-chars-backward " \t") + (setq c (char-after (1- (point))))) + (if (or (null c) (= c ?\n) (= c ?\;)) + (progn + (setq nest (cons (point) nest)) + (setq depth (1+ depth)))))) (t (error (format "bad string %s" w))))))) - (if in-paren (message "in-paren")) (list in-string in-paren (car nest) depth))) (defun ruby-calculate-indent (&optional parse-start) @@ -224,7 +263,7 @@ The variable ruby-indent-level controls the amount of indentation. (indent 0)) (if parse-start (goto-char parse-start) - (beginning-of-defun) + (ruby-beginning-of-defun) (setq parse-start (point))) (setq state (ruby-parse-region parse-start indent-point)) (cond @@ -234,9 +273,9 @@ The variable ruby-indent-level controls the amount of indentation. ((nth 1 state) ; in paren (goto-char (nth 2 state)) (setq indent - (if (looking-at "$") - (+ (current-indentation) ruby-indent-level) - (current-column)))) + (if (and (eq (nth 1 state) ?\( ) (not (looking-at "$"))) + (current-column) + (+ (current-indentation) ruby-indent-level)))) ((> (nth 3 state) 0) ; in nest (goto-char (nth 2 state)) @@ -258,7 +297,7 @@ The variable ruby-indent-level controls the amount of indentation. With argument, do this that many times. Returns t unless search stops due to end of buffer." (interactive "p") - (and (re-search-backward (concat "^\\(" ruby-block-beg-re "\\)") + (and (re-search-backward (concat "^\\(" ruby-block-beg-re "\\)\\b") nil 'move (or arg 1)) (progn (beginning-of-line) t))) @@ -266,7 +305,7 @@ Returns t unless search stops due to end of buffer." "Move forward to next end of defun. An end of a defun is found by moving forward from the beginning of one." (interactive "p") - (and (re-search-forward (concat "^\\(" ruby-block-end-re "\\)") + (and (re-search-forward (concat "^\\(" ruby-block-end-re "\\)\\b") nil 'move (or arg 1)) (progn (beginning-of-line) t)) (forward-line 1)) diff --git a/sample/sieve.rb b/sample/sieve.rb new file mode 100644 index 0000000000..0228243fda --- /dev/null +++ b/sample/sieve.rb @@ -0,0 +1,16 @@ +sieve = [] +unless max = $ARGV.shift; max = 100; end +max = max.to_i + +print "1" +for i in 2 .. max + protect + for d in sieve + fail if i % d == 0 + end + print ", " + print i + sieve.push(i) + resque + end +end diff --git a/sample/t2.rb b/sample/t2.rb index 7f5b9df480..a34c171837 100644 --- a/sample/t2.rb +++ b/sample/t2.rb @@ -7,7 +7,7 @@ def println(*args) print(a) end print("\n") -end def +end def tt for i in 1..10 @@ -16,9 +16,8 @@ def tt end end -test = -do tt() using i +test = tt{i| if i == 3; break end println("ttt: ", i); -end +} #exit() diff --git a/sample/trojan.rb b/sample/trojan.rb index bd49d44357..b42fd166a8 100644 --- a/sample/trojan.rb +++ b/sample/trojan.rb @@ -2,11 +2,13 @@ path = $ENV['PATH'].split(/:/) for dir in path - for f in d = Dir.open(dir) - fpath = dir+"/"+f - if File.f(fpath) && (File.stat(fpath).mode & 022) != 0 - printf("file %s is writable from other users\n", fpath) + if File.d(dir) + for f in d = Dir.open(dir) + fpath = dir+"/"+f + if File.f(fpath) && (File.stat(fpath).mode & 022) != 0 + printf("file %s is writable from other users\n", fpath) + end end + d.close end - d.close end diff --git a/sample/tt.rb b/sample/tt.rb index cb863e3527..c53ec39d68 100644 --- a/sample/tt.rb +++ b/sample/tt.rb @@ -5,13 +5,13 @@ module Print print(a) end print("\n") - end def + end def println2(*args) print(*args) print("\n") - end def -end module + end +end module Print2 def println(*args) @@ -52,20 +52,20 @@ class Fib:Object if args; println(*args) end args = args.grep(/^c/) super(*args) - end def + end def init println("in Fib.init"); - end def + end def fib(n) a =0; b = 1 while b <= n c = a; a = b; b = c+b - end while + end return b - end def + end end def Object.test(*args) @@ -96,8 +96,5 @@ def tt end end -test = do tt() using i - if i == 2; break end -end - +test = tt() {i|break if i == 2} println([1,2,3,4].join(":")) |