about summary refs log tree commit diff stats
path: root/apps/hex
Commit message (Collapse)AuthorAgeFilesLines
* 7526Kartik Agaram2021-01-161-0/+0
|
* 7329 - snapshot: advent day 4 part 2Kartik Agaram2020-12-041-0/+0
| | | | | | | | | | | | I've found two bugs in SubX libraries: 1. next-word had an out-of-bounds read 2. next-word was skipping comments, because that's what I need during bootstrapping. I've created a new variant called next-raw-word that doesn't skip comments. These really need better names. We're now at the point where 4b.mu has the right structure and returns identical result to 4a.mu.
* 7238 - mu.subx: final restrictions on 'addr'Kartik Agaram2020-11-151-0/+0
| | | | I had to tweak one app that wasn't following the rules.
* 7225Kartik Agaram2020-11-111-0/+0
| | | | | | | Both manual tests described in commit 7222 now work. To make them work I had to figure out how to copy a file. It requires a dependency on a new syscall: lseek.
* 7173Kartik Agaram2020-11-031-0/+0
| | | | All tests passing again.
* 7138 - type-check array 'length' instructionKartik Agaram2020-10-291-0/+0
|
* 7101 - tile: remove quotes when evaluating stringsKartik Agaram2020-10-251-0/+0
| | | | This found several bugs due to me not checking for null strings.
* 6946 - print floats somewhat intuitively in hexKartik Agaram2020-10-041-0/+0
|
* 6908 - compiling all floating-point operationsKartik Agaram2020-09-301-0/+0
| | | | | We don't yet support emulating these instructions in `bootstrap`. But generated binaries containing them run natively just fine.
* 6783Kartik Agaram2020-09-161-0/+0
| | | | An extra test that should have been in commit 6781.
* 6781 - new app: RPN (postfix) calculatorKartik Agaram2020-09-151-0/+0
| | | | This was surprisingly hard; bugs discovered all over the place.
* 6733 - read utf-8 'grapheme' from byte streamKartik Agaram2020-08-281-0/+0
| | | | | | No support for combining characters. Graphemes are currently just utf-8 encodings of a single Unicode code-point. No support for code-points that require more than 32 bits in utf-8.
* 6719 - error-checking for 'index' instructionsKartik Agaram2020-08-211-0/+0
| | | | | | | | 1000+ LoC spent; just 300+ excluding tests. Still one known gap; we don't check the entirety of an array's element type if it's a compound. So far we just check if say both sides start with 'addr'. Obviously that's not good enough.
* 6622 - new syscalls: time and ntimeKartik Agaram2020-07-081-0/+0
| | | | | As a side-effect I find that my Linode can print ~100k chars/s. At 50 rows and 200 columns per screen, it's 10 frames/s.
* 6604 - new appKartik Agaram2020-07-011-0/+0
| | | | | | https://archive.org/details/akkartik-2min-2020-07-01 In the process I found a bug, added a new syscall, and 'emulated' it.
* 6597Kartik Agaram2020-06-291-0/+0
|
* 6596Kartik Agaram2020-06-291-0/+0
|
* 6595Kartik Agaram2020-06-291-0/+0
|
* 6594 - start standardizing the meaning of 'print'Kartik Agaram2020-06-291-0/+0
|
* 6528Kartik Agaram2020-06-151-0/+0
|
* 6520 - new app: parse-intKartik Agaram2020-06-141-0/+0
| | | | | | Several bugs fixed in the process, and expectation of further bugs is growing. I'd somehow started assuming I don't need to have separate cases for rm32 as a register vs mem. That's not right. We might need more reg-reg Primitives.
* 6508 - support null exit-descriptorKartik Agaram2020-06-101-0/+0
|
* 6507 - use syscall names everywhereKartik Agaram2020-06-101-0/+0
|
* 6409 - primitives for text-mode UIsKartik Agaram2020-05-271-0/+0
|
* 6406 - primitive 'copy-handle'Kartik Agaram2020-05-251-0/+0
|
* 6382 - re-enable mu.subx in CIKartik Agaram2020-05-221-0/+0
| | | | | | | | | | | I thought I'd done this in the previous commit, but I hadn't. And, what's more, there was a bug that seemed pretty tough for a time. Turns out my self-hosted translator doesn't support '.' comment tokens in data segments. Hopefully I'm past the valley of the shadow of death now. "I HAVE NO TOOLS BECAUSE I’VE DESTROYED MY TOOLS WITH MY TOOLS." -- James Mickens (https://www.usenix.org/system/files/1311_05-08_mickens.pdf)
* update binariesKartik Agaram2020-05-221-0/+0
| | | | CI should start passing again now.
* handle nulls in lookupKartik Agaram2020-05-181-0/+0
| | | | | | | | | Cleaner abstraction, but adds 3 instructions to our overhead for handles, including one potentially-hard-to-predict jump :/ I wish I could have put the alloc id in eax for the comparison as well, to save a few bytes of instruction space. But that messes up the non-null case.
* support 'fake' handles allocated staticallyKartik Agaram2020-05-181-0/+0
| | | | | | | | Mystery solved of why the syntax sugar phases don't work even though they don't use any functions whose signatures changed in the migration to handles. The answer: they use the Registers table, and it needs to use handles rather than raw strings.
* support 'fake' handles allocated staticallyKartik Agaram2020-05-181-0/+0
| | | | | | | | | | | | | | | | | | | | | | Mystery solved of why the syntax sugar phases don't work even though they don't use any functions whose signatures changed in the migration to handles. The answer: they use the Registers table, and it currently doesn't use handles. Rather than create a whole new set of functions that operate on addresses, I'm going to create fake handles that are never intended to be reclaimed. Which raises the question of the best way to do that. I'd like to continue using string syntax, so I'm going to use a prefix in the payload that can also be rendered as a string. But all the printable characters start with 0x20, and we don't currently have escape sequences for null or any other non-printable characters. I _could_ use newlines, but that seems overly clever. So instead I'll once again not worry about some hypothetical problem with running out of alloc-ids, and just carve out half of the id space that can't be used for real alloc ids. Ascii doesn't use the most significant bit of bytes, so it seems like a natural separation.
* Rebuild phases of self-hosted SubX translatorKartik Agaram2020-05-181-0/+0
| | | | For this one commit we need to bootstrap ourselves with subx_translate_debug.
* 6208Kartik Agaram2020-04-221-0/+0
|
* 6182 - start of support for safe handlesKartik Agaram2020-04-031-0/+0
| | | | | | | | | | | | | | So far it's unclear how to do this in a series of small commits. Still nibbling around the edges. In this commit we standardize some terminology: The length of an array or stream is denominated in the high-level elements. The _size_ is denominated in bytes. The thing we encode into the type is always the size, not the length. There's still an open question of what to do about the Mu `length` operator. I'd like to modify it to provide the length. Currently it provides the size. If I can't fix that I'll rename it.
* 6181Kartik Agaram2020-04-031-0/+0
|
* 6153 - switch 'main' to use Mu stringsKartik Agaram2020-03-151-0/+0
| | | | | | | | | | | At the SubX level we have to put up with null-terminated kernel strings for commandline args. But so far we haven't done much with them. Rather than try to support them we'll just convert them transparently to standard length-prefixed strings. In the process I realized that it's not quite right to treat the combination of argc and argv as an array of kernel strings. Argc counts the number of elements, whereas the length of an array is usually denominated in bytes.
* 6094 - new 'compute-offset' instructionKartik Agaram2020-03-071-0/+0
| | | | | | | | | | | | | | | | | | | | | | | If indexing into a type with power-of-2-sized elements we can access them in one instruction: x/reg1: (addr int) <- index A/reg2: (addr array int), idx/reg3: int This translates to a single instruction because x86 instructions support an addressing mode with left-shifts. For non-powers-of-2, however, we need a multiply. To keep things type-safe, it is performed like this: x/reg1: (offset T) <- compute-offset A: (addr array T), idx: int y/reg2: (addr T) <- index A, x An offset is just an int that is guaranteed to be a multiple of size-of(T). Offsets can only be used in index instructions, and the types will eventually be required to line up. In the process, I have to expand Input-size because mu.subx is growing big.
* 6085Kartik Agaram2020-03-061-0/+0
| | | | Support parsing ints from strings rather than slices.
* 6083Kartik Agaram2020-03-061-0/+0
|
* 6070Kartik Agaram2020-02-291-0/+0
|
* 6064Kartik Agaram2020-02-271-0/+0
| | | | Fix CI.
* 6000 - clean up after no-local branchesKartik Agaram2020-02-091-0/+0
|
* 5999Kartik Agaram2020-02-091-0/+0
| | | | | Fix CI. apps/survey was running out of space in the trace segment when translating apps/mu.subx
* 5948 - branching to named blocksKartik Agaram2020-01-291-0/+0
|
* 5933Kartik Agaram2020-01-271-0/+0
| | | | Expand some buffer sizes to continue building mu.subx natively.
* 5898 - strengthen slice-empty? checkKartik Agaram2020-01-191-0/+0
| | | | | | | | | | | Anytime we create a slice, the first check tends to be whether it's empty. If we handle ill-formed slices here where start > end, that provides a measure of safety. In the Mu translator (mu.subx) we often check for a trailing ':' or ',' and decrement slice->end to ignore it. But that could conceivably yield ill-formed slices if the slice started out empty. Now we make sure we never operate on such ill-formed slices.
* 5887 - reorganize libraryKartik Agaram2020-01-141-0/+0
| | | | | | | Layers 0-89 are used in self-hosting SubX. Layers 90-99 are not needed for self-hosting SubX, and therefore could use transitional levels of syntax sugar. Layers 100 and up use all SubX syntax sugar.
* 5847 - literal inputsKartik Agaram2019-12-311-0/+0
|
* 5804Kartik Agaram2019-12-081-0/+0
| | | | | Try to make the comments consistent with the type system we'll eventually have.
* 5803Kartik Agaram2019-12-071-0/+0
|
* 5792Kartik Agaram2019-12-051-0/+0
| | | | | Fix a bug in one test: it checks eax when the component under test returns nothing. It's been just accidentally passing all these months.
} td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; } span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; } td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; } span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; } .highlight .hll { background-color: #ffffcc } .highlight .c { color: #888888 } /* Comment */ .highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */ .highlight .k { color: #008800; font-weight: bold } /* Keyword */ .highlight .ch { color: #888888 } /* Comment.Hashbang */ .highlight .cm { color: #888888 } /* Comment.Multiline */ .highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */ .highlight .cpf { color: #888888 } /* Comment.PreprocFile */ .highlight .c1 { color: #888888 } /* Comment.Single */ .highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */ .highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */ .highlight .gr { color: #aa0000 } /* Generic.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
import std/strutils

proc toggle[T](s: var set[T], t: T): bool =
  result = t notin s
  if result:
    s.incl(t)
  else:
    s.excl(t)

type BracketState = enum
  bsNone, bsInBracketRef, bsInBracket, bsAfterBracket, bsInParen, bsInImage,
  bsInTag

proc getId(line: openArray[char]): string =
  result = ""
  var i = 0
  var bs = bsNone
  var escape = false
  while i < line.len:
    let c = line[i]
    if bs == bsInParen:
      if escape:
        escape = false
        inc i
        continue
      if c == ')':
        bs = bsNone
      elif c == '\\':
        escape = true
      inc i
      continue
    case c
    of 'A'..'Z': result &= char(int(c) - int('A') + int('a'))
    of 'a'..'z', '-', '_', '.': result &= c
    of ' ': result &= '-'
    of '[':
      if bs != bsNone:
        bs = bsInBracket
    of ']':
      if bs == bsInBracket:
        bs = bsAfterBracket
    of '(':
      if bs == bsAfterBracket:
        bs = bsInParen
    else: discard
    inc i

type InlineState = enum
  isItalic, isBold, isCode, isComment, isDel

const AsciiAlphaNumeric = {'0'..'9', 'A'..'Z', 'a'..'z'}
func startsWithScheme(s: string): bool =
  for i, c in s:
    if i > 0 and c == ':':
      return true
    if c notin AsciiAlphaNumeric:
      break
  false

proc parseInline(line: openArray[char]) =
  var state: set[InlineState] = {}
  var bs = bsNone
  var i = 0
  var bracketChars = ""
  var quote = false
  var image = false
  template append(s: untyped) =
    if bs in {bsInBracketRef, bsInBracket}:
      bracketChars &= s
    else:
      stdout.write(s)
  while i < line.len:
    let c = line[i]
    if bs == bsAfterBracket and c != '(':
      stdout.write("[" & bracketChars & "]")
      bracketChars = ""
      bs = bsNone
      image = false
    if quote:
      append c
    elif isComment in state:
      if i + 2 < line.len and line.toOpenArray(i, i + 2) == "-->":
        state.excl(isComment)
        append "-->"
        i += 2
      else:
        append c
    elif bs == bsInTag:
      if c == '>': # done
        if bracketChars.startsWithScheme(): # link
          var linkChars = ""
          for c in bracketChars:
            if c == '\'':
              linkChars &= "&apos"
            else:
              linkChars &= c
          stdout.write("<A HREF='" & linkChars & "'>" & bracketChars & "</A>")
        else: # tag
          stdout.write('<' & bracketChars & '>')
        bracketChars = ""
        bs = bsNone
      elif c == '<':
        stdout.write('<' & bracketChars)
        bracketChars = ""
      else:
        bracketChars &= c
    elif isCode in state:
      case c
      of '<': append "&lt;"
      of '>': append "&gt;"
      of '"': append "&quot;"
      of '\'': append "&apos;"
      of '&': append "&amp;"
      of '`':
        append "</CODE>"
        state.excl(isCode)
      else: append c
    elif c == '\\':
      quote = true
    elif c == '*' or c == '_' and
        (i == 0 or line[i - 1] notin AsciiAlphaNumeric or
        i + 1 >= line.len or line[i + 1] notin AsciiAlphaNumeric + {'_'}):
      if i + 1 < line.len and line[i + 1] == c:
        if state.toggle(isBold):
          append "<B>"
        else:
          append "</B>"
        inc i
      else:
        if state.toggle(isItalic):
          stdout.write("<I>")
        else:
          stdout.write("</I>")
    elif c == '`':
      state.incl(isCode)
      append "<CODE>"
    elif c == '~' and i + 1 < line.len and line[i + 1] == '~':
      if state.toggle(isDel):
        append "<DEL>"
      else:
        append "</DEL>"
      inc i
    elif c == '!' and bs == bsNone and i + 1 < line.len and line[i + 1] == '[':
      image = true
    elif c == '[' and bs == bsNone:
      bs = bsInBracket
      if i + 1 < line.len and line[i + 1] == '^':
        inc i
        bs = bsInBracketRef
    elif c == ']' and bs == bsInBracketRef:
      let id = bracketChars.getId()
      stdout.write("<A HREF='#" & id & "'>" & bracketChars & "</A>")
      bracketChars = ""
    elif c == ']' and bs == bsInBracket:
      bs = bsAfterBracket
    elif c == '(' and bs == bsAfterBracket:
      if image:
        stdout.write("<IMG SRC='")
      else:
        stdout.write("<A HREF='")
      bs = bsInParen
    elif c == ')' and bs == bsInParen:
      if image:
        stdout.write("' ALT='" & bracketChars & "'>")
      else:
        stdout.write("'>" & bracketChars & "</A>")
      image = false
      bracketChars = ""
      bs = bsNone
    elif c == '\'' and bs == bsInParen:
      stdout.write("&apos;")
    elif c == '<' and bs == bsNone:
      bs = bsInTag
      bracketChars = ""
    elif i + 4 < line.len and line.toOpenArray(i, i + 3) == "<!--":
      append "<!--"
      i += 3
      state.incl(isComment)
    else:
      append c
    inc i
  if bracketChars != "":
    stdout.write(bracketChars)
  if isBold in state:
    stdout.write("</B>")
  if isItalic in state:
    stdout.write("</I>")

proc parseHash(line: openArray[char]): bool =
  var n = -1
  for i, c in line:
    if line[i] != '#':
      if line[i] != ' ':
        return false
      n = i + 1
      break
  if n == -1:
    return false
  n = min(n, 6)
  let L = n
  var H = line.high
  for i in countdown(line.high, L):
    if line[i] != '#':
      if line[i] != ' ':
        break
      H = i - 1
      break
  H = max(L - 1, H)
  let id = line.toOpenArray(L, H).getId()
  stdout.write("<H" & $n & " id='" & id & "'>")
  line.toOpenArray(L, H).parseInline()
  stdout.write("</H" & $n & ">\n")
  return true

type ListType = enum
  ltOl, ltUl

proc getListDepth(line: string): tuple[depth, len: int, ol: ListType] =
  var depth = 0
  for i, c in line:
    if c == '\t':
      depth += 8
    elif c == ' ':
      inc depth
    elif c in {'*', '-'}:
      let i = i + 1
      if i < line.len and line[i] in {'\t', ' '}:
        return (depth, i, ltUl)
      break
    elif c in {'0'..'9'}:
      let i = i + 1
      if i < line.len and line[i] == '.':
        let i = i + 1
        if i < line.len and line[i] in {'\t', ' '}:
          return (depth, i, ltOl)
      break
    else:
      break
  return (-1, -1, ltUl)

proc matchHTMLPreStart(line: string): bool =
  var tagn = ""
  for i, c in line:
    if i == 0:
      if c != '<':
        return false
      continue
    if c in {' ', '\t', '>'}:
      break
    if c notin {'A'..'Z', 'a'..'z'}:
      return false
    tagn &= c.toLowerAscii()
  return tagn in ["pre", "script", "style", "textarea"]

proc matchHTMLPreEnd(line: string): bool =
  var tagn = ""
  for i, c in line:
    if i == 0:
      if c != '<':
        return false
      continue
    if i == 1:
      if c != '/':
        return false
      continue
    if c in {' ', '\t', '>'}:
      break
    if c notin {'A'..'Z', 'a'..'z'}:
      return false
    tagn &= c.toLowerAscii()
  return tagn in ["pre", "script", "style", "textarea"]

type
  BlockType = enum
    btNone, btPar, btList, btPre, btTabPre, btSpacePre, btHTML, btHTMLPre,
    btComment

  ParseState = object
    blockType: BlockType
    blockData: string
    listDepth: int
    lists: seq[ListType]
    hasp: bool
    reprocess: bool
    numPreLines: int

proc pushList(state: var ParseState, t: ListType) =
  case t
  of ltOl: stdout.write("<OL>\n<LI>")
  of ltUl: stdout.write("<UL>\n<LI>")
  state.lists.add(t)

proc popList(state: var ParseState) =
  case state.lists.pop()
  of ltOl: stdout.write("</OL>\n")
  of ltUl: stdout.write("</UL>\n")

proc parseNone(state: var ParseState, line: string) =
  if line == "":
    discard
  elif line[0] == '#' and line.toOpenArray(1, line.high).parseHash():
    discard
  elif line.startsWith("<!--"):
    state.blockType = btComment
    state.reprocess = true
  elif line[0] == '<' and line.find('>') == line.high:
    state.blockType = if line.matchHTMLPreStart(): btHTMLPre else: btHTML
    state.reprocess = true
  elif line.startsWith("```"):
    state.blockType = btPre
    stdout.write("<PRE>")
  elif line.startsWith("    "):
    state.blockType = btSpacePre
    if state.hasp:
      state.hasp = false
      stdout.write("</P>\n")
    stdout.write("<PRE>")
    state.blockData = line.substr(4) & "\n"
  elif line.startsWith("\t"):
    state.blockType = btTabPre
    if state.hasp:
      state.hasp = false
      stdout.write("</P>\n")
    stdout.write("<PRE>")
    state.blockData = line.substr(1) & "\n"
  elif (let (n, len, t) = line.getListDepth(); n != -1):
    state.blockType = btList
    state.listDepth = n
    state.hasp = false
    state.pushList(t)
    state.blockData = line.substr(len + 1) & "\n"
  else:
    state.blockType = btPar
    state.hasp = true
    stdout.write("<P>\n")
    state.reprocess = true

proc parsePre(state: var ParseState, line: string) =
  if line.startsWith("```"):
    state.blockType = btNone
    stdout.write("</PRE>\n")
  else:
    stdout.write(line & "\n")

proc parseList(state: var ParseState, line: string) =
  if line == "":
    state.blockData.parseInline()
    state.blockData = ""
    while state.lists.len > 0:
      state.popList()
    state.blockType = btNone
  elif (let (n, len, t) = line.getListDepth(); n != -1):
    state.blockData.parseInline()
    state.blockData = ""
    if n < state.listDepth:
      if state.lists.len > 0:
        state.popList()
      else:
        state.pushList(t)
    elif n > state.listDepth:
      state.pushList(t)
    stdout.write("<LI>")
    state.listDepth = n
    state.blockData = line.substr(len + 1) & "\n"
  else:
    state.blockData &= line & "\n"

proc parsePar(state: var ParseState, line: string) =
  if line == "":
    state.blockData.parseInline()
    state.blockData = ""
    state.blockType = btNone
  elif line[0] == '<' and line.find('>') == line.high:
    state.blockData.parseInline()
    state.blockData = ""
    if line.matchHTMLPreStart():
      state.blockType = btHTMLPre
    else:
      state.blockType = btHTML
    state.reprocess = true
  elif line.len >= 3 and line.startsWith("```"):
    state.blockData.parseInline()
    state.blockData = ""
    state.blockType = btPre
    state.hasp = false
    stdout.write("<PRE>")
  else:
    state.blockData &= line & "\n"

proc parseHTML(state: var ParseState, line: string) =
  if state.hasp:
    state.hasp = false
    stdout.write("</P>\n")
  if line == "":
    state.blockData.parseInline()
    state.blockData = ""
    state.blockType = btNone
  else:
    state.blockData &= line & "\n"

proc parseHTMLPre(state: var ParseState, line: string) =
  if state.hasp:
    state.hasp = false
    stdout.write("</P>\n")
  if line.matchHTMLPreEnd():
    stdout.write(state.blockData)
    state.blockData = ""
    state.blockType = btNone
  else:
    state.blockData &= line & "\n"

proc parseTabPre(state: var ParseState, line: string) =
  if line.len == 0:
    inc state.numPreLines
  elif line[0] != '\t':
    state.numPreLines = 0
    stdout.write(state.blockData)
    stdout.write("</PRE>")
    state.blockData = ""
    state.reprocess = true
    state.blockType = btNone
  else:
    while state.numPreLines > 0:
      state.blockData &= '\n'
      dec state.numPreLines
    state.blockData &= line.substr(1) & "\n"

proc parseSpacePre(state: var ParseState, line: string) =
  if line.len == 0:
    inc state.numPreLines
  elif not line.startsWith("    "):
    state.numPreLines = 0
    stdout.write(state.blockData)
    stdout.write("</PRE>")
    state.blockData = ""
    state.reprocess = true
    state.blockType = btNone
  else:
    while state.numPreLines > 0:
      state.blockData &= '\n'
      dec state.numPreLines
    state.blockData &= line.substr(4) & "\n"

proc parseComment(state: var ParseState, line: string) =
  let i = line.find("-->")
  if i != -1:
    stdout.write(line.substr(0, i + 2))
    state.blockType = btNone
    line.substr(i + 3).parseInline()
  else:
    stdout.write(line & "\n")

proc main() =
  var line: string
  var state = ParseState(listDepth: -1)
  while state.reprocess or stdin.readLine(line):
    state.reprocess = false
    case state.blockType
    of btNone: state.parseNone(line)
    of btPre: state.parsePre(line)
    of btTabPre: state.parseTabPre(line)
    of btSpacePre: state.parseSpacePre(line)
    of btList: state.parseList(line)
    of btPar: state.parsePar(line)
    of btHTML: state.parseHTML(line)
    of btHTMLPre: state.parseHTMLPre(line)
    of btComment: state.parseComment(line)
  state.blockData.parseInline()

main()