diff --git a/PLati/Makefile b/PLati/Makefile new file mode 100644 index 0000000..7623517 --- /dev/null +++ b/PLati/Makefile @@ -0,0 +1,6 @@ +main: + -pass + +oper: + $$(git rev-parse --show-toplevel)/ass/plug -f algo.pks -u oper + $$(git rev-parse --show-toplevel)/ass/plug -f algo.pks -d oper "$$(m4 oper.m4)" -g oper diff --git a/PLati/algo.pks b/PLati/algo.pks index 3253625..f82c095 100644 --- a/PLati/algo.pks +++ b/PLati/algo.pks @@ -120,7 +120,38 @@ CREATE OR REPLACE PACKAGE BODY algo AS res := opr1; WHEN '.' THEN res := CONCAT(opr1, opr2); - -- #placeholder<oper> + WHEN '%' THEN + res := MOD(aint(opr1), aint(opr2)); + WHEN '|' THEN + res := CONCAT(opr1, opr2); + -- #placeholder<oper> BEGIN + WHEN '-' THEN + res := aint(opr1) - aint(opr2); + WHEN '+' THEN + res := aint(opr1) + aint(opr2); + WHEN '*' THEN + res := aint(opr1) * aint(opr2); + WHEN '/' THEN + res := aint(opr1) / aint(opr2); + WHEN '>' THEN + IF aint(opr1) > aint(opr2) THEN + res := TRUE_; + ELSE + res := FALSE_; + END IF; + WHEN '<' THEN + IF aint(opr1) < aint(opr2) THEN + res := TRUE_; + ELSE + res := FALSE_; + END IF; + WHEN '=' THEN + IF aint(opr1) = aint(opr2) THEN + res := TRUE_; + ELSE + res := FALSE_; + END IF; + -- #placeholder<oper> END ELSE NULL; END CASE; diff --git a/PLati/lexer.pks b/PLati/lexer.pks new file mode 100644 index 0000000..0cc1d72 --- /dev/null +++ b/PLati/lexer.pks @@ -0,0 +1,9 @@ +CREATE OR REPLACE PACKAGE algo AS + TYPE JMP IS ( + 'nop', + 'jmp', + 'je', + 'jne' + ); +END; +/ diff --git a/PLati/lexer.tbl b/PLati/lexer.tbl new file mode 100644 index 0000000..9520302 --- /dev/null +++ b/PLati/lexer.tbl @@ -0,0 +1,11 @@ +DROP TABLE tolex; + +CREATE TABLE tolex( + jump ENUM('nop', 'jmp', 'je', 'jne') + DESTINATION ASTRING, + assignemt ASTRING, + OPR1 ASTRING, + oper CHAR, + OPR2 ASTRING, + COMMENT VARCHAR2(256) +); diff --git a/PLati/oper.m4 b/PLati/oper.m4 new file mode 100644 index 0000000..3f3a219 --- /dev/null +++ b/PLati/oper.m4 @@ -0,0 +1,17 @@ +define(aoper,dnl +` WHEN ''$1'` THEN + res := aint(opr1) $1 aint(opr2)')dnl +define(loper,dnl +` WHEN ''$1'` THEN + IF aint(opr1) $1 aint(opr2) THEN + res := TRUE_; + ELSE + res := FALSE_; + END IF')dnl +aoper(`-'); +aoper(`+'); +aoper(`*'); +aoper(`/'); +loper(`>'); +loper(`<'); +loper(`='); diff --git a/ass/plug b/ass/plug new file mode 100755 index 0000000..1a3e7ad --- /dev/null +++ b/ass/plug @@ -0,0 +1,123 @@ +#!/bin/python3 +# PLUG - "PlacehoLder Un- and Generator" + +import sys +import re + +def usage(): + print( +'''{0} <options> + -d <name> <value> : define placeholder + -u <placeholder> : ungenerate placeholder (collapse) + -g <placeholder> : generate placeholder (expand) + -f <file> : specify I/O file + -h : print help and exit + Options are evaluated in the order they are found and can be repeated. + If multiple files are specified, actions apply to all of them. + \"@all\" is a special pseudo placeholder with the meaning \'every placeholder\'. + NOTE: do not forget to specify your file before the desired actions.'''.format(sys.argv[0])) + + +placeholders = {} +placeholder = '#placeholder<{0}>' +phl_col = placeholder + ' COLLAPSED' +phl_exp_beg = placeholder + ' BEGIN' +phl_exp_end = placeholder + ' END' +re_phl_col = re.compile('''^(.*){0}.*'''.format(phl_col.format('''(\w+)''')), re.M) +re_phl_exp_beg = re.compile('''^(.*){0}.*'''.format(phl_exp_beg.format('''(\w+)''')), re.M) +re_phl_exp_end = re.compile('''^.*{0}.*'''.format(phl_exp_end.format('''(\w+)''')), re.M) + + +def gen(s, phl): + ret = '' + l = 0 + is_all = (phl == '@all') + + for m in re_phl_col.finditer(s): + if((not is_all) and m.group(2) != phl): + continue + ret += s[l : m.start(0)] + ret += m.group(1) + phl_exp_beg.format(phl) + '\n' + ret += placeholders[phl] + ret += '\n' + m.group(1) + phl_exp_end.format(phl) + l = m.end(0) + ret += s[l:] + return ret + +def ungen(s, phl): + ret = '' + l = 0 + is_all = (phl == '@all') + for m in re_phl_exp_beg.finditer(s): + if((not is_all) and m.group(2) != phl): + continue + ret += s[l : m.start(0)] + ret += m.group(1) + phl_col.format(phl) + l = m.end(0) + for me in re_phl_exp_end.finditer(s[m.end(0):]): + if(me.group(1) != phl): + continue + l = m.end(0) + me.end(0) + break + ret += s[l:] + return ret + +def get_param(argv, i, opt): + try: + param = argv[i] + except: + print('Missing parameter to flag \'{0}\'.'.format(opt)) + exit(2) + return param + +def plug(argv): + sfiles = [] + i = -1 + while i < len(argv)-1: + i = i + 1 + + # 0 param opt + if argv[i] == '-h': + usage() + exit(0) + + # 2 param opt + if argv[i] == '-d': + try: + placeholders[argv[i+1]] = argv[i+2] + i = i + 2 + except: + print('Unterminated definition (-d).') + exit(3) + continue + + # 1 param opt + i = i + 1 + if argv[i-1] == '-u': + for sf in sfiles: + with open(sf, 'r') as f: + s = ungen(f.read(), get_param(argv, i, '-u')) + with open(sf, 'w') as f: + f.write(s) + continue + + if argv[i-1] == '-g': + for sf in sfiles: + with open(sf, 'r') as f: + s = gen(f.read(), get_param(argv, i, '-g')) + with open(sf, 'w') as f: + f.write(s) + continue + + if argv[i-1] == '-f': + sfiles.append(get_param(argv, i, '-f')) + continue + + print("Unrecognized flag '{0}'.".format(argv[i])) + usage() + exit(1) + return 0 + + +if __name__ == '__main__': + raise SystemExit(plug(sys.argv[1:]))