update
[dotfiles.git] / Scripts / cormackscript
1 #!/bin/sh
2
3 # CORMACKSCRIPT General Syntax
4 #
5 # IMPORTANT!
6 # Make sure you change the 'scriptfile' and 'targetfile' variables as you see fit.
7 # In addition, place  
8 #
9 # For extra niceness, if you have the racket program installed it will auto execute it.
10 #
11 #
12 # Example:
13 #
14 # A CormackScript file contains 3 columns separated by whitespace in the format
15 # opcode target source
16 #
17 #
18 #
19 # You can comment with ;
20 #
21 #
22 # Sample code
23
24 #
25
26 scriptfile="A12d.cmks" # cormackscript file
27 targetfile="A12d.rkt" # destination file
28
29 # You can redefine these to whatever you want
30
31 inst0="inc"   # [tt] = [tt] + 1
32 inst1="add"   # [tt] = [tt] + [ss]
33 inst2="sub"   # [tt] = max([tt] - [ss], 0)
34 inst3="mov"   # [tt] = [ss]
35 inst4=""      # [tt] = [tt] + 1, if [ss] = 0
36 inst5=""      # [tt] = [[ss]]
37 inst6=""      # [[tt]] = [ss]
38 inst7="print" # display [ss]
39 inst8="read"  # read [tt]
40 inst9="quit"  # halt
41
42 # Config ends here =-=-=-=-=-=-=-=-=
43
44 sub() {
45     cat "$scriptfile" |\
46         sed 's/\s*;.*//g; /^\s*$/ d;' |\
47         sed "s/$inst0/0/"
48
49         # finally removes any non alphanumerics in case sm breaks
50 }
51
52 inject() {
53     tmp="$(mktemp 'tempXXX')"
54     sub >> "$tmp"
55     sed -i "/\; CMKS START/,/\; CMKS END/{/\; CMKS START/!{/\; CMKS END/!d}}; /\; CMKS START/r $tmp" "$targetfile"
56     rm "$tmp"
57 }    
58
59
60 [ -f "$scriptfile" ] && [ -f "$targetfile" ] || { echo "You are missing a file, please check that the current directory contains $scriptfile and $targetfile" && exit 1; }
61
62 # If racket is installed run it directly
63 # [ ! -z "$(command -v racket)" ] && racket "$targetfile"
64
65 inject