Esempio n. 1
0
def eval_seq(ast, context):
    context.indent += 3
    print ' ' * context.indent, '/----\\'
    for item in ast.data:
        send(item, 'eval', context)
    print ' ' * context.indent, '\\____/'
    context.indent -= 3
Esempio n. 2
0
def eval_seq(ast, context):
  context.indent += 3
  print ' ' * context.indent, '/----\\'
  for item in ast.data:
    send(item, 'eval', context)
  print ' ' * context.indent, '\\____/'
  context.indent -= 3
Esempio n. 3
0
 def bracket(context):
     send(context, 'addMethod', 'collecting', True)
     b = send(context, 'lookup', 'basket')
     f(context)
     if send(context, 'lookup', 'success'):
         send(context, 'push', post_process(''.join(b)))
     del b[:]
     send(context, 'addMethod', 'collecting', False)
Esempio n. 4
0
 def bracket(context):
   send(context, 'addMethod', 'collecting', True)
   b = send(context, 'lookup', 'basket')
   f(context)
   if send(context, 'lookup', 'success'):
     send(context, 'push', post_process(''.join(b)))
   del b[:]
   send(context, 'addMethod', 'collecting', False)
Esempio n. 5
0
def evaluate(ast, context):
    sname = name_of_symbol_of(ast)

    if sname == 'symbol':
        try:
            return send(context, 'lookup', ast.data)
        except:
            return str(ast.data) + '?'

    if sname == 'literal':
        return ast.data

    first, rest = ast.data[0], ast.data[1:]

    sname = name_of_symbol_of(first)
    if sname == 'symbol':

        if first.data == 'define':
            define(rest, context)
            return

        if first.data == 'lambda':
            return make_lambda_ast(rest, context)

    exp = tuple(evaluate(it, context) for it in ast.data)
    if callable(exp[0]):
        return exp[0](*exp[1:])

    return exp
Esempio n. 6
0
def evaluate(ast, context):
  sname = name_of_symbol_of(ast)

  if sname == 'symbol':
    try: return send(context, 'lookup', ast.data)
    except: return str(ast.data) + '?'

  if sname == 'literal':
    return ast.data

  first, rest = ast.data[0], ast.data[1:]

  sname = name_of_symbol_of(first)
  if sname == 'symbol':

    if first.data == 'define':
      define(rest, context)
      return

    if first.data == 'lambda':
      return make_lambda_ast(rest, context)

  exp = tuple(evaluate(it, context) for it in ast.data)
  if callable(exp[0]):
    return exp[0](*exp[1:])

  return exp
Esempio n. 7
0
def advance(context):
  send(context, 'collect')
  stream = send(context, 'lookup', 'stream')
  try:
    char = stream.next()
  except StopIteration:
    if not send(context, 'lookup', 'success'):
      raise
    else:
      send(context, 'addMethod', 'current', '')
  else:
    send(context, 'addMethod', 'current', char)
Esempio n. 8
0
def advance(context):
    send(context, 'collect')
    stream = send(context, 'lookup', 'stream')
    try:
        char = stream.next()
    except StopIteration:
        if not send(context, 'lookup', 'success'):
            raise
        else:
            send(context, 'addMethod', 'current', '')
    else:
        send(context, 'addMethod', 'current', char)
Esempio n. 9
0
def evaluate_list_(ast, context):
  first = ast.data[0]
  sname = name_of_symbol_of(first)
  if sname == 'symbol' and first.data in ('define', 'lambda'):
    return evaluate_special(ast, context)

  send(context, 'start_frame')
  for inner_ast in ast.data:
    send(inner_ast, 'eval', context)
  send(context, 'finish_frame')

  current_frame = send(context, 'lookup', 'current_frame')
  exp = current_frame.pop().data
  if callable(exp[0]):
    result = exp[0](*exp[1:])
  else:
    result = exp
  current_frame.append(result)
Esempio n. 10
0
def evaluate_list_(ast, context):
    first = ast.data[0]
    sname = name_of_symbol_of(first)
    if sname == 'symbol' and first.data in ('define', 'lambda'):
        return evaluate_special(ast, context)

    send(context, 'start_frame')
    for inner_ast in ast.data:
        send(inner_ast, 'eval', context)
    send(context, 'finish_frame')

    current_frame = send(context, 'lookup', 'current_frame')
    exp = current_frame.pop().data
    if callable(exp[0]):
        result = exp[0](*exp[1:])
    else:
        result = exp
    current_frame.append(result)
Esempio n. 11
0
        if first.data == 'define':
            define(rest, context)
            return

        if first.data == 'lambda':
            return make_lambda_ast(rest, context)

    exp = tuple(evaluate(it, context) for it in ast.data)
    if callable(exp[0]):
        return exp[0](*exp[1:])

    return exp


def define((var, exp), context):
    send(context, 'addMethod', var.data, evaluate(exp, context))
    return


def make_lambda_ast((variables, exp), context):
    variables = tuple(v.data for v in variables.data)
    exp = list_(*exp.data)

    def inner(*args):
        new_context = send(context, 'delegated')
        for k, v in zip(variables, args):
            send(new_context, 'addMethod', k, v)
        return evaluate(exp, new_context)

    return inner
Esempio n. 12
0
def Eval(tree, context):
    symbol = send(tree, 'typeOf')
    closure = send(symbol, 'transform', context)
    new_tree = closure(tree, context)
    if new_tree is not None:
        return send(new_tree, 'eval', context)
Esempio n. 13
0
def prep_eval_context(context):
    send(context, 'addMethod', 'lambda', evaluate_lambda)
    send(context, 'addMethod', 'define', evaluate_define)
    send(context, 'addMethod', 'literal', evaluate_literal)
    send(context, 'addMethod', 'symbol', evaluate_symbol)
    send(context, 'addMethod', 'list', evaluate_list_)
Esempio n. 14
0
def name_of_symbol_of(ast): return send(send(ast, 'typeOf'), 'getName')


# Some AST symbol types.
SYMBOL = make_kind('symbol')
Esempio n. 15
0
def finish_frame(context):
  frame_stack = send(context, 'lookup', 'frame_stack')
  current_frame = send(context, 'lookup', 'current_frame')
  frame_stack[-1].append(list_(*current_frame))
  send(context, 'addMethod', 'current_frame', frame_stack.pop())
Esempio n. 16
0
def name_of_symbol_of(ast):
    return send(send(ast, 'typeOf'), 'getName')
Esempio n. 17
0
def delegated(vt):
    return send(vt, 'delegated')
Esempio n. 18
0
def finish_frame(context):
    frame_stack = send(context, 'lookup', 'frame_stack')
    current_frame = send(context, 'lookup', 'current_frame')
    frame_stack[-1].append(list_(*current_frame))
    send(context, 'addMethod', 'current_frame', frame_stack.pop())
Esempio n. 19
0
def finish_frame(context):
    send(context, 'finish_frame')
Esempio n. 20
0
def push(context, term):
    send(context, 'lookup', 'current_frame').append(term)
Esempio n. 21
0
def start_frame(context):
    frame_stack = send(context, 'lookup', 'frame_stack')
    current_frame = send(context, 'lookup', 'current_frame')
    frame_stack.append(current_frame)
    send(context, 'addMethod', 'current_frame', [])
Esempio n. 22
0
def collect(context):
    if send(context, 'lookup', 'collecting'):
        basket = send(context, 'lookup', 'basket')
        current = send(context, 'lookup', 'current')
        basket.append(current)
Esempio n. 23
0
def evaluate_symbol(ast, context):
    try:
        it = send(context, 'lookup', ast.data)
    except:
        it = str(ast.data) + '?'
    _append_result(context, it)
Esempio n. 24
0
def prep_emit_context(context):
    send(context, 'addMethod', 'literal', emit_lit)
    send(context, 'addMethod', 'symbol', emit_word)
    send(context, 'addMethod', 'list', emit_seq)
    context.indent = 0
Esempio n. 25
0
def allocate(vt):
    return send(vt, 'allocate')
Esempio n. 26
0
def start_frame(context):
  send(context, 'start_frame')
Esempio n. 27
0
def make_kind(kind):
    return send(allocate(symbol_vt), 'setName', kind)
Esempio n. 28
0
def delegated(vt):
  return send(vt, 'delegated')
Esempio n. 29
0
    if first.data == 'define':
      define(rest, context)
      return

    if first.data == 'lambda':
      return make_lambda_ast(rest, context)

  exp = tuple(evaluate(it, context) for it in ast.data)
  if callable(exp[0]):
    return exp[0](*exp[1:])

  return exp


def define((var, exp), context):
  send(context, 'addMethod', var.data, evaluate(exp, context))
  return


def make_lambda_ast((variables, exp), context):
  variables = tuple(v.data for v in variables.data)
  exp = list_(*exp.data)

  def inner(*args):
    new_context = send(context, 'delegated')
    for k, v in zip(variables, args):
      send(new_context, 'addMethod', k, v)
    return evaluate(exp, new_context)

  return inner
Esempio n. 30
0
def finish_frame(context):
  send(context, 'finish_frame')
Esempio n. 31
0
def make_kind(kind): return send(allocate(symbol_vt), 'setName', kind)
def make_ast(KIND, value): return send(allocate(ast_vt), 'init', KIND, value)
Esempio n. 32
0
def init_ast(ast, symbol, value):
    ast.data = value
    send(symbol, 'setSymbol', ast)
    return ast
Esempio n. 33
0
def _append_result(context, result):
    current_frame = send(context, 'lookup', 'current_frame')
    current_frame.append(result)
Esempio n. 34
0
def Eval(tree, context):
    symbol = send(tree, 'typeOf')
    closure = send(symbol, 'transform', context)
    new_tree = closure(tree, context)
    if new_tree is not None:
        return send(new_tree, 'eval', context)
Esempio n. 35
0
from co import bootstrap, send
from la import setUpTransformEngine


object_vt, vtvt = bootstrap()
symbol_vt, ast_vt = setUpTransformEngine(object_vt, vtvt)


def p(*a):
  print a


context_vt = send(vtvt, 'delegated')
send(context_vt, 'addMethod', 'bil', lambda *args: p(args[0] is c))

c = send(context_vt, 'allocate')
send(c, 'bil')
##send(i, 'bil')



'''

     [context addMethod [[context allocate] makekind 'symbol']  'SYMBOL']
     [[context allocate] makeast [context lookup 'SYMBOL'] 'Barry']

     [ context addMethod 'twentythree' 23 ]
     ( twentythree )
     ( ooo [ context addMethod 'g' [context lookup 'twentythree'] ])

     (define p (divide 1 1000000000))
Esempio n. 36
0
def init_ast(ast, symbol, value):
    ast.data = value
    send(symbol, 'setSymbol', ast)
    return ast
Esempio n. 37
0
def transform(symbol, context):
    return send(context, 'lookup', send(symbol, 'getName'))
Esempio n. 38
0
def evaluate_define(ast, context):
    var, exp = ast.data
    send(exp, 'eval', context)
    current_frame = send(context, 'lookup', 'current_frame')
    send(context, 'addMethod', var.data, current_frame.pop())
Esempio n. 39
0
def setUpTransformEngine(object_vt, vtvt):

    # Create a Symbol object type.
    symbol_vt = send(vtvt, 'delegated')

    # Create ast object type.
    ast_vt = send(object_vt, 'delegated')

    send(object_vt, 'addMethod', 'eval', Eval)
    send(object_vt, 'addMethod', 'transform', Eval)
    send(object_vt, 'addMethod', 'typeOf', getSymbol)

    send(symbol_vt, 'addMethod', 'setName', setName)
    send(symbol_vt, 'addMethod', 'getName', getName)
    send(symbol_vt, 'addMethod', 'setSymbol', setSymbol)
    send(symbol_vt, 'addMethod', 'transform', transform)

    send(ast_vt, 'addMethod', 'init', init_ast)

    return symbol_vt, ast_vt
Esempio n. 40
0
def make_ast(KIND, value):
  return send(allocate(ast_vt), 'init', KIND, value)
Esempio n. 41
0
def transform(symbol, context):
    return send(context, 'lookup', send(symbol, 'getName'))
Esempio n. 42
0
def _append_result(context, result):
  current_frame = send(context, 'lookup', 'current_frame')
  current_frame.append(result)
Esempio n. 43
0
def setUpTransformEngine(object_vt, vtvt):

    # Create a Symbol object type.
    symbol_vt = send(vtvt, 'delegated')

    # Create ast object type.
    ast_vt = send(object_vt, 'delegated')

    send(object_vt, 'addMethod', 'eval', Eval)
    send(object_vt, 'addMethod', 'transform', Eval)
    send(object_vt, 'addMethod', 'typeOf', getSymbol)

    send(symbol_vt, 'addMethod', 'setName', setName)
    send(symbol_vt, 'addMethod', 'getName', getName)
    send(symbol_vt, 'addMethod', 'setSymbol', setSymbol)
    send(symbol_vt, 'addMethod', 'transform', transform)

    send(ast_vt, 'addMethod', 'init', init_ast)

    return symbol_vt, ast_vt
Esempio n. 44
0
def evaluate_symbol(ast, context):
  try:
    it = send(context, 'lookup', ast.data)
  except:
    it = str(ast.data) + '?'
  _append_result(context, it)
Esempio n. 45
0
 def inner(*args):
     new_context = send(context, 'delegated')
     for k, v in zip(variables, args):
         send(new_context, 'addMethod', k, v)
     return evaluate(exp, new_context)
Esempio n. 46
0
def name_of_symbol_of(ast):
  return send(send(ast, 'typeOf'), 'getName')
Esempio n. 47
0
def start_frame(context):
    send(context, 'start_frame')
Esempio n. 48
0
def evaluate_define(ast, context):
  var, exp = ast.data
  send(exp, 'eval', context)
  current_frame = send(context, 'lookup', 'current_frame')
  send(context, 'addMethod', var.data, current_frame.pop())
Esempio n. 49
0
def make_ast(KIND, value):
    return send(allocate(ast_vt), 'init', KIND, value)
Esempio n. 50
0
def prep_eval_context(context):
  send(context, 'addMethod', 'lambda', evaluate_lambda)
  send(context, 'addMethod', 'define', evaluate_define)
  send(context, 'addMethod', 'literal', evaluate_literal)
  send(context, 'addMethod', 'symbol', evaluate_symbol)
  send(context, 'addMethod', 'list', evaluate_list_)
Esempio n. 51
0
 def kst(context):
     while send(context, 'lookup', 'success'):
         term(context)
     send(context, 'addMethod', 'success', True)
Esempio n. 52
0
def prep_emit_context(context):
  send(context, 'addMethod', 'literal', emit_lit)
  send(context, 'addMethod', 'symbol', emit_word)
  send(context, 'addMethod', 'list', emit_seq)
  context.indent = 0
Esempio n. 53
0
 def inner(*args):
   new_context = send(context, 'delegated')
   for k, v in zip(variables, args):
     send(new_context, 'addMethod', k, v)
   return evaluate(exp, new_context)
Esempio n. 54
0
def OR(context):
    if send(context, 'lookup', 'success'):
        raise PopFrame
    send(context, 'addMethod', 'success', True)
Esempio n. 55
0
def allocate(vt): return send(vt, 'allocate')
def make_kind(kind): return send(allocate(symbol_vt), 'setName', kind)
Esempio n. 56
0
def collect(context):
  if send(context, 'lookup', 'collecting'):
    basket = send(context, 'lookup', 'basket')
    current = send(context, 'lookup', 'current')
    basket.append(current)
Esempio n. 57
0
def make_ast(KIND, value): return send(allocate(ast_vt), 'init', KIND, value)
def name_of_symbol_of(ast): return send(send(ast, 'typeOf'), 'getName')
Esempio n. 58
0
def push(context, term):
  send(context, 'lookup', 'current_frame').append(term)
Esempio n. 59
0
 def tok(context):
     if start <= send(context, 'lookup', 'current') <= stop:
         send(context, 'advance')
     else:
         send(context, 'addMethod', 'success', False)
Esempio n. 60
0
def start_frame(context):
  frame_stack = send(context, 'lookup', 'frame_stack')
  current_frame = send(context, 'lookup', 'current_frame')
  frame_stack.append(current_frame)
  send(context, 'addMethod', 'current_frame', [])