diff --git a/tinyboot.py b/tinyboot.py index 93f3dad..52f9733 100755 --- a/tinyboot.py +++ b/tinyboot.py @@ -127,6 +127,7 @@ def end_loop(): '[': start_conditional, ']': end_conditional, '{': start_loop, '}': end_loop, ' ': nop, '\n': nop, + "'": eat_byte, # ignore the literal character } def tbfcompile(): @@ -222,6 +223,9 @@ def loop(): if not stack.pop(): return jump() +def literal_byte(): + # you put 'A into your program to get 65, or 'B to get 66, etc. + stack.append(ord(eat_byte())) run_time_dispatch = { '(': jump, @@ -238,6 +242,7 @@ def loop(): '[': conditional, ']': nop, '{': nop, '}': loop, ' ': nop, '\n': nop, + "'": literal_byte, } for digit in '0123456789': run_time_dispatch[digit] = push_literal diff --git a/tinyboot1.tbf1 b/tinyboot1.tbf1 index b43686a..6bdc8d4 100644 --- a/tinyboot1.tbf1 +++ b/tinyboot1.tbf1 @@ -1,7 +1,9 @@ -( Second step toward a tiny bootstrap Forth compiler written in itself. +( A tiny bootstrap Forth compiler written in itself. -Generates an ELF executable for x86 Linux that exits with return code -42, following Brian Raiter’s lead. Uses functions. +Generates an ELF executable for x86 Linux. + +Bytes preceded by ' and numbers are compiled into instructions to push +their values on the stack. Compile-time primitives: ( — defines a comment that extends to the next right-paren @@ -277,7 +279,7 @@ v B # 0 ( buffer for w ) ( routine to dispatch a byte being compiled ) : D - d 40 = [ p { G 41 - } ; ] ( drop comments until right paren ) + d '( = [ p { G 41 - } ; ] ( drop comments until right paren ) d 32 = [ p ; ] ( drop spaces ) d 10 = [ p ; ] ( drop newlines ) d 35 = [ p w 48 - n E ; ] ( # writes the next number as data literally ) @@ -292,6 +294,7 @@ v B # 0 ( buffer for w ) d 93 = [ p P ; ] ( ] backpatches the address on the compile stack ) d 123 = [ p % H @ ; ] ( { merely pushes an address to jump back to ) d 125 = [ p j ; ] ( } compiles a conditional jump to that address ) + d '' = [ p G L ; ] ( ' pushes a character literal ) d 118 = [ p w V ; ] ( v registers the next non-blank as a variable label ) d 45 = [ p 41 . 4 . 36 . 88 . ; ] ( - is `sub %eax, [%esp]; pop %eax` ) diff --git a/trim.py b/trim.py index 4b335ea..67e5a62 100755 --- a/trim.py +++ b/trim.py @@ -22,5 +22,6 @@ if not firstline: sys.stdout.write('\n') elif wsp: sys.stdout.write(' ') sys.stdout.write(byte) + if byte == "'": sys.stdout.write(sys.stdin.read(1)) wsp = newline = firstline = False sys.stdout.write('\n')