2014-11-09 01:56:36 +03:00
|
|
|
types = require "./types.coffee"
|
|
|
|
|
|
|
|
# Env
|
|
|
|
exports.Env = class Env
|
|
|
|
constructor: (@outer=null, @binds=[], @exprs=[]) ->
|
|
|
|
@data = {}
|
|
|
|
if @binds.length > 0
|
|
|
|
for b,i in @binds
|
|
|
|
if types._symbol_Q(b) && b.name == "&"
|
2015-03-15 01:14:32 +03:00
|
|
|
@data[@binds[i+1].name] = @exprs[i..]
|
2014-11-09 01:56:36 +03:00
|
|
|
break
|
|
|
|
else
|
2015-03-15 01:14:32 +03:00
|
|
|
@data[b.name] = @exprs[i]
|
2014-11-09 01:56:36 +03:00
|
|
|
find: (key) ->
|
2014-12-19 05:33:49 +03:00
|
|
|
if not types._symbol_Q(key)
|
|
|
|
throw new Error("env.find key must be symbol")
|
|
|
|
if key.name of @data then @
|
2014-11-09 01:56:36 +03:00
|
|
|
else if @outer then @outer.find(key)
|
|
|
|
else null
|
2014-12-19 05:33:49 +03:00
|
|
|
set: (key, value) ->
|
|
|
|
if not types._symbol_Q(key)
|
|
|
|
throw new Error("env.set key must be symbol")
|
|
|
|
@data[key.name] = value
|
2014-11-09 01:56:36 +03:00
|
|
|
get: (key) ->
|
2014-12-19 05:33:49 +03:00
|
|
|
if not types._symbol_Q(key)
|
|
|
|
throw new Error("env.get key must be symbol")
|
2014-11-09 01:56:36 +03:00
|
|
|
env = @find(key)
|
2014-12-19 05:33:49 +03:00
|
|
|
throw new Error("'" + key.name + "' not found") if !env
|
|
|
|
env.data[key.name]
|
2014-11-09 01:56:36 +03:00
|
|
|
|
|
|
|
# vim: ts=2:sw=2
|