Module: Lasp

Defined in:
lib/lasp.rb,
lib/lasp/fn.rb,
lib/lasp/env.rb,
lib/lasp/repl.rb,
lib/lasp/macro.rb,
lib/lasp/errors.rb,
lib/lasp/params.rb,
lib/lasp/parser.rb,
lib/lasp/corelib.rb,
lib/lasp/version.rb,
lib/lasp/interpreter.rb,
lib/lasp/params_builder.rb,
lib/lasp/variadic_params.rb

Defined Under Namespace

Classes: Env, Fn, Interpreter, Macro, Params, ParamsBuilder, Parser, Repl, VariadicParams

Constant Summary collapse

STDLIB_PATH =
File.expand_path("../lasp/stdlib.lasp", __FILE__)
LaspError =
Class.new(StandardError)
SyntaxError =
Class.new(LaspError)
ArgumentError =
Class.new(LaspError)
NameError =
Class.new(LaspError)
CORELIB =
{
  :+          => -> (*args)         { args.reduce(:+)                            },
  :-          => -> (*args)         { args.reduce(:-)                            },
  :*          => -> (*args)         { args.reduce(:*)                            },
  :/          => -> (*args)         { args.reduce(:/)                            },
  :<          => -> (*args)         { args.each_cons(2).all? { |a, b| a < b  }   },
  :>          => -> (*args)         { args.each_cons(2).all? { |a, b| a > b  }   },
  :<=         => -> (*args)         { args.each_cons(2).all? { |a, b| a <= b }   },
  :>=         => -> (*args)         { args.each_cons(2).all? { |a, b| a >= b }   },
  :"="        => -> (*args)         { args.uniq.count == 1                       },
  :list       => -> (*args)         { args                                       },
  :head       => -> (list)          { list.first                                 },
  :tail       => -> (list)          { list.drop(1)                               },
  :cons       => -> (item, list)    { [item] + list                              },
  :dict       => -> (*args)         { Hash[*args]                                },
  :get        => -> (key, a)        { a[key]                                     },
  :assoc      => -> (a, key, val)   { a.dup.tap { |a| a[key] = val  }            },
  :dissoc     => -> (a, key)        { a.dup.tap { |a| a.delete(key) }            },
  :not        => -> (arg)           { !arg                                       },
  :print      => -> (*output)       { STDOUT.print(*output)                      },
  :readln     => -> ()              { STDIN.gets.chomp                           },
  :apply      => -> (f, list)       { f.call(*list)                              },
  :send       => -> (m, o, *args)   { o.public_send(m, *args)                    },
}
VERSION =
"0.12.0"

Class Method Summary collapse

Class Method Details

.env_with_corelibObject



24
25
26
# File 'lib/lasp.rb', line 24

def env_with_corelib
  Env.new(CORELIB.dup)
end

.env_with_stdlibObject



28
29
30
31
32
# File 'lib/lasp.rb', line 28

def env_with_stdlib
  env_with_corelib.tap do |env|
    Lasp::execute_file(STDLIB_PATH, env)
  end
end

.execute(program, env = env_with_corelib) ⇒ Object



13
14
15
# File 'lib/lasp.rb', line 13

def execute(program, env = env_with_corelib)
  Interpreter.eval(Parser.parse(program), env)
end

.execute_file(path, env = env_with_corelib) ⇒ Object



17
18
19
20
21
22
# File 'lib/lasp.rb', line 17

def execute_file(path, env = env_with_corelib)
  env[:__FILE__] = path
  result = execute("(do #{File.read(path)})", env)
  env[:__FILE__] = nil
  result
end