C언어 #define 처럼 컴파일러가 문법 치환해주는 건데 뒤늦게 추가하고 있음.
C언어처럼 매크로 함수로 인자 넘기거나 토큰 합치기도 추가하면 완벽해질 듯.
기존에도 macro 문이 있었는데 사실 잠시 코드 상에서 점프해서 수행하고 돌아오는 기능이었음.
#one #1 define
one puti
#로 시작하면 컴파일러에 문자열 자체를 넘김. define은 앞의 문자열을 뒤의 문자열로 치환. one이 1로 치환되어서 1이 출력.
#one { 1 puti } define
one
중괄호로 감싸면 감싼 부분만큼을 넘김. one이 1 puti로 치환. 1이 출력.
#three { 3 puti } define
#two #three define
#one #two define
one
이런 식으로 연이어서 쓰면 one이 two로 two가 three로 three가 3으로 치환돼서 결국 one은 3 puti로 치환. 3이 출력.
에러 표시 기능을 좀 신경쓰고 있는데 에러가 나면 잘못된 코드 위치를 줌.
what
예를 들어 뜬금없는 단어 하나를 컴파일하면
error : Unknown keyword
what in line 1, column 5
from file /home/mhcoma/sabr/build/a.sabrc
error : Parsing failure
error : Tokenization failure
error : Compilation failure
에러를 표시함. 근데 위의 매크로처럼 계속 치환되는 경우에는 거슬러 올라가서 잘못된 코드 위치를 짚어주도록 만들었다.
#three #what define
#two #three define
#one #two define
one
three가 뜬금없는 what으로 치환되는데 에러가 나야 함.
error : Unknown keyword
what in line 1, column 13
from file /home/mhcoma/sabr/build/a.sabrc
error : Parsing failure
error : Tokenization failure
three in line 2, column 12
from file /home/mhcoma/sabr/build/a.sabrc
error : Parsing failure
error : Tokenization failure
two in line 3, column 10
from file /home/mhcoma/sabr/build/a.sabrc
error : Parsing failure
error : Tokenization failure
one in line 4, column 4
from file /home/mhcoma/sabr/build/a.sabrc
error : Parsing failure
error : Tokenization failure
error : Compilation failure
거슬러 올라가서 해줌.
#:b.sabrc import
#one #two define
one
이런 식으로 위 부분의 코드를 b.sabrc에 따로 작성하고 임포트해도
error : Unknown keyword
what in line 1, column 13
from file /home/mhcoma/sabr/build/b.sabrc
error : Parsing failure
error : Tokenization failure
three in line 2, column 12
from file /home/mhcoma/sabr/build/b.sabrc
error : Parsing failure
error : Tokenization failure
two in line 2, column 10
from file /home/mhcoma/sabr/build/a.sabrc
error : Parsing failure
error : Tokenization failure
one in line 3, column 4
from file /home/mhcoma/sabr/build/a.sabrc
error : Parsing failure
error : Tokenization failure
error : Compilation failure
에러난 파일과 그 파일에서의 위치를 알려줌.
https://gforth.org/manual/Macros.html
비교 대상으로 포스의 매크로를 준비했음. 포스의 매크로는 강력하지만 꽤 어렵다고 생각함. 근데 일관성 있게 :와 ;로 정의한다는 건 좋은듯.
반면 내 언어는 일관성이 없음.
두 수를 더하는 단어를 구현할 때, 함수로는 $add func + end
컴파일러 매크로로는 #add { + } define
전혀 일관성 없음.
댓글 0