<commit_before>#!/usr/bin/env python

from asm import *

class Program:
  def __init__(self, address):
    self.comment = 0
    self.lineNumber, self.filename = 0, None
    self.blocks, self.block = [0], 1
    self.loops = {} # block -> address of last do
    self.conds = {} # block -> address of continuation

    # Configure for start address
    self.vPC = address
    ld(val(address&0xff),regX)
    ld(val(address>>8), regY)

  def thisBlock(self):
    return self.blocks[-1]

  def line(self, line):
    """Process a line by tokenizing and processing the words"""

    self.lineNumber += 1
    nextWord = ''

    for nextChar in line:
      if self.comment > 0:
        # Inside comments anything goes
        if nextChar == '{': self.comment += 1
        if nextChar == '}': self.comment -= 1
      elif nextChar not in '{}[]()':
        if nextChar.isspace():
          self.word(nextWord)
          nextWord = ''
        else:
          nextWord += nextChar
      else:
        self.word(nextWord)
        nextWord = ''
        if nextChar == '{': self.comment += 1
        elif nextChar == '}': self.error('Spurious %s' % repr(nextChar))
        elif nextChar == '[': self.blocks.append(self.block); self.block +=  1
        elif nextChar == ']':
          if len(self.blocks) <= 1:
            self.error('Unexpected %s' % repr(nextChar))
          block = self.blocks.pop()
          if block in self.conds:
            # There was an if-statement in this block
            # Define the label to jump here
            define('$if.%d.%d' % (block, self.conds[block]), prev(self.vPC))
            del self.conds[block]
        elif nextChar == '(': pass
        elif nextChar == ')': pass
    self.word(nextWord)

  def getAddress(self, var):
    if len(var) > 1:
      self.error('Name too long %s' % repr(var))
    o = ord(var[0]) - ord('A')
    return 0x81 + 2*o # XXX Proper allocation

  def emit(self, byte):
    """Next program byte in RAM"""
    st(val(byte), eaYXregOUTIX) # XXX Use ROM tables
    self.vPC += 1

  def word(self, word):
    """Process a word and emit its code"""
    if len(word) == 0:
      return

    if word == 'loop':
      to = self.loops[self.thisBlock()]
      to = prev(to)
      if self.vPC>>8 != to>>8:
        self.error('Loop outside page' % repr(word))
      self.emit(lo('BRA'))
      self.emit(to&0xff)
    elif word == 'do':
      self.loops[self.thisBlock()] = self.vPC
    elif word == 'if<0':
      self.emit(lo('SIGNW'))
      self.emit(lo('BGE'))
      block = self.thisBlock()
      self.emit(lo('$if.%d.0' % block))
      self.conds[block] = 0
    elif word == 'if>0':
      self.emit(lo('SIGNW'))
      self.emit(lo('BLE'))
      block = self.thisBlock()
      self.emit(lo('$if.%d.0' % block))
      self.conds[block] = 0
    elif word == 'if<0':
      self.emit(lo('SIGNW'))
      self.emit(lo('BGE'))
      block = self.thisBlock()
      self.emit(lo('$if.%d.0' % block))
      self.conds[block] = 0
    elif word == 'if<>0':
      self.emit(lo('SIGNW'))
      self.emit(lo('BEQ'))
      block = self.thisBlock()
      self.emit(lo('$if.%d.0' % block))
      self.conds[block] = 0
    elif word == 'if>=0':
      self.emit(lo('SIGNW'))<commit_msg>Add if=0<commit_after>#!/usr/bin/env python

from asm import *

class Program:
  def __init__(self, address):
    self.comment = 0
    self.lineNumber, self.filename = 0, None
    self.blocks, self.block = [0], 1
    self.loops = {} # block -> address of last do
    self.conds = {} # block -> address of continuation

    # Configure for start address
    self.vPC = address
    ld(val(address&0xff),regX)
    ld(val(address>>8), regY)

  def thisBlock(self):
    return self.blocks[-1]

  def line(self, line):
    """Process a line by tokenizing and processing the words"""

    self.lineNumber += 1
    nextWord = ''

    for nextChar in line:
      if self.comment > 0:
        # Inside comments anything goes
        if nextChar == '{': self.comment += 1
        if nextChar == '}': self.comment -= 1
      elif nextChar not in '{}[]()':
        if nextChar.isspace():
          self.word(nextWord)
          nextWord = ''
        else:
          nextWord += nextChar
      else:
        self.word(nextWord)
        nextWord = ''
        if nextChar == '{': self.comment += 1
        elif nextChar == '}': self.error('Spurious %s' % repr(nextChar))
        elif nextChar == '[': self.blocks.append(self.block); self.block +=  1
        elif nextChar == ']':
          if len(self.blocks) <= 1:
            self.error('Unexpected %s' % repr(nextChar))
          block = self.blocks.pop()
          if block in self.conds:
            # There was an if-statement in this block
            # Define the label to jump here
            define('$if.%d.%d' % (block, self.conds[block]), prev(self.vPC))
            del self.conds[block]
        elif nextChar == '(': pass
        elif nextChar == ')': pass
    self.word(nextWord)

  def getAddress(self, var):
    if len(var) > 1:
      self.error('Name too long %s' % repr(var))
    o = ord(var[0]) - ord('A')
    return 0x81 + 2*o # XXX Proper allocation

  def emit(self, byte):
    """Next program byte in RAM"""
    st(val(byte), eaYXregOUTIX) # XXX Use ROM tables
    self.vPC += 1

  def word(self, word):
    """Process a word and emit its code"""
    if len(word) == 0:
      return

    if word == 'loop':
      to = self.loops[self.thisBlock()]
      to = prev(to)
      if self.vPC>>8 != to>>8:
        self.error('Loop outside page' % repr(word))
      self.emit(lo('BRA'))
      self.emit(to&0xff)
    elif word == 'do':
      self.loops[self.thisBlock()] = self.vPC
    elif word == 'if<0':
      self.emit(lo('SIGNW'))
      self.emit(lo('BGE'))
      block = self.thisBlock()
      self.emit(lo('$if.%d.0' % block))
      self.conds[block] = 0
    elif word == 'if>0':
      self.emit(lo('SIGNW'))
      self.emit(lo('BLE'))
      block = self.thisBlock()
      self.emit(lo('$if.%d.0' % block))
      self.conds[block] = 0
    elif word == 'if<0':
      self.emit(lo('SIGNW'))
      self.emit(lo('BGE'))
      block = self.thisBlock()
      self.emit(lo('$if.%d.0' % block))
      self.conds[block] = 0
    elif word == 'if=0':
      self.emit(lo('SIGNW'))
      self.emit(lo('BNE'))
      block = self.thisBlock()
      self.emit(lo('$if.%d.0' % block))
      self.conds[block] = 0
    elif word == 'if<>0':
      self.emit(lo('SIGNW'))
      self.emit(lo('BEQ'))
      block = self.thisBlock()
      self.emit(lo('$if.%d.0' % block))
      self.conds[block] = 0
    elif word == 'if>=0':
      self.emit(lo('SIGNW'))
{'fraction_non_alphanumeric': 0.16840988328027892, 'fraction_numerical': 0.012126724268606942, 'mean_word_length': 2.2091439688715955, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3116962', 'n_tokens_mistral': 2316, 'n_tokens_neox': 2240, 'n_words': 541}