redid cormackscript
[dotfiles.git] / Scripts / cormackscript
1 #!/bin/sh
2
3 # CORMACKSCRIPT 
4 # written by daniel
5 #
6 # Usage: cormackscript [cmks file] [output file]
7 #
8 # IMPORTANT!
9 # Place the comments (important to get spaces right too):
10 # ; CMKS START
11 # ; CMKS END
12 # into your racket program, the 'compiled' code will be injected here.
13 #
14 # For extra niceness, if you have the racket program installed it will auto execute it.
15 #
16 # A CormackScript file contains 3 columns separated by whitespace in the format [opcode|target|source]  
17 # The inc, print and read instructions read from the first column
18 # The quit instruction can be standalone
19 #
20 # Here's some sample code
21 #
22 # mov 20 30
23 # inc 1
24 # read 21
25 # print 21
26 # quit
27 #
28 # Note, for commands that only need one param you don't need to add empty columns
29 # oh yeah, you can also comment with ;
30
31 # You can redefine these to whatever you want
32
33 inst0="inc"   # [tt] = [tt] + 1
34 inst1="add"   # [tt] = [tt] + [ss]
35 inst2="sub"   # [tt] = max([tt] - [ss], 0)
36 inst3="mov"   # [tt] = [ss]
37 inst4="addeq" # [tt] = [tt] + 1, if [ss] = 0
38 inst5="fetch" # [tt] = [[ss]]
39 inst6="store" # [[tt]] = [ss]
40 inst7="print" # display [ss]
41 inst8="read"  # read [tt]
42 inst9="quit"  # halt
43
44 # Config ends here =-=-=-=-=-=-=-=-=
45
46 [ "$#" -ne 2 ] && { echo "Usage: cormackscript [cmks file] [output file]" && exit 1; }
47 scriptfile="$1"; targetfile="$2"
48 [ -f "$scriptfile" ] && [ -f "$targetfile" ] || { echo "Either $scriptfile or $targetfile do not exist" && exit 1; }
49
50 tmp="$(mktemp 'tempXXX')"
51 cat "$scriptfile" |\
52     sed -r "s/\s*;.*//g; /^\s*$/ d; s/\s+/ /g; s/^\s+//" |\
53         awk "
54                 /^$inst0\s/ {printf \"0%02d00 ; [%02d] = [%02d] + 1\n\",\$2,\$2,\$2}
55                 /^$inst1\s/ {printf \"1%02d%02d ; [%02d] = [%02d] + [%02d]\n\",\$2,\$3,\$2,\$2,\$3}
56                 /^$inst2\s/ {printf \"2%02d%02d ; [%02d] = max([%02d] - [%02d], 0)\n\",\$2,\$3,\$2,\$2,\$3}
57                 /^$inst3\s/ {printf \"3%02d%02d ; [%02d] = [%02d]\n\",\$2,\$3,\$2,\$3}
58                 /^$inst4\s/ {printf \"4%02d%02d ; [%02d] = [%02d] + 1, if [%02d] = 0\n\",\$2,\$3,\$2,\$2,\$3}
59                 /^$inst5\s/ {printf \"5%02d%02d ; [%02d] = [[%02d]]\n\",\$2,\$3,\$2,\$3}
60                 /^$inst6\s/ {printf \"6%02d%02d ; [[%02d]] = [%02d]\n\",\$2,\$3,\$2,\$3}
61                 /^$inst7\s/ {printf \"700%02d ; display [%02d]\n\",\$2,\$2}
62                 /^$inst8\s/ {printf \"8%02d00 ; read [%02d]\n\",\$2,\$2}
63                 /^$inst9/ {print \"90000 ; halt\"}
64                 !/^$inst0\s|^$inst1\s|^$inst2\s|^$inst3\s|^$inst4\s|^$inst5\s|^$inst6\s|^$inst7\s|^$inst8\s|^$inst9/
65             " >> "$tmp"
66 sed -i "/\; CMKS START/,/\; CMKS END/{/\; CMKS START/!{/\; CMKS END/!d}}; /\; CMKS START/r $tmp" "$targetfile"
67 rm "$tmp"
68
69 # If racket is installed run it directly
70 [ ! -z "$(command -v racket)" ] && racket "$targetfile" && echo ""