 		when 'ObjectExpression'
 			for prop in block.properties
 				treeify prop.value, scope
+		when 'Program'
+			# Program creates a new scope
+			scope = new Tree null, new Scope
+			treeify block.body, scope
 		when 'VariableDeclarator'
 			treeify block.id, scope, true
 			treeify block.init, scope

editor.getSession().setMode 'ace/mode/javascript'
editor.setShowPrintMargin false
editor.getSession().on 'change', (event) ->
	try
		display treeify(parse editor.getValue()).toString()

output = ace.edit 'output'
output.setTheme 'ace/theme/monokai'


parse = (code) ->
	try
		esprima.parse code,
			range: true
	catch err
		{ 'Parse error': err }
		{}


display = (data) ->
	# try
	# 	data = JSON.stringify(data, null, 2) unless typeof data == 'string'
	# catch err
	# 	data = { 'JSON error': err }
	output.setValue(JSON.stringify(data, null, 2))
	output.clearSelection()



Syntax =
