nendo 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
data/bin/nendo CHANGED
@@ -50,6 +50,8 @@ def userOptionEater
50
50
  i += 1
51
51
  when '-l'
52
52
  i += 2
53
+ when '-O'
54
+ i += 2
53
55
  when /^[^-]/
54
56
  break
55
57
  else
@@ -63,32 +65,19 @@ def userOptionEater
63
65
  return userOptions
64
66
  end
65
67
 
66
- def cache_exist?( fn )
67
- dotdir_path = File.expand_path( "~/.nendo" )
68
- cache_path = File.expand_path( "~/.nendo/cache" )
69
- Dir.mkdir( dotdir_path ) unless File.exist?( dotdir_path )
70
- Dir.mkdir( cache_path ) unless File.exist?( cache_path )
71
- sha1 = Digest::SHA1.hexdigest( File.expand_path( fn ))
72
- cache_name = cache_path + "/" + sha1 + "_" + File.basename( fn ) + ".rb"
73
- if File.exist?( cache_name )
74
- [ true, cache_name ]
75
- else
76
- [ false, cache_name ]
77
- end
78
- end
79
-
80
68
  def main
81
69
  loadInit = true
82
70
  compile = false
83
71
  conflict = 0
84
- core = Nendo.new()
72
+ core = Nendo::Core.new()
85
73
 
86
74
  userOptions = userOptionEater
87
75
  opts = GetoptLong.new(
88
76
  [ '-h', GetoptLong::NO_ARGUMENT ],
89
77
  [ '-q', GetoptLong::NO_ARGUMENT ],
90
78
  [ '-c', GetoptLong::NO_ARGUMENT ],
91
- [ '-l', GetoptLong::REQUIRED_ARGUMENT ]
79
+ [ '-l', GetoptLong::REQUIRED_ARGUMENT ],
80
+ [ '-O', GetoptLong::REQUIRED_ARGUMENT ]
92
81
  ).each { |opt, arg|
93
82
  case opt
94
83
  when '-h'
@@ -108,6 +97,9 @@ def main
108
97
  -c:
109
98
  compile nendo script file. (and initialization file was loaded)
110
99
 
100
+ -O:
101
+ set optimize level. (0:no optimize, 1:tail call optimize)
102
+
111
103
  EOF
112
104
  exit 0
113
105
  when '-q'
@@ -120,6 +112,8 @@ def main
120
112
  when '-l'
121
113
  core.load( arg )
122
114
  STDERR.printf( "loaded file [%s]\n", arg )
115
+ when '-O'
116
+ core.setOptimizeLevel( arg.to_i )
123
117
  end
124
118
  }
125
119
 
@@ -0,0 +1,15 @@
1
+ #!/bin/sh
2
+ :; #-*- mode: nendo; syntax: scheme -*-;;
3
+ :; exec /usr/local/bin/nendo $0 $*
4
+
5
+ (define count 0)
6
+
7
+ (define (loop1)
8
+ (when (eq? 0 (% count 1000))
9
+ (printf "count = %6d\n" count))
10
+ (set! count (+ count 1))
11
+ (loop1))
12
+
13
+ (define (main args)
14
+ (loop1))
15
+
@@ -0,0 +1,14 @@
1
+ #!/bin/sh
2
+ :; #-*- mode: nendo; syntax: scheme -*-;;
3
+ :; exec /usr/local/bin/nendo $0 $*
4
+
5
+
6
+ (define (main args)
7
+ (letrec ((loop1
8
+ (lambda (count)
9
+ (when (eq? 0 (% count 1000))
10
+ (printf "count = %6d\n" count))
11
+ (loop1 (+ count 1)))))
12
+ (loop1 0)))
13
+
14
+
data/example/scratch.nnd CHANGED
@@ -4,3 +4,21 @@
4
4
  (disable-idebug)
5
5
  (define debug-print-length 256)
6
6
 
7
+ ;; Bug : got in `translate': undefined method `sourcefile' for #<Nendo::Cell:0x00000101580668> (NoMethodError)
8
+ (let-values (((a b) (car+cdr '(1 . 2))))
9
+ (or (and (= a 1) (= b 2))
10
+ (fail 'car+cdr:1)))
11
+
12
+ (define (foo lis)
13
+ (second lis))
14
+
15
+ (foo '(1 2 3))
16
+ (foo 1)
17
+
18
+ '(1 2 3)
19
+ (exit)
20
+
21
+ ;; circular
22
+ (load-library "srfi-1")
23
+ (list?
24
+ (circular-list 1 2))
data/example/tak.nnd CHANGED
@@ -1,13 +1,14 @@
1
- ;;-*- mode: scheme; syntax: scheme -*-;;
1
+ #!/bin/sh
2
+ :; #-*- mode: nendo; syntax: scheme -*-;;
3
+ :; exec /usr/local/bin/nendo $0 $*
2
4
 
3
- ;; takeuchi function
5
+ ;; takeuchi function ( tarai mawashi bench )
4
6
  (define (tak x y z)
5
- (if (> x y)
7
+ (if (>= y x)
8
+ y
6
9
  (tak (tak (- x 1) y z)
7
10
  (tak (- y 1) z x)
8
- (tak (- z 1) x y))
9
- y))
10
-
11
- (print (tak 8 4 0))
12
-
11
+ (tak (- z 1) x y))))
13
12
 
13
+ (define (main argv)
14
+ (print (tak 10 5 0)))
@@ -0,0 +1,38 @@
1
+ #!/bin/sh
2
+ :; #-*- mode: nendo; syntax: scheme -*-;;
3
+ :; exec /usr/local/bin/nendo $0 $*
4
+
5
+ (require "rubygems")
6
+ (require "twitter")
7
+
8
+ (define (display-tl tl)
9
+ (for-each
10
+ (lambda (x)
11
+ (printf "%15s : %s \n"
12
+ (hash-table-get (assq-ref "user" x) "screen_name")
13
+ (assq-ref "text" x)))
14
+ tl))
15
+
16
+ (define (main argv)
17
+ (let* ((httpauth (Twitter::HTTPAuth.new (first argv) (second argv)))
18
+ (client (Twitter::Base.new httpauth))
19
+ (lst (client.friends_timeline.to_a.to_list)))
20
+ (let1 tl (map
21
+ (lambda (h)
22
+ (hash-table->alist h))
23
+ lst)
24
+ (display-tl tl))))
25
+
26
+
27
+
28
+
29
+
30
+
31
+
32
+
33
+
34
+
35
+
36
+
37
+
38
+
@@ -3,58 +3,65 @@
3
3
  # generated "nendo -c src" command.
4
4
  #
5
5
 
6
- callProcedure( 'require',
6
+ trampCall(
7
+ delayCall( 'require',
7
8
  begin
8
9
  if @global_lisp_binding.has_key?('_require') then
9
- @_require
10
+ trampCall(@_require)
10
11
  else raise NameError.new( "Error: undefined variable _require", "_require" ) end
11
12
  rescue => __e ; __e.set_backtrace( ["./lib/debug/syslog.nnd:37"] + __e.backtrace ) ; raise __e
12
13
  end ,
13
14
  Cell.new(
14
15
  "syslog"
15
16
  ))
17
+ )
16
18
  #--------------------
17
19
 
20
+ trampCall(
18
21
  begin
19
- @global_lisp_binding['_debug_MIMARKprint_MIMARKoutput_MIMARKfunc'] = true
22
+ def self._debug_MIMARKprint_MIMARKoutput_MIMARKfunc_METHOD( origname, pred, args ) callProcedure( origname, pred, args ) end
23
+ @global_lisp_binding['_debug_MIMARKprint_MIMARKoutput_MIMARKfunc'] = self.method( :_debug_MIMARKprint_MIMARKoutput_MIMARKfunc_METHOD )
20
24
  @_debug_MIMARKprint_MIMARKoutput_MIMARKfunc =
21
- Proc.new { |_str|
22
- begin
23
- Syslog.open(
24
- )
25
- rescue => __e ; __e.set_backtrace( ["./lib/debug/syslog.nnd:40"] + __e.backtrace ) ; raise __e
26
- end
27
- begin
28
- Syslog.log(
29
- begin
30
- Syslog::LOG_WARNING
31
- rescue => __e ; __e.set_backtrace( ["./lib/debug/syslog.nnd:41"] + __e.backtrace ) ; raise __e
32
- end ,
33
- "%s" ,
34
- callProcedure( '+',
35
- begin
36
- if @global_lisp_binding.has_key?('__PLMARK') then
37
- @__PLMARK
38
- else raise NameError.new( "Error: undefined variable __PLMARK", "__PLMARK" ) end
39
- rescue => __e ; __e.set_backtrace( ["./lib/debug/syslog.nnd:41"] + __e.backtrace ) ; raise __e
40
- end ,
41
- Cell.new(
42
- "Nendo: " ,Cell.new(
25
+ trampCall(
26
+ Proc.new { |_str|
27
+ begin
28
+ trampCall(Syslog).open(
29
+ )
30
+ rescue => __e ; __e.set_backtrace( ["./lib/debug/syslog.nnd:40"] + __e.backtrace ) ; raise __e
31
+ end
32
+ begin
33
+ trampCall(Syslog).log(
34
+ begin
35
+ trampCall(Syslog::LOG_WARNING)
36
+ rescue => __e ; __e.set_backtrace( ["./lib/debug/syslog.nnd:41"] + __e.backtrace ) ; raise __e
37
+ end ,
38
+ "%s" ,
39
+ trampCall( self.__PLMARK_METHOD( '+',
43
40
  begin
44
- _str
41
+ if @global_lisp_binding.has_key?('__PLMARK') then
42
+ trampCall(@__PLMARK)
43
+ else raise NameError.new( "Error: undefined variable __PLMARK", "__PLMARK" ) end
45
44
  rescue => __e ; __e.set_backtrace( ["./lib/debug/syslog.nnd:41"] + __e.backtrace ) ; raise __e
46
- end
47
- )))
48
- )
49
- rescue => __e ; __e.set_backtrace( ["./lib/debug/syslog.nnd:41"] + __e.backtrace ) ; raise __e
50
- end
51
- begin
52
- Syslog.close(
53
- )
54
- rescue => __e ; __e.set_backtrace( ["./lib/debug/syslog.nnd:42"] + __e.backtrace ) ; raise __e
55
- end
56
- }
45
+ end ,
46
+ Cell.new(
47
+ "Nendo: " ,Cell.new(
48
+ begin
49
+ trampCall(_str)
50
+ rescue => __e ; __e.set_backtrace( ["./lib/debug/syslog.nnd:41"] + __e.backtrace ) ; raise __e
51
+ end
52
+ ))))
53
+ )
54
+ rescue => __e ; __e.set_backtrace( ["./lib/debug/syslog.nnd:41"] + __e.backtrace ) ; raise __e
55
+ end
56
+ begin
57
+ trampCall(Syslog).close(
58
+ )
59
+ rescue => __e ; __e.set_backtrace( ["./lib/debug/syslog.nnd:42"] + __e.backtrace ) ; raise __e
60
+ end
61
+ }
62
+ )
57
63
  end
64
+ )
58
65
 
59
66
 
60
67
  # -------------------------------------------------------
data/lib/init.nnd CHANGED
@@ -124,16 +124,16 @@
124
124
  (define (negative? n) (< n 0))
125
125
  (define (abs n) (if (>= n 0) n (- n)))
126
126
  (define (max . lst)
127
- (foldr (lambda (a b) (if (> a b) a b)) (car lst) (cdr lst)))
127
+ (fold-right (lambda (a b) (if (> a b) a b)) (car lst) (cdr lst)))
128
128
  (define (min . lst)
129
- (foldr (lambda (a b) (if (< a b) a b)) (car lst) (cdr lst)))
129
+ (fold-right (lambda (a b) (if (< a b) a b)) (car lst) (cdr lst)))
130
130
  (define (succ x) (+ x 1))
131
131
  (define (pred x) (- x 1))
132
132
  (define (nth n lst)
133
133
  (letrec ((nth-iter
134
134
  (lambda (n index lst)
135
135
  (if (null? lst)
136
- nil
136
+ '()
137
137
  (if (not (pair? lst))
138
138
  (error "Error: nth got improper list.")
139
139
  (if (eqv? n index)
@@ -352,7 +352,7 @@
352
352
  (lambda (x)
353
353
  (first x))
354
354
  (second lst))
355
- ,(third lst))))
355
+ ,@(cddr lst))))
356
356
  (,(first lst)
357
357
  ,@(map
358
358
  (lambda (x)
@@ -450,10 +450,10 @@
450
450
  ;; ----------------------------------------
451
451
  ;; List utilities imported from TinyScheme by Kiyoka Nishiyama
452
452
  ;; and Fixed bugs in `map' and `for-each'.
453
- (define (foldr f x lst)
453
+ (define (fold-right f x lst)
454
454
  (if (null? lst)
455
455
  x
456
- (foldr f (f x (car lst)) (cdr lst))))
456
+ (fold-right f (f x (car lst)) (cdr lst))))
457
457
 
458
458
  (define (unzip1-with-cdr . lists)
459
459
  (unzip1-with-cdr-iterative lists '() '()))
@@ -470,30 +470,57 @@
470
470
 
471
471
 
472
472
  (define (map proc . lists)
473
- (if (null? lists)
474
- (apply proc)
473
+ (letrec ((result '())
474
+ (map-arg1
475
+ (lambda (proc lst)
476
+ (if (null? lst)
477
+ #t
478
+ (begin
479
+ (set! result
480
+ (cons (proc (car lst))
481
+ result))
482
+ (map-arg1 proc (cdr lst)))))))
483
+ (cond
484
+ ((null? lists)
485
+ (apply proc))
486
+ ((eq? 1 (length lists))
487
+ (map-arg1 proc (car lists)) ;; tail call optimization version
488
+ (reverse result))
489
+ (else
475
490
  (if (null? (car lists))
476
- '()
477
- (let1 unz (apply unzip1-with-cdr lists)
491
+ '()
492
+ (let1 unz (apply unzip1-with-cdr lists)
478
493
  (let ((cars (car unz))
479
494
  (cdrs (cdr unz)))
480
495
  (cons (apply proc cars)
481
496
  (if (null? cdrs)
482
497
  '()
483
- (apply map (cons proc cdrs)))))))))
498
+ (apply map (cons proc cdrs)))))))))))
499
+
484
500
 
485
501
  (define (for-each proc . lists)
486
- (if (null? lists)
487
- (apply proc)
488
- (if (null? (car lists))
489
- #t
490
- (let1 unz (apply unzip1-with-cdr lists)
491
- (let ((cars (car unz))
492
- (cdrs (cdr unz)))
493
- (apply proc cars)
494
- (if (null? cdrs)
495
- '()
496
- (apply map (cons proc cdrs))))))))
502
+ (define (for-each-arg1 proc lst)
503
+ (if (null? lst)
504
+ #t
505
+ (begin
506
+ (proc (car lst))
507
+ (for-each-arg1 proc (cdr lst)))))
508
+ (cond
509
+ ((null? lists)
510
+ (apply proc))
511
+ ((eq? 1 (length lists))
512
+ (for-each-arg1 proc (car lists))) ;; tail call optimization version
513
+ (else
514
+ (if (null? (car lists))
515
+ #t
516
+ (let1 unz (apply unzip1-with-cdr lists)
517
+ (let ((cars (car unz))
518
+ (cdrs (cdr unz)))
519
+ (apply proc cars)
520
+ (if (null? cdrs)
521
+ '()
522
+ (apply for-each (cons proc cdrs)))))))))
523
+
497
524
 
498
525
  (define (filter pred lst)
499
526
  (if (null? lst)
@@ -931,6 +958,21 @@
931
958
  (newline))
932
959
 
933
960
 
961
+ ;; ----------------------------------------
962
+ ;; optional argument parser
963
+ ;; ----------------------------------------
964
+ (define get-optional
965
+ (macro (restarg default)
966
+ (let1 _restarg (gensym)
967
+ `(let1 ,_restarg ,restarg
968
+ (if (null? ,_restarg)
969
+ ,default
970
+ (car ,_restarg))))))
971
+
972
+ ;; pending
973
+ (define check-arg
974
+ (macro (a b . c)
975
+ `(begin)))
934
976
 
935
977
  ;; ----------------------------------------
936
978
  ;; global variables bind checker
@@ -983,7 +1025,7 @@
983
1025
  "require 'rubygems'"
984
1026
  "require 'nendo'"
985
1027
  ""
986
- "core = Nendo.new()"
1028
+ "core = Nendo::Core.new()"
987
1029
  "core.loadInitFile()"
988
1030
  "core.setArgv( ARGV )"
989
1031
  "core.load_compiled_code_from_string( " ,(write-to-string str) " ) "
@@ -1014,9 +1056,142 @@
1014
1056
  "w")))
1015
1057
 
1016
1058
 
1059
+ ;; ----------------------------------------
1060
+ ;; compile phase ( it can be written in Nendo! )
1061
+ ;; ----------------------------------------
1062
+ ;;
1063
+ ;; example:
1064
+ ;; %compile-phase-functions is ( func1 func2 func3 )
1065
+ ;; then this function evals.
1066
+ ;; (func3 (func2 (func1 sexp)))
1067
+ ;;
1068
+ (define (%compile-phase sexp)
1069
+ (cond
1070
+ ((list? sexp)
1071
+ (for-each
1072
+ (lambda (func)
1073
+ (set! sexp (func sexp)))
1074
+ %compile-phase-functions)
1075
+ sexp)
1076
+ (else
1077
+ sexp)))
1078
+
1079
+
1080
+ ;; ----------------------------------------
1081
+ ;; tail-call-optimization
1082
+ ;; ----------------------------------------
1083
+ (define (setup-tailcall-mark sexp)
1084
+ (define (reserved? sym)
1085
+ (memq sym '(quote macro begin lambda if let letrec define)))
1086
+
1087
+ (define (setup-let-args args)
1088
+ (map
1089
+ (lambda (arg)
1090
+ (let ((name (first arg))
1091
+ (body (second arg)))
1092
+ (list name
1093
+ (if (list? body)
1094
+ (if (reserved? (car body))
1095
+ (setup-tailcall-mark body)
1096
+ body)
1097
+ body))))
1098
+ args))
1099
+
1100
+ (define (setup-proc sexp)
1101
+ (cond
1102
+ ((null? sexp)
1103
+ sexp)
1104
+ ((and (list? sexp)
1105
+ (< 0 (length sexp)))
1106
+ (if (reserved? (car sexp))
1107
+ (setup-tailcall-mark sexp)
1108
+ (if (pair? (car sexp))
1109
+ sexp ;; e.g. ((lambda (x) ...)
1110
+ `(%tailcall ,sexp))))
1111
+ (else
1112
+ sexp)))
1113
+
1114
+ (define (setup-proc-body sexp)
1115
+ (if (or (not (list? sexp))
1116
+ (null? sexp))
1117
+ sexp
1118
+ (let* ((r (reverse sexp))
1119
+ (last (car r))
1120
+ (other (cdr r)))
1121
+ (if (not (pair? last))
1122
+ sexp
1123
+ (reverse
1124
+ (cons
1125
+ (if (reserved? (car last))
1126
+ ;; recursive
1127
+ (setup-tailcall-mark last)
1128
+ ;; this is the tailcall!
1129
+ `(%tailcall ,last))
1130
+ other))))))
1131
+
1132
+ (cond
1133
+ ((not (pair? sexp))
1134
+ sexp)
1135
+ ((null? sexp)
1136
+ '())
1137
+ ((list? sexp)
1138
+ (case (car sexp)
1139
+ (('quote)
1140
+ sexp)
1141
+ (('macro)
1142
+ sexp)
1143
+ (('begin)
1144
+ `(begin
1145
+ ,@(setup-proc-body (cdr sexp))))
1146
+ (('lambda)
1147
+ `(lambda
1148
+ ,(second sexp)
1149
+ ,@(setup-proc-body (cddr sexp))))
1150
+ (('if)
1151
+ (case (length sexp)
1152
+ ((3)
1153
+ `(if
1154
+ ,(second sexp)
1155
+ ,(setup-proc (third sexp))))
1156
+ ((4)
1157
+ `(if
1158
+ ,(second sexp)
1159
+ ,(setup-proc (third sexp))
1160
+ ,(setup-proc (fourth sexp))))))
1161
+ (('let)
1162
+ `(let
1163
+ ,(setup-let-args (second sexp))
1164
+ ,@(setup-proc-body (cddr sexp))))
1165
+ (('letrec)
1166
+ `(letrec
1167
+ ,(setup-let-args (second sexp))
1168
+ ,@(setup-proc-body (cddr sexp))))
1169
+ (('define)
1170
+ (let1 val (third sexp)
1171
+ `(define
1172
+ ,(second sexp)
1173
+ ,(if (and (list? val)
1174
+ (not (null? val))
1175
+ (reserved? (car val)))
1176
+ (setup-tailcall-mark val)
1177
+ val))))
1178
+ (else
1179
+ (if (symbol? (car sexp))
1180
+ `(%tailcall ,sexp)
1181
+ sexp))))
1182
+ (else
1183
+ sexp)))
1184
+
1185
+
1186
+ ;; definition of 'compile-phase'
1187
+ (set! %compile-phase-functions
1188
+ (list
1189
+ setup-tailcall-mark
1190
+ ))
1191
+
1017
1192
  ;; ----------------------------------------
1018
1193
  ;; global variables
1019
1194
  ;; ----------------------------------------
1020
1195
  (define *nendo-version*
1021
- "0.3.2" ;;NENDO-VERSION
1196
+ "0.3.3" ;;NENDO-VERSION
1022
1197
  )