2015-07-08 22:14:05 +00:00
|
|
|
{car, cdr, cons, listp, nilp, nil,
|
|
|
|
list, pairp, listToString} = require 'cons-lists/lists'
|
2015-07-28 23:51:01 +00:00
|
|
|
{astObject} = require './astAccessors'
|
2015-07-30 14:29:39 +00:00
|
|
|
{Symbol} = require './reader_types'
|
2015-07-03 22:47:04 +00:00
|
|
|
|
|
|
|
# RICH_AST -> LISP_AST
|
2015-07-08 02:56:11 +00:00
|
|
|
|
2015-07-03 22:47:04 +00:00
|
|
|
normalizeForm = (form) ->
|
2015-07-30 14:29:39 +00:00
|
|
|
|
2015-07-03 22:47:04 +00:00
|
|
|
listToRecord1 = (l) ->
|
|
|
|
o = Object.create(null)
|
|
|
|
while(l != nil)
|
|
|
|
o[normalizeForm(car l)] = normalizeForm(car cdr l)
|
|
|
|
l = cdr cdr l
|
|
|
|
null
|
|
|
|
o
|
2015-07-08 22:14:05 +00:00
|
|
|
|
2015-07-03 22:47:04 +00:00
|
|
|
listToVector1 = (l) ->
|
|
|
|
while(l != nil) then p = normalizeForm(car l); l = cdr l; p
|
|
|
|
|
|
|
|
id = (a) -> a
|
|
|
|
|
2015-07-08 22:14:05 +00:00
|
|
|
methods =
|
2015-07-03 22:47:04 +00:00
|
|
|
'list': normalizeForms
|
|
|
|
'vector': (atom) -> listToVector1(atom)
|
|
|
|
'record': (atom) -> listToRecord1(atom)
|
|
|
|
|
|
|
|
# Basic native types. Meh.
|
2015-07-31 14:34:12 +00:00
|
|
|
'symbol': (id) -> new Symbol(id)
|
2015-07-03 22:47:04 +00:00
|
|
|
'number': id
|
2015-07-30 14:29:39 +00:00
|
|
|
'string': id
|
2015-07-03 22:47:04 +00:00
|
|
|
'nil': (atom) -> nil
|
2015-07-08 22:14:05 +00:00
|
|
|
|
2015-07-03 22:47:04 +00:00
|
|
|
# Values inherited from the VM.
|
|
|
|
'true': (atom) -> true
|
|
|
|
'false': (atom) -> false
|
|
|
|
'null': (atom) -> null
|
|
|
|
'undefined': (atom) -> undefined
|
|
|
|
|
2015-07-28 23:51:01 +00:00
|
|
|
methods[form.type](form.value)
|
2015-07-03 22:47:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
normalizeForms = (forms) ->
|
|
|
|
# Yes, this reifies the expectation than an empty list and 'nil' are
|
|
|
|
# the same.
|
|
|
|
return nil if nilp forms
|
2015-07-08 02:56:11 +00:00
|
|
|
|
|
|
|
# Handle dotted list.
|
|
|
|
if (astObject forms)
|
|
|
|
return normalizeForm(forms)
|
2015-07-03 22:47:04 +00:00
|
|
|
cons(normalizeForm(car forms), normalizeForms(cdr forms))
|
2015-07-08 22:14:05 +00:00
|
|
|
|
2015-07-03 22:47:04 +00:00
|
|
|
module.exports =
|
|
|
|
normalizeForm: normalizeForm
|
|
|
|
normalizeForms: normalizeForms
|
|
|
|
|