Class: Evaluator
Overview
Translate S expression to Ruby expression and Evaluation
Instance Method Summary
collapse
-
#_enable_debug ⇒ Object
-
#_eval(sexp) ⇒ Object
-
#_gensym ⇒ Object
-
#_load(filename) ⇒ Object
-
#apply(car, cdr, sourcefile, lineno, lambda_flag = false) ⇒ Object
-
#callProcedure(origname, pred, args) ⇒ Object
-
#execFunc(funcname, args, sourcefile, lineno, lambda_flag) ⇒ Object
-
#genQuote(sexp, str = "") ⇒ Object
-
#initialize(debug = false) ⇒ Evaluator
constructor
A new instance of Evaluator.
-
#isRubyInterface(name) ⇒ Object
-
#letArgumentList(sexp) ⇒ Object
insert quote in let argument list ((sym1 list1) (sym2 list2) (sym3 list3)) will be transformed (((quote sym1) list1) ((quote sym2) list2) ((quote sym3) list3)).
-
#lispCompile(sexp) ⇒ Object
-
#lispEval(sexp, sourcefile, lineno) ⇒ Object
-
#macroexpand_1(sexp) ⇒ Object
-
#makeBegin(args) ⇒ Object
-
#makeClosure(sym, args) ⇒ Object
-
#makeIf(args) ⇒ Object
-
#makeLet(args) ⇒ Object
-
#quoting(sexp) ⇒ Object
-
#toLispSymbol(name) ⇒ Object
-
#toRubyArgument(origname, pred, args) ⇒ Object
-
#toRubyParameter(argform) ⇒ Object
-
#toRubySymbol(name) ⇒ Object
-
#toRubyValue(val) ⇒ Object
-
#translate(sexp) ⇒ Object
#__assertFlat, #__assertList, #_car, #_cdr, #_cons, #_display, #_div, #_eq_QMARK, #_equal_QMARK, #_eqv_QMARK, #_exit, #_ge_QMARK, #_gt_QMARK, #_intern, #_le_QMARK, #_length, #_list, #_lt_QMARK, #_macro_QMARK, #_macroexpand_1, #_minus, #_mod, #_multi, #_newline, #_not, #_null_QMARK, #_number_QMARK, #_pair_QMARK, #_plus, #_print, #_printf, #_procedure_QMARK, #_range, #_read, #_require, #_reverse, #_sort, #_sprintf, #_string_QMARK, #_string_join, #_symbol_QMARK, #_to_list, #_to_s, #_uniq, #_write, #_write_to_string
Constructor Details
#initialize(debug = false) ⇒ Evaluator
Returns a new instance of Evaluator.
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
|
# File 'lib/nendo.rb', line 694
def initialize( debug = false )
@binding = binding
@debug = debug
@alias = {'+' => 'plus',
'-' => 'minus',
'*' => 'multi',
'/' => 'div',
'%' => 'mod',
'=' => 'eq?',
'==' => 'eq?',
'>' => 'gt?',
'>=' => 'ge?',
'<' => 'lt?',
'<=' => 'le?',
'===' => 'eqv?',
}
@sym = Hash.new
self.methods.grep( /^_/ ) { |rubySymbol|
@sym[ rubySymbol ] = self.method( rubySymbol )
}
rubyExp = @sym.keys.map { |name|
sprintf( "%s = @sym[ '%s' ] ", name, name )
}.join( " ; " )
eval( rubyExp, @binding )
rubyExp = self.methods.select { |x|
x.to_s.match( /^_/ )
}.map { |name|
sprintf( "%s = self.method( :%s ).to_proc", name, name )
}.join( " ; " )
eval( rubyExp, @binding )
@gensym_counter = 0
end
|
Instance Method Details
#_enable_debug ⇒ Object
1136
1137
1138
|
# File 'lib/nendo.rb', line 1136
def _enable_debug()
@debug = true
end
|
#_eval(sexp) ⇒ Object
1132
1133
1134
|
# File 'lib/nendo.rb', line 1132
def _eval( sexp )
self.lispEval( sexp, "dynamic S-expression ( no source )", 1 )
end
|
#_gensym ⇒ Object
734
735
736
737
|
# File 'lib/nendo.rb', line 734
def _gensym( )
@gensym_counter += 1
sprintf( "__gensym__%d", @gensym_counter ).intern
end
|
#_load(filename) ⇒ Object
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
|
# File 'lib/nendo.rb', line 1115
def _load( filename )
printer = Printer.new( @debug )
open( filename ) {|f|
reader = Reader.new( f, filename, false )
while true
lineno = reader.lineno
s = reader._read
if s[1]
break
elsif Nil != s[0].class
printf( "\n readExp=<<< %s >>>\n", printer._print(s[0]) ) if @debug
self.lispEval( s[0], reader.sourcefile, lineno )
end
end
}
end
|
#apply(car, cdr, sourcefile, lineno, lambda_flag = false) ⇒ Object
939
940
941
942
943
944
945
946
|
# File 'lib/nendo.rb', line 939
def apply( car, cdr, sourcefile, lineno, lambda_flag = false )
cdr.each { |x|
if Cell == x.class
x.car = translate( x.car )
end
}
execFunc( car, cdr, sourcefile, lineno, lambda_flag )
end
|
#callProcedure(origname, pred, args) ⇒ Object
817
818
819
820
|
# File 'lib/nendo.rb', line 817
def callProcedure( origname, pred, args )
rubyArgument = toRubyArgument( origname, pred, args )
pred.call( *rubyArgument )
end
|
#execFunc(funcname, args, sourcefile, lineno, lambda_flag) ⇒ Object
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
|
# File 'lib/nendo.rb', line 822
def execFunc( funcname, args, sourcefile, lineno, lambda_flag )
case funcname
when :set!
sprintf( "%s = %s", toRubySymbol( args.car.to_s.sub( /^:/, "" )), toRubyValue( args.cdr.car ))
when :error
sprintf( 'begin raise RuntimeError, %s ; rescue => __e ; __e.set_backtrace( ["%s:%d"] + __e.backtrace ) ; raise __e ; end ', toRubyValue( args.car ), sourcefile, lineno )
else
if (not lambda_flag) and isRubyInterface( funcname )
argStr = args.map { |x| toRubyValue( x.car ) }.join( "," )
sprintf( "%s( %s )", toRubySymbol( funcname ), argStr )
else
argStr = args.map { |x| toRubyValue( x.car ) }.join( " ,Cell.new(" )
argStr += args.map { |x| "" }.join( ")" )
if lambda_flag
"anonymouse"
sprintf( "callProcedure( 'anonymouse', %s, Cell.new( %s ))", funcname, argStr )
else
origname = funcname.to_s
funcname = funcname.to_s
funcname = @alias[ funcname ] if @alias[ funcname ]
sprintf( "callProcedure( '%s', %s, Cell.new( %s ))", origname, toRubySymbol( funcname ), argStr )
end
end
end
end
|
#genQuote(sexp, str = "") ⇒ Object
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
|
# File 'lib/nendo.rb', line 948
def genQuote( sexp, str = "" )
origStr = str
case sexp
when Cell
if sexp.isNull
str += "Nil.new"
else
arr = sexp.map { |x| genQuote( x.car ) }
str += "Cell.new("
str += arr.join( " ,Cell.new(" )
lastAtom = sexp.lastAtom
str += "," + genQuote( lastAtom ) if lastAtom
str += arr.map{ |e| ")" }.join
end
when Symbol
str += sprintf( ":\"%s\"", sexp.to_s )
when String
str += sprintf( "\"%s\"", sexp )
when TrueClass, FalseClass, NilClass
str += toRubyValue( sexp )
else
str += sprintf( "%s", sexp )
end
str
end
|
#isRubyInterface(name) ⇒ Object
769
770
771
|
# File 'lib/nendo.rb', line 769
def isRubyInterface( name )
name.to_s.match( /[.]/ )
end
|
#letArgumentList(sexp) ⇒ Object
insert quote in let argument list
((sym1 list1)
(sym2 list2)
(sym3 list3))
will be transformed
(((quote sym1) list1)
((quote sym2) list2)
((quote sym3) list3))
1033
1034
1035
1036
1037
1038
1039
|
# File 'lib/nendo.rb', line 1033
def letArgumentList( sexp )
sexp.each { |arg|
arg.car.car = Cell.new( :quote, Cell.new( arg.car.car ))
arg.car.cdr = quoting( arg.car.cdr )
}
sexp
end
|
#lispCompile(sexp) ⇒ Object
1094
1095
1096
1097
1098
1099
1100
1101
1102
|
# File 'lib/nendo.rb', line 1094
def lispCompile( sexp )
converge = true
begin
newSexp = macroexpand_1( sexp )
converge = _equal_QMARK( newSexp, sexp )
sexp = newSexp
end until converge
sexp
end
|
#lispEval(sexp, sourcefile, lineno) ⇒ Object
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
|
# File 'lib/nendo.rb', line 1104
def lispEval( sexp, sourcefile, lineno )
sexp = lispCompile( sexp )
sexp = quoting( sexp );
if @debug
printf( "\n quoting=<<< %s >>>\n", (Printer.new())._print(sexp))
end
rubyExp = translate( sexp );
printf( " rubyExp=<<< %s >>>\n", rubyExp ) if @debug
eval( rubyExp, @binding, sourcefile, lineno );
end
|
#macroexpand_1(sexp) ⇒ Object
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
|
# File 'lib/nendo.rb', line 1069
def macroexpand_1( sexp )
case sexp
when Cell
if :quote == sexp.car
sexp
else
sym = sexp.car.to_s
sym = @alias[ sym ] if @alias[ sym ]
sym = toRubySymbol( sym )
if isRubyInterface( sym )
arr = sexp.map { |x| macroexpand_1( x.car ) }
arr.to_list( sexp.lastAtom )
elsif sexp.car.class == Symbol and eval( sprintf( "(defined? %s and LispMacro == %s.class)", sym,sym ), @binding )
eval( sprintf( "@_macro = %s", sym ), @binding )
callProcedure( sym, @_macro, sexp.cdr )
else
arr = sexp.map { |x| macroexpand_1( x.car ) }
arr.to_list( sexp.lastAtom )
end
end
else
sexp
end
end
|
#makeBegin(args) ⇒ Object
852
853
854
855
856
857
|
# File 'lib/nendo.rb', line 852
def makeBegin( args )
ar = args.map { |e|
translate( e.car )
}
"begin " + ar.join( ";" ) + " end"
end
|
#makeClosure(sym, args) ⇒ Object
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
|
# File 'lib/nendo.rb', line 877
def makeClosure( sym, args )
first = args.car
if args.car.car == :quote
first = args.car.cdr.car
end
rest = args.cdr
argStr = toRubyParameter( first )
str = case sym
when :macro
sprintf( "LispMacro.new { %s ", argStr )
when :lambda
sprintf( " Proc.new { %s ", argStr )
else
raise "Error: makeClosure: unknown symbol type " + sym
end
ar = rest.map { |e|
translate( e.car )
}
str += ar.join( ";" ) + "}"
end
|
#makeIf(args) ⇒ Object
898
899
900
901
902
903
904
905
906
907
908
909
910
|
# File 'lib/nendo.rb', line 898
def makeIf( args )
_condition = translate( args.car )
_then = translate( args.cdr.car )
_else = nil
if 2 < args.length
_else = translate( args.cdr.cdr.car )
end
if _else
str = sprintf( "if ( %s ) then %s else %s end ", _condition, _then, _else )
else
str = sprintf( "if ( %s ) then %s end ", _condition, _then )
end
end
|
#makeLet(args) ⇒ Object
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
|
# File 'lib/nendo.rb', line 912
def makeLet( args )
_name = "___lambda"
str = ""
argvals = []
if args.car.is_a? Nil
str = sprintf( "begin %s = lambda { || ", _name )
rest = args.cdr
else
if :quote == args.car.car
_name = args.car.cdr.car.to_s
args = args.cdr
end
rest = args.cdr
argsyms = args.car.map { |x|
toRubySymbol( x.car.car.cdr.car.to_s )
}
argvals = args.car.map { |x|
translate( x.car.cdr.car )
}
str = sprintf( "begin %s = lambda { |%s| ", _name, argsyms.join( "," ))
end
ar = rest.map { |e| translate( e.car ) }
str += ar.join( ";" ) + "} ; "
str += sprintf( "%s.call( %s ) end ", _name, argvals.join( "," ))
end
|
#quoting(sexp) ⇒ Object
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
|
# File 'lib/nendo.rb', line 1041
def quoting( sexp )
case sexp
when Cell
if :quote == sexp.car or :quasiquote == sexp.car
sexp
elsif :set! == sexp.car or :lambda == sexp.car or :macro == sexp.car
sexp.cdr.car = Cell.new( :quote, Cell.new( sexp.cdr.car ))
sexp.cdr.cdr = quoting( sexp.cdr.cdr )
sexp
elsif :let == sexp.car
case sexp.cdr.car
when Cell
sexp.cdr = Cell.new( letArgumentList( sexp.cdr.car ),
quoting( sexp.cdr.cdr ))
when Symbol
sexp.cdr.car = Cell.new( :quote, Cell.new( sexp.cdr.car ))
sexp.cdr.cdr = Cell.new( letArgumentList( sexp.cdr.cdr.car ),
quoting( sexp.cdr.cdr.cdr ))
end
sexp
else
Cell.new( quoting( sexp.car ), quoting( sexp.cdr ))
end
else
sexp
end
end
|
#toLispSymbol(name) ⇒ Object
773
774
775
776
777
778
|
# File 'lib/nendo.rb', line 773
def toLispSymbol( name )
name = name.to_s if Symbol == name.class
raise ArgumentError if not ('_' == name[0])
name = name[1..-1]
name.gsub( /_AMARK/, '*' ).gsub( /_QMARK/, '?' ).gsub( /_EMARK/, '!' )
end
|
#toRubyArgument(origname, pred, args) ⇒ Object
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
|
# File 'lib/nendo.rb', line 780
def toRubyArgument( origname, pred, args )
num = pred.arity
if 0 == num
raise ArgumentError, sprintf( "at function `%s'", origname ) if 0 != args.length
[]
elsif 0 < num
if args.isNull
[ Nil.new ]
else
raise ArgumentError, sprintf( "at function `%s'", origname ) if num != args.length
args.map { |x| x.car }
end
else
num = num.abs( )-1
raise ArgumentError, sprintf( "at function `%s'", origname ) if num > args.length
params = []
rest = []
args.each_with_index { |x,i|
if i < num
params << x.car
else
rest << x.car
end
}
result = []
if 0 < params.length
result = params
end
if 0 == rest.length
result << Cell.new
else
result << rest.to_list
end
result
end
end
|
#toRubyParameter(argform) ⇒ Object
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
|
# File 'lib/nendo.rb', line 859
def toRubyParameter( argform )
argsyms = []
rest = nil
if Symbol == argform.class
rest = argform
else
argsyms = argform.map { |x| toRubySymbol( x.car ) }
rest = argform.lastAtom
end
if rest
rest = toRubySymbol( rest )
argsyms << "*__rest__"
sprintf( "|%s| %s = __rest__[0] ; ", argsyms.join( "," ), rest )
else
sprintf( "|%s|", argsyms.join( "," ))
end
end
|
#toRubySymbol(name) ⇒ Object
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
|
# File 'lib/nendo.rb', line 751
def toRubySymbol( name )
name = name.to_s if Symbol == name.class
if 0 == name.length
""
else
arr = name.split( /[.]/ )
arr[0] = arr[0].gsub( /[*]/, '_AMARK' ).gsub( /[?]/, '_QMARK' ).gsub( /[!]/, '_EMARK' ).gsub( /[-]/, '_' ).gsub( /["]/, '' )
if arr[0].match( /^[A-Z]/ )
elsif arr[0] == ""
arr[0] = 'Kernel'
else
arr[0] = '_' + arr[0]
end
arr.join( "." )
end
end
|
#toRubyValue(val) ⇒ Object
739
740
741
742
743
744
745
746
747
748
749
|
# File 'lib/nendo.rb', line 739
def toRubyValue( val )
if NilClass == val.class
"nil"
elsif TrueClass == val.class
val.to_s
elsif FalseClass == val.class
val.to_s
else
val.to_s
end
end
|
#translate(sexp) ⇒ Object
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
|
# File 'lib/nendo.rb', line 974
def translate( sexp )
str = ""
case sexp
when Cell
if :quote == sexp.car
genQuote( sexp.cdr.car )
elsif sexp.isDotted
print "Error: can't eval dotted pair"
raise NameError
elsif sexp.isNull
str += "Cell.new()"
elsif Cell == sexp.car.class
if :lambda == sexp.car.car
str += self.apply( translate( sexp.car ), sexp.cdr, sexp.car.car.sourcefile, sexp.car.car.lineno, true )
else
str += translate( sexp.car )
end
elsif :begin == sexp.car
str += self.makeBegin( sexp.cdr )
elsif :lambda == sexp.car
str += self.makeClosure( :lambda, sexp.cdr )
elsif :macro == sexp.car
str += self.makeClosure( :macro, sexp.cdr )
elsif :if == sexp.car
str += self.makeIf( sexp.cdr )
elsif :let == sexp.car
str += self.makeLet( sexp.cdr )
else
str += self.apply( sexp.car, sexp.cdr, sexp.car.sourcefile, sexp.car.lineno )
end
else
case sexp
when Symbol
sym = sexp.to_s
sym = @alias[ sym ] if @alias[ sym ]
sym = toRubySymbol( sym )
str += sprintf( 'begin %s ; rescue NameError => __e ; __e.set_backtrace( ["%s:%d"] + __e.backtrace ) ; raise __e ; end', sym, sexp.sourcefile, sexp.lineno )
when Fixnum
str += sexp.to_s
when LispString
str += sprintf( "\"%s\"", sexp )
when Nil
str += "Nil.new"
when TrueClass, FalseClass, NilClass
str += toRubyValue( sexp )
else
str += sexp.to_s
end
end
end
|