Not logged in

Artifact f617e407166befd836dc6d0051a0609a7fbb5fa8

File langs/bf.rx part of check-in [d844f9fff0] - add assembler, brainf*** compiler by charleschilders on 2011-05-31 03:24:50. [annotate]


( Support functions: basic input, output, and data pointer support )
:wait
  #0 #0 out,
  wait,
  ret,

:bye
  #-9 #5 out,
  ret,

:dp 32768 m,

:bf_>
  dp # @,
  1+,
  dp # !,
  ret,

:bf_<
  dp # @,
  1-,
  dp # !,
  ret,

:bf_+
  dp # @, @,
  1+,
  dp # @, !,
  ret,

:bf_-
  dp # @, @,
  1-,
  dp # @, !,
  ret,

:bf_.
  dp # @, @,
  #1 #2 out,
  call wait
  #0 #3 out,
  ret,

:bf_,
  #1 #1 out,
  call wait
  #1 in,
  ret,

( Actual BrainF*** compiler )
variable ip
variable lp

: getLabel
  @lp toString "L" ^strings'prepend ;

: run
  t-here putn space @ip @ putc cr
  @ip @ ip ++
  [ '> = ] [ drop bf_> m, ] when
  [ '< = ] [ drop bf_< m, ] when
  [ '+ = ] [ drop bf_+ m, ] when
  [ '- = ] [ drop bf_- m, ] when
  [ '. = ] [ drop bf_. m, ] when
  [ ', = ] [ drop bf_, m, ] when
  [ '[ = ] [ drop getLabel header t-here @last !d->xt lp ++ ] when
  [ '] = ] [ drop dp # @, @, lit, 0 m, !jump, lp -- getLabel find drop @d->xt m, ] when
  drop ;

: do
  [ run @ip @ ] while ;

: bf: ( "- )
  '~ accept tib keepString !ip do ;

( Start Compilation of BrainF*** code after this )
:main