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

Defined Under Namespace

Classes: Fn, Interpreter, Macro, Params, Parser, Repl

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)                              },
  :"."        => -> (obj, m, *args) { obj.send(m, *args)                         },
  :require    => -> (p)             { execute_file(File.expand_path(p, __dir__)) },
}
VERSION =
"0.10.0"

Class Method Summary collapse

Class Method Details

.execute(program, env = global_env) ⇒ Object



16
17
18
# File 'lib/lasp.rb', line 16

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

.execute_file(path) ⇒ Object



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

def execute_file(path)
  execute("(do #{File.read(path)})")
end

.global_envObject



6
7
8
# File 'lib/lasp/env.rb', line 6

def global_env
  @global_env ||= {}.merge(CORELIB)
end

.load_stdlib!Object



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

def load_stdlib!
  Lasp::execute_file(STDLIB_PATH)
end