X-Git-Url: https://git.danieliu.xyz/?p=dotfiles.git;a=blobdiff_plain;f=Scripts%2Fcormackscript;h=9f7cc728e616c85f7b3aed948e5c413483e8d139;hp=ed12e5770029b6b55067da3353302d3de25bb9e4;hb=bb16cdd8187a06a71fb5b3b3afe8c3c9ad3b81a3;hpb=1e538d57480c7eba19393a8b365f662bf0a6dfb5 diff --git a/Scripts/cormackscript b/Scripts/cormackscript index ed12e57..9f7cc72 100755 --- a/Scripts/cormackscript +++ b/Scripts/cormackscript @@ -1,65 +1,82 @@ #!/bin/sh -# CORMACKSCRIPT General Syntax -# -# IMPORTANT! -# Make sure you change the 'scriptfile' and 'targetfile' variables as you see fit. -# In addition, place -# -# For extra niceness, if you have the racket program installed it will auto execute it. +# CORMACKSCRIPT +# written by daniel # +# Usage: cormackscript [cmks file] [output file] # -# Example: -# -# A CormackScript file contains 3 columns separated by whitespace in the format -# opcode target source -# +# IMPORTANT! +# Place the comments (important to get spaces right too): +# ; CMKS START +# ; CMKS END +# into your racket program, the 'assembled' code will be injected here. # +# For extra niceness, if you have the racket program installed it will auto execute it your file. # -# You can comment with ; +# A CormackScript file contains 3 columns separated by whitespace in the format [opcode|target|source] +# - The inc, print and read instructions read from the first column +# - The quit instruction can be standalone +# - you can comment with ; +# - extraneous whitespace is ignored # +# Here's some sample code +# =-=-=-=-=-=-=-=-=-=-=-=-=-=-= +# ; setting up memory +# 6 ; [0]: first instruction +# 1 ; [1]: literal 1 +# 2 ; [2]: literal 2 +# 7 ; [3]: literal 7 +# 0 ; [4]: n +# 0 ; [5]: acc # -# Sample code -# +# ; actual instructions +# read 4 ; input n +# addeq 00 4 ; skip next if n = 0 +# add 00 2 ; skip next 2 instructions +# print 5 ; out acc +# quit +# add 05 04 ; acc = acc + n +# sub 04 01 ; n = n - 1 +# sub 00 03 ; go back 7 (from next instr) +# =-=-=-=-=-=-=-=-=-=-=-=-=-=-= # - -scriptfile="A12d.cmks" # cormackscript file -targetfile="A12d.rkt" # destination file - # You can redefine these to whatever you want inst0="inc" # [tt] = [tt] + 1 inst1="add" # [tt] = [tt] + [ss] inst2="sub" # [tt] = max([tt] - [ss], 0) inst3="mov" # [tt] = [ss] -inst4="" # [tt] = [tt] + 1, if [ss] = 0 -inst5="" # [tt] = [[ss]] -inst6="" # [[tt]] = [ss] +inst4="addeq" # [tt] = [tt] + 1, if [ss] = 0 +inst5="fetch" # [tt] = [[ss]] +inst6="store" # [[tt]] = [ss] inst7="print" # display [ss] inst8="read" # read [tt] inst9="quit" # halt # Config ends here =-=-=-=-=-=-=-=-= -sub() { - cat "$scriptfile" |\ - sed 's/\s*;.*//g; /^\s*$/ d;' |\ - sed "s/$inst0/0/" - - # finally removes any non alphanumerics in case sm breaks -} +[ "$#" -ne 2 ] && { echo "Usage: cormackscript [cmks file] [output file]" && exit 1; } +scriptfile="$1"; targetfile="$2" +[ -f "$scriptfile" ] && [ -f "$targetfile" ] || { echo "Either $scriptfile or $targetfile do not exist" && exit 1; } -inject() { - tmp="$(mktemp 'tempXXX')" - sub >> "$tmp" - sed -i "/\; CMKS START/,/\; CMKS END/{/\; CMKS START/!{/\; CMKS END/!d}}; /\; CMKS START/r $tmp" "$targetfile" - rm "$tmp" -} - - -[ -f "$scriptfile" ] && [ -f "$targetfile" ] || { echo "You are missing a file, please check that the current directory contains $scriptfile and $targetfile" && exit 1; } +tmp="$(mktemp 'tempXXX')" +cat "$scriptfile" |\ + sed -r "s/\s*;.*//g; /^\s*$/ d; s/\s+/ /g; s/^\s+//" |\ + awk " + /^$inst0\s/ {printf \"0%02d00 ; [%02d] = [%02d] + 1\n\",\$2,\$2,\$2} + /^$inst1\s/ {printf \"1%02d%02d ; [%02d] = [%02d] + [%02d]\n\",\$2,\$3,\$2,\$2,\$3} + /^$inst2\s/ {printf \"2%02d%02d ; [%02d] = max([%02d] - [%02d], 0)\n\",\$2,\$3,\$2,\$2,\$3} + /^$inst3\s/ {printf \"3%02d%02d ; [%02d] = [%02d]\n\",\$2,\$3,\$2,\$3} + /^$inst4\s/ {printf \"4%02d%02d ; [%02d] = [%02d] + 1, if [%02d] = 0\n\",\$2,\$3,\$2,\$2,\$3} + /^$inst5\s/ {printf \"5%02d%02d ; [%02d] = [[%02d]]\n\",\$2,\$3,\$2,\$3} + /^$inst6\s/ {printf \"6%02d%02d ; [[%02d]] = [%02d]\n\",\$2,\$3,\$2,\$3} + /^$inst7\s/ {printf \"700%02d ; display [%02d]\n\",\$2,\$2} + /^$inst8\s/ {printf \"8%02d00 ; read [%02d]\n\",\$2,\$2} + /^$inst9/ {print \"90000 ; halt\"} + !/^$inst0\s|^$inst1\s|^$inst2\s|^$inst3\s|^$inst4\s|^$inst5\s|^$inst6\s|^$inst7\s|^$inst8\s|^$inst9/ + " >> "$tmp" +sed -i "/\; CMKS START/,/\; CMKS END/{/\; CMKS START/!{/\; CMKS END/!d}}; /\; CMKS START/r $tmp" "$targetfile" +rm "$tmp" # If racket is installed run it directly -# [ ! -z "$(command -v racket)" ] && racket "$targetfile" - -inject +[ ! -z "$(command -v racket)" ] && racket "$targetfile" && echo ""