Browse Source

Autoformat sources

master^2
Vitalii Elenhaupt 6 years ago
parent
commit
eb8466793a
No known key found for this signature in database GPG Key ID: 7558EF3A4056C706
9 changed files with 114 additions and 124 deletions
  1. +0
    -1
      spec/helper.cr
  2. +9
    -10
      src/crisp/core.cr
  3. +2
    -4
      src/crisp/env.cr
  4. +1
    -0
      src/crisp/error.cr
  5. +87
    -91
      src/crisp/evaluator.cr
  6. +4
    -2
      src/crisp/expr.cr
  7. +2
    -4
      src/crisp/interpreter.cr
  8. +7
    -7
      src/crisp/printer.cr
  9. +2
    -5
      src/crisp/reader.cr

+ 0
- 1
spec/helper.cr View File

@ -1,3 +1,2 @@
require "spec" require "spec"
require "../src/crisp" require "../src/crisp"

+ 9
- 10
src/crisp/core.cr View File

@ -43,11 +43,11 @@ module Crisp
end end
def pr_str(args) def pr_str(args)
args.map{|a| Printer.new.print(a)}.join(" ")
args.map { |a| Printer.new.print(a) }.join(" ")
end end
def str(args) def str(args)
args.map{|a| Printer.new(false).print(a)}.join
args.map { |a| Printer.new(false).print(a) }.join
end end
def prn(args) def prn(args)
@ -56,7 +56,7 @@ module Crisp
end end
def println(args) def println(args)
puts args.map{|a| Printer.new(false).print(a)}.join(" ")
puts args.map { |a| Printer.new(false).print(a) }.join(" ")
nil nil
end end
@ -86,7 +86,7 @@ module Crisp
args.each_with_object(Crisp::List.new) do |arg, list| args.each_with_object(Crisp::List.new) do |arg, list|
a = arg.unwrap a = arg.unwrap
Crisp.eval_error "arguments of concat must be list" unless a.is_a?(Array) Crisp.eval_error "arguments of concat must be list" unless a.is_a?(Array)
a.each{|e| list << e}
a.each { |e| list << e }
end end
end end
@ -141,7 +141,7 @@ module Crisp
f = case func f = case func
when Crisp::Closure then func.fn when Crisp::Closure then func.fn
when Crisp::Func then func when Crisp::Func then func
else Crisp.eval_error "1st argument of map must be function"
else Crisp.eval_error "1st argument of map must be function"
end end
list.each_with_object(Crisp::List.new) do |elem, mapped| list.each_with_object(Crisp::List.new) do |elem, mapped|
@ -213,7 +213,7 @@ module Crisp
Crisp.eval_error "assoc must take a list and even number of arguments" unless (args.size - 1).even? Crisp.eval_error "assoc must take a list and even number of arguments" unless (args.size - 1).even?
map = Crisp::HashMap.new map = Crisp::HashMap.new
head.each{|k, v| map[k] = v}
head.each { |k, v| map[k] = v }
args[1..-1].each_slice(2) do |kv| args[1..-1].each_slice(2) do |kv|
k = kv[0].unwrap k = kv[0].unwrap
@ -229,7 +229,7 @@ module Crisp
Crisp.eval_error "1st argument of assoc must be hashmap" unless head.is_a? Crisp::HashMap Crisp.eval_error "1st argument of assoc must be hashmap" unless head.is_a? Crisp::HashMap
map = Crisp::HashMap.new map = Crisp::HashMap.new
head.each{|k,v| map[k] = v}
head.each { |k, v| map[k] = v }
args[1..-1].each do |arg| args[1..-1].each do |arg|
key = arg.unwrap key = arg.unwrap
@ -259,7 +259,7 @@ module Crisp
def keys(args) def keys(args)
head = args.first.unwrap head = args.first.unwrap
Crisp.eval_error "1st argument of assoc must be hashmap" unless head.is_a? Crisp::HashMap Crisp.eval_error "1st argument of assoc must be hashmap" unless head.is_a? Crisp::HashMap
head.keys.each_with_object(Crisp::List.new){|e,l| l << Crisp::Expr.new(e)}
head.keys.each_with_object(Crisp::List.new) { |e, l| l << Crisp::Expr.new(e) }
end end
def vals(args) def vals(args)
@ -377,7 +377,7 @@ module Crisp
"nth" => func(:nth), "nth" => func(:nth),
"first" => func(:first), "first" => func(:first),
"rest" => func(:rest), "rest" => func(:rest),
"throw" => -> (args : Array(Crisp::Expr)) { raise Crisp::RuntimeException.new args[0] },
"throw" => ->(args : Array(Crisp::Expr)) { raise Crisp::RuntimeException.new args[0] },
"apply" => func(:apply), "apply" => func(:apply),
"map" => func(:map), "map" => func(:map),
"nil?" => func(:nil_value?), "nil?" => func(:nil_value?),
@ -410,5 +410,4 @@ module Crisp
"conj" => func(:conj), "conj" => func(:conj),
"time-ms" => func(:time_ms), "time-ms" => func(:time_ms),
} of String => Crisp::Func } of String => Crisp::Func
end end

+ 2
- 4
src/crisp/env.cr View File

@ -2,7 +2,6 @@ require "./expr"
require "./error" require "./error"
module Crisp module Crisp
class Env class Env
property data property data
@ -23,10 +22,10 @@ module Crisp
if sym.str == "&" if sym.str == "&"
Crisp.eval_error "missing variable parameter name" if binds.size == idx Crisp.eval_error "missing variable parameter name" if binds.size == idx
next_param = binds[idx+1].unwrap
next_param = binds[idx + 1].unwrap
Crisp.eval_error "bind name must be symbol" unless next_param.is_a? Crisp::Symbol Crisp.eval_error "bind name must be symbol" unless next_param.is_a? Crisp::Symbol
var_args = Crisp::List.new var_args = Crisp::List.new
exprs[idx..-1].each{|e| var_args << e} if idx < exprs.size
exprs[idx..-1].each { |e| var_args << e } if idx < exprs.size
@data[next_param.str] = Crisp::Expr.new var_args @data[next_param.str] = Crisp::Expr.new var_args
break break
end end
@ -63,5 +62,4 @@ module Crisp
e.data[key] e.data[key]
end end
end end
end end

+ 1
- 0
src/crisp/error.cr View File

@ -9,6 +9,7 @@ module Crisp
class RuntimeException < Exception class RuntimeException < Exception
getter :thrown getter :thrown
def initialize(@thrown : Crisp::Expr) def initialize(@thrown : Crisp::Expr)
super() super()
end end

+ 87
- 91
src/crisp/evaluator.cr View File

@ -9,34 +9,33 @@ require "./core"
require "./error" require "./error"
module Crisp module Crisp
class Evaluator class Evaluator
def func_of(env, binds, body) def func_of(env, binds, body)
-> (args : Array(Crisp::Expr)) {
new_env = Crisp::Env.new(env, binds, args)
eval(body, new_env)
->(args : Array(Crisp::Expr)) {
new_env = Crisp::Env.new(env, binds, args)
eval(body, new_env)
}.as Crisp::Func }.as Crisp::Func
end end
def eval_ast(ast, env) def eval_ast(ast, env)
return ast.map{|n| eval(n, env).as Crisp::Expr} if ast.is_a? Array
return ast.map { |n| eval(n, env).as Crisp::Expr } if ast.is_a? Array
val = ast.unwrap val = ast.unwrap
Crisp::Expr.new case val Crisp::Expr.new case val
when Crisp::Symbol when Crisp::Symbol
if e = env.get(val.str) if e = env.get(val.str)
e
e
else else
Crisp.eval_error "'#{val.str}' not found"
Crisp.eval_error "'#{val.str}' not found"
end end
when Crisp::List when Crisp::List
val.each_with_object(Crisp::List.new){|n, l| l << eval(n, env)}
val.each_with_object(Crisp::List.new) { |n, l| l << eval(n, env) }
when Crisp::Vector when Crisp::Vector
val.each_with_object(Crisp::Vector.new){|n, l| l << eval(n, env)}
val.each_with_object(Crisp::Vector.new) { |n, l| l << eval(n, env) }
when Crisp::HashMap when Crisp::HashMap
new_map = Crisp::HashMap.new new_map = Crisp::HashMap.new
val.each{|k, v| new_map[k] = eval(v, env)}
val.each { |k, v| new_map[k] = eval(v, env) }
new_map new_map
else else
val val
@ -51,9 +50,9 @@ module Crisp
list = ast.unwrap list = ast.unwrap
unless pair?(list) unless pair?(list)
return Crisp::Expr.new(
Crisp::List.new << gen_type(Crisp::Symbol, "quote") << ast
)
return Crisp::Expr.new(
Crisp::List.new << gen_type(Crisp::Symbol, "quote") << ast
)
end end
head = list.first.unwrap head = list.first.unwrap
@ -62,7 +61,7 @@ module Crisp
# ("unquote" ...) # ("unquote" ...)
when head.is_a?(Crisp::Symbol) && head.str == "unquote" when head.is_a?(Crisp::Symbol) && head.str == "unquote"
list[1] list[1]
# (("splice-unquote" ...) ...)
# (("splice-unquote" ...) ...)
when pair?(head) && (arg0 = head.first.unwrap).is_a?(Crisp::Symbol) && arg0.str == "splice-unquote" when pair?(head) && (arg0 = head.first.unwrap).is_a?(Crisp::Symbol) && arg0.str == "splice-unquote"
tail = Crisp::Expr.new list[1..-1].to_crisp_value tail = Crisp::Expr.new list[1..-1].to_crisp_value
Crisp::Expr.new( Crisp::Expr.new(
@ -92,7 +91,6 @@ module Crisp
def macroexpand(ast, env) def macroexpand(ast, env)
while macro_call?(ast, env) while macro_call?(ast, env)
# Already checked in macro_call? # Already checked in macro_call?
list = ast.unwrap.as Crisp::List list = ast.unwrap.as Crisp::List
func_sym = list[0].unwrap.as Crisp::Symbol func_sym = list[0].unwrap.as Crisp::Symbol
@ -149,85 +147,83 @@ module Crisp
return invoke_list(list, env) unless head.is_a? Crisp::Symbol return invoke_list(list, env) unless head.is_a? Crisp::Symbol
return Crisp::Expr.new case head.str return Crisp::Expr.new case head.str
when "def!"
Crisp.eval_error "wrong number of argument for 'def!'" unless list.size == 3
a1 = list[1].unwrap
Crisp.eval_error "1st argument of 'def!' must be symbol: #{a1}" unless a1.is_a? Crisp::Symbol
env.set(a1.str, eval(list[2], env))
when "let*"
Crisp.eval_error "wrong number of argument for 'def!'" unless list.size == 3
bindings = list[1].unwrap
Crisp.eval_error "1st argument of 'let*' must be list or vector" unless bindings.is_a? Array
Crisp.eval_error "size of binding list must be even" unless bindings.size.even?
new_env = Crisp::Env.new env
bindings.each_slice(2) do |binding|
key, value = binding
name = key.unwrap
Crisp.eval_error "name of binding must be specified as symbol #{name}" unless name.is_a? Crisp::Symbol
new_env.set(name.str, eval(value, new_env))
end
ast, env = list[2], new_env
next # TCO
when "do"
if list.empty?
ast = Crisp::Expr.new nil
next
end
eval_ast(list[1..-2].to_crisp_value, env)
ast = list.last
next # TCO
when "if"
ast = unless eval(list[1], env).unwrap
list.size >= 4 ? list[3] : Crisp::Expr.new(nil)
else
list[2]
end
next # TCO
when "fn*"
params = list[1].unwrap
unless params.is_a? Array
Crisp.eval_error "'fn*' parameters must be list or vector: #{params}"
end
Crisp::Closure.new(list[2], params, env, func_of(env, params, list[2]))
when "quote"
list[1]
when "quasiquote"
ast = quasiquote list[1]
next # TCO
when "defmacro!"
Crisp.eval_error "wrong number of argument for 'defmacro!'" unless list.size == 3
a1 = list[1].unwrap
Crisp.eval_error "1st argument of 'defmacro!' must be symbol: #{a1}" unless a1.is_a? Crisp::Symbol
env.set(a1.str, eval(list[2], env).tap{|n| n.is_macro = true})
when "macroexpand"
macroexpand(list[1], env)
when "try*"
catch_list = list[2].unwrap
return eval(list[1], env) unless catch_list.is_a? Crisp::List
catch_head = catch_list.first.unwrap
return eval(list[1], env) unless catch_head.is_a? Crisp::Symbol
return eval(list[1], env) unless catch_head.str == "catch*"
begin
eval(list[1], env)
rescue e : Crisp::RuntimeException
new_env = Crisp::Env.new(env, [catch_list[1]], [e.thrown])
eval(catch_list[2], new_env)
rescue e
new_env = Crisp::Env.new(env, [catch_list[1]], [Crisp::Expr.new e.message])
eval(catch_list[2], new_env)
end
when "def!"
Crisp.eval_error "wrong number of argument for 'def!'" unless list.size == 3
a1 = list[1].unwrap
Crisp.eval_error "1st argument of 'def!' must be symbol: #{a1}" unless a1.is_a? Crisp::Symbol
env.set(a1.str, eval(list[2], env))
when "let*"
Crisp.eval_error "wrong number of argument for 'def!'" unless list.size == 3
bindings = list[1].unwrap
Crisp.eval_error "1st argument of 'let*' must be list or vector" unless bindings.is_a? Array
Crisp.eval_error "size of binding list must be even" unless bindings.size.even?
new_env = Crisp::Env.new env
bindings.each_slice(2) do |binding|
key, value = binding
name = key.unwrap
Crisp.eval_error "name of binding must be specified as symbol #{name}" unless name.is_a? Crisp::Symbol
new_env.set(name.str, eval(value, new_env))
end
ast, env = list[2], new_env
next # TCO
when "do"
if list.empty?
ast = Crisp::Expr.new nil
next
end
eval_ast(list[1..-2].to_crisp_value, env)
ast = list.last
next # TCO
when "if"
ast = unless eval(list[1], env).unwrap
list.size >= 4 ? list[3] : Crisp::Expr.new(nil)
else else
invoke_list(list, env)
list[2]
end end
next # TCO
when "fn*"
params = list[1].unwrap
unless params.is_a? Array
Crisp.eval_error "'fn*' parameters must be list or vector: #{params}"
end
Crisp::Closure.new(list[2], params, env, func_of(env, params, list[2]))
when "quote"
list[1]
when "quasiquote"
ast = quasiquote list[1]
next # TCO
when "defmacro!"
Crisp.eval_error "wrong number of argument for 'defmacro!'" unless list.size == 3
a1 = list[1].unwrap
Crisp.eval_error "1st argument of 'defmacro!' must be symbol: #{a1}" unless a1.is_a? Crisp::Symbol
env.set(a1.str, eval(list[2], env).tap { |n| n.is_macro = true })
when "macroexpand"
macroexpand(list[1], env)
when "try*"
catch_list = list[2].unwrap
return eval(list[1], env) unless catch_list.is_a? Crisp::List
catch_head = catch_list.first.unwrap
return eval(list[1], env) unless catch_head.is_a? Crisp::Symbol
return eval(list[1], env) unless catch_head.str == "catch*"
begin
eval(list[1], env)
rescue e : Crisp::RuntimeException
new_env = Crisp::Env.new(env, [catch_list[1]], [e.thrown])
eval(catch_list[2], new_env)
rescue e
new_env = Crisp::Env.new(env, [catch_list[1]], [Crisp::Expr.new e.message])
eval(catch_list[2], new_env)
end
else
invoke_list(list, env)
end
end end
end end
end end
end end

+ 4
- 2
src/crisp/expr.cr View File

@ -5,6 +5,7 @@ module Crisp
class Symbol class Symbol
property :str property :str
def initialize(@str : String) def initialize(@str : String)
end end
@ -26,6 +27,7 @@ module Crisp
class Atom class Atom
property :val property :val
def initialize(@val : Expr) def initialize(@val : Expr)
end end
@ -36,6 +38,7 @@ module Crisp
class Closure class Closure
property :ast, :params, :env, :fn property :ast, :params, :env, :fn
def initialize(@ast : Expr, @params : Array(Crisp::Expr), @env : Env, @fn : Func) def initialize(@ast : Expr, @params : Array(Crisp::Expr), @env : Env, @fn : Func)
end end
end end
@ -109,7 +112,6 @@ end
class Array class Array
def to_crisp_value(t = Crisp::List) def to_crisp_value(t = Crisp::List)
each_with_object(t.new){|e, l| l << e}
each_with_object(t.new) { |e, l| l << e }
end end
end end

+ 2
- 4
src/crisp/interpreter.cr View File

@ -10,15 +10,14 @@ require "./error"
require "./evaluator" require "./evaluator"
module Crisp module Crisp
class Interpreter class Interpreter
def initialize(args = nil) def initialize(args = nil)
@printer = Printer.new @printer = Printer.new
@evaluator = Evaluator.new @evaluator = Evaluator.new
@env = Crisp::Env.new @env = Crisp::Env.new
Crisp::NameSpace.each{|k,v| @env.set(k, Crisp::Expr.new(v))}
@env.set("eval", Crisp::Expr.new -> (args: Array(Crisp::Expr)){ @evaluator.eval(args[0], @env) })
Crisp::NameSpace.each { |k, v| @env.set(k, Crisp::Expr.new(v)) }
@env.set("eval", Crisp::Expr.new ->(args : Array(Crisp::Expr)) { @evaluator.eval(args[0], @env) })
eval_string "(def! not (fn* (a) (if a false true)))" eval_string "(def! not (fn* (a) (if a false true)))"
eval_string "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))" eval_string "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"
@ -73,4 +72,3 @@ module Crisp
end end
end end
end end

+ 7
- 7
src/crisp/printer.cr View File

@ -7,19 +7,19 @@ module Crisp
def print(value) def print(value)
case value case value
when Nil then "nil"
when Bool then value.to_s
when Int32 then value.to_s
when Crisp::List then "(#{value.map{|v| print(v).as String}.join(" ")})"
when Crisp::Vector then "[#{value.map{|v| print(v).as String}.join(" ")}]"
when Nil then "nil"
when Bool then value.to_s
when Int32 then value.to_s
when Crisp::List then "(#{value.map { |v| print(v).as String }.join(" ")})"
when Crisp::Vector then "[#{value.map { |v| print(v).as String }.join(" ")}]"
when Crisp::Symbol then value.str.to_s when Crisp::Symbol then value.str.to_s
when Crisp::Func then "<function>" when Crisp::Func then "<function>"
when Crisp::Closure then "<closure>" when Crisp::Closure then "<closure>"
when Crisp::HashMap when Crisp::HashMap
"{#{value.map{|k, v| "#{print(k)} #{print(v)}"}.join(" ")}}"
"{#{value.map { |k, v| "#{print(k)} #{print(v)}" }.join(" ")}}"
when String when String
case case
when value.empty?()
when value.empty?
@print_readably ? value.inspect : value @print_readably ? value.inspect : value
when value[0] == '\u029e' when value[0] == '\u029e'
":#{value[1..-1]}" ":#{value[1..-1]}"

+ 2
- 5
src/crisp/reader.cr View File

@ -33,7 +33,7 @@ module Crisp
def read_sequence(init, open, close) def read_sequence(init, open, close)
token = self.next token = self.next
Crisp.parse_error "expected '#{open}', got EOF" unless token Crisp.parse_error "expected '#{open}', got EOF" unless token
Crisp.parse_error "expected '#{open}', got #{token}" unless token[0] == open
Crisp.parse_error "expected '#{open}', got #{token}" unless token[0] == open
loop do loop do
token = peek token = peek
@ -119,12 +119,11 @@ module Crisp
else read_atom else read_atom
end end
end end
end end
def tokenize(str) def tokenize(str)
regex = /[\s,]*(~@|[\[\]{}()'`~^@]|"(?:\\.|[^\\"])*"|;.*|[^\s\[\]{}('"`,;)]*)/ regex = /[\s,]*(~@|[\[\]{}()'`~^@]|"(?:\\.|[^\\"])*"|;.*|[^\s\[\]{}('"`,;)]*)/
str.scan(regex).map{|m| m[1]}.reject(&.empty?)
str.scan(regex).map { |m| m[1] }.reject(&.empty?)
end end
def read_str(str) def read_str(str)
@ -137,6 +136,4 @@ module Crisp
end end
end end
end end
end end

Loading…
Cancel
Save