2021-12-09 15:48:51 -05:00
|
|
|
from method import Argument, Method
|
2021-09-30 23:58:50 -04:00
|
|
|
from mode import Fundamental
|
2021-12-28 22:47:24 -05:00
|
|
|
from lex import Grammar, PatternRule, PatternMatchRule, RegionRule
|
2021-12-09 15:48:51 -05:00
|
|
|
import re
|
2021-09-30 23:58:50 -04:00
|
|
|
|
2021-12-28 22:47:24 -05:00
|
|
|
class CommentGrammar(Grammar): pass
|
|
|
|
|
|
|
|
comment_rule = RegionRule('comment', r'\((?:[ \t\n]|$)', CommentGrammar, r'\)(?:[ \t\n]|$)')
|
|
|
|
|
|
|
|
CommentGrammar.rules = [
|
|
|
|
comment_rule,
|
|
|
|
PatternRule('data', r'[^\(\) \t\n][^ \t\n]*|[\(\)][^ \t\n]+'),
|
|
|
|
PatternRule('spaces', r'[ \t]+'),
|
|
|
|
]
|
|
|
|
|
2021-09-30 23:58:50 -04:00
|
|
|
class TalGrammar(Grammar):
|
|
|
|
rules = [
|
|
|
|
PatternRule('spaces', '[ \t]+'),
|
2021-12-28 22:47:24 -05:00
|
|
|
comment_rule,
|
2021-09-30 23:58:50 -04:00
|
|
|
PatternRule('delimiter', r'[\[\]{}]'),
|
2021-12-23 00:44:45 -05:00
|
|
|
PatternRule('tal.inst', r'(BRK|LIT|INC|POP|DUP|NIP|SWP|OVR|ROT|EQU|NEQ|GTH|LTH|JMP|JCN|JSR|STH|LDZ|STZ|LDR|STR|LDA|STA|DEI|DEO|ADD|SUB|MUL|DIV|AND|ORA|EOR|SFT)2?k?r?(?![a-zA-z0-9_])'), # instructions
|
2021-12-09 15:48:51 -05:00
|
|
|
PatternRule('tal.defmacro', r'%[^ \t\n]+'), # macro-define
|
|
|
|
PatternRule('tal.pad', r'\|[^ \t\n]+'), # pad (absolute)
|
|
|
|
PatternRule('tal.pad', r'\$[^ \t\n]+'), # pad (relative)
|
|
|
|
PatternRule('tal.deflabel', r'@[^ \t\n]+'), # label-define
|
|
|
|
PatternRule('tal.defsublabel', r'&[^ \t\n]+'), # sublabel-define
|
|
|
|
PatternRule('tal.spacer', r'/[^ \t\n]+'), # sublabel-spacer
|
|
|
|
PatternRule('tal.number', r'#[0-9a-fA-F]+'), # literal number
|
|
|
|
PatternRule('tal.addr', r'\.[^/ \t\n]+'), # little addr (zero page)
|
|
|
|
PatternRule('tal.addr', r',[^/ \t\n]+'), # little addr (relative)
|
|
|
|
PatternRule('tal.addr', r';[^/ \t\n]+'), # little addr (absolute)
|
|
|
|
PatternRule('tal.addr', r':[^/ \t\n]+'), # raw addr
|
|
|
|
PatternRule('tal.addr', r'\'[^/ \t\n]+'), # raw char
|
|
|
|
PatternRule('tal.addr', r'\"[^/ \t\n]+'), # raw word
|
2021-12-28 22:47:24 -05:00
|
|
|
PatternMatchRule('x', r'(~)(.+)$', 'tal.include', 'tal.filename'),
|
2021-12-09 15:48:51 -05:00
|
|
|
PatternRule('tal.word', r'[^ \t\n]+'),
|
2021-09-30 23:58:50 -04:00
|
|
|
PatternRule('eol', '\n'),
|
|
|
|
]
|
|
|
|
|
|
|
|
# white is for delimiters, operators, numbers
|
|
|
|
default = ('default', 'default')
|
|
|
|
|
|
|
|
# magenta is for keywords/builtins
|
|
|
|
lo_magenta = ('magenta202', 'default')
|
|
|
|
hi_magenta = ('magenta414', 'default')
|
|
|
|
|
|
|
|
# red is for comments
|
|
|
|
lo_red = ('red300', 'default')
|
|
|
|
hi_red = ('red511', 'default')
|
|
|
|
|
|
|
|
# orange is for macro definitions, headers and constants
|
|
|
|
hi_orange = ('yellow531', 'default')
|
|
|
|
lo_orange = ('yellow520', 'default')
|
|
|
|
|
|
|
|
# yellow is for parts of macros
|
|
|
|
hi_yellow = ('yellow551', 'default')
|
|
|
|
lo_yellow = ('yellow330', 'default')
|
|
|
|
|
|
|
|
# green is for strings and characters
|
|
|
|
lo_green = ('green030', 'default')
|
|
|
|
hi_green = ('green050', 'default')
|
|
|
|
|
|
|
|
# cyan is for types
|
|
|
|
lo_cyan = ('cyan033', 'default')
|
|
|
|
hi_cyan = ('cyan155', 'default')
|
|
|
|
|
|
|
|
# blue is definitions, functions and some macros
|
|
|
|
lo_blue = ('blue113', 'default')
|
|
|
|
hi_blue = ('blue225', 'default')
|
|
|
|
|
2022-01-27 21:55:10 -05:00
|
|
|
class Talasm(Method):
|
|
|
|
'''Assemble the given .tal file'''
|
|
|
|
bname = '*Talasm*'
|
|
|
|
def run_pipe(self, w, args, switch, mname=None):
|
|
|
|
return w.application.run_pipe(args, w.buffer, self.bname, switch, mname)
|
2022-03-24 22:41:26 -04:00
|
|
|
def get_dest(self, w):
|
2022-01-27 21:55:10 -05:00
|
|
|
path = w.buffer.path
|
|
|
|
if path.endswith('.tal'):
|
2022-03-24 22:41:26 -04:00
|
|
|
return path[:-4] + '.rom'
|
2022-01-27 21:55:10 -05:00
|
|
|
else:
|
|
|
|
raise Exception('invalid path: %s' % path)
|
2022-03-24 22:41:26 -04:00
|
|
|
def _execute(self, w, **vargs):
|
|
|
|
path = w.buffer.path
|
|
|
|
dest = self.get_dest(w)
|
2022-01-27 21:55:10 -05:00
|
|
|
args = ['uxnasm', path, dest]
|
|
|
|
r = self.run_pipe(w, args, lambda x: x != 0, 'error')
|
|
|
|
b = w.application.get_buffer_by_name(self.bname)
|
|
|
|
b.orig_path = w.buffer.path
|
|
|
|
if r == 0: w.set_error(b.lines[-2])
|
|
|
|
|
2022-03-24 22:41:26 -04:00
|
|
|
class TalCheck(Talasm):
|
|
|
|
'''Check the syntax of the current .tal file'''
|
|
|
|
def get_dest(self, w):
|
|
|
|
return '/dev/null'
|
|
|
|
|
2021-12-09 15:48:51 -05:00
|
|
|
class Taldoc(Method):
|
|
|
|
'''View documentation'''
|
|
|
|
ops = {
|
|
|
|
# instructions
|
|
|
|
'BRK': ['Break', [[], []], None, 'halt the program'],
|
|
|
|
'LIT': ['Literal', [[], ['a']], None, 'push the next value onto the stack'],
|
|
|
|
'INC': ['Increment', [['a'] , ['b']], None, 'adds one to the top of the stack'],
|
|
|
|
'POP': ['Pop', [['a'], []], None, 'remove the top of the stack'],
|
|
|
|
'DUP': ['Duplicate', [['a'], ['a', 'a']], None, 'duplicate the top of the stack'],
|
|
|
|
'NIP': ['Nip', [['a', 'b'], ['b']], None, 'remove the second value (a)'],
|
|
|
|
'SWP': ['Swap', [['a', 'b'], ['b', 'a']], None, 'swap the top two stack values'],
|
|
|
|
'OVR': ['Over', [['a', 'b'], ['a', 'b', 'a']], None, 'duplicate the second value (a) to the top of the stack'],
|
2021-12-20 00:10:53 -05:00
|
|
|
'ROT': ['Rotate', [['a', 'b', 'c'], ['b', 'c', 'a']], None, 'rotate the top three values to the left'],
|
2021-12-09 15:48:51 -05:00
|
|
|
# logic
|
2021-12-20 00:10:53 -05:00
|
|
|
'EQU': ['Equal', [['a', 'b'], ['bool^']], None, 'push 01 if a == b; push 00 otherwise'],
|
|
|
|
'NEQ': ['Not Equal', [['a', 'b'], ['bool^']], None, 'push 01 if a != b; push 00 otherwise'],
|
2021-12-09 15:48:51 -05:00
|
|
|
'GTH': ['Greater Than', [['a', 'b'], ['bool^']], None, 'push 01 if a > b; push 00 otherwise'],
|
|
|
|
'LTH': ['Less Than', [['a', 'b'], ['bool^']], None, 'push 01 if a < b; push 00 otherwise'],
|
|
|
|
# control flow
|
2021-12-20 00:10:53 -05:00
|
|
|
'JMP': ['Jump', [['x'], []], None, 'modify the pc using addr'],
|
|
|
|
'JCN': ['Jump Conditional', [['bool^ addr'], []], None, 'if bool != 00, modify the pc using addr'],
|
|
|
|
'JSR': ['Jump Stash Return', [['addr'], []], [[], ['pc']], 'store pc onto return stack; modify pc using addr'],
|
|
|
|
'STH': ['Stash', [['a'], []], [[], ['a']], 'move the top of the stack to the return stack'],
|
2021-12-09 15:48:51 -05:00
|
|
|
# memory
|
2021-12-20 00:10:53 -05:00
|
|
|
'LDZ': ['Load Zero-Page', [['addr^'], ['val']], None, 'load value from first 256 bytes of memory onto the stack'],
|
2021-12-09 15:48:51 -05:00
|
|
|
'STZ': ['Store Zero-Page', [['val', 'addr^'], []], None, 'write top of stack into the first 256 bytes of memory'],
|
2021-12-20 00:10:53 -05:00
|
|
|
'LDR': ['Load Relative', [['addr^'], ['val']], None, 'load relative address onto the stack'],
|
|
|
|
'STR': ['Store Relative', [['val', 'addr^'], []], None, 'write top of stack to relative address'],
|
|
|
|
'LDA': ['Load Absolute', [['addr*'], ['val']], None, 'load absolute address onto the stack'],
|
|
|
|
'STA': ['Store Absolute', [['val', 'addr*'], []], None, 'write top of stack to absolute address'],
|
|
|
|
'DEI': ['Device In', [['addr^'], ['val']], None, 'load from the given device onto the stack'],
|
|
|
|
'DEO': ['Device Out', [['val', 'addr^'], []], None, 'write top of stack to the given device'],
|
2021-12-09 15:48:51 -05:00
|
|
|
# arithmetic
|
2021-12-20 00:10:53 -05:00
|
|
|
'ADD': ['Add', [['a', 'b'], ['a+b']], None, 'addition (a + b)'],
|
|
|
|
'SUB': ['Subtract', [['a', 'b'], ['a-b']], None, 'subtraction (a - b)'],
|
|
|
|
'MUL': ['Multiply', [['a', 'b'], ['a*b']], None, 'multiplication (a * b)'],
|
|
|
|
'DIV': ['Divide', [['a', 'b'], ['a/b']], None, 'division (a / b)'],
|
2021-12-09 15:48:51 -05:00
|
|
|
# bitwise
|
2021-12-20 00:10:53 -05:00
|
|
|
'AND': ['And', [['a', 'b'], ['a&b']], None, 'bitwise-and (a & b)'],
|
|
|
|
'ORA': ['Or', [['a', 'b'], ['a|b']], None, 'bitwise-or (a | b)'],
|
|
|
|
'EOR': ['Exclusive Or', [['a', 'b'], ['a^b']], None, 'bitwise-xor (a ^ b)'],
|
|
|
|
'SFT': ['Shift', [['a', 'b^'], ['c']], None, 'bitshift left (b >> 4) then right (b & 0xf)'],
|
2021-12-09 15:48:51 -05:00
|
|
|
}
|
|
|
|
inst_re = re.compile(r'^([A-Z]{3})(2?k?r?)$')
|
|
|
|
addr_dict = {
|
|
|
|
'.': 'zero-page address',
|
|
|
|
',': 'relative address',
|
|
|
|
';': 'absolute address',
|
|
|
|
':': 'raw address',
|
|
|
|
'\'': 'raw character',
|
|
|
|
'\"': 'raw word',
|
|
|
|
}
|
2021-12-12 15:55:35 -05:00
|
|
|
def find_macro(self, name, w):
|
|
|
|
toks = w.get_highlighter().tokens
|
|
|
|
target = '%' + name
|
|
|
|
found = False
|
|
|
|
start = None
|
|
|
|
end = None
|
|
|
|
defn = None
|
|
|
|
for line in toks:
|
|
|
|
for t in line:
|
|
|
|
if not found:
|
|
|
|
found = t.name == "tal.defmacro" and t.string == target
|
|
|
|
elif start is None:
|
|
|
|
if t.name == 'delimiter' and t.string == '{':
|
|
|
|
start = (t.x + 1, t.y)
|
|
|
|
elif end is None:
|
|
|
|
if t.name == 'delimiter' and t.string == '}':
|
|
|
|
end = (t.x, t.y)
|
|
|
|
break
|
|
|
|
if start is None or end is None:
|
|
|
|
return None
|
|
|
|
(sx, sy) = start
|
|
|
|
(ex, ey) = end
|
|
|
|
lines = w.buffer.lines
|
|
|
|
if sy == ey:
|
|
|
|
# 1 lines
|
|
|
|
return lines[sy][sx:ex]
|
|
|
|
elif sy == ey + 1:
|
|
|
|
# 2 lines
|
|
|
|
return lines[sy][sx:] + ' ' + lines[ey][:e.x]
|
|
|
|
else:
|
|
|
|
# 3+ lines
|
|
|
|
out = []
|
|
|
|
out.append(lines[sy][sx:])
|
|
|
|
out.extend(lines[sy+1:ey])
|
|
|
|
out.append(lines[ey][:ex])
|
|
|
|
return ' '.join(out)
|
2021-12-09 15:48:51 -05:00
|
|
|
def _execute(self, w, **vargs):
|
|
|
|
tok = w.get_token()
|
|
|
|
word = tok.string
|
|
|
|
if word is None:
|
|
|
|
w.set_error('no word selected')
|
|
|
|
return
|
|
|
|
m = self.inst_re.match(word)
|
|
|
|
if m and m.group(1) in self.ops:
|
|
|
|
prefix, suffix = m.groups()
|
2021-12-20 00:10:53 -05:00
|
|
|
glyph = '^'
|
2021-12-09 15:48:51 -05:00
|
|
|
name, ds, rs, desc = self.ops[prefix]
|
2021-12-20 00:10:53 -05:00
|
|
|
if 'r' in suffix:
|
|
|
|
rs, ds = ds, rs
|
|
|
|
if '2' in suffix:
|
|
|
|
glyph = '*'
|
|
|
|
if 'k' in suffix:
|
2021-12-23 00:44:45 -05:00
|
|
|
if ds is not None:
|
|
|
|
ds = [ds[0], ds[0] + ds[1]]
|
|
|
|
if rs is not None:
|
|
|
|
rs = [rs[0], rs[0] + rs[1]]
|
2021-12-20 00:10:53 -05:00
|
|
|
def mark(x):
|
|
|
|
if x.endswith('^') or x.endswith('*'):
|
|
|
|
return x
|
|
|
|
else:
|
|
|
|
return x + glyph
|
|
|
|
def stk(xss):
|
|
|
|
return ' -- '.join([' '.join([mark(x) for x in xs]) for xs in xss])
|
2021-12-09 15:48:51 -05:00
|
|
|
if rs is None:
|
|
|
|
msg = '%s (%s) %s: %s' % (word, stk(ds), name, desc)
|
2021-12-20 00:10:53 -05:00
|
|
|
elif ds is None:
|
2021-12-23 00:44:45 -05:00
|
|
|
msg = '%s {%s} %s: %s' % (word, stk(rs), name, desc)
|
2021-12-09 15:48:51 -05:00
|
|
|
else:
|
|
|
|
msg = '%s (%s) {%s} %s: %s' % (word, stk(ds), stk(rs), name, desc)
|
|
|
|
w.set_error(msg)
|
|
|
|
elif tok.name == 'tal.number':
|
|
|
|
n = int(word[1:], 16)
|
|
|
|
w.set_error('%r is a number (%d)' % (word, n))
|
|
|
|
elif tok.name == 'tal.defmacro':
|
|
|
|
w.set_error('%r is a macro definition' % word)
|
|
|
|
elif tok.name == 'tal.pad':
|
|
|
|
n = int(word[1:], 16)
|
|
|
|
if word[0] == '|':
|
|
|
|
w.set_error('%r is an absolute pad (%d)' % (word, n))
|
|
|
|
else:
|
|
|
|
w.set_error('%r is a relative pad (+%d)' % (word, n))
|
|
|
|
elif tok.name == 'tal.deflabel':
|
|
|
|
w.set_error('%r is a label definition' % word)
|
|
|
|
elif tok.name == 'tal.defsublabel':
|
|
|
|
w.set_error('%r is a sublabel definition' % word)
|
|
|
|
elif tok.name == 'tal.addr':
|
|
|
|
kind = self.addr_dict.get(word[0], 'unknown address')
|
|
|
|
w.set_error('%r is a %s' % (word, kind))
|
|
|
|
else:
|
2021-12-12 15:55:35 -05:00
|
|
|
defn = self.find_macro(word, w)
|
|
|
|
if defn is None:
|
|
|
|
w.set_error('%r is not recognized' % word)
|
|
|
|
else:
|
|
|
|
w.set_error('%r is a macro: %s' % (word, defn))
|
2021-12-09 15:48:51 -05:00
|
|
|
|
|
|
|
|
2021-09-30 23:58:50 -04:00
|
|
|
class Tal(Fundamental):
|
|
|
|
name = 'tal'
|
|
|
|
extensions = ['.tal']
|
|
|
|
grammar = TalGrammar
|
2022-03-24 22:41:26 -04:00
|
|
|
actions = [Taldoc, Talasm, TalCheck]
|
2021-09-30 23:58:50 -04:00
|
|
|
colors = {
|
|
|
|
'tal.addr': hi_cyan,
|
|
|
|
'tal.defmacro': hi_blue,
|
|
|
|
'tal.addr': hi_yellow,
|
|
|
|
'tal.inst': hi_magenta,
|
|
|
|
'tal.deflabel': hi_blue,
|
|
|
|
'tal.defsublabel': hi_cyan,
|
2021-12-09 15:48:51 -05:00
|
|
|
'tal.number': hi_green,
|
2021-09-30 23:58:50 -04:00
|
|
|
'tal.pad': hi_orange,
|
|
|
|
'tal.spacer': hi_orange,
|
2021-12-28 22:47:24 -05:00
|
|
|
'tal.include': lo_cyan,
|
|
|
|
'tal.filename': hi_cyan,
|
2021-09-30 23:58:50 -04:00
|
|
|
}
|
2021-12-22 22:01:33 -05:00
|
|
|
opentokens = ('delimiter',)
|
|
|
|
opentags = {'(': ')', '[': ']', '{': '}'}
|
|
|
|
closetokens = ('delimiter',)
|
|
|
|
closetags = {')': '(', ']': '[', '}': '{'}
|
2021-09-30 23:58:50 -04:00
|
|
|
_bindings = {
|
2022-03-24 22:41:26 -04:00
|
|
|
'talasm': ('C-c c', 'C-c C-c',),
|
|
|
|
'tal-check': ('C-c s',),
|
2021-12-09 15:48:51 -05:00
|
|
|
'taldoc': ('C-c p',),
|
2021-09-30 23:58:50 -04:00
|
|
|
'close-paren': (')',),
|
|
|
|
'close-brace': ('}',),
|
|
|
|
'close-bracket': (']',),
|
|
|
|
}
|
|
|
|
|
|
|
|
install = Tal.install
|