Beispiel #1
0
def literal(value): return make_ast(LITERAL, value)
def list_(*values): return make_ast(LIST, list(values))


# META-II compiler for a simple s-expression language, it generates AST
# objects using the helper functions above when evaluated.
cola_machine = comp(r'''

  .SYNTAX PROGRAM

  PROGRAM = .OUT('(') args .OUT(')') '.' ;

  args = $ term ;

  term = ( list | literal | symbol ) .OUT(', ') ;

  list = '(' .OUT('list_(') args ')' .OUT(')') ;

  literal = ( .STRING | .NUMBER ) .OUT('literal('*')') ;

  symbol = .ID .OUT('symbol("'*'")') ;

  .END

''', open('metaii.asm').read())


#########################################################################
#########################################################################
#########################################################################
Beispiel #2
0
send(vtvt, 'addMethod', 'makeKind', lambda context, kind: make_kind(kind))
send(vtvt, 'addMethod', 'makeAst', lambda context, KIND, value: make_ast(KIND, value))


# META-II compiler for a simple s-expression language, it generates AST
# objects using the helper functions above when evaluated.
cola_machine = comp(r'''

  .SYNTAX PROGRAM

  PROGRAM = .OUT('(') args .OUT(')') '.' ;

  args = $ term ;

  term = ( list | literal | symbol ) .OUT(', ') ;

  list = '(' .OUT('list_(') args ')' .OUT(')') ;

  literal = ( .STRING | .NUMBER ) .OUT('literal('*')') ;

  symbol = .ID .OUT('symbol("'*'")') ;

  .END

''', open('metaii.asm').read())


#########################################################################
#########################################################################
#########################################################################
Beispiel #3
0
    return make_ast(LIST, list(values))


# META-II compiler for a simple s-expression language, it generates AST
# objects using the helper functions above when evaluated.
cola_machine = comp(
    r'''

  .SYNTAX PROGRAM

  PROGRAM = .OUT('(') args .OUT(')') '.' ;

  args = $ term ;

  term = ( list | literal | symbol ) .OUT(', ') ;

  list = '(' .OUT('list_(') args ')' .OUT(')') ;

  literal = ( .STRING | .NUMBER ) .OUT('literal('*')') ;

  symbol = .ID .OUT('symbol("'*'")') ;

  .END

''',
    open('metaii.asm').read())

#########################################################################
#########################################################################
#########################################################################
Beispiel #4
0
def compile_(source, compiler=cola_machine):
  body = 'def a():\n' + comp(source, cola_machine)
  if __debug__:
    print body
  exec body
  return a
Beispiel #5
0
  symbol = .ID .OUT(*) ;

  new_list = '+*' .OUT('[]') ;

  term = sending | literal | symbol | new_list ;

  store = .ID .OUT(*' = \') ':=' term ;

  it = store | sending ;

  PROGRAM = it $ it '.' .OUT('return locals()') ;

  .END

'''
cola_machine = comp(cola_metaii, open('metaii.asm').read())


def evaluate(it):
  if isinstance(it, tuple) and len(it) > 0 and callable(it[0]):
    return it[0](*map(evaluate, it[1:]))
  return it


def compile_(source, compiler=cola_machine):
  body = 'def a():\n' + comp(source, cola_machine)
  if __debug__:
    print body
  exec body
  return a
send(vtvt, 'addMethod', 'makeAst',
     lambda context, KIND, value: make_ast(KIND, value))

# META-II compiler for a simple s-expression language, it generates AST
# objects using the helper functions above when evaluated.
cola_machine = comp(
    r'''

  .SYNTAX PROGRAM

  PROGRAM = .OUT('(') args .OUT(')') '.' ;

  args = $ term ;

  term = ( list | literal | symbol ) .OUT(', ') ;

  list = '(' .OUT('list_(') args ')' .OUT(')') ;

  literal = ( .STRING | .NUMBER ) .OUT('literal('*')') ;

  symbol = .ID .OUT('symbol("'*'")') ;

  .END

''',
    open('metaii.asm').read())

#########################################################################
#########################################################################
#########################################################################
def compile_(source, compiler=cola_machine):
    body = 'def a():\n' + comp(source, cola_machine)
    if __debug__:
        print body
    exec body
    return a
  symbol = .ID .OUT(*) ;

  new_list = '+*' .OUT('[]') ;

  term = sending | literal | symbol | new_list ;

  store = .ID .OUT(*' = \') ':=' term ;

  it = store | sending ;

  PROGRAM = it $ it '.' .OUT('return locals()') ;

  .END

'''
cola_machine = comp(cola_metaii, open('metaii.asm').read())


def evaluate(it):
    if isinstance(it, tuple) and len(it) > 0 and callable(it[0]):
        return it[0](*map(evaluate, it[1:]))
    return it


def compile_(source, compiler=cola_machine):
    body = 'def a():\n' + comp(source, cola_machine)
    if __debug__:
        print body
    exec body
    return a