@@ -49,6 +49,8 @@ def call(*args)
4949 end
5050 end
5151
52+ EOF_OBJECT = :"#<eof-object>"
53+
5254 GLOBAL_DICT = {
5355 :+ => proc { |a , b | a + b } ,
5456 :* => proc { |a , b | a * b } ,
@@ -98,14 +100,29 @@ def call(*args)
98100 :"string-split" => proc { |s , sep | s . split ( sep ) } ,
99101 :"string-trim" => proc { |s | s . strip } ,
100102 :"string-upcase" => proc { |s | s . upcase } ,
103+ :"eof-object?" => proc { |a | a == EOF_OBJECT } ,
101104 :symbol? => proc { |a | a . is_a? ( Symbol ) } ,
102105 :"with-exception-handler" => proc { |handler , body |
103106 begin
104107 body . call
105108 rescue => e
106109 handler . call ( e )
107110 end
108- }
111+ } ,
112+ :"open-output-file" => proc { |filename | [ File . open ( filename , "w" ) , "w" ] } ,
113+ :"open-input-file" => proc { |filename | [ File . open ( filename , "r" ) , "r" ] } ,
114+ :"close-port" => proc { |port_tuple | port_tuple [ 0 ] . close } ,
115+ :"port?" => proc { |port_tuple | port_tuple [ 0 ] . is_a? ( File ) } ,
116+ :"output-port?" => proc { |port_tuple | port_tuple [ 0 ] . is_a? ( File ) && port_tuple [ 1 ] == "w" } ,
117+ :"input-port?" => proc { |port_tuple | port_tuple [ 0 ] . is_a? ( File ) && port_tuple [ 1 ] == "r" } ,
118+ :"read-char" => proc { |port_tuple |
119+ begin
120+ port_tuple [ 0 ] . readchar
121+ rescue EOFError
122+ EOF_OBJECT
123+ end } ,
124+ :"write-char" => proc { |char , port_tuple | port_tuple [ 0 ] . write ( char ) }
125+
109126 } . entries . transpose
110127 GLOBAL_ENV = Environment . new ( GLOBAL_DICT [ 0 ] , GLOBAL_DICT [ 1 ] )
111128
0 commit comments