Skip to content

Instantly share code, notes, and snippets.

@tim-smart
Created February 28, 2010 04:59
Show Gist options
  • Select an option

  • Save tim-smart/317224 to your computer and use it in GitHub Desktop.

Select an option

Save tim-smart/317224 to your computer and use it in GitHub Desktop.
# A class literal, including optional superclass and constructor.
exports.ClassNode: class ClassNode extends BaseNode
type: 'Class'
constructor: (variable, parent, props) ->
@children: compact flatten [@variable: variable, @parent: parent, @properties: props or []]
compile_node: (o) ->
extension: @parent and new ExtendsNode(@variable, @parent)
constructor: null
props: new Expressions()
o.top: true
ret: del o, 'returns'
for prop in @properties
if prop.variable and prop.variable.base.value is 'constructor'
func: prop.value
func.body.push(new ReturnNode(new LiteralNode('this')))
constructor: new AssignNode(@variable, func)
else
if prop.variable
val: new ValueNode(@variable, [new AccessorNode(prop.variable, 'prototype')])
prop: new AssignNode(val, prop.value)
props.push prop
constructor: if constructor then constructor
else if extension
# TODO: Add call to parent constructor
new AssignNode(@variable, new CodeNode())
else
new AssignNode(@variable, new CodeNode())
construct: @idt() + constructor.compile(o) + ';\n'
props: if props.empty() then '' else props.compile(o) + '\n'
extension: if extension then @idt() + extension.compile(o) + ';\n' else ''
returns: if ret then '\n' + @idt() + 'return ' + @variable.compile(o) + ';' else ''
construct + extension + props + returns
statement ClassNode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment