Skip to content
Browse files

added support for Sstring's (SchemeStrings)

also added 'rb' builtin method that takes a Sstring and eval's it
as ruby code and returns an Sstring value back to scheme
  • Loading branch information...
1 parent 27584a1 commit c663b324cdab5d4a78eb1c0daaa27f1fa4d040cc Ezra Zygmuntowicz committed
Showing with 47 additions and 14 deletions.
  1. +16 −12 lib/ez-scheme/builtins.rb
  2. +28 −2 lib/ez-scheme/expr.rb
  3. +3 −0 lib/ez-scheme/parser.rb
View
28 lib/ez-scheme/builtins.rb
@@ -155,18 +155,21 @@ def make_comparison_operator_builtin(op)
def make_arith_operator_builtin(op)
lambda{ |args| Number.new(args.map{|a| a.value}.inject(op)) }
end
-
+
+def eval_ruby_code
+ lambda{|args| Sstring.new(eval(args[0]).to_s)}
+end
$builtins_map = {
- 'eqv?' => lambda{ |args| builtin_eqv(args) },
- 'eq?' => lambda{ |args| builtin_eqv(args) },
- 'pair?' => lambda{ |args| builtin_pair_p(args) },
- 'zero?' => lambda{ |args| builtin_zero_p(args) },
- 'boolean?' => lambda{ |args| builtin_boolean_p(args) },
- 'symbol?' => lambda{ |args| builtin_symbol_p(args) },
- 'number?' => lambda{ |args| builtin_number_p(args) },
- 'null?' => lambda{ |args| builtin_null_p(args) },
- 'cons' => lambda{ |args| builtin_cons(args) },
+ 'eqv?' => lambda{ |args| builtin_eqv(args) },
+ 'eq?' => lambda{ |args| builtin_eqv(args) },
+ 'pair?' => lambda{ |args| builtin_pair_p(args) },
+ 'zero?' => lambda{ |args| builtin_zero_p(args) },
+ 'boolean?' => lambda{ |args| builtin_boolean_p(args) },
+ 'symbol?' => lambda{ |args| builtin_symbol_p(args) },
+ 'number?' => lambda{ |args| builtin_number_p(args) },
+ 'null?' => lambda{ |args| builtin_null_p(args) },
+ 'cons' => lambda{ |args| builtin_cons(args) },
'list' => lambda{ |args| builtin_list(args) },
'car' => lambda{ |args| builtin_car(args) },
'cdr' => lambda{ |args| builtin_cdr(args) },
@@ -180,11 +183,12 @@ def make_arith_operator_builtin(op)
'+' => make_arith_operator_builtin(:+),
'-' => make_arith_operator_builtin(:-),
'*' => make_arith_operator_builtin(:*),
- 'quotient' => make_arith_operator_builtin(:/),
+ 'quotient' => make_arith_operator_builtin(:/),
'modulo' => make_arith_operator_builtin(:%),
'=' => make_comparison_operator_builtin(:==),
'>=' => make_comparison_operator_builtin(:>=),
'<=' => make_comparison_operator_builtin(:<=),
'>' => make_comparison_operator_builtin(:>),
- '<' => make_comparison_operator_builtin(:<)
+ '<' => make_comparison_operator_builtin(:<),
+ 'rb' => eval_ruby_code
}
View
30 lib/ez-scheme/expr.rb
@@ -60,6 +60,32 @@ def ==(other)
end
+class Sstring
+
+ attr_reader :value
+
+ def initialize(value)
+ @value = value.gsub('"', '')
+ end
+
+ def to_s
+ @value.to_s
+ end
+
+ def to_str
+ @value.to_str
+ end
+
+ def ==(other)
+ if Sym === other
+ @value == other.value
+ else
+ @value == other
+ end
+ end
+
+end
+
class Boolean
attr_reader :value
@@ -94,7 +120,7 @@ module Predicates
def repr_rec(obj)
if obj.nil?
return '()'
- elsif [Boolean, Sym, Number].any? { |c| obj.instance_of?(c) }
+ elsif [Boolean, Sym, Number, Sstring].any? { |c| obj.instance_of?(c) }
return obj.to_s
elsif obj.instance_of?(Pair)
str = '(' + repr_rec(obj.first)
@@ -160,7 +186,7 @@ def is_scheme_expr(exp)
# section 4.1.2 of SICP.
#
def is_self_evaluating(exp)
- [Number, Boolean].any?{|c| exp.instance_of?(c)}
+ [Number, Boolean, Sstring].any?{|c| exp.instance_of?(c)}
end
def is_variable(exp)
View
3 lib/ez-scheme/parser.rb
@@ -134,6 +134,8 @@ def simple_datum()
end
elsif @cur_token._type == 'ID'
retval = Sym.new(@cur_token.value)
+ elsif @cur_token._type == 'STRING'
+ retval = Sstring.new(@cur_token.value)
else
parse_error("Unexpected token '#{@cur_token.value}'")
end
@@ -252,6 +254,7 @@ def lexing_rules()
[%r{\(}, 'LPAREN'],
[%r{\)}, 'RPAREN'],
[%r{\'}, 'QUOTE'],
+ [%r{\".*?\"}, 'STRING']
]
rules
end

0 comments on commit c663b32

Please sign in to comment.
Something went wrong with that request. Please try again.