cvsuser 04/02/04 05:19:52
Modified: . MANIFEST
src packfile.c
t/pmc eval.t
Added: examples/assembly nanoforth.pasm nanoforth2.pasm
Log:
another eval test: nanoforth
Revision Changes Path
1.544 +2 -0 parrot/MANIFEST
Index: MANIFEST
===================================================================
RCS file: /cvs/public/parrot/MANIFEST,v
retrieving revision 1.543
retrieving revision 1.544
diff -u -w -r1.543 -r1.544
--- MANIFEST 3 Feb 2004 18:25:28 -0000 1.543
+++ MANIFEST 4 Feb 2004 13:18:44 -0000 1.544
@@ -285,6 +285,8 @@
examples/assembly/mandel.pasm [main]doc
examples/assembly/mops.pasm [main]doc
examples/assembly/mops_p.pasm [main]doc
+examples/assembly/nanoforth.pasm [main]doc
+examples/assembly/nanoforth2.pasm [main]doc
examples/assembly/ncurses_life.imc [main]doc
examples/assembly/pcre.imc [main]doc
examples/assembly/pmcmops.pasm [main]doc
1.1 parrot/examples/assembly/nanoforth.pasm
Index: nanoforth.pasm
===================================================================
=pod
=head1 nanoforth - a totally stripped down forth kernel
Its intended to investigate dynamic compilation. It can read one line of
code and understands:
+ add
- sub
\d a number (single digit only)
. print
: x compile single-letter word x
; end compile
=cut
.macro core(op, label)
set_addr I3, .label
set P16[.op], I3
.endm
_main:
new P16, .PerlHash
.core("+", _add)
.core("-", _sub)
.core(".", _print)
.core("0", _const)
.core("1", _const)
.core("2", _const)
.core("3", _const)
.core("4", _const)
.core("5", _const)
.core("6", _const)
.core("7", _const)
.core("8", _const)
.core("9", _const)
.core(":", _start_compile)
getstdin P3
readline S17, P3
set I1, 0 # 1 = compile
parse:
length I0, S17
unless I0, fin
# S17 is rest of input, S16 is current word
substr S16, S17, 0, 1, ""
eq S16, ';', end_compile
eq S16, ' ', parse
eq S16, "\n", parse
set I3, P16[S16]
unless I3, next
if I1, compile
jsr I3
branch parse
compile:
ord I2, S16
lt I2, 0x30, no_num
gt I2, 0x39, no_num
sub I2, 0x30
concat S18, "save "
set S2, I2
concat S18, S2
concat S18, "\n"
branch parse
no_num:
concat S18, "\nset I16, "
set S1, I3
concat S18, S1
concat S18, "\njsr I16\n"
branch parse
next:
printerr "? "
printerr S16
printerr "\n"
branch syntax_error
end_compile:
set I1, 0
concat S18, "ret\n"
## print S18
compreg P2, "PASM"
compile P1, P2, S18
# dan's hack
set I16, P1[1]
# alternate - not working
find_global P3, "_entry"
get_addr I17, P3
eq I16, I17, ok
ok:
# end
set P16[S19], I16
branch parse
fin:
print "\n"
end
syntax_error:
print "syntax error\n"
end
_start_compile:
substr S16, S17, 0, 1, ""
eq S16, " ", _start_compile
# word to cpmpile
set S19, S16
set S18, "noop\n.pcc_sub _entry:\n"
set I1, 1
ret
_add:
restore I16
restore I17
add I16, I17, I16
save I16
ret
_sub:
restore I16
restore I17
sub I16, I17, I16
save I16
ret
_print:
restore I16
print I16
ret
# single digit 0..9 only
_const:
ord I16, S16
sub I16, 0x30
save I16
ret
1.1 parrot/examples/assembly/nanoforth2.pasm
Index: nanoforth2.pasm
===================================================================
=pod
=head1 nanoforth2 - a totally stripped down forth kernel
Like nanoforth but with PCC
Its intended to investigate dynamic compilation. It can read one line of
code and understands:
+ add
- sub
\d a number
. print
: x compile single-letter word x
; end compile
=cut
.macro core(op, label)
find_global P3, .label
set P16[.op], P3
.endm
_main:
getstdin P3
readline S5, P3
find_global P0, "_nano_forth_compiler"
invokecc
end
.pcc_sub _nano_forth_compiler:
set P21, P1 #preserve ret cont
set S17, S5 #input src code
new P16, .PerlHash
.core("+", "_add")
.core("-", "_sub")
.core(".", "_print")
.core("0", "_const")
.core("1", "_const")
.core("2", "_const")
.core("3", "_const")
.core("4", "_const")
.core("5", "_const")
.core("6", "_const")
.core("7", "_const")
.core("8", "_const")
.core("9", "_const")
.core(":", "_start_compile")
#set S17, ": a + ; 2 1 a ."
set I1, 0 # 1 = compile
parse:
length I0, S17
unless I0, fin
# S17 is rest of input, S16 is current word
substr S16, S17, 0, 1, ""
eq S16, ';', end_compile
eq S16, ' ', parse
eq S16, "\n", parse
set P0, P16[S16]
defined I0, P0
unless I0, next
if I1, compile
invokecc P0
branch parse
compile:
ord I2, S16
lt I2, 0x30, no_num
gt I2, 0x39, no_num
sub I2, 0x30
concat S18, "save "
set S2, I2
concat S18, S2
concat S18, "\n"
branch parse
no_num:
concat S18, "pushbottomp\n"
concat S18, 'set P0, P16["'
concat S18, S16
concat S18, '"]'
concat S18, "\ninvokecc\n"
concat S18, "popbottomp\n"
branch parse
next:
printerr "? "
printerr S16
printerr "\n"
branch syntax_error
end_compile:
set I1, 0
concat S18, "invoke P1\n"
## print S18
compreg P2, "PASM"
compile P1, P2, S18
# find _entry_X
set S0, "_entry_"
concat S0, S19
find_global P3, S0
set P16[S19], P3
branch parse
fin:
set I5, 0
invoke P21
syntax_error:
printerr "syntax error\n"
set I5, 1
invoke P21
.pcc_sub _start_compile:
substr S16, S17, 0, 1, ""
eq S16, " ", _start_compile
# word to cpmpile
set S19, S16
# should check that S19 is alphabetic
set S18, ".pcc_sub _entry_"
concat S18, S19
concat S18, ":\n"
set I1, 1
invoke P1
.pcc_sub _add:
restore I16
restore I17
add I16, I17, I16
save I16
invoke P1
.pcc_sub _sub:
restore I16
restore I17
sub I16, I17, I16
save I16
invoke P1
.pcc_sub _print:
restore I16
print I16
print "\n"
invoke P1
# single digit 0..9 only
.pcc_sub _const:
ord I16, S16
sub I16, 0x30
save I16
invoke P1
1.133 +0 -0 parrot/src/packfile.c
Index: packfile.c
===================================================================
RCS file: /cvs/public/parrot/src/packfile.c,v
retrieving revision 1.132
retrieving revision 1.133
diff -u -w -r1.132 -r1.133
--- packfile.c 4 Feb 2004 12:11:48 -0000 1.132
+++ packfile.c 4 Feb 2004 13:19:32 -0000 1.133
@@ -2,7 +2,7 @@
Copyright (C) 2001-2002 Gregor N. Purdy. All rights reserved.
This program is free software. It is subject to the same license as
Parrot itself.
-$Id: packfile.c,v 1.132 2004/02/04 12:11:48 leo Exp $
+$Id: packfile.c,v 1.133 2004/02/04 13:19:32 leo Exp $
=head1 NAME
1.4 +30 -1 parrot/t/pmc/eval.t
Index: eval.t
===================================================================
RCS file: /cvs/public/parrot/t/pmc/eval.t,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -w -r1.3 -r1.4
--- eval.t 4 Feb 2004 12:12:25 -0000 1.3
+++ eval.t 4 Feb 2004 13:19:51 -0000 1.4
@@ -1,6 +1,6 @@
#! perl -w
-use Parrot::Test tests => 5;
+use Parrot::Test tests => 6;
use Test::More;
# PASM1 is like PASM but appends an C<end> opcode
@@ -81,3 +81,32 @@
fin
OUTPUT
+output_is(<<'CODE', <<'OUTPUT', "nano forth sub");
+_main:
+ load_bytecode "examples/assembly/nanoforth2.pasm"
+ print "ok 1\n"
+ find_global P0, "_nano_forth_compiler"
+ defined I0, P0
+ if I0, ok2
+ print "not "
+ok2:
+ print "ok 2\n"
+ set S5, "1 7 + . 2 3 - .\n"
+ pushp
+ invokecc
+ popp
+ set S5, ": i 1 + ; 5 i .\n"
+ pushp
+ invokecc
+ popp
+ set S5, ": i 1 + ; : j i i ; 9 j .\n"
+ invokecc
+ end
+CODE
+ok 1
+ok 2
+8
+-1
+6
+11
+OUTPUT