Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle string literals within macro arguments #685

Merged
merged 7 commits into from
Feb 17, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
280 changes: 219 additions & 61 deletions src/asm/lexer.c

Large diffs are not rendered by default.

12 changes: 11 additions & 1 deletion test/asm/macro-arg-in-string.asm
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
print1: MACRO
if _NARG == 2
assert !STRCMP("\1", \2)
endc
PRINTLN "\1"
ENDM

print1 John "Danger" Smith
print1 \\A\nB
print1 \\\\A\\nB\n, "\\\\A\\nB\n"
print1 C\
D
print1 E\!F ; illegal character escape
Expand All @@ -15,3 +18,10 @@ ENDM

s EQUS "hello"
iprint s

symprint: MACRO
PRINTLN {\1}
ENDM

hello EQUS "\"goodbye\""
symprint s
2 changes: 1 addition & 1 deletion test/asm/macro-arg-in-string.err
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
ERROR: macro-arg-in-string.asm(9) -> macro-arg-in-string.asm::print1(2):
ERROR: macro-arg-in-string.asm(12):
Illegal character escape '!'
error: Assembly aborted (1 errors)!
7 changes: 4 additions & 3 deletions test/asm/macro-arg-in-string.out
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
John "Danger" Smith
\A
B
\\A\nB

CD
E\F
E!F
hello
goodbye
3 changes: 2 additions & 1 deletion test/asm/multi-line-strings.asm
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ printarg: MACRO
ENDM

printarg "
printarg """
printarg """multi-line
string argument"""

EMPTY1 EQUS ""
EMPTY2 EQUS "\ ; comment
Expand Down
5 changes: 4 additions & 1 deletion test/asm/multi-line-strings.err
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
warning: multi-line-strings.asm(34): [-Wuser]
ERROR: multi-line-strings.asm(23):
Unterminated string
warning: multi-line-strings.asm(35): [-Wuser]
check the line number
error: Assembly aborted (1 errors)!
6 changes: 4 additions & 2 deletions test/asm/multi-line-strings.out
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ The multi-line string can contain:
!
arg <">
arg (")
arg <""">
arg (""")
arg <"""multi-line
string argument""">
arg ("""multi-line
string argument""")
()
14 changes: 14 additions & 0 deletions test/asm/quine.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
R:MACRO
REPT _NARG
PRINT STRSUB("\n\"\\ ENRST1ABCDFGHIMOPU_n#()+,:>",\1+1,1)
SHIFT
ENDR
ENDM
N:MACRO
R \#
REPT _NARG
PRINT"\1",STRSUB("\n,",(_NARG>1)+1,1)
SHIFT
ENDR
ENDM
N 6,29,18,10,12,6,19,0,6,4,20,8,3,22,5,10,6,15,0,20,6,17,5,8,3,7,8,6,7,21,11,25,1,2,23,2,1,2,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,1,28,2,9,27,9,28,9,26,0,7,16,17,14,8,0,4,5,13,6,0,4,5,13,18,0,5,29,18,10,12,6,19,0,3,6,3,2,24,0,6,4,20,8,3,22,5,10,6,15,0,20,6,17,5,8,1,2,9,1,28,7,8,6,7,21,11,25,1,2,23,28,1,28,25,22,5,10,6,15,30,9,26,27,9,28,9,26,0,7,16,17,14,8,0,4,5,13,6,0,4,5,13,18,0,3,5,3
Empty file added test/asm/quine.err
Empty file.
14 changes: 14 additions & 0 deletions test/asm/quine.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
R:MACRO
REPT _NARG
PRINT STRSUB("\n\"\\ ENRST1ABCDFGHIMOPU_n#()+,:>",\1+1,1)
SHIFT
ENDR
ENDM
N:MACRO
R \#
REPT _NARG
PRINT"\1",STRSUB("\n,",(_NARG>1)+1,1)
SHIFT
ENDR
ENDM
N 6,29,18,10,12,6,19,0,6,4,20,8,3,22,5,10,6,15,0,20,6,17,5,8,3,7,8,6,7,21,11,25,1,2,23,2,1,2,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,1,28,2,9,27,9,28,9,26,0,7,16,17,14,8,0,4,5,13,6,0,4,5,13,18,0,5,29,18,10,12,6,19,0,3,6,3,2,24,0,6,4,20,8,3,22,5,10,6,15,0,20,6,17,5,8,1,2,9,1,28,7,8,6,7,21,11,25,1,2,23,28,1,28,25,22,5,10,6,15,30,9,26,27,9,28,9,26,0,7,16,17,14,8,0,4,5,13,6,0,4,5,13,18,0,3,5,3
4 changes: 4 additions & 0 deletions test/asm/quine2.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
q: macro
println \1,"\1"
endm
q "q: macro\n\tprintln \\1,\"\\1\"\nendm\n\tq "
Empty file added test/asm/quine2.err
Empty file.
4 changes: 4 additions & 0 deletions test/asm/quine2.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
q: macro
println \1,"\1"
endm
q "q: macro\n\tprintln \\1,\"\\1\"\nendm\n\tq "
38 changes: 38 additions & 0 deletions test/asm/raw-macro-args.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
printargs: MACRO
rept _NARG
println \1
shift
endr
ENDM

printlit: MACRO
rept _NARG
println "\1"
shift
endr
ENDM

NUM EQU 42
STR EQUS "str\"ing"

printargs NUM
printargs "{d:NUM}"
printargs "{STR}", 16 ; comment 1
printargs "\"literal \\\"\\\\\\\"\""
printargs "literal \"\\\"", \ ; comment 2
"""multi-"line"
""string"" arg"""
printargs MUL(2.0\, 3.0)
printargs "unclosed

printlit NUM
printlit "{d:NUM}"
printlit "{STR}", 16 ; comment 3
printlit "\"literal \\\"\\\\\\\"\""
printlit "literal \"\\\"", \ ; comment 4
"""multi-"line"
""string"" arg"""
printlit MUL(2.0\, 3.0)
printlit this\n is\, \{not\} a\\n syntax\" error
printlit "unclosed
printlit """EOF
9 changes: 9 additions & 0 deletions test/asm/raw-macro-args.err
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
ERROR: raw-macro-args.asm(26):
Unterminated string
ERROR: raw-macro-args.asm(26) -> raw-macro-args.asm::printargs(2) -> raw-macro-args.asm::printargs::REPT~1(3):
Unterminated string
ERROR: raw-macro-args.asm(37):
Unterminated string
ERROR: raw-macro-args.asm(38):
Unterminated string
error: Assembly aborted (4 errors)!
23 changes: 23 additions & 0 deletions test/asm/raw-macro-args.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
$2A
42
str"ing
$10
"literal \"\\\""
literal "\"
multi-"line"
""string"" arg
$60000
unclosed
NUM
"42"
"str\"ing"
16
"\"literal \\\"\\\\\\\"\""
"literal \"\\\""
"""multi-"line"
""string"" arg"""
MUL(2.0, 3.0)
this
is, {not} a\n syntax" error
"unclosed
"""EOF