-
Notifications
You must be signed in to change notification settings - Fork 2
/
z80_edit_functions.vim
140 lines (109 loc) · 3.21 KB
/
z80_edit_functions.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
" File: z80_edit_functions.vim
" Author: Krusty/Benediction
" Description: This file contains useful functions to help z80 development
" Last Modified: janvier 20, 2012
if exists('did_z80_edit_functions') || &cp || version < 700
finish
endif
let did_z80_edit_functions = 1
" Z80 assembler conversion {{{
" Split a line with several commands in several independent lines
" (allow to easily convert files from one assembly language to another one)
" Mainly written to convert sjamsplus source to vasm source
function! Z80SplitInstructionsInSeveralLines() range
let lnum = a:firstline
let s:linecounter = 0
while lnum <= a:lastline
let s:line = getline(lnum + s:linecounter)
" Works only on right lines
" and when : is not after a comment
if -1 == match(s:line, '.*:.*') || -1 != match(s:line, '^\s*[^:]*;\|\*')
let lnum = lnum + 1
continue
endif
let s:instructions = split(s:line, ':')
let s:empty = matchstr(s:line, '^\s*')
let s:code = substitute(s:line, '^\s*\(.*$\)', '\1', '')
let s:i=1
call append(lnum + s:linecounter, s:empty.'; '.s:code.' {{{ Automatic expanding')
" Copy each instruction
for instruction in s:instructions
if s:i == 1
call append(lnum+s:i+s:linecounter, ' '.instruction)
else
call append(lnum+s:i+s:linecounter, s:empty.instruction)
endif
let s:i=s:i +1
endfor
call append(lnum+s:i+s:linecounter, s:empty.'; }}}')
" Delete line
exec (lnum+s:linecounter)."d"
let s:linecounter = s:linecounter + s:i
let lnum = lnum + 1
endwhile
endfunction
function! Z80WholeFileSplitInstructionsInSeveralLines()
exec "%call Z80SplitInstructionsInSeveralLines()"
endfunction
" Remove the comments in C format and replace them by ;
function Z80TranslateCComments()
" /**
try
exec "%s/\\(\\s*\\)\\/\\*\\*/\\1 ;;/"
catch
endtry
" /*
try
exec "%s/\\(\\s*\\)\\/\\*/\\1 ;/"
catch
endtry
try
exec "%s/\\(\\s*\\)\\*\\//\\1;/"
catch
endtry
" * "
try
exec "%s/\\(\\s*\\)\\*/\\1;/"
catch
endtry
endfunction
" Convert the source file from sjamsplus format to vasm format.
" Conversion involves the following process:
" - Splitting of lines with several instructions
" - Translate sjasmplus comments
" - Remove output command
" - use the write macro syntax
"
" TODO write the macro transformation function
" TODO protect macro arguments
function! Z80Sjamsplus2vasm()
call Z80WholeFileSplitInstructionsInSeveralLines()
call Z80TranslateCComments()
" TODO use a list to comment these things
" remove the output command
try
exec "%s/^\\(\\s*output\\s*.*$\\)/;\\1 ; Automatic remove/"
catch
endtry
" comment the assertions which are not available :(
try
exec "%s/^\\(\\s*assert\\s*.*$\\)/;\\1 ; Automatic remove/"
catch
endtry
" Repetition
try
exec "%s/\\(.*[^e]\\)dup\\(\\s*.*\\)/\\1repeat\\2/"
catch
endtry
" todo use endrepeat when corrected
try
exec "%s/\\(.*\\)edup\\(\\s*.*\\)/\\1endr\\2/"
catch
endtry
" Remove bracket notation for ix/iy
try
exec "%s/\\[\\(i[xy]\\)\\(*\\)\\]/(\1\2)/"
catch
endtry
endfunction
" }}}