example of parsing pdp7 with antlr4

This commit is contained in:
Tom Everett
2016-03-01 19:37:14 -07:00
parent 511361fbb0
commit d7639c04c1
17 changed files with 3864 additions and 1 deletions
@@ -0,0 +1,35 @@
package com.khubla.pdp7parse;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Collection;
import org.apache.commons.io.FileUtils;
import org.junit.Assert;
import org.junit.Test;
import com.khubla.pdp7parse.antlr4.pdp7Parser.ProgContext;
public class BasicTests {
@Test
public void test1() {
String[] extensions = new String[] { "s" };
Collection<File> files = FileUtils.listFiles(new File("src/test/resources"), extensions, true);
for (File file : files) {
testFile(file);
}
}
private void testFile(File file) {
try {
System.out.println(file.getAbsolutePath());
InputStream is = new FileInputStream(file);
ProgContext progContext = PDP7Parser.parse(is, System.out);
Assert.assertNotNull(progContext);
} catch (Exception e) {
e.printStackTrace();
Assert.fail();
}
}
}
+151
View File
@@ -0,0 +1,151 @@
" cat: cat [arg1 arg2 ...]
" Load the pointer pointer in 017777 to see if we have any arguments
lac 017777 i
sad d4 " Skip if we have more than four argument words
jmp nofiles " Only four argument words, so no arguments
lac 017777 " Move five words past the argument word count
tad d1 " so that AC points at the first argument
tad d4 " and save the pointer in name
dac name
loop:
sys open; name:0; 0 " Open file, get fd back
spa
jmp badfile " Negative fd, exit with an error message
dac fi " Save file descriptor in fi
1:
jms getc " Get a character in AC
sad o4
jmp 1f " Break the loop when we get a ctrl-D
jms putc " Write the character on stdout
jmp 1b " and loop back
1:
lac fi " Close the file descriptor in fi
sys close
loop1:
-4
tad 017777 i " Subtract 4 from the count of argument words
dac 017777 i
sad d4 " Is the value 4, i.e. no args left?
jmp done " Yes, so exit
lac name " Still an argument, so move up
tad d4 " to the next filename argument
dac name
jmp loop " and loop back to cat this file
badfile:
lac name " Get the pointer to the filename
dac 1f " Store it in 1f below
lac d8 " Load fd 8 which is stderr
sys write; 1:0; 4 " Write the four words of the filename
lac d8
sys write; 1f; 2 " and then write " ?\n"
jmp loop1 " Now try doing the next argument
1: 040;077012 " String literal: " ?\n"
nofiles:
lac d8
sys write; 1f; 5 " Write "No files\n" to stderr
sys exit " and exit
1: <no>; 040; <fi>;<le>;<s 012
done:
lac noc " Is the number of characters left zero?
sna
sys exit " Yes, exit
and d1
sna cla
jmp 1f
jms putc " Store the character in the buffer
jmp done " and loop back
1:
lac noc " Get the number of characters in the buffer
rcr " Divide by two to convert to words
dac 1f " Save in the write's word count below
lac fo " Load fd 1, stdout
sys write; iopt+1; 1:.. " Write the leftover buffer and exit
sys exit
getc: 0
lac ipt " Load the pointer to the next word in the buffer
sad eipt
jmp 1f " We've reached the end of the buffer, so read more
dac 2f " Save the pointer
add o400000 " Flip the msb and save into ipt
dac ipt
ral " Move the msb into the link register
lac 2f i " Load the word from the buffer
szl " Skip if this is the second character in the word
lrss 9 " It's the first char, shift down the top character
and o177 " Keep the lowest 7 bits
sna
jmp getc+1 " Skip a NUL characters and read another one
jmp getc i " Return the character from the subroutine
1:
lac fi " Buffer is empty, read another 64 characters
sys read; iipt+1; 64
sna
jmp 1f " No characters were read in
tad iipt " Add the word count to the base of the buffer
dac eipt " and store in the end buffer pointer
lac iipt " Reset the ipt to the base of the buffer
dac ipt
jmp getc+1 " and loop back to get one character
1:
lac o4 " No character, return with ctrl-D
jmp getc i " return from subroutine
putc: 0
and o177 " Keep the lowest 7 bits and save into 2f+1
dac 2f+1
lac opt " Save the pointer to the empty buffer
dac 2f " position to 2f
add o400000 " Flip the msb and save back into opt
dac opt " This also has the effect of incrementing
" the opt pointer every second addition!
spa " If the bit was set, we already have one
jmp 1f " character at 2f+1. If no previous character,
lac 2f i " merge the old and new character together
xor 2f+1
jmp 3f " and go to the "save it in buffer" code
1:
lac 2f+1 " Move the character up into the top half
alss 9
3:
dac 2f i " Save the word into the buffer
isz noc " Add 1 to the char count, never skipping
lac noc " Have we reached 128 characters, 64 words?
sad d128
skp
jmp putc i " No, so return (more room still in the buffer)
lac fo " Load fd1 (i.e stdout)
sys write; iopt+1; 64 " and write out the 64 words in the buffer
lac iopt
dac opt " Set opt pointing back to base of buffer
dzm noc " Set the number of chars in the buffer to 0
jmp putc i " and return
2: 0;0 " Current input and output word pointers
ipt: 0 " Current input buffer base
eipt: 0 " Pointer to end of data read in input buffer
iipt: .+1; .=.+64 " 64 word input buffer and pointer to it
fi: 0 " Input file descriptor
opt: .+2 " Current output buffer base
iopt: .+1; .=.+64 " 64 word output buffer and pointer to it
noc: 0 " Number of output characters
fo: 1 " Output file descriptor, fd 1 is stdout
d1: 1 " Octal and decimal constants
o4:d4: 4
d8: 8
o400000: 0400000 " Msb toggle bit
o177: 0177 " ASCII mask
d128: 128 " 128 words in the output buffer
+76
View File
@@ -0,0 +1,76 @@
" chown
lac 017777 i
sad d4
jmp error
lac 017777
tad d4
dac 8
tad d1
dac name
dzm octal
dzm nochar
-8
dac c1
1:
lac nchar
dzm nchar
sza
jmp 2f
lac 8 i
lmq
and o177
dac nchar
lacq
lrss 9
2:
sad o40
jmp 3f
tad om60
lmq
lac octal
cll; als 3
omq
dac octal
3:
isz c1
jmp 1b
loop:
lac 017777 i
sad d8
sys exit
tad dm4
dac 017777 i
lac name
tad d4
dac name
lac octal
sys chowner; name:0
sma
jmp loop
lac name
dac 1f
lac d1
sys write; 1:0; 4
lac d1
sys write; 1f; 2
jmp loop
1:
040;077012
error:
lac d1
sys write; 1b+1; 1
sys exit
om60: -060
o40: 040
d1: 1
d8: 8
dm4: -4
d4: 4
o177: 0177
nchar: .=.+1
c1: .=.+1
octal: .=.+1
+501
View File
@@ -0,0 +1,501 @@
" ds
lac 017777 i
sad d8
skp
sys exit
lac 017777
tad d5
dac .+3
law 017
sys creat; ..
dac fo
law 017
sys creat; scrname
spa; jms error
dac fso
sys open; scrname; 0
spa; jms error
dac fsi
sys chdir; dd
spa; jms error
lac d1
sys write; pass1; 1
law fsobuf
dac fsopt
dzm nfiles
law fbuf
dac filp
dzm ndirs
law dbuf
dac dirp
dzm fsloc
sys open; dotdot; 0
spa; jms error
dac fd
jms readdir; dotdot
law statbuf
sys status; dotdot; dotdot
spa; jms error
lac statbuf+12 " i index
dac dirp i
isz dirp
-1
tad nfiles
cma
dac ddfiles
law fbuf
dac ddfilp
loop:
-1
tad ndirs
cma
dac c1
law dbuf
dac i1
1:
isz i1
lac i1 i
sad ddfilp i
jmp 2f
isz i1
isz c1
jmp 1b
lac ddfilp
tad i1
dac i1
lac i1 i
dac .+3
lac fsi
sys seek; ..; 0
lac fsi
sys read; scrname; 4
law statbuf
sys status; dotdot; scrname
spa; jms error
lac statbuf+0 " flags
and o20
sna
jmp 2f
sys open; scrname; 0
spa; jms error
dac fd
jms readdir; scrname
lac ddfilp i
dac dirp i
isz dirp
2:
isz ddfilp
isz ddfilp
isz ddfiles
jmp loop
" output phase
lac fso
sys write; fsobuf; 64
lac d1
sys write; pass2; 2
-500
dac c1
1:
law dbuf+2
dac i1
dzm fflg
law fbuf
dac i2
r1
tad nfiles
cma
dac c2
2:
lac c1
tad d501
sad i2 i
skp
jmp 3f
-1
tad i1
dac i3
iac i3 i
dac c3
law fbuf
dac i3
0:
lac i3 i
sad c3
jmp 0f
isz i3
isz i3
jmp 0b
0:
lac i3
tad d1
dac c3
lac c3 i
dac .+3
lac fsi
sys seek; ..; 0
lac fsi
sys read; scrname; 4
lac i2
tad d1
dac c3
lac c3 i
dac .+3
lac fsi
sys seek; ..; 0
lac fsi
sys read; dd; 4
lac fflg
sza
jmp 0f
lac nlinkt
sad nlinka
skp
jms fishy
dzm nlinka
law 012
jms putc
law statbuf
sys status; scrname; dd
spa; jms error
-1
tad statbuf+9
cma
dac nlinkt
-1
dac fflg
jms longout
law 012
jms putc
0:
isz nlinka
jms putname; scrname
jms putname; dd
law 012
jms putc
3:
isz i2
isz i2
lac i2
sad i1 i
skp
jmp .+3
isz i1
isz i1
isz c2
jmp 2b
isz c1
jmp 1b
lac nlinkt
sad nlinka
skp
jms fishy
sys chdir; system
jmp done
fishy: 0
jms asters
jms asters
law 012
jms putc
lac d1
sys write; 1f; 1
jmp fishy i
1: 052012
nlinka: 0
nlinkt: 0
asters: 0
-10
dac c
1:
law 052
jms putc
isz c
jmp 1b
jmp asters i
longout: 0
lac statbuf+12 " i
jms octal; -3
lac statbuf+0 " flags
jms octal; -2
lac statbuf+8 " uid
jms octal; -2
-1
tad statbuf+9 " nlinks
cma
jms octal; -2
lac statbuf+10
jms octal; -5
jmp longout i
readdir: 0
law 012
jms putc
law 012
jms putc
jms asters
lac readdir i
dac 5f
dac .+2
jms putname; ..
jms asters
law 012
jms putc
law 012
jms putc
isz readdir
isz ndirs
lac filp
dac dirp i
isz dirp
0:
jms copyz; buf; 64
lac fd
sys read; buf; 64
spa; jms error
sna
jmp 4f
-8
dac c1
law buf
dac i1
1:
lac i1 i
sna
jmp 3f
isz nfiles
dac filp i
isz filp
lac fsloc
dac filp i
tad d4
dac fsloc
isz filp
lac i1
tad d1
dac .+4
law statbuf
sys status; 5:..; ..
spa; jms error
jms longout
lac i1
tad d1
dac .+2
jms putname; ..
law 012
jms putc
lac i1
dac 8
lac 8 i
dac fsopt i
isz fsopt
lac 8 i
dac fsopt i
isz fsopt
lac 8 i
dac fsopt i
isz fsopt
lac 8 i
dac fsopt i
isz fsopt
law fsobuf+64
sad fsopt
skp
jmp 3f
lac fso
sys write; fsobuf; 64
law fsobuf
dac fsopt
3:
law 8
tad i1
dac i1
isz c1
jmp 1b
jmp 0b
4:
lac fd
sys close
jmp readdir i
putname: 0
-1
tad putname i
dac 8
-4
dac c
1:
lac 8 i
lrss 9
jms putc
llss 9
jms putc
isz c
jmp 1b
isz putname
jmp putname i
octal: 0
lmq
lac d5
tad octal i
cma
dac c
1:
llss 3
isz c
jmp 1b
lac octal i
dac c
1:
ecla llss 3
tad o60
jms putc
isz c
jmp 1b
law 040
jms putc
isz octal
jmp octal i
error: 0
-1
tad error
hlt
sys save
copyz: 0
-1
tad copyz i
dac 8
isz copyz
-1
tad copyz i
cma
dac 2f
isz copyz
1:
dzm 8 i
isz 2f
jmp 1b
jmp copyz i
2: 0
done:
lac noc
sna
sys exit
and d1
sna cla
jmp 1f
jms putc
jmp done
1:
lac noc
rcr
dac 1f
lac fo
sys write; obuf; 1;..
sys exit
putc: 0
and o177
dac 2f+1
lac opt
dac 2f
add o400000
dac opt
spa
jmp 1f
lac 2f i
xor 2f+1
jmp 3f
1:
lac 2f+1
alss 9
3:
dac 2f i
isz noc
lac noc
sad d128
skp
jmp putc i
lac fo
sys write; obuf; 64
lac iopt
dac opt
dzm noc
jmp putc i
2: 0;0
opt: obuf
iopt: obuf
noc: 0
fo: 1
d1: 1
o177: 0177
o400000: 0400000
d128: 128
d4: 4
d5: 5
d8: 8
o60: 060
o20: 020
d501: 501
dd:
<dd>; 040040; 040040; 040040
dotdot:
056056; 040040; 040040; 040040
system:
<sy>;<st>;<em>; 040040
scrname:
<*s>;<rc>;040040;040040
pass2:
<i
pass1:
<i 012
fso: .=.+1
fsi: .=.+1
fsloc: .=.+1
nfiles: .=.+1
fflg: .=.+1
buf: .=.+64
obuf: .=.+64
fd: .=.+1
filp: .=.+1
ddfilp: .=.+1
ddfiles: .=.+1
statbuf: .=.+13
c: .=.+1
i1: .=.+1
i2: .=.+1
i3: .=.+1
c1: .=.+1
c2: .=.+1
c3: .=.+1
ndirs: .=.+1
dirp: .=.+1
fsopt: .=.+1
fsobuf: .=.+64
dbuf: .=.+100
fbuf:
+26
View File
@@ -0,0 +1,26 @@
" dsksav
iof
hlt
dzm track
-640
dac c1
1:
lac track
jms dskrd1
lac track
jms dskwr0
lac track
tad d10
dac track
isz c1
jmp 1b
hlt
sys exit
track: 0
c1: 0
d10: 10
+519
View File
@@ -0,0 +1,519 @@
"** 08-rest.pdf page 21
"[handwritten page number top right of scan - 14]
" ed2
cs:
jsm getsc; tal
sad o40
jmp cs
sad o12
jmp error
dac delim
jms compile
lac tbufp
dac tal1
1:
jms getsc; tal
sad delim
jmp 1f
sad o12
jmp error
jms putsc; tal1
jmp 1b
1:
lac o12
jms putsc; tal1
jms newline
jms setdd
lac addr1
sad zerop
jmp error
1:
dac addr1
lac i addr1
jms execute
jmp 2f
lac addr1
dac dot
law line-1
dac 8
law nlist-1
dac 9
-64
dac c1
3:
lac i 8
dac i 9
isz c1
jmp 3b
-1
tad fchrno
dac linsiz
rcr
szl
xor o400000
tad linep
dac tal1 "???
lac tbufp
dac tal
3:
jms getsc; tal
sad o12
jmp 3f
jms putsc; tal1
isz linsiz "???
"** 08-rest.pdf page 22
"[handwritten page number top right of scan - 15]
jmp 3b
3:
-1
tad lcrhno
rcr
szl
xor o400000
tad nlistp
dac tal
3:
jms getsc; tal
jms putsc; tal1
isz linsiz
sad o12
skp
jmp 3b
jms addline
2:
lac addr1
sad addr2
jmp advanc
tad d1
jmp 1b
fsrch:
dac delim
jms compile
jms srcsav
lac dot
floop:
tad d1
dac addr
lac i addr
sza
jmp 1f
lac zerop
dac addr
jmp 2f
1:
jms execute
jmp 2f
jms srcres
jmp ad1
2:
lac addr
sad dot
jmp error
jmp floop
bsrch:
dac delim
jms compile
jms srcsav
lac dot
dad zerop
lac eofp
bloop:
tad dm1
dac addr
lac i addr
"** 08-rest.pdf page 23
"[handwritten page number top right of scan - 16]
sza
jmp 1f
lac eofp
dac addr
jmp 2f
1:
jms execute
jmp 2f
jms srcres
jmp ad1
2:
lac addr
sad dot
jmp error
jmp bloop
srcsav: 0
lac minflg
sza
jmp error
lac addr
sma
jmp error
law line-1
dac 8
law tbuf-1
dac 9
-64
dac c1
1:
lac i 8
dac i 9
isz c1
jmp 1b
jmp i srcsav
srcres: 0
law tbuf-1
dac 8
law line-1
dac 9
-64
dac c1
1:
lac i 8
dac i 9
isz c1
jmp 1b
jmp i srcres
compile: 0
law compbuf-1
dac 8
dzm prev
dzm compflg
cadvanc:
jms getsc; tal
sad delim
jmp cdone
dac compflg "???
"** 08-rest.pdf page 24
"[handwritten page number top right of scan - 17]
dzm lastre
sad o12
jmp error
"sad o133
"jmp chrcls
sad o136 "???
jmp beglin
sad o44
jmp endlin
"sad o52
"jmp clsure
dac 1f
jmp comp
1; jms matchar; 1: 0; 0
jmp cadvanc
cdone:
lac compflg
sna
jmp 1f
dac lastre
jms comp
1; jms found; 0
jmp i compile
1: "???
lac lastre
sna
jmp error
jmp i compile
chrcls:
jmp error
beglin: "???
jms comp
1; jms matbol; 0
dzm prev
jmp cadvanc
endlin: "???
jms comp
1; jms mateol; 0
dzm prev
jmp cadvanc
clsure:
lac prev
sna
jmp error
tad d1
dac 1f
jms comp
1; jms matclo; 1: 0; 0
dzm prev
jmp cadvanc
comp: 0 "???
-1
tad comp
dac 9
lac 8 "???
"** 08-rest.pdf page 25
"[handwritten page number top right of scan - 18]
dac prev
1: "???
lac i 9
sna
jmp i 9
dac i 8
jmp 1b
execute: 0
jms gline
lac linep
dac tal1
dzm charno
dzm fchrno
dzm lchrno
lac jmpclist
dac trvect
lac jmpnlist
dac trvect+1
lac jmpxchg
dac i trvect+1
jmp 1f
exchg: "???
lacq
sad o12
jmp i execute
lac jmpxchg
dac i 8
1: "???
lac trvect
lmq
lac trvect+1
dac trvect
lacq
dac trvect+1
tad dm1
dac 8
jms getsc; tal1
lmq
isz charno
jms compbuf
charno:
0
trvect:
0;0
matchar: 0 "???
-2
tad matchar
dac exret
lac i exret
dac exret
lacq
sad i matchar
skp
jmp 1f
lac matchar
adn o17777
tad jms1
dac i 8 "??? [unreadable page cutoff]
"** 08-rest.pdf page 26
"[handwritten page number top right of scan - 19]
lac i exret
dac i 8
1: "???
isz exret
jmp i exret
found: 0
-2
tad found
dac exret
lac i exret
dac exret
lac fchrno
sza
jmp 1f
isz execute
jmp 2f
1: "???
sad i exret
jmp 1f
cma
tad i exret
spa
jmp 2f
jmp 3f
1: "???
lac charno
cma
tad charno
spa
jmp 3f
2: "???
lac i exret
dac fchrno
lac charno
dac lchrno
3: "???
isz exret
jmp i exret
matbol: 0 "???
lac charno
sad d1
jmp 1f
lac matbol
jmp 2f
1: "???
lac matbol
jmp 3f
mateol: 0 "???
lacq
sad o12
jmp 1f
lac mateol
2: "???
tad dm2
dac exret
lac i exret
dac 9
"??? [line is cut off in scan, maybe lac i 8 or jmp i 9]
"** 08-rest.pdf page 27
"[handwritten page number top right of scan - 20]
1: "???
lac mateol
3: "???
tad dm3
dac 9
lac i 9
isz 9
dac i 9
jmp i 9
matclo: 0 "???
-2
tad matclo
dac exret
lac i exret
dac cloret
lac i cloret
dac 1f
dac 2f
lac i matclo
dac exret
jms i exret; 1: 0
isz matclo
jms i matclo; 2: 0
isz cloret
jmp i cloret
"??? the remainder of this scan had an unreadable first character
"??? I did the best I could to recreate the characters appropriately
o1: 1
o133: 0133
dm3: -3
o136: 0136
dm2: -2
o52: 052
o57: 057
o77: 077
o40: 040
o12: 012
d47: 47
d58: 58
dm48: -48
d10: 10
d8: 8
d48: o60: 060
d100000: 100000
o44: 044
o53: 053
o56: 056
o55: 055
o11: 011
o400000: 0400000
o17777: 017777
o144: 0144
dm1: -1
o56012: 056012
o777: 0777
o100: 0100
o43: 043
o777000: 0777000
o75: 075
o167: 0167
o161: 0161
"** 08-rest.pdf page 28
"[handwritten page number top right of scan - 21]
o160: 0160
o143: 0143
o141: 0141
o1777: 01777
d1024: 1024
o776000: 0776000
o162: 0162
o163: 0163
o73: 073
o54: 054
o17: 017
tname:
0145056;0164155;0160040;040040
tbufp: tbuf
linep: line
nlistp: nlist
fbufp: fbuf
dskbfp: dskbuf "[line crossed out - scan markup]
edskbfp: dskbuf+1024 "[line crossed out - scan markup]
lnodp: lnodes
linpm1: line-1
jmpclist: jmp clist
jmpnlist: jmp nlist
jmpxchg: jmp xchg
jms1: jms 1
tal: .=.+1
exret: .=.+1
cloret: .=.+1
delim: .=.+1
prev: .=.+1
compflg: .=.+1
tal1: .=.+1
c1: .=.+1
ital: .=.+1
otal: .=.+1
diskin: .=.+1
glint: .=.+1
c2: .=.+1
num: .=.+1
zermp: .=.+1
minflg: .=.+1
adrflg: .=.+1
dot: .=.+1
addr: .=.+1
addr1: .=.+1
addr2: .=.+1
eofp: .=.+1
zerop: .=.+1
dskadr: .=.+1
linsiz: .=.+1
tfi: .=.+1
fchrno: .=.+1
lchrno: .=.+1
lastre: .=.+1
bett1: .=.+1
bett2: .=.+1
wrflg: .=.+1
apt1: .=.+1
"[page cuts off one label]
"** 08-rest.pdf page 29
"[handwritten page number top right of scan - 22]
sfo: .=.+1
sctal: .=.+1
sctalp: .=.+1
char: .=.+1
fbuf: .=.+4 "not sure if this is fbuf, but
tbuf: .=.+64 "there is a write; tbuf; 64 call
line: .=.+64
nlist: .=.+50
"??? ?list: .=.+50, unable to determine label
compbuf: .=.+100
dskbuf: .=.+1 "[line crossed out - scan markup]
lnodes: .=.+1000